Simplify Navigation with a Maneuver Function
Functions with parameters can allow you to set not only wheel speeds but run times. The can also adjust for making the right wheel turn the opposite direction to give you a more intuitive set of statements. Here is an example of a function call that makes the cyber:bot roll full speed forward for 3 seconds. Note that the right wheel speed is no longer negative. The rule for the maneuver function is that positive values are for forward, for either wheel. The 3rd argument (3000) is the number of milliseconds to execute the maneuver.
Script: general-maneuver-function
- Make sure the cyber:bot board’s power switch is set to 1 and the battery pack is plugged into the cyber:bot board.
- Open cyberbot-template-with-blink with the micro:bit Python Editor at http://python.microbit.org/v/3
- Replace its default script with individual-maneuver-functions-example shown below.
- Click the Save button to save a copy of your work.
Tests
- Click the Send to micro:bit button to flash the script into the micro:bit.
- Disconnect the programing cable and put the cyber:bot on the floor.
- Move the switch to position 2 and press/release the reset button on the micro:bit. (Or, set PWR to 0, then back to 2.).
- Verify that the script makes the cyber:bot roll forward for 3 seconds, then turn in place to the right.
- Set the PWR switch to 0 when the cyber:bot is not in use, and remember to unplug the battery pack from the cyber:bot board if you are done for the day.
How It Works
On line 5, the maneuver function definition has three parameters: vL (velocity left) vR (velocity right), and t (time, in milliseconds). The three arguments in the function call on line 12 get passed to those parameters. Likewise with the function call on line 13. Line 7 takes the negative of vR, which is how the function takes positive values for forward and negative ones for backward. The if vR is not None: statement prevents the vR = -vR statement from being executed when vR is None. vR = -None would cause an exception because None is not a number and does not work with arithmetic operators.
Try This
Allowing the motors to come to a full stop can make maneuvers more predictable. This next script makes the cyber:bot go forward, left, right, left, backwards, stop. Between each actual maneuver, there’s a call setting the wheel speeds to None for 0.5 s. This allows the wheels to come to a full stop before the next maneuver.
- Try this script as-is first.
- Then, comment all but the last maneuver(None, None, 500) call by adding a hashtag symbol to its left. Then, re-run the script.
- What differences did you notice in the cyber:bot robot’s navigation?
Your Turn
While your cyber:bot SumoBot is searching for its opponent, splitting up maneuvers into little pieces will be important. By checking the sensors between each 26 ms maneuver() call, your bot can keep rolling while searching for an opponent. Here is an example where the cyber:bot calls the maneuver function 100 times, which takes about 2.6 seconds. Adding sensor measurements between each call will make it take longer, but your robot will still be able to check for its opponent or the sumo ring’s border many times per second. More about this in upcoming activities.