Robotics StackExchange | Archived questions

using ros topic tool from c++

Hi,

I need to implement a topic mux(several topics to a single target). I found out there is a tool for that! topictool mux does it. So all I need to do is to invoke topictool from my c++ code. but I can't find the way to do it or any example.

I saw that once I created a mux I can change it with ros services from my c++ code, but I don't know how to start the ros node except of running it from cmd.

Thanks

Asked by matanros on 2019-08-22 04:02:47 UTC

Comments

Answers

Hi. Welcome to ROS.

In your code, you just have to use regular publishers and subscribers. The multiplexing is then done outside. So for example if you run:

rosrun topic_tools mux sel_cmdvel auto_cmdvel joystick_cmdvel mux:=mux_cmdvel

Then you could publish in your nodes on the topics auto_cmdvel or joystick_cmdvel

ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("auto_cmdvel", 5);
geometry_msgs::Twist cmd;
cmd.linear.x = 1
pub.publish(cmd);

And in another node you can subscribe to sel_cmdvel (just like you would to any other topic)

ros::Subscriber sub = nh.subscribe("sel_cmdvel", 1, callback);

Look here for more info on topics in general: https://wiki.ros.org/ROS/Tutorials/UnderstandingTopics and in cpp: https://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers

I saw that once I created a mux I can change it with ros services from my c++ code, but I don't know how to start the ros node except of running it from cmd.

For this you need a launchfile: https://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch#The_Launch_File


edit:

To use the service, you have to follow https://wiki.ros.org/roscpp_tutorials/Tutorials/WritingServiceClient#Writing_the_Client_Node For example to add a new input topic:

ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<topic_tools::MuxAdd>("mux/add");
topic_tools::MuxAdd srv;
srv.request.topic = "my_topic_to_add";
client.call(srv)

But you have to start the topic_tools mux command beforehand. your code then communicates with its services. For example in a launchfile:

<launch>
  <node pkg="topic_tools" name="my_little_mux" type="mux"/>
</launch>

You'll have to try it but you can probably do all the configuration from your cpp program, then.

Asked by ct2034 on 2019-08-22 05:45:59 UTC

Comments

Thanks but I'm looking for a way to run the rosrun topic_tools mux sel_cmdvel auto_cmdvel joystick_cmdvel mux:=mux_cmdvel from the c++ so it will be more abstract. I need to toggle between few unknown sources so I want to start the mux from the c++ ...

Asked by matanros on 2019-08-22 05:56:34 UTC