Sideways

Strafing: moving without turning, the thing wheels cannot do.

1.4DrivingRobot club15 min

Do this lesson in the simulator

A car has to turn to change direction. BugBot does not. Its four vibrating feet can slide it sideways, or diagonally, while it keeps facing the same way. This is called strafing, and it is the move that makes BugBot different.

left and right

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# drive forward at 60 for 20 cm, then stop
forward(60, distance=20)
# slide right at 60 for 20 cm, then stop
right(60, distance=20)
# drive backward at 60 for 20 cm, then stop
backward(60, distance=20)
# slide left at 60 for 20 cm, then stop
left(60, distance=20)
print("back at", position(), "still facing", round(heading()))

Run this in the simulator

The robot drew a square and never turned. Watch the white nose: it pointed the same way the whole time. Compare with the square in lesson 1.3, which needed four turns.

Why it matters

Turning takes time and adds error: every turn_right(30, angle=90) is a little off. Strafing keeps the heading you have. It also lets the robot keep its camera and distance sensor pointed at something while it moves around it. Real robots with this ability are called holonomic, and they are rare and expensive. Yours is one.

Diagonals

drive(forward, sideways, rotation) sets all three at once, from -100 to 100 each. Forward and sideways together is a diagonal:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# forward and right at the same time
drive(60, 60, 0)
# pause 1.5 s (the robot keeps doing what it was told)
wait(1.5)
# all motors off
stop()
print("at", position())
# forward and left
drive(60, -60, 0)
# pause 1.5 s (the robot keeps doing what it was told)
wait(1.5)
# all motors off
stop()
print("at", position())

Run this in the simulator

drive does not take a distance; it is the raw command the other moves are built from. It returns at once, so you need wait and stop around it.

A gentle curve

Add a little rotation and the robot curves:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# forward, sideways, rotation: -100 to 100 each, until the next command
drive(60, 0, 20)
# pause 3 s (the robot keeps doing what it was told)
wait(3)
# all motors off
stop()
print("facing", round(heading()), "at", position())

Run this in the simulator

Sideways by distance, with a sensor

You can strafe until a sensor says something, exactly like driving forward until a wall:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# slide right at 50
right(50)
# pause 1 s (the robot keeps doing what it was told)
wait(1)
# all motors off
stop()
print("moved right to x =", position()[0])

Run this in the simulator

position()[0] is just the x part: [0] picks the first item, as with any list.

Task: reach the green square

There is a wall between you and the green square. Get the robot into the square within 20 seconds without touching the wall.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# drive forward at 60 for 20 cm, then stop
forward(60, distance=20)
# your turn: go round the wall and into the square

The wall is 20 cm wide and the square 20 cm beyond it. Forward, sideways, forward, sideways, and no turning needed. Remember the margin for coasting.

Challenges

  1. Solve reach-the-square by strafing first, then forward, then strafing back.
  2. Draw a diamond with four drive calls.
  3. Drive a circle: drive(50, 0, rotation) with the right rotation for three seconds. Hint: the heading should change by 360.