ROS Basics using Python ROSJECT

In the first part of my rosject I have subscribed to the /scan node. Now how do I implement the logic for making it understand the distance to wall using /scan and then proceed further?

Hi @priyam7983ddb06e5f4641

Inside of the entired course, you see scan topic and his parts. If you don’t remember check this link: sensor_msgs/LaserScan Documentation

Here you can find ranges, which has all the measurements of the lidar ray.

Hi @priyam7983ddb06e5f4641

The ‘ranges’ array (/scan data) has a lot of values. The ones that are in the middle of the array represent the distances that the laser is detecting right in front of it. This means that the values in the middle of the array will be the ones that detect the wall. So in order to avoid the wall, you just have to read these values. The scope of the laser is about 180 degrees from right to left. This means that the values at the beginning and at the end of the ‘ranges’ array will be the ones related to the readings on the sides of the laser (left and right), while the values in the middle of the array will be the ones related to the front of the laser. Have a look at the image below in order to better understand this.

So logic of the code will be something like this

def callback(msg):
Pub1.publish(move_robot)
if(msg.ranges[360] < 1):
move_robot.linear.x = 0.0
move_robot.angular.z = 0.5
if(msg.ranges[719] < 1 and msg.ranges[400] == float(‘inf’)):
move_robot.linear.x = 0.5
move_robot.angular.z = 0.0
if(msg.ranges[10] == float(‘inf’) and msg.ranges[719] == float(‘inf’) and msg.ranges[400] == float(‘inf’)):
move_robot.linear.x = 0.0
move_robot.angular.z = 0.0
rospy.loginfo(‘I Stopped…!’)

Moreover I am attaching a link to tutorial which will give you indepth idea about logic of code:https://youtu.be/RFNNsDI2b6c?si=iOshHcw2zLK7cPi9

Regards,
Raunak