Ask Your Question

Mapping with Sonar Data?

asked 2012-08-15 22:05:33 -0500

MarkyMark2012 gravatar image

updated 2016-04-12 02:15:13 -0500

Hi All,

Had anyone had any success creating a reasonable map in ROS with just sonar and odometry data?

If so I'd like to know what map type; setup etc you used.

Currently the map is a mess (An only home brew solution works just fine) so I'm sure it must be possible.

FYI the Sonar data is being published as both Range and laser_scan message types.

EDIT - Sonar Laser Code for benefit of others:

bool ToeminatorROSBridge::publishSonarAsLaserData(ros::Time time, RobotData* pRobotData)
tf::TransformBroadcaster laser_broadcaster;
ros::NodeHandle n;

// Laser/Sonar Range Test
bool publish_laser = true;
n.param("publish_laser", publish_laser, publish_laser);
ros::Publisher laser_pub;

     laser_pub = n.advertise<sensor_msgs::LaserScan>("sonar_laser_scan", 1000);

unsigned int num_readings = 7;
double laser_frequency = 2;
//populate the LaserScan message
sensor_msgs::LaserScan scan;
   scan.header.stamp = time;
   scan.header.frame_id = "base_laser";
   scan.angle_min = -1.57;
   scan.angle_max = 1.57;
   scan.angle_increment = 3.14 / num_readings;
   scan.time_increment = (1 / laser_frequency) / (num_readings);
   scan.range_min = 0.0;
   scan.range_max = 10.0;


   for(int i=1; i<=7; i++)
    scan.ranges[i] = pRobotData->SonarMeasurements[i-1]/100.0; // Convert to Meters             


laser_broadcaster.sendTransform(tf::StampedTransform(tf::Transform(tf::createQuaternionFromRPY(0, 0, degree_to_radian(ROS_FrontSonarAngle)), 
                                                                    tf::Vector3(0.0, 0.15, 0.0)),
return true;

Many Thanks


edit retag flag offensive close merge delete


Hi Mark, Did you make any progress on this? I am currently grappling with the exact same question. Thanks!

Kristof gravatar image Kristof  ( 2013-07-14 02:44:28 -0500 )edit

Hi Kirstof - sorry for the late reply. No not really - I upgraded to groovy and knackered my system & not really had time to sort it out. I was slightly perplexed why I couldn't get anything decent when my home brew app could do a reasonable job.

MarkyMark2012 gravatar image MarkyMark2012  ( 2013-08-27 10:58:45 -0500 )edit

Hi Mark, I did the same thing with 13 sonar. But have problem to clear obstacles in map when the obstacle is not there anymore. Can you give me some advice?

albert gravatar image albert  ( 2016-05-25 00:24:02 -0500 )edit

@albert What map(s) set up do you have?

MarkyMark2012 gravatar image MarkyMark2012  ( 2016-05-31 11:00:01 -0500 )edit

I used the costmap_2d package in the navigation stack,

albert gravatar image albert  ( 2016-06-01 02:04:47 -0500 )edit

can you upload the code of costmap_2d ? i have the same problem

bill gravatar image bill  ( 2017-01-12 12:46:10 -0500 )edit

costmap_2d is part of the navigation stack. Like most ROS packages, the link to the source code is on the costmap_2d wiki page , and that points to github:

ahendrix gravatar image ahendrix  ( 2017-01-12 13:07:11 -0500 )edit

i mean ,how you connect the sonar data with the costmap_2d, sorry for the stupid question but i am new in ros

bill gravatar image bill  ( 2017-01-13 10:40:10 -0500 )edit

6 Answers

Sort by ┬╗ oldest newest most voted

answered 2012-08-16 14:32:57 -0500

cagatay gravatar image

Ultrasonic sensors suffer from several drawbacks such as bandwith and cross-sensivity. Published accuracy values for ultrasonics are based on perpendicular reflections of the sound wave off an acoustically reflective material. Narrow opening angle for the sound beam in order to obtain precise directional information about objects that are encountered, you cannot acquire depth data points but rather entire regions of constant depth this means the sensors tell us only that there is a object at the area of the measurement cone. If two or more sonar's are fired together then they could pick up each other "ping" resulting in a false readings. These limitations make ultrasonic rangers bad map builders in my opinion. Hope helps you

edit flag offensive delete link more


Yep I know - as mentioned maps I've generated in the past (as have others) - non ROS give a resonable depiction of the environment. I'm thinking SLAM needs more data in this case. Any suggestion for a simple occupancy grid I could use?

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-08-16 22:38:54 -0500 )edit

answered 2012-08-16 06:15:02 -0500

I did something similar.

Since most of the packages use a laser message you can construct a laser_scanner message using the different readings of your sonar and then use this message in any of the Slam packages available on ROS. However the results may not be very good depending on the quality of the readings.

edit flag offensive delete link more


Yeah that's what I've done - the 7 sonars are represeted as 7 laser_scan messages. Although results are very poor. Did you get anything approaching that of usable out of it? Part of the problem, I think, is that the cone of the sonar isn't represented.

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-08-16 06:22:18 -0500 )edit

Hello mark, I am also thinking of doing the same thing with Sonar...but where did u write the code for it? And how?

mukut_noob gravatar image mukut_noob  ( 2016-04-07 00:32:11 -0500 )edit


I used the laser message format to construct a message from the sonar data. Obviously this data is very course on comparison and to be honest the results weren't that good.

I'll put the code when I get a chance if you need it


MarkyMark2012 gravatar image MarkyMark2012  ( 2016-04-07 02:41:00 -0500 )edit

Yes please! It would be very helpful.

mukut_noob gravatar image mukut_noob  ( 2016-04-07 02:55:22 -0500 )edit

Added to the original question

MarkyMark2012 gravatar image MarkyMark2012  ( 2016-04-11 16:30:56 -0500 )edit

Okk thank you very much...BTW what microcontroller are u using?

mukut_noob gravatar image mukut_noob  ( 2016-04-12 00:26:17 -0500 )edit

Actually I am on a Raspberry Pi 2 and For the Sonar I need to mention the gpio pin numbering so where can I do it and can the code that u have written be written in python?

mukut_noob gravatar image mukut_noob  ( 2016-04-12 00:28:07 -0500 )edit

Re Python - Yes that can be converted, then you can add the sonar driver/gpio code to it.

MarkyMark2012 gravatar image MarkyMark2012  ( 2016-04-12 02:11:02 -0500 )edit

answered 2016-05-31 12:15:11 -0500

TimboInSpace gravatar image

Hi MarkyMark,

I have had success with this setup. I made a small differential-drive bot that mapped using sonar, wheel odometry, and inertial sensors. Some of the key parts that made it work: - Stagger your sonar sensor's phase, especially if the sensors are looking in the same direction (or opposite by 180degrees). Use time division on the sampling. This greatly reduces noise. - BUFFER YOUR SONAR DATA. This is extremely important! My implementation used two sonar sensors that swept side to side. To get it to work, I had to buffer it for ~0.5s (thus creating a "laserscan" of about 25 data points) - it will not work very well if you publish single- or two-point "laserscans" - Use a coarse occupancy gridmap. I had good results with only 2.5cm pixels and large. (Also, since I was doing this on an arduino, it was a great way to drop the floating point numbers and use raw bytes instead) - Your localization will suffer from the extra delay due to buffering the sonar. If your robot doesn't need to respond quickly, it's better to use a coordinate transform snapshotted during the middle of the buffering operation, not at the end.

So YES this was a success for my project. Slow localization wasn't an issue for the environment I was using the robot in. If you have an extra couple hundred dollars, I'd opt for a cheap laserscanner. The objective of my project was to do SLAM on as small a budget as possible.

edit flag offensive delete link more


Hi Timboln, I am using a similar setup with 5 sonar, odometry and imu with arduino. can you give some more idea on converting sonar data to laser data ? any links or video of ya robot will help. I want to do collision avoidance and localization in a known map.

roborag gravatar image roborag  ( 2017-01-21 01:52:08 -0500 )edit

@TimboInSpace´╝îwhat did you mean " Use time division on the sampling. BUFFER YOUR SONAR DATA,To get it to work, I had to buffer it for ~0.5s (thus creating a "laserscan" of about 25 data points)",how did you create 25 points with your 2 sonars? Thanks a lot

jxl gravatar image jxl  ( 2017-04-13 03:14:52 -0500 )edit

Example: assuming the range is 3 meters, the time needed to capture 1 sample is (6 m / 340 m/s) = 0.0029 s. Rounding down. Assuming that you get up to 5 measurements per sample and running nav stack at 10 Hz, that results in 25 measurements per cycle.

Mbuijs gravatar image Mbuijs  ( 2018-05-28 03:08:07 -0500 )edit

answered 2012-08-16 04:44:59 -0500

joq gravatar image

I've never tried it.

Maybe someone has succeeded, but most people report needing something more accurate, like a laser range finder.

edit flag offensive delete link more



My home brew (non-ROS) mapping s/w does a reasonable job. It's a dynamic occupancy grid however. Was hoping there was a ROS way :)

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-08-16 05:15:49 -0500 )edit

Would you be able to post your homebrew solution somewhere? I'm attempting a very similar project and am about to go down the ultrasonic > laserscan conversion route. Also did you eventually have any success with this approach?

TimboInSpace gravatar image TimboInSpace  ( 2014-04-18 13:05:08 -0500 )edit

Hi - Nope didn't get anywhere with it annoyingly - although trying to get an XTION to play ball at the moment. Re home brew solution - the core is a Bayesian probability map with a navigation side. Nothing to do with ROS - not sure if that's what you want?

MarkyMark2012 gravatar image MarkyMark2012  ( 2014-04-22 09:23:15 -0500 )edit

"nothing to do with ROS" what does that even mean? Just wrap your code into a ROS package and let people use it if it is as good as it sounds.

Mehdi. gravatar image Mehdi.  ( 2016-04-04 04:53:26 -0500 )edit

means I wrote my own algo's/mapping/nav code from scratch

MarkyMark2012 gravatar image MarkyMark2012  ( 2016-05-31 10:58:39 -0500 )edit

hi,@MarkyMark2012, which slam packages did you use for sonar slam? gmapping, karto? Thanks!

scopus gravatar image scopus  ( 2017-10-22 00:47:08 -0500 )edit

Not sure about MarkyMark2012, but I used mine with gmapping on a fairly coarse map. Worked decently in the end. Good luck!

TimboInSpace gravatar image TimboInSpace  ( 2017-10-23 11:19:37 -0500 )edit

Thank you very much for your advices@TimboInSpace

scopus gravatar image scopus  ( 2017-10-24 19:33:41 -0500 )edit

answered 2017-12-20 00:31:16 -0500

sonarsensor gravatar image

Hello, what controller did you use?

edit flag offensive delete link more


Please don't use an answer to ask a question. You can use the comments for that in this situation. This isn't a forum.

jayess gravatar image jayess  ( 2018-01-04 03:47:47 -0500 )edit

answered 2017-10-23 12:04:36 -0500

RiccardoGiubilato gravatar image

Hi! Please take a look at the node "laser_to_occupancy_grid" in this repository . Here I control a cheap car using an arduino with an ultrasonic sensor to build a very coarse map based on imu/wheel odometry. You can also find the arduino sketch and ready-to-use launch files.. Hope it helps! Riccardo

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2012-08-15 22:05:33 -0500

Seen: 5,259 times

Last updated: Oct 23 '17