How to subscribe "/scan" topic, modify message Laserscan.ranges and publish it again to "/scan" topic?

Hi everyone,

I would like to improve turtlebot3 LDS-01 sensor by applying some algorithm to the sensor. So my strategy is to improve and modify the value of Laserscan.ranges (which is the distance between the sensor to the obstacles) by subscribing the “/scan” topic, applying the algorithm to the Laserscan.ranges and publish it again to the “/scan” topic. How can I do that?. I have some idea in my mind like:

  1. Create new topic, publish the sensor value to the new topic, subscribe the new topic and applying the algorithm and lastly, publish it to the “/scan” topic. But this first idea, I cannot find the cpp or python file that publish to “/scan” topic. Where can I find that file? So that I can change the coding to publish the Laserscan.ranges to the new topic.

  2. Modify the sensor_msgs/LaserScan by applying the algorithm to the float32 range. But this second idea, I dont know is it the message can be modified? If can, how to modify the sensor_msgs/LaserScan to apply the algorithm?

And lastly, if there are any suggestion, tutorial or online courses that I can take related to this question, I would be very happy to hear.

This image is summary for the idea 1

oie_OwHCiDGuQDom|631x231

Publishing to /scan (or whatever the original LaserScan topic is) wouldn’t work because it will be overwritten by the next LaserScan message.

What you can try:

  1. Create a subscriber to the /scan topic as usual. Let’s call that sub1.
  2. Create a publisher for a new scan topic, say /scan_plus_plus. Call it pub1. This topic will be created automatically once you publish to it.
  3. Create a second subscriber to the /scan_plus_plus topic. Call it sub2 or so.
  4. In the callback for sub1, apply your algorithm to the LaserScan message sent. The message is just a regular Python object, and you can find the structure by running
rosmsg show LaserScan
  1. Still in the sub1 callback, publish the modified message to the new topic using the publisher pub1.
  2. In the callback of sub2, put the logic to control your robot.
  3. Add any other logic or refinement needed.
1 Like

Thank you very much for the answer. I’ll try it

I would like to ask. if I use Gmapping package, where can I edit to subscribe a new topic? Because I have search and didnt find the file that I can modified to subscribe the new topic

You should be able to do that by pointing your “dummy” topic to the gmapping package, as indicated in the docs:

rosrun gmapping slam_gmapping scan:=scan_plus_plus

The example I gave you is just hypothetical and assumed that you are going to create your own subscriber that uses the modified scan messages. If the modified scan messages will be used by a 3rd party package, you just need to find how to specify the topic to use in that package.

1 Like

Thank you very much sir. If you do not mind, can you help me answer this question also and it is related to this question. How to subscribe "/scan" topic, modify message Laserscan.ranges and publish it to the new topic?

You’re welcome. I see that someone has answered that question. Closing this for now.

1 Like