Turning and heading

turn_left() and turn_right() by an angle, heading() as a compass, and turning on the spot.

1.3DrivingRobot club15 min

Do this lesson in the simulator

A robot that only goes forward and back lives on a line. Turning gives it the whole mat. BugBot has a compass, and it uses it to turn by exactly the angle you ask.

turn_left and turn_right

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

print("facing", round(heading()))
# turn 90 degrees clockwise, then stop
turn_right(30, angle=90)
print("facing", round(heading()))
# drive forward at 60 for 20 cm, then stop
forward(60, distance=20)

Run this in the simulator

turn_right(30, angle=90) spins the robot a quarter turn clockwise and waits until it is done. turn_left(30, angle=90) goes anticlockwise. heading() is the compass: degrees clockwise from where the robot started, 0 to 360.

Which way is which

Picture a clock face from above. turn_right goes the way the hands go; turn_left goes back the other way. Predict the headings, then run:

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

# turn 90 degrees clockwise, then stop
turn_right(30, angle=90)
print("after right 90:", round(heading()))
# turn 90 degrees clockwise, then stop
turn_right(30, angle=90)
print("after another right 90:", round(heading()))
# turn 180 degrees anticlockwise, then stop
turn_left(30, angle=180)
print("after left 180:", round(heading()))

Run this in the simulator

A square, with turns

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

# do this 4 times (side counts from 0)
for side in range(4):
    # drive forward at 60 for 25 cm, then stop
    forward(60, distance=25)
    # turn 90 degrees clockwise, then stop
    turn_right(30, angle=90)
print("home?", position(), "facing", round(heading()))

Run this in the simulator

Four sides and four right angles. The robot ends up near where it started, facing nearly the way it started. Nearly: the small errors in each move add up. Module 2 is about noticing and fixing that.

Turning without stopping

With an angle, turning blocks until the angle is done. Leave the angle out and the robot just starts turning and carries on, like forward(speed) does:

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

# turn clockwise on the spot at 40, and keep going
turn_right(40)
# pause 1 s (the robot keeps doing what it was told)
wait(1)
# all motors off
stop()
print("turned about", round(heading()), "degrees in one second")

Run this in the simulator

Give an angle when you know how far to turn. Leave it out when you want to keep turning until something happens, such as a sensor seeing a target.

Other shapes

Any regular shape is "drive, turn by 360 divided by the number of sides":

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

sides = 6
for i in range(sides):
    # drive forward at 60 for 12 cm, then stop
    forward(60, distance=12)
    turn_right(30, angle=360 / sides)
print("facing", round(heading()))

Run this in the simulator

Task: face east

Turn so the robot faces 90 degrees (to the right of where it started), then print facing: and its heading. Do not drive anywhere.

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

# turn 45 degrees clockwise, then stop
turn_right(30, angle=45)
print("facing:", heading())

Task: drive a square

Visit the four corner zones in order and finish where you started, facing the way you started (within 15 degrees).

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

# corners are 40 cm apart
# drive forward at 60 for 40 cm, then stop
forward(60, distance=40)

Challenges

  1. Draw a triangle, then a pentagon, with the same loop and a different sides.
  2. Turn 360 in four steps of 90 and print the heading after each. How far off is the last one?
  3. Use turn_left(30) with no angle and a while loop to stop when heading() is close to 270.