Ask Your Question
0

How to convert sensor_msgs/LaserScan.msg to sensor_msgs/MultiEchoLaserScan.msg

asked 2017-01-13 09:08:34 -0500

farhatm gravatar image

Hello,

I have a node that publish /scan topic and this topic has the massage type sensor_msgs/LaserScan.msg. another node can only subscribe sensor_msgs/MultiEchoLaserScan.msg massage type. The two massage type are very similar only ranges and intensities are different.

I thought I'd make a new node and this node convert the msg. My problem is, i have now idea how can i publich my convert message again. this code is my node "convert_LaserScan_to_MultiEchoLaserScan":

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "sensor_msgs/LaserScan.h"
#include "sensor_msgs/MultiEchoLaserScan.h"

class convert_LaserScan_to_MultiEchoLaserScan
 {
 public:
 convert_LaserScan_to_MultiEchoLaserScan()
  {
 //Topic you want to publish
  pub_ = n_.advertise<sensor_msgs::LaserScan>("/horizontal_laser_2d", 1000);

//Topic you want to subscribe
  sub_ = n_.subscribe("/scan", 1000, &convert_LaserScan_to_MultiEchoLaserScan::callback, this);
 }


 void callback(const sensor_msgs::LaserScan::ConstPtr& input)
{
sensor_msgs::LaserScan output;

sensor_msgs::MultiEchoLaserScan muls;
for (int i = 0; i < input->ranges.size(); ++i) {
    const float &range = input->ranges[i];
    ROS_INFO("ranges: [%f]",range);
    sensor_msgs::LaserEcho echo;
    echo.echoes.push_back(range);
    muls.ranges.push_back(echo);

}

pub_.publish(output);
 }

  private:
  ros::NodeHandle n_;
  ros::Publisher pub_;
  ros::Subscriber sub_;

  };//End of class convert_LaserScan_to_MultiEchoLaserScan


  int main(int argc, char **argv)
 {  
  //Initiate ROS
  ros::init(argc, argv, "convert_LaserScan_to_MultiEchoLaserScan");

  //Create an object of class SubscribeAndPublish that will take care of everything
  convert_LaserScan_to_MultiEchoLaserScan SAPObject;

  ros::spin();



  return 0;
}

I am grateful for your help

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2017-01-13 10:08:29 -0500

updated 2017-01-15 16:55:08 -0500

Okay, I think I've found the problem with your code. There were two things wrong, firstly the message publisher you had setup was publishing sensor_msgs::LaserScan messages. So it was setup to receive and broadcast the same message type. Secondly in your callback function, you created two message types. A sensor_msgs::LaserScan message called output and a sensor_msgs::MultiEchoLaserScan message called muls. You then copied the input data into the new muls message correctly, but then broadcast the empty output message.

I've updated the constructor and callback function in your code below. The callback function now creates a single MultiEchoLaserScan message and copies the input data into it. Then it publishes this new message type. I also updated the definition of the publisher in the constructor so that it publishes the right message type.

 convert_LaserScan_to_MultiEchoLaserScan()
  {
 //Topic you want to publish
  pub_ = n_.advertise<sensor_msgs::MultiEchoLaserScan>("/horizontal_laser_2d", 1000);

//Topic you want to subscribe
  sub_ = n_.subscribe("/scan", 1000, &convert_LaserScan_to_MultiEchoLaserScan::callback, this);
 }


 void callback(const sensor_msgs::LaserScan::ConstPtr& input)
{
//sensor_msgs::LaserScan output;

sensor_msgs::MultiEchoLaserScan muls;
for (int i = 0; i < input->ranges.size(); ++i) {
    const float &range = input->ranges[i];
    ROS_INFO("ranges: [%f]",range);
    sensor_msgs::LaserEcho echo;
    echo.echoes.push_back(range);
    muls.ranges.push_back(echo);

}

pub_.publish(muls);
 }
edit flag offensive delete link more

Comments

No, I d'ont get any error. but my problem is when i use

rostopic echo /horizontal_laser_2d

nothing change in the variable in my msg. my problem is i d´ont know how to use

pub_.publish(output);

so i can publich the msg right my node publich only 0

farhatm gravatar image farhatm  ( 2017-01-13 16:33:11 -0500 )edit

Thanks, now it works :)

farhatm gravatar image farhatm  ( 2017-01-16 04:10:04 -0500 )edit

I think muls.ranges.push_back(echo); is in the wrong place. The relative part code should be like this:

sensor_msgs::MultiEchoLaserScan muls;
sensor_msgs::LaserEcho echo;
for (int i = 0; i < input->ranges.size(); ++i) {
   ....
    }
muls.ranges.push_back(echo);
Tider gravatar image Tider  ( 2018-01-25 20:08:31 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-01-13 09:08:34 -0500

Seen: 3,239 times

Last updated: Jan 16 '17