Welcome to MicroROS-Pi5 repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
3. Object recognition and tracking
Object recognition and tracking
Note: The ROS_DOMAIN_ID of the Raspberry Pi and the microROS control board need to be consistent. You can check [MicroROS Control Board Parameter Configuration] to set the microROS control board ROS_DOMAIN_ID. Check the tutorial [Connect MicroROS Agent] to determine whether the IDs are consistent.
1、Program function description
After the program is started, select the object that needs to be tracked with the mouse, press the space bar, and the gimbal servo of the car will enter the tracking mode. The car gimbal will follow the movement of the tracked object and always ensure that the tracked object remains in the center of the screen.
2、Steps
2.1、start up
Enter docker and enter the following command in the terminal.
ros2 run yahboomcar_astra mono_Tracker
2.2、identify
After starting, enter the selection mode, use the mouse to select the target location, as shown in the figure below, release it to start recognition.

Keyboard key control:
【r】:In selection mode, you can use the mouse to select the area where you want to identify the target, as shown in the picture above.
【q】:exit the program.
【space bar】:Target tracking; just move the target slowly while following. If you move too fast, you will lose the target.

You can also use the dynamic parameter adjuster to debug PID parameters and input it into the Docker terminal.
xxxxxxxxxxros2 run rqt_reconfigure rqt_reconfigure

The adjustable parameters include the PID parameters of the car gimbal. After modifying the parameters, click "refresh" to refresh the data.
2.3、View node topic communication diagram
You can view the topic communication between nodes through the following command.

3、core code
The principle of function implementation is similar to that of color tracking. It calculates the rotation angle of s1 and s2 servos based on the sum of the center coordinates of the target, and then publishes it to the chassis. Part of the code is as follows.
x
#This part is to obtain the center coordinates after selecting the object, which is used to calculate the steering gear angle.if len(contours) != 0: areas = [] for c in range(len(contours)): areas.append(cv.contourArea(contours[c])) max_id = areas.index(max(areas)) max_rect = cv.minAreaRect(contours[max_id]) max_box = cv.boxPoints(max_rect) max_box = np.int0(max_box) (color_x, color_y), color_radius = cv.minEnclosingCircle(max_box)#This part is to calculate the values of center_x and distance.center_x = targEnd_x / 2 + targBegin_x / 2center_y = targEnd_y / 2 + targBegin_y / 2width = targEnd_x - targBegin_xhigh = targEnd_y - targBegin_yself.point_pose = (center_x, center_y, min(width, high))if self.Track_state == 'tracking': if self.circle[2] != 0: threading.Thread(target=self.execute, args=(self.circle[0], self.circle[1])).start()#Calculate the steering gear movement angle through pid[x_Pid, y_Pid] = self.PID_controller.update([point_x - 320, point_y - 240]) if self.img_flip == True: self.target_servox -= x_Pid self.target_servoy += y_Pid else: self.target_servox -= x_Pid self.target_servoy += y_Pid if self.target_servox >= 90: self.target_servox = 90 elif self.target_servox <= -90: self.target_servox = -90 if self.target_servoy >= 20: self.target_servoy = 20 elif self.target_servoy <= -90: self.target_servoy = -90