"

Chapter 2 – Sensor Fusion and Coding Structures

2.4 – IR Sensors

Zumi IR Sensors
Zumi is equipped with 6 IR sensors: two in the front, two in the back, and two on the bottom. They all have an index so that it’s easier to read their data:

* IR 0 = front right
* IR 1 = bottom right
* IR 2 = back right
* IR 3 = bottom left
* IR 4 = back left
* IR 5 = front left

image

Display IR data on the screen
You can display the IR data on Zumi’s screen. To get IR data, you’ll need a `Zumi` object, and to display on the screen, you need to create a `Screen` object.

from zumi.zumi import Zumi
from zumi.util.screen import Screen
import time

zumi = Zumi()
screen = Screen()

IR sensors work by emitting an infrared pulse and measuring the infrared light that returns after it bounces off an object. This number will be between 0 and 255. A lower number indicates that something in the vicinity is reflecting the IR light back to the sensor.

Front IR sensors

for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
front_right_ir = ir_readings[0]
front_left_ir = ir_readings[5]

message = " IR readings "
message = message + str(front_right_ir) + ", " + str(front_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")

Back IR sensors

for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
back_right_ir = ir_readings[2]
back_left_ir = ir_readings[4]

message = " IR readings "
message = message + str(back_right_ir) + ", " + str(back_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")

Bottom IR sensors
Bottom IR sensors are great for line following or detection. Use the Bottom IR worksheet to test values between light and dark.

for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
bottom_right_ir = ir_readings[1]
bottom_left_ir = ir_readings[3]

message = " IR readings "
message = message + str(bottom_right_ir) + ", " + str(bottom_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")

Lists and Accessing Values

If you take a closer look at the IR data, you will notice that in all of the examples, the `get_all_IR_data()` function is used. However, since there are six IR sensors, how can all the data be stored in the same variable? The answer is simple – all this data is actually stored in a list. In Python, lists can be used to store and organize data of the same type without creating multiple variables.

To retrieve the IR sensor data, the `get_all_IR_data()` function is called, which requests all the sensor data and saves them in a list with six items. In this example, the list is named `ir_readings`. Unlike a vertical shopping list, a Python list is written horizontally and enclosed in [brackets]. The code block below will take a reading and print out the values of the IR sensor data.

ir_readings = zumi.get_all_IR_data()
print(ir_readings)

Printing the list name will display all of the values in the list. However, what if you only want to check one or two values? You can access each element in the list by its index, which is like the home address of where the element is stored in the list. In Python, lists start at 0. Take this list for example. What is the value of the first element? What about the last?

image

Below is an example of accessing the first element at index 0. Practice printing all of the values and make sure to save the IR values in a variable with a descriptive name. As your programs become more complicated, you don’t want to forget which value corresponds to what sensor.

ir_readings = zumi.get_all_IR_data()
front_right_ir = ir_readings[0]
print(front_right_ir)

Now that you know how to access and store data in a list, you can manipulate the values in the list to suit your needs.

Conclusion
We have covered various topics such as the basics of coding in Python, controlling Zumi’s movements, and using the sensors to gather data. Additionally, we have explored more advanced concepts such as object detection and computer vision.

Demo Video

Questions