About The Line Following Module
The robot comes equipped with a 4 channel IR module attached to the bottom of the robot. There are LEDs on top of the module that indicate the state of the sensors automatically without the need to write any code.
From the datasheet
Infrared sensor data:
- Address: 0x0A
- Type: Read
- Range: 0x0-0xF
- Additional Info:
- 1 bit = 1 sensor state
- 0 means LED is lit, 1 means LED is off
- If we label sensors from left to right accordingly: s1, s2, s3, s4; then output byte corresponds to: s2, s1, s3, s4
Label sensors from left to right: s1, s2, s3, s4; respectively
So if we get an output 0b0101
Then:
\(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) |
---|---|---|---|
s2 | s1 | s3 | s4 |
0 | 1 | 0 | 1 |
Then that implies: - s1: LED off - s2: LED lit - s3: LED lit - s4: LED off
To get the value of a certain bit, for instance this \(\text{0b1}\underline{1}\text{01}\)
- Shift to the right by \(n\) where \(n\) is the amount of places to the left of lowest bit (or \(2^n\) in base 10). meaning
0b1101 >> 2
results in0b11
. - Remove all bits to the left by using the AND operation with
0b1
.0b11&0b01
outputs1
which is exactly the binary digit we're trying to retrieve.
Calibrating The Sensors
To correctly output the right value based on your surroundings, we need to calibrate the sensors. Place all the sensors over a black material and adjust accordingly: 1. Turn all the knob until no LED lights up 2. Turn all the knobs until LED lights up and keep it there
- remove to surface
- check that LED doesn't light up
Reading Data From Sensor
reg = 0x0A
desired_blocks_of_data = 1
track_data = bot.read_data_array(reg, desired_blocks_of_data)
Formatting the data
Pulling data out from the list and converting to int
type
track_data = int(track_data[0])
If we convert it to binary, we get an output as such:
0b1011
reg = 0x0A
desired_blocks_of_data = 1
w_for_ir = widgets.Text(
description='State: ',
disabled=True
)
display(w_for_ir)
while True:
track_data = bot.read_data_array(reg, desired_blocks_of_data)[0]
w_for_ir.value = bin(track_data)