Autonomous vehicles

Autonomous vehicles

Using sensors

Having worked through the obstacle course, you’ll notice how fussy and annoying the robot is. Start the robot slightly forward or back of your mark and it’ll miss the turn. Get the starting angle slightly wrong and the robot can end up in the wrong place entirely. We can minimise this problem by introducing some sensors. Sensors are ways the computer can find out about the world around it. Our robot is equipped with four sensors:

This table shows their names and which port they should be connected to:

PortNameSensor
1LCOLORLeft color sensor
2TOUCHTouch sensor
3USONICUltrasonic sensor
4RCOLORRight color sensor

A typical thing you might want to do with a sensor is code snippet like this:

A wait-until statement in Scratch.

In most languages end up writing something more like this:

start driving
loop
    if touching something then
        break
    end-if
end-loop
stop driving

For our robot, this becomes:

robot.drive(100, 0)
while True:
    if robot.read(TOUCH):
        break
robot.stop()

Before we use the sensor, we need to add it to our setup command, so the full code would read:

1
2
3
4
5
6
7
robot.setup(DRIVE_BASE, TOUCH)

robot.drive(100, 0)
while True:
    if robot.read(TOUCH):
        break
robot.stop()