EasyManua.ls Logo

SunFounder picar-x - Line Tracking

Default Icon
153 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder picar-x
4.5 Line Tracking
This project will use the Grayscale module to make the PiCar-X move forward along a line. Use dark-colored tape
to make a line as straight as possible, and not too curved. Some experimenting might be needed if the PiCar-X is
derailed.
Run the Code
cd /home/pi/picar-x/example
sudo python3 minecart_plus.py
After running the code, PiCar-X will move forward along a line.
Code
Note: You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path
like picar-x/example. After modifying the code, you can run it directly to see the effect.
from picarx import Picarx
if __name__=='__main__':
try:
px = Picarx()
# px = Picarx(grayscale_pins=['A0', 'A1', 'A2'])
px_power = 10
while True:
gm_val_list = px.get_grayscale_data()
print("gm_val_list:",gm_val_list)
gm_status = px.get_line_status(gm_val_list)
print("gm_status:",gm_status)
if gm_status == 'forward':
print(1)
px.forward(px_power)
elif gm_status == 'left':
px.set_dir_servo_angle(12)
px.forward(px_power)
elif gm_status == 'right':
px.set_dir_servo_angle(-12)
px.forward(px_power)
else:
px.set_dir_servo_angle(0)
px.stop()
finally:
px.stop()
How it works?
The grayscale sensor module grayscale_module is also imported in the picarx module, and we can use some of
these methods to detect black lines.
The function to detect the black line looks like this:
4.5. Line Tracking 57

Related product manuals