"

Chapter 3 – Vision and Navigation Technologies with Zumi

3.1 – Obstacle Avoidance

Have you experienced a situation where emergency auto brakes were activated in your car? What was the outcome? How does a car detect when to initiate emergency braking? Self-driving cars require the ability to swiftly maneuver around obstructions during emergencies, and they rely on advanced sensors such as LIDAR (Light Imaging Detection and Ranging) and RADAR rather than infrared technology. LIDAR employs laser light to create a map of the surrounding environment, while RADAR employs radio waves. However, despite lacking these sophisticated sensors, Zumi can still skillfully navigate around obstacles.

Step 1: Import Libraries

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

 Step 2: Stop for pedestrians

When Zumi sees an obstacle or a pedestrian, it must stop and not keep driving. With the code below, we can show an example of Zumi stopping when Zumi encounters an obstacle.

for x in range(300):
ir_readings = zumi.get_all_IR_data()
front_right_ir = ir_readings[0]
front_left_ir = ir_readings[5]

if front_right_ir <= 111 or front_left_ir <= 135:
zumi.stop()
else:
zumi.go_straight(30, 0)

zumi.stop() # Don't forget to stop at the end!
print("Done!")

Here we will use a for loop to loop for 300 iterations, which will constantly get the IR readings of the front sensors, and once the right or left detects your hand, so if the right IR sensor is less than or equal to 111 or the left IR sensor is less than or equal to 135, then Zumi will stop. Otherwise, she will keep going straight.
We use the values 111 and 135 because we checked what the ir readings are when there is no obstacle compared to when there is an obstacle.

while True:
ir_readings = zumi.get_all_IR_data()
print(ir_readings)
time.sleep(0.5)

This is the output from above. Keep in mind that we focus on index 0 and 5 because 0 is the front right IR sensor and 5 is the front left IR sensor.

image

Activity

Code Zumi to turn left or right when she sees an obstacle to avoid it instead of just stopping. If front right IR is triggered (if front_right_ir &lt; 100), go left and add 30 degrees. If front left IR is triggered (if front_left_ir &lt; 100), go right and subtract 30 degrees. If both are triggered, write code to stop, reverse and change direction by 180. Use the pseudocode instructions to guide you.
Use this template to fill in the code inside the for loop.

image

Hints:

image

Pseudocode instructions

image

Conclusion
Congratulations, you can start coding Zumi to Avoid Obstacles in Python!

Demo Video 

Questions