EasyManua.ls Logo

SunFounder PiCar-X Kit - Page 83

Default Icon
181 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 Kit
(continued from previous page)
px.forward(speed)
sleep(0.05)
else :
px.forward(0)
sleep(0.05)
if __name__ == "__main__":
try:
main()
finally:
px.stop()
print("stop and exit")
sleep(0.1)
How it works?
You need to pay attention to the following three parts of this example:
1. Define the main function:
Start the camera using Vilib.camera_start().
Display the camera feed using Vilib.display().
Enable color detection and specify the target color as “red” using Vilib.color_detect("red").
Initialize variables: speed for car movement speed, dir_angle for the direction angle of the cars move-
ment, x_angle for the cameras pan angle, and y_angle for the cameras tilt angle.
2. Enter a continuous loop (while True) to track the red-colored object:
Check if there is a detected red-colored object (Vilib.detect_obj_parameter['color_n'] != 0).
If a red-colored object is detected, obtain its coordinates (coordinate_x and coordinate_y).
Calculate new pan and tilt angles (x_angle and y_angle) based on the detected object’s position and
adjust them to track the object.
Limit the pan and tilt angles within the specified range using the clamp_number function.
Set the camera’s pan and tilt angles using px.set_cam_pan_angle() and px.set_cam_tilt_angle()
to keep the object in view.
3. Control the car’s movement based on the difference between dir_angle and x_angle:
If dir_angle is greater than x_angle, decrement dir_angle by 1 to gradually change the direction angle.
If dir_angle is less than x_angle, increment dir_angle by 1.
Set the direction servo angle using px.set_dir_servo_angle() to steer the car’s wheels accordingly.
Move the car forward at the specified speed using px.forward(speed).
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.
4.12. 10. Bull Fight 79

Table of Contents

Related product manuals