SunFounder PiCar-X Kit
The try...finally block ensures safety by stopping the robot’s motion in case of an interruption
or error. This is a crucial part for preventing uncontrollable behavior of the robot.
try:
# [Control logic]
finally:
px.forward(0)
• Execution Entry Point:
The standard Python entry point if __name__ == "__main__": is used to run the main function
when the script is executed as a standalone program.
if name == "main":
main()
In summary, the script uses the Picarx module to control a robot, utilizing an ultrasonic sensor for distance measurement.
The robot’s movement is adapted based on these measurements, ensuring safe operation through careful control and a
safety mechanism in the finally block.
Note: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook!
Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
• Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
• Learn & Share: Exchange tips and tutorials to enhance your skills.
• Exclusive Previews: Get early access to new product announcements and sneak peeks.
• Special Discounts: Enjoy exclusive discounts on our newest products.
• Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
Ready to explore and create with us? Click [] and join today!
4.7 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 ~/picar-x/example
sudo python3 5.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.
60 Chapter 4. Play with Python