Welcome to ROSMASTER X3 Plus repository
18.7 AI Autopilot
Turn on automatic driving
(SUPER image environment version does not support this case!!!)
Turn on the automatic driving function
Open VNC to remotely log in to the system desktop.
Note: Since image display requires a desktop display, you need to connect a display or use VNC to remotely log in to the desktop.
Open the terminal and enter the auto_drive directory
cd ~/Rosmaster/auto_drive
Start running the car’s automatic driving code.
xxxxxxxxxxpython3 yolov5_auto.py

Notice:
Please put the car in the air or place it on the map first to prevent the car from suddenly moving forward.
Since the program is time-consuming to load the model and takes a long time to run, the actual measurement takes about 5 minutes to start, so please wait patiently.
After starting the program, an image preview box will pop up, and the system memory will be full, causing the system to run slowly. It is best not to operate other functions.
If you need to exit the program, press the Q key in the image preview box and press Ctrl+C in the terminal.
Introduction to autonomous driving functions
First, you need to adjust the angle of the camera to a suitable position. Under normal circumstances, the bracket should be parallel to the ground.

If there is a problem of not turning in time and causing the map to appear, please fine-tune the camera bracket upward; if there is a problem of premature turning, please fine-tune the camera bracket downward. Please tighten the screws to fix the angle after fine adjustment.
After the program is run, the car will move forward and be placed on the track on the map. The car will move along the track.

At this time, place the road sign in the road sign placement area, and the car will execute the corresponding command after recognizing the road sign.
For example, if a whistle road sign is placed here, when the car passes by and recognizes the whistle road sign, it will sound a buzzer.

Code explanation for road sign function implementation
1.Adjustment of forward road sign function
The self.auto_control variable controls whether the car is automatically driven. If it is set to True, it means that the car will analyze and control the direction of the front wheel of the car based on the picture read by the camera. If it is set to False, it means that the car needs to perform a specific function.
xxxxxxxxxxdef car_run(self):self.car.set_car_motion(self.car_speed, 0, 0)self.auto_control = Trueself.car_state = 1
Adjustment of right turn road sign function
The car's right-turn function is specifically implemented by moving forward for a period of time and then turning right at the intersection. If there is a problem that the turn is too large or too small, you can appropriately adjust the delay time of each action.
xxxxxxxxxxdef car_turn_right(self):time.sleep(5)self.auto_control = Falsetime.sleep(.1)for i in range(20):self.car.set_car_motion(self.car_speed, 0, -3.0)time.sleep(.1)self.auto_control = True
Buzzer sound
In order to prevent the buzzer from sounding continuously, a delay time of 500 milliseconds is added.
xxxxxxxxxxdef car_whistle(self):self.car.set_beep(500)time.sleep(.5)
Sidewalk sign function
When passing the sidewalk, the car stops for a while and then continues forward.
xxxxxxxxxxdef car_sidewalk(self):self.auto_control = Falsetime.sleep(.1)self.car_stop()time.sleep(2)self.car_run()self.auto_control = Truetime.sleep(3)
Speed limit road sign function
Since '5' is written on the speed limit road sign, the function set here is to accelerate at 0.4m/s for a period of time and then resume the original speed.
xxxxxxxxxxdef car_limiting_velocity(self):self.auto_control = Trueself.car.set_car_motion(0.4, 0, 0)time.sleep(3)self.car.set_car_motion(self.car_speed, 0, 0)
Parking sign function
The stop sign function is to stop the car and turn off the automatic driving function.
xxxxxxxxxxdef car_shutdown(self):self.auto_control = Falsetime.sleep(.1)self.car_stop()
School section road sign function
The function of the road sign on the school section is to reduce the speed for a period of time and then return to the original speed.
xxxxxxxxxxdef car_school_decelerate(self):self.auto_control = Trueself.car.set_car_motion(0.1, 0, 0)time.sleep(5)self.car.set_car_motion(self.car_speed, 0, 0)
Reverse parking road sign function
The reversing and parking road sign function is to stop for a while, then drive forward for a certain distance, then execute the reversing and parking function, and finally stop. During the actual test, the time of each step may need to be adjusted to make the reversing effect better.
xxxxxxxxxxdef car_parking(self):self.car.set_car_motion(0, 0, 0)time.sleep(1)self.car.set_car_motion(0.25, 0, 0)time.sleep(5)self.auto_control = Falseself.car.set_car_motion(0, 0, 0)time.sleep(.1)self.car.set_car_motion(-0.25, 0, 1.5)time.sleep(4)self.car.set_car_motion(-0.25, 0, 0)time.sleep(1)self.car.set_car_motion(0, 0, 0)
Traffic light function
When the red light goes out, the car moves forward
xxxxxxxxxxdef car_no_light(self):self.car_run()time.sleep(5)
When the red light comes on, the car stops
xxxxxxxxxxdef car_red_light(self):if self.car_state == 1:self.auto_control = Falseself.car_stop()time.sleep(.5)self.car.set_car_motion(-self.car_speed, 0, 0.01)time.sleep(2)self.car_stop()time.sleep(.1)self.car_state = 0
Precautions
Since traffic lights only use red lights, and the red lights are relatively high, in practical applications it is necessary to adjust the angle of the camera and slightly increase the camera angle of view.

If the traffic light function is used and the camera angle is increased, the car will turn in advance, which may cause the problem of pressing the inner line.
Since it takes a long time to run the code, for the sake of safety, the four wheels of the car are raised in the air to avoid the problem of the car running around after starting.