how to dynamic set subscribe type

asked 2021-07-07 00:46:04 -0500

improve100 gravatar image

Some people have a good way to dynamically replace topic_type to rslidar_msgs :: rslidarscan type from argument?


#include "std_msgs/Empty.h"
#include <rslidar_msgs/rslidarScan.h>
ros::Publisher pub;

template <typename T>
void Callback(T  msg) {
    // ROS_INFO("Callback");
    msg.header.frame_id = "lidar";
    pub.publish(msg); 
}

int main(int argc, char **argv){
   ros::init(argc, argv, "relaytopic", ros::init_options::AnonymousName);
   ros::NodeHandle nh("~");
   std::string topic_in,topic_out;
   std_msgs::Empty topic_type;
   topic_in = nh.param<std::string>("in","topic_in");
   topic_out = nh.param<std::string>("out","topic_out");
  // topic_type = nh.param<std_msgs::Empty>("~type",Empty);
  if(topic_in == "topic_in" || topic_out == "topic_out")
  std::cout<<"usage e.g.:./relaytopic _in:=\"hello\" _out:=\"hello2\" _type:=Int32"<<std::endl;
  ros::Subscriber sub = nh.subscribe(topic_in, 1000, Callback<rslidar_msgs::rslidarScan>);
  pub = nh.advertise<rslidar_msgs::rslidarScan>(topic_out, 1000);
  ros::spin();

  return 0;
}
edit retag flag offensive close merge delete