ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to know the correct publisher type

asked 2013-05-13 09:31:15 -0500

Iván gravatar image

updated 2014-04-20 14:09:21 -0500

ngrennan gravatar image


I'm trying to make a program works with roscopter ( In python I don't have any problem, but when I try to translate that to cpp, I don't know how to use the publisher and the subscriber.

In python, the publisher is just:

pub_rc = rospy.Publisher('send_rc', roscopter.msg.RC)

where "roscopter.msg.RC" is the type, and 'send_rc' is the variable that I want to publish.

In cpp, I think that I have to do something like this:

ros::NodeHandle nh; ros::Publisher publisher = nh.advertise< ::roscopter::RC>("send_rc", 1);

But that doesn't work, and I think than it is because < ::roscopter::RC> is not the correct type. Does someone know some way to know the right type?

Thank you!

---- EDIT ----

With the solution of jarvisschultz it compiles. But I think there is still a problem with the type. "msg" should be an array int32[8], but I cannot fill that array.

I try just with: roscopter::RC msg; msg[2] = 1000;

And the error is: no match for ‘operator[]’ in ‘msg[2]’

edit retag flag offensive close merge delete


The first set of double colon's inside of the typedef seem weird to me... try <roscopter::RC> . Also be sure to #include "roscopter/RC.h"

jarvisschultz gravatar image jarvisschultz  ( 2013-05-13 10:38:03 -0500 )edit

I already have that include. And I used < ::roscopter::RC> just because I thought it should be some similar to that and in the RC.h file I read this line: typedef boost::shared_ptr< ::roscopter::RC_<ContainerAllocator> > Ptr; and a lot of similar lines.

Iván gravatar image Iván  ( 2013-05-13 11:33:07 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2013-05-14 06:03:32 -0500

Here is a tiny little example adapted from the roscpp publisher/subscriber tutorial.

#include <ros/ros.h>
#include "roscopter/RC.h"

int main(int argc, char **argv)
    ros::init(argc, argv, "rc_sender");
    ros::NodeHandle n;
    ros::Publisher rc_pub = n.advertise<roscopter::RC>("send_rc", 1000);
    ros::Rate loop_rate(10);
    int count = 0;
    while (ros::ok())
    roscopter::RC msg;
    return 0;

My manifest.xml (or package.xml in Groovy) declares the roscopter package as a dependency. Also, my CMakeLists.txt file is set to compile this file. Running the produced executable indeed publishes an empty roscopter/RC message at 10 Hz.

edit flag offensive delete link more


Thank you, now it compiles. But now I have the problem that I show at the end of my question.

Iván gravatar image Iván  ( 2013-05-15 01:08:30 -0500 )edit

Your line of code should probably be something like[2] = 1000;

jarvisschultz gravatar image jarvisschultz  ( 2013-05-15 04:00:27 -0500 )edit

Thank you!!! It works!!

Iván gravatar image Iván  ( 2013-05-15 08:44:16 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-05-13 09:31:15 -0500

Seen: 312 times

Last updated: May 15 '13