Welcome to ROSMASTER A1 repository
6. Traffic light recognition
Traffic Light Recognition
1. Course Content
Note: This program runs on a sandbox map; you will need to adjust the program for your personal map.
- Learn the actions the car takes after recognizing traffic lights
- Learn the newly emerged key source code
2. Road Sign Detection
Road sign detection source code path:
/home/jetson/yahboomcar_auto/src/auto_driver/scripts/auto_drive.py
3. Program Startup
3.1 Startup Command
- Parameter Configuration
Parameter path: /home/jetson/yahboomcar_auto/src/auto_driver/config/auto_drive_node.yaml
xxxxxxxxxxturn_radius0.4linear_speed0.13angular_speed-0.55duration2.0response_distance_12900response_distance_2132cooldown_time0.2DEBUG_MODEFalseDEBUG_MODE_2TrueSTART_AutoDriveTrueKp-0.62Ki0.0Kd-0.1max_integral1000.0image_size640 480After modifying parameters, restart the launch file to use the modified parameters.
- Start camera + chassis + autonomous driving node (need to run in virtual environment terminal)
xxxxxxxxxxroslaunch auto_driver auto_drive.launch
After successful startup, the display will not move to test road sign recognition. (If you want it to move, change the parameter START_AutoDrive in auto_drive_node.yaml to True)

After the program runs, it will display the YOLO recognition screen. The car will run centered on the lane lines and perform corresponding actions based on the road signs recognized during driving.
3.2 Car movement after recognizing traffic lights
When the program recognizes red light, the program will print

The car will keep stopping at red light and wait for green light

The red light handling function is in the auto_drive.py file. Below is the function that is entered after recognizing red light. Users can modify the recognized actions themselves
xxxxxxxxxxdef red_stop(self): self.flag = True rospy.loginfo('red_stop') self._set_current_speed(linear_x=0.0)When the program recognizes green light, the program will print Go straight, the car releases the parking state and moves forward along the initial speed.

The green light handling function is in the auto_drive.py file. Below is the function that is entered after recognizing green light. Users can modify the car's movement after recognizing the road sign themselves
x
def go_straight(self): rospy.loginfo('Go straight') self.flag = False self._set_current_speed(linear_x=self.drive_speed, angular_z=0.0)4. Source Code Analysis
4.1, auto_drive.py
YOLO signpost message reception:
xxxxxxxxxxresults = self.sign_detect.get_infer_result(frame, True)annotated_frame = frame.copy()Multiple filtering mechanisms: mainly to prevent continuously recognizing road signs and triggering actions; the action will only be unlocked when different road signs are recognized.
xxxxxxxxxx # If a sign that requires cooldown is detected, start cooldownif detected_sign_requires_cooldown: self.sign_cooldown = True self.last_sign_time = current_time rospy.loginfo(f"Sign detected, starting {self.cooldown_duration}s cooldown") # Check if the repeated sign should be ignoredshould_ignore = Falseif obj.class_name == self.last_sign: should_ignore = TrueEvent queue processing
xxxxxxxxxxdef _event_handling_worker(self): '''Event handling worker thread''' # Sign handling mapping table sign_handlers = { "straight": self.go_straight, "turnright": lambda: self.turn('right'), "turnright_ground": self.do_nothing, "honking": self.car_horn, "sidewalk": self.slow_down, "sidewalk_ground": self.do_nothing, "speedlimit": self.slow_down, "stop": self.stop, "school": self.school, "parkA": self.parking_A, "parkB": self.parking_A, "greenlight": self.go_straight, "redlight": self.red_stop, "yellowlight": self.stop, "out_park": self.out_parking } # Sign enable index mapping sign_enable_map = { "straight": 10, "turnright": 11, "honking": 1, "sidewalk": 6, "sidewalk_ground": 7, "speedlimit": 8, "stop": 9, "school": 5, "parkA": 2, "parkB": 3, "greenlight": 0, "redlight": 4, "yellowlight": 13 } Specific action implementation
x
def red_stop(self): rospy.loginfo('red_stop') self.flag = True self._set_current_speed(linear_x=0.0)def go_straight(self): rospy.loginfo('Go straight') self.flag = False self._set_current_speed(linear_x=self.drive_speed, angular_z=0.0)