0
$\begingroup$

Rosanswers logo

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


Originally posted by farhatm on ROS Answers with karma: 28 on 2017-01-13

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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);
 }

Originally posted by PeteBlackerThe3rd with karma: 9529 on 2017-01-13

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by farhatm on 2017-01-13:
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

Comment by farhatm on 2017-01-16:
Thanks, now it works :)

Comment by Tider on 2018-01-25:
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);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.