Welcome to MicroROS Self-balancing Car repository
1.Lidar avoid
Radar obstacle avoidance
Note: The virtual machine needs to be in the same LAN as the car, and the ROS_DOMAIN_ID needs to be consistent. You can check [Must-read before use] to set the IP and ROS_DOMAIN_ID on the board.
1. Program function description
The car connects to the proxy and runs the program. The radar on the car scans whether there are obstacles within the set range. If there are obstacles, it will automatically adjust the speed according to the location of the obstacle to avoid the obstacle. The dynamic parameter regulator can adjust the parameters such as the radar detection range and obstacle avoidance detection distance.
2. Start and connect the agent
Take the matching virtual machine as an example, enter the following command to start the agent,
sudo docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm --privileged --net=host microros/micro-ros-agent:humble udp4 --port 8899 -v4 
Then, turn on the car switch and wait for the car to connect to the agent. The connection is successful as shown in the figure below,

3. Start the program
3.1 Run command
Take the matching virtual machine as an example, input in the terminal,
xxxxxxxxxxros2 run yahboomcar_laser laser_Avoidance 
As shown in the figure above, if the radar on the car does not detect an obstacle, it will move forward. You can set some parameters through the dynamic parameter regulator, input in the terminal,
xxxxxxxxxxros2 run rqt_reconfigure rqt_reconfigure

Note: When you first open it, there may be no above nodes. Click Refresh to refresh and you can see all the nodes. The displayed laser_Avoidance is the node of radar obstacle avoidance.
The above parameters are explained as follows:
linera: Linear velocity
angular: Angular velocity
LaserAngle: Radar detection angle
ResponseDist: Obstacle detection distance. When the detected object is within this range, it is considered an obstacle
Switch: Gameplay switch
After modifying the above parameters, you need to click on the blank space to pass the parameters into the program.
4. Code analysis
Source code reference path (taking the supporting virtual machine as an example):
laser_Avoidance, the core code is as follows,
x#Create a radar subscriber to subscribe to radar data and remote control data and a speed publisher to publish speed data#create a subself.sub_laser = self.create_subscription(LaserScan,"/scan",self.registerScan,1)self.sub_JoyState = self.create_subscription(Bool,'/JoyState', self.JoyStateCallback,1)#create a pubself.pub_vel = self.create_publisher(Twist,'/cmd_vel_bl',1)
#Radar callback function: process subscribed radar datadef registerScan(self, scan_data): if not isinstance(scan_data, LaserScan): return ranges = np.array(scan_data.ranges) self.Right_warning = 0 self.Left_warning = 0 self.front_warning = 0
for i in range(len(ranges)): angle = (scan_data.angle_min + scan_data.angle_increment * i) * RAD2DEG #Determine whether there are obstacles in front, left, or right according to the set radar detection angle and obstacle detection distanceif angle > 180: angle = angle - 360#60 means that the range of radar detection is set to 120 degreesif 20 < angle < self.LaserAngle: if ranges[i] < self.ResponseDist: self.Left_warning += 1if -self.LaserAngle < angle < -20: if ranges[i] < self.ResponseDist: self.Right_warning += 1if abs(angle) <= 20: if ranges[i] <= self.ResponseDist: self.front_warning += 1 #According to the detected obstacles, publish the speed of the car to let the car avoid the obstaclesif self.front_warning > 10 and self.Left_warning > 10 and self.Right_warning > 10: print ('1, there are obstacles in the left and right, turn right') twist.linear.x = self.linear twist.angular.z = -self.angular self.pub_vel.publish(twist) sleep(0.2)
elif self.front_warning > 10 and self.Left_warning <= 10 and self.Right_warning > 10: print ('2, there is an obstacle in the middle right, turn left') twist.linear.x = self.linear twist.angular.z = self.angular self.pub_vel.publish(twist) sleep(0.2) if self.Left_warning > 10 and self.Right_warning <= 10: twist.linear.x = self.linear twist.angular.z = -self.angular self.pub_vel.publish(twist) sleep(0.5).....