Left and Right Turns

All it takes to get other motions out of your cyber:bot are different value combinations in the v parameters in your bot(18) and bot(19) calls to servo_speed(v)

Rotating in Place

These two calls will make your cyber:bot rotate in place to make a left turn (note these are not complete scripts):

# Turn left in place
bot(18).servo_speed(-75)            #Left wheel clockwise
bot(19).servo_speed(-75)            #Right wheel clockwise

These two calls will make your cyber:bot rotate in place for a right turn:

# Turn right in place
bot(18).servo_speed(75)            #Left wheel counterclockwise
bot(19).servo_speed(75)            #Right wheel counterclockwise

The turns above can be called rotation turns because the wheel speeds are equal, but velocity is opposite to make the cyber:bot turn about its center.  This rotation is great for making the robot face a new direction in a tight place and without changing its position. Imagine a pen going through the center of the cyber:bot's "axle." A rotating turn would not draw any curved lines; it would just rotate on this point making the dot darker! You could think of this as an arc with a radius of zero.

Let’s combine these two commands with a forwards and backwards command into a single script that makes the cyber:bot move forward, turn left, turn right, then move backward.

Save Time of Adding the cyber:bot Module to a Project

Any project that has been saved after adding the cyberbot.py module can be used with a new script for the cyber:bot.  This will save you the time of adding the cyberbot.py module to each cyber:bot script. 

Examples of files you can reopen include:

  • cyberbot-template-with-blink.hex:  It can be downloaded from Add modules to your micro:bit.  Just make sure to right-click the orange link and select Save link as... to download cyberbot-template-with-blink.hex.  
  • The last cyber:bot robot project you worked with:  At this point, it's probably backward_three_seconds.hex, which was saved to your Downloads folder at the end of the previous activity.

So long as you have a previously working cyber:bot project, just open it, delete what you don't need, and make sure to update the project name.

  • In the micro:bit Python Editor at python.microbit.org, click Open.
  • Find and open an earlier script that worked with the cyber:bot, and open it.
  • Delete everything below the from cyberbot import * line, and be ready to replace it with the new script.

Example script: forward_left_right_backward  

  • Set the PWR switch to 0.
  • Set the project's name to forward_left_right_backward, enter the script below, and then click Save.
# forward_left_right_backward
 
from cyberbot import *
 
#Full speed forward
bot(18).servo_speed(75)               #Left wheel counterclockwise
bot(19).servo_speed(-75)              #Right wheel clockwise
sleep(2000)                           #...for 2 seconds
 
#Turn left in place
bot(18).servo_speed(-75)              #Left wheel clockwise
bot(19).servo_speed(-75)              #Right wheel clockwise
sleep(600)                            #...for 0.6 seconds
 
#Turn right in place
bot(18).servo_speed(75)               #Left wheel counterclockwise
bot(19).servo_speed(75)               #Right wheel counterclockwise
sleep(600)                            #...for 0.6 seconds
 
#Full speed backwards
bot(18).servo_speed(-75)              #Left wheel clockwise
bot(19).servo_speed(75)               #Right wheel counterclockwise
sleep(2000)                           #...for 2 seconds
 
bot(18).servo_speed(None)             #Stop the servos
bot(19).servo_speed(None)


PRO TIP: To enter this script quickly, Copy and Paste to make four copies of the four lines that make up a maneuver.  Then, modify each one with individual values.


  • Click Send to micro:bit to flash the script into it.
  • Set the PWR switch to 2.
  • Verify that the cyber:bot makes the forward-left-right-backward motions. 
  • Remember, to repeat the maneuvers, press and release the micro:bit module's Reset button.  It's under the micro:bit next to its USB connector.

Pivot Turns

Another option for turning is by pivoting, where one wheel turns forward or backward while the other stays still. Now the center of the circle is the wheel that is not moving, and the cyber:bot pivots about this point. An imaginary pen through the center of the robot would draw a circle with a radius equal to about half of the cyber:bot robot’s wheelbase.  The animation below shows a right-wheel forward pivot.


Here are the four routines for forward and backward pivot turns (these are not complete scripts):

# Pivot forward-left
bot(18).servo_speed(0)         # Left wheel stop
bot(19).servo_speed(-75)       # Right wheel clockwise
sleep(600)

# Pivot forward-right
bot(18).servo_speed(75)        # Left wheel counterclockwise
bot(19).servo_speed(0)         # Right wheel stop
sleep(600) 

# Pivot backward-left
bot(18).servo_speed(0)         # Left wheel stop
bot(19).servo_speed(75)        # Right wheel counterclockwise
sleep(600) 

# Pivot backward-right
bot(18).servo_speed(-75)       # Left wheel clockwise
bot(19).servo_speed(0)         # Right wheel stop
sleep(600) 

Try This: pivot_tests script

  • Change the v parameter value in each servo_speed call so the script will perform all four pivot maneuvers: forward-left, forward-right, backward-left, backward-right.
  • Set the project's name to pivot_tests, and then click Save.
  • Flash the modified script with Send to micro:bit and verify that the different pivot actions work.
  • Try experimenting with the sleep calls for each maneuver so that each one runs long enough to execute a 90° turn.

Your Turn: Arcs

So far, our turns have had either one or both servos turning full speed, or not moving at all. What happens when the servos turn at unequal velocities, but neither one is at full speed or stopped?  The answer is, the cyber:bot will drive in an arc.

Arcs can be tight, where one wheel turns backward while the other turns forward, but at unequal speeds.

       

Arcs can be wide, where both wheels turn the same direction, but at unequal speeds:

  • Try writing and test-driving a script to make your cyber:bot drive in a tight arc.
  • Try writing and test-driving a script to make your cyber:bot drive in a wide arc.