Unit 4: Senses
Numbers that come from the real world.
Unit 4 of 8 in Robotics coding for kids. Its 4 lessons are The Beam, Both Sides, Bump and Measure the Room — below is everything each one explains, and a question or two from it to try.
Every program on this page was driven round the simulated room before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🔦 The Beam
One thin beam
The rangefinder sends out a pulse and times how long it takes to come back — like a bat, or a submarine.
That means it only knows about whatever is straight in front of the nose. Turn the robot and you get a completely different number, because you are asking about a different direction.
Python
robot.say(robot.distance())
robot.left(90)
robot.say(robot.distance())
It prints
152.0 92.0
Try it yourself
There is a block over on the left, but neither reading found it. Why not?
- The beam only measures where the nose is pointing, and it was never pointed at the block
- The block is too small to see
- The sensor only works in front of walls
- The block is too far away
Now the robot is level with the block. What does this print?
Python
robot.left(90)
robot.say(robot.distance())
Answer them in the app
👀 Both Sides
Two more beams
Bolt has a rangefinder on each side as well. robot.distance_left() and robot.distance_right() measure straight out of the left and right flanks.
In a corridor those two numbers tell you something the front one never can: which side you are closer to.
Python
robot.say(robot.distance_left())
robot.say(robot.distance_right())
It prints
12.0 52.0
Try it yourself
Left reads 12 and right reads 52. Where is the robot?
- Close to the left-hand wall, with lots of room on its right
- Exactly in the middle
- Close to the right-hand wall
- Facing the wrong way
The corridor is 80 cm wide. What does this print?
Python
robot.right(90)
robot.forward(20)
robot.left(90)
robot.say(robot.distance_left())
robot.say(robot.distance_right())
Answer them in the app
💥 Bump
What happens when it hits something
The robot does not go through walls and it does not break. It stops where it touched and remembers.
robot.bumped() is True when the last move ended in a bump, and the rangefinder then reads 0, because there is nothing at all between the nose and the wall.
Python
robot.forward(200)
robot.say(robot.bumped())
robot.say(robot.distance())
It prints
True 0.0
Try it yourself
The room is 200 cm tall, and the robot drives at the top wall. Where does it stop?
Python
robot.forward(200)
robot.say(robot.y())
The robot asked for 200 but only drove 172. What does that tell you?
- It ran into something and stopped there
- It ran out of battery
- forward() always drives a bit less than you ask
- It turned round part way
Answer them in the app
🏆 Measure the Room
Try it yourself
You add the left reading to the right reading to measure across a room. Why is the answer 16 cm too small?
- Because the robot itself is 16 cm wide and stands in the gap
- Because the beams are slow
- Because the readings are rounded
- It is not — the answer is right
Answer it in the app