The last question in the previous step asked you about the disadvantages of using flight commands for drone navigation. You may have noticed the following:
- It is easy to forget to reset the values.
- It makes it harder to separate movements
- It requires extra lines of code
- It is not very intuitive
When we see issues like extra code and difficulty to implement or understand, as programmers we want to apply a concept called ABSTRACTION. Abstraction is the process of hiding extra or confusing features to reduce complexity and increase efficiency. So when we see, as a programmer, that commands are repetitive or difficult to use, we create new commands or features that make our code simpler to use. This is what the go() command does. It takes the code we learned from the individual flight commands and the move() command and abstracts them into a single, easier to use command. This is what the function looks like:
drone.go(direction, duration, power)
This is what each part means:
- Direction: where the drone will fly. You can use FORWARD, BACKWARD, LEFT, RIGHT, UP, and DOWN, but make sure you type in all caps like you see here. You will also need to type Direction before it, so it will look like Direction.FORWARD.
- Duration: how long the movement will happen in seconds. You can leave it as blank, zero, or any positive number. If duration is zero, it runs indefinitely. If duration has no number, it automatically sets to 1. If you put any positive number, duration is equal to that value.
- Power: the speed of the drone. It can be any number between 0-100, with 0 being no power and 100 being full power. If power has no number, it will default to 50.
Here are some examples:
drone.go(Direction.UP, 3, 75) # go up for 3 seconds at 75% power
drone.go(Direction.FORWARD) # go forward for 1 second at 50% power
drone.go(Direction.LEFT, 6) # go left for 6 seconds at 50% power
If you want to use drone.go()
in your programs, you’ll need to include this line at the top, right underneath import CoDrone_mini:
from CoDrone_mini import Direction
This tells the program to look for and use the specific part of the CodDrone package called Direction because it is not used by default.
After that, you’re good to go! Try using drone.go() in the program you wrote for Step 3 in place of drone.set() and drone.move(). What happened?
Once you have that completed, try going back and redoing the box flights in Challenge 1. When you are done you should be able to answer the following questions:
- Is using the go() command easier than the set() commands for this challenge? Why or why not?
- Did you use fewer lines of code with the go() command instead of the set() commands?
- Do you think there is a time to use set() commands versus a time to use go() commands? If so, describe examples of when to use each set of commands.