ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

using ros topic tool from c++

asked 2019-08-22 04:02:47 -0600

matanros gravatar image

Hi,

I need to implement a topic mux(several topics to a single target). I found out there is a tool for that! topic_tool mux does it. So all I need to do is to invoke topic_tool 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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-08-22 05:45:59 -0600

ct2034 gravatar image

updated 2019-08-22 07:46:08 -0600

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/Un... and in cpp: https://wiki.ros.org/roscpp/Overview/...

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/Us...


edit:

To use the service, you have to follow https://wiki.ros.org/roscpp_tutorials... 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.

edit flag offensive delete link more

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++ ...

matanros gravatar image matanros  ( 2019-08-22 05:56:34 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-08-22 04:02:47 -0600

Seen: 996 times

Last updated: Aug 22 '19