12. line_track.py

Share for us

Procedures of line tracking: Use speech prompt and speech command to control. When the car starts running, there will be a speech prompt asking whether calibration is needed or not. You can answer yes or no and the PiSmart will act accordingly.

During the calibration, the PiSmart car will prompt you step by step such as whether to calibrate with a white line or a black one. After a prompt, there is few seconds for you to place the sensor on the car to the corresponding line. When all calibration completes, the final reference calibration value will be printed on the screen.

In the process of line following, the car will speak “Turn left” or “Turn right”. If the car is out of the map and can’t find the way back, it will call for help. Then you can say commands like “left”, “right” to help. If it comes back to the track, it will express its gratitude. If the car can’t back to the right line under your instructions, it will query you and then stop.

The whole process is a simple interaction between the PiSmart car and users. Therefore, with no need of keyboard or other controlling devices, just talk to it!  

Diagram of connection:

Line FollowerPiSmart
SCLSCL
SDASDA
VCC5V
GNDGND

The line-follower module realizes I2C communication by the chips on the module. The five infrared intrusion detectors can tell black from white by different light rays reflected from deep and light-colored surfaces. The data of the five detectors will be processed in the chip on the module and then output to the PiSmart via I2C. The I2C address of the line-follower module is 0X11.

Connect SCL, SDA, VCC, and GND in the module to the I2C ports SCL, SDA, VCC, and GND in PiSmart respectively.

Use I2C-tools to see whether the I2C devices have been recognized. 

sudo i2cdetect –y 1

pi@raspberrypi:~ $ sudo i2cdetect -y 1
      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:           -- -- -- -- -- -- -- -- -- -- -- -- --
10: 10 11 -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --


You will find that the I2C address of the line-follower module is 11. If you cannot find it, there must be some problem with the module connection. Please check the wiring between the module and the PiSmart carefully.

Run the code:

pi@raspberrypi:~/SunFounder_PiSmart_Car/examples $ python line_track.py

Phenomenon: After running the code, the conversation between PiSmart and users starts, just as described previously.

When the PiSmart’s query finishes, the LED ring on top lights up, indicating the car is ready to listen to commands. Then you can say effective commands to it. 

Store the status data collected by the five detectors in the list It_status, and then decide the actions of the car according to the list.

Deviation rectification of the car’s direction can be divided into 4 types: slightly turn a_step, partly turn b_step, half turn c_step, and greatly turn d_step. The more it deviates from the black line, the bigger the rectification angle (difference between right_speed &left_speed) is.

The programming flow chart: