If you have a ROS driver for the laser scanner on your robot, then you should also have laser scan messages published on a specific topic name.
Find out the topic and then look for the message type.
Normally planar laser scans are published using the LaserScan
message which is defined in the sensor_msgs
package.
See here:
sensor_msgs/LaserScan Message
You will see that the message does not contain the angle of each measurement, but the start angle of the scan and the angle increment.
Taking everything into account, these are the main steps to get the distance and angle for each ray in a LaserScan
message:
- Create a ros node that subscribes to your laser scanner topic
- Specify a callback function to run when the subscriber receives a message
- Upon incoming messages, iterate over all laser rays
- Skip laser rays with the value 'inf' (rays that didn't hit anything)
To have your measurements in polar coordinates (distance and angle w.r.t. your device):
Use the fields angle_min
, the start angle of the scan [rad] and angle_increment
, the angular distance between measurements [rad], to get each's ray angle, like this:
ray_angle = angle_min + (i * angle_increment)
, where i
is the loop counter that change with each iteration of the loop
Get the distance of the corresponding ray from the data field ranges[i]
This is how to find the position of a detected obstacle in polar coordinates in the reference frame of your sensor.
Most probably you will also want to transform these values to get cartesian coordinates from polar ones.
Also consider if you want to transform these into the global frame or not.