Ask Your Question

Revision history [back]

At first glance you seem to have everything setup correctly. The line in your code

pub_.publish(output);

Is how you publish the new converted message.

Can you describe you problem in more detail, are you getting any error messages? You could add a ROSINFO call within your callback method to verify that it's being executed when a /scan message is published.

At first glance you seem 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 have everything setup correctly. The line 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.

pub_.publish(output);
 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);
 }

Is how you publish the new converted message.

Can you describe you problem in more detail, are you getting any error messages? You could add a ROSINFO call within your callback method to verify that it's being executed when a /scan message is published.