"

Chapter 3 – Vision and Navigation Technologies with Zumi

3.2 – Crosswalk and Line Following

Overview

Cars need to share the road with pedestrians and cyclists. This is why there are safe distances for cars to stop at an intersection. In this lesson, we will use the bottom IR sensors to program Zumi to stop at a crosswalk and follow lines.

Let’s first import the necessary libraries:

from zumi.zumi import Zumi
import time
zumi = Zumi()

Stopping at the crosswalk

The bottom IR sensors can be used to sense a white or black line on the road and stop Zumi. Let’s take a look at how this works:

Activity

Using what we’ve learned about line detection, let’s write a program to allow Zumi to detect and stop at crosswalks. Starter code has been provided below to get you started.

HINT: Figure out what your IR sensor values should be, and use them inside your loop!

for x in range(300):
# Write your code here
# if the left IR sensor is ___ and the right IR sensor is ___:
zumi.stop() # Don't forget to stop at the end!

print("Done!")

Line Following

Now instead of stopping when Zumi sees the line, she is going to follow it. On the road, Zumi uses the camera to detect lanes and stay inside of them. In this case, you will make Zumi stay on the line by calling a function.

To do this, we’ll be using the line_follower() function, which takes in 1 parameter called duration, which indicates to Zumi, for how long she needs to follow the line.

For example, if we wanted Zumi to follow the line for 3 seconds, we would do something like this:

zumi.line_follower(3)

Addressing Issues with Line Detection

If you think that Zumi is not detecting the lines very well, or that the difference between dark and light is not big enough, you might need to adjust the thresholds, or limits, that the IR sensors will detect. Their default is set to 100. Refer to the IR Sensor lesson to test your IR sensors and set your thresholds.

Here is an example:

zumi.line_follower(3, left_thresh=100,right_thresh=100)

Try running the code above to see how this function is used.

Note: This function calls a while loop. If both of the IR sensors see white (the end of the line), the code will automatically stop, even if the time was not completed.

 

Demo Video

 

Questions