Chapter 4 – Visual Processing and Distance Measurement Techniques
4.2 – Advanced IR
This lesson is focused on utilizing Zumi’s IR sensors to perform challenging tasks. By the end of this lesson, you will be able to program Zumi to avoid obstacles, stop at crosswalks, and follow lines with ease.
Step 1: Introduction to obstacle avoidance
To help Zumi avoid obstacles, we can make use of the front and back IR sensors. Unlike self-driving cars which use more advanced sensors like LIDAR and RADAR, Zumi can avoid obstacles by detecting the infrared light emitted by them.
Step 2: Stop for pedestrians
To enable Zumi to stop for pedestrians or other objects, we will use the front left or front right IR sensors. The code below can be used to drive Zumi forward using forward_step() until she detects an object with either of the sensors. When the object is removed, Zumi will continue to move. In case there is no object detected, the program should stop at the end of the loop.
To check the back IR sensors, Zumi can be programmed to drive in reverse.
Step 3: Avoid Collision Functions
To simplify the process of avoiding collisions, we have a function that can be used to drive Zumi until an obstacle is detected. Once an obstacle has been detected, the function will stop running, even if the duration has not been completed. If no obstacle is detected within the timeout, Zumi will stop automatically.
For forward movement:
To check the back IR sensors while reversing at an angle:
Step 4: Challenge: Escape a Maze
To enable Zumi to escape a maze, we can make use of the front IR sensors. When an obstacle is detected by either the right or left sensor, we will tell Zumi to turn slightly to the left or right to avoid it, depending on which sensor is triggered.
Initialize Heading
Start by creating a variable “heading” that is initialized to 0. Then, set up a loop with two if statements. If the right sensor detects an obstacle, then Zumi’s angle should change a few degrees to the left. If the left sensor detects an obstacle, then Zumi’s angle should change a few degrees to the right. There will be no else statement because Zumi will continue driving regardless if the object is on the left or on the right.
Check sensors
Since Zumi will do something different depending on which IR sensor is detected, separate the right sensor and left sensor detection into their own if statements.
Change heading
If an object is on Zumi’s right side, then Zumi will need to turn to left. Remember that in mathematics, turning to the left means the degrees increase while turning to the right degrees decrease. Add or subtract 30 degrees in the if statements.
The last step in the loop before it repeats is to drive in that direction using the forward_step() block.
Step 5: Line following
To enable Zumi to follow lines, we can make use of the bottom IR sensors. The line_follow_gyro() function checks both IR sensors to detect lines. If the right sensor detects a deviation from the line, Zumi will turn to the left and vice versa.
To use this function, line Zumi up on the starting black line and use the line_follow_gyro() block to set the speed and program duration.
**NOTE:** You may need to adjust the thresholds and angle adjustment parameter to suit your needs.
Step 6: Review
Throughout this tutorial, you have gained knowledge on leveraging Zumi’s infrared sensors to prevent collisions with nearby objects. Additionally, you have learned how to incorporate collision avoidance with Zumi’s heading, enabling Zumi to navigate around these obstacles. Lastly, you have acquired the ability to utilize Zumi’s bottom infrared sensors to enable Zumi to follow lines.
Demo Video