The worksheetDownload the PDF
Answers

2.1 Where am I?

Sensing · Robot club · about 15 min

BugBotLab

What this lesson is about

position(), heading(), velocity(), and resetting them.

Questions 7 marks in all

  1. [1 mark]What do the two numbers in position() mean?

    1. Ax is centimetres to the right and y is centimetres forward, from where the program started
    2. Bx is the speed and y is the heading
    3. Cx is forward and y is to the right
    4. DThey count from the middle of the mat
    Answer: A. Both are in centimetres from where the program started, measured along the mat: x across it to the right, y up it. For a robot that starts facing up the mat, that is right and forward.
  2. [1 mark]The robot starts at heading 0, runs turn_right(30, angle=45), then turn_left(30, angle=135). What does heading() say? Give a whole number of degrees.

    Answer: 270 (accept within 1). 45 take away 135 is -90, and headings go 0 to 360, so -90 shows as 270.
  3. [1 mark]What is velocity() especially useful for?

    1. AKnowing whether the robot has actually stopped
    2. BSetting how fast the robot drives
    3. CFinding how far away the wall is
    4. DResetting the position
    Answer: A. It tells you how fast the robot is moving right now. After stop() the robot may still be sliding for a moment.
  4. [1 mark]Roughly what does the last line print?

    forward(60, distance=20)
    turn_right(30, angle=90)
    reset_position()
    reset_heading()
    forward(60, distance=10)
    print(position())
    1. A(0, 10)
    2. B(10, 20)
    3. C(0, 30)
    4. D(10, 0)
    Answer: D. The resets made that spot (0, 0) and the direction it faced heading 0, but position() still measures along the mat. After the right turn, forward is across the mat, so the 10 cm shows up in x: it prints (10.1, -0.0).
  5. [1 mark]Which sensors are inside the robot's 9-axis inertial sensor?

    Tick every answer that is true.

    1. AGyroscope
    2. BAccelerometer
    3. CMagnetometer
    4. DOptical-flow sensor
    5. ETime-of-flight sensor
    Answer: A, B, C. The compass combines a gyroscope, accelerometer and magnetometer. The optical-flow sensor underneath measures position instead.
  6. [1 mark]What does this program print?

    import math
    x, y = 3, 4
    print("from home:", math.sqrt(x*x + y*y), "cm")
    Answer:
    from home: 5.0 cm

    Straight-line distance from home is the square root of x times x plus y times y. 9 plus 16 is 25, and its square root is 5.

  7. [1 mark]After driving a square, the robot is not quite back where it started, and a few degrees off. What is the right way to think about that?

    1. AEvery move is a little off, and small errors add up
    2. BThe optical-flow sensor is broken
    3. Cposition() only works in straight lines
    4. DThe robot really is exactly home
    Answer: A. Each drive and turn is nearly right, and on the real robot each reading of where it is is nearly right too. Real robots correct it by looking at landmarks, which the camera does in Module 4.

The task: report from the corner

Get to the point 30 cm right and 30 cm forward of where you start, and print a line in the shape at (30.1, 29.8) facing 1.2 from position() and heading().

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

# drive forward at 60 for 30 cm, then stop
forward(60, distance=30)

The hint students can ask for: Get to the point 30 cm right and 30 cm up from where you start (forward then right, or right then forward), and print your position and heading in the form at (30.1, 29.8) facing 1.2

A solution

from bugbot import *
connect()
forward(60, distance=30)
right(60, distance=30)
x, y = position()
print(f'at ({x}, {y}) facing {heading()}')

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.