Publish data of an existing C++ project into a ROS topic
Hi, I'm using a pretty complex c++ program from which I want to publish data as a ROS topic. I'd like not to change the existing program, I mean not much. What's the simplest/best way to do it.
Cheers
Asked by jamman on 2014-05-12 04:17:31 UTC
Answers
Check http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers
In terms of code, all that you need is to call ros::init, create a NodeHandle, and create a Publisher to start publishing data. You may have to define some custom .msg files if none of the provided work for your code. You will also need to create a Subscriber to listen to the data and do something with it.
Refer to http://wiki.ros.org/ROS/Tutorials/CreatingPackage to see how to create a ROS package.
Asked by sterlingm on 2014-05-12 10:37:43 UTC
Comments
Thank you. Is it mandatory to create a ROS package to use ROS functions ? Because I tried to call ros::init and I added "INCLUDE_DIRECTORIES("/opt/ros/hydro/include/")" in my CMakeLists.txt file and I get "undefined reference to ros::init....". So I'm wondering if that comes from the non use of ROS package.
Asked by jamman on 2014-05-12 22:52:50 UTC
In all of my ROS packages' CMakeLists.txt, I have target_link_libraries(executable_name ${catkin_LIBRARIES} ). You can try adding that since you have a linking error.
Asked by sterlingm on 2014-05-13 03:17:59 UTC
You don't need to create a ROS Package in order to publish messages. I also had to find a way to publish some messages from complex programs without a massive change or complex dependencies. So basically you have two options:
- Rosbridge: I consider this to be the most elegant and portable solution. You can publish and subscribe to topics from ANY programming languages that supports websockets. So if you think that you might later want to publish messages from other complex programs that are not in C++, this option is the best for you. More information about it in http://rosbridge.org/doku.php?id=rosbridge_protocol_v2.0
- Linking roscpp : This is also a solution you could consider. There is no need to learn new things like in the case of rosbridge. You just link the roscpp library and can directly use it in your C++ code, creating a node, subscribing,publishing etc... More details on how to do it in this wonderful tutorial : http://jbohren.com/articles/gentle-catkin-intro/ Under the section "Building with GNU make" it shows how to build a ros node manually and what libraries you need to link.
I hope this could help.
Asked by Mehdi. on 2014-05-13 19:02:04 UTC
Comments