0
$\begingroup$

Rosanswers logo

Hi,

I'm trying to make a program works with roscopter (https://code.google.com/p/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]’


Originally posted by Iván on ROS Answers with karma: 3 on 2013-05-13

Post score: 0


Original comments

Comment by jarvisschultz on 2013-05-13:
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"

Comment by Iván on 2013-05-13:
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_ > Ptr; and a lot of similar lines.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Here is a tiny little example adapted from the roscpp [publisher/subscriber](http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber(c%2B%2B) 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;
    rc_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
    }
    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.


Originally posted by jarvisschultz with karma: 9031 on 2013-05-14

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Iván on 2013-05-15:
Thank you, now it compiles. But now I have the problem that I show at the end of my question.

Comment by jarvisschultz on 2013-05-15:
Your line of code should probably be something like msg.channel[2] = 1000;

Comment by Iván on 2013-05-15:
Thank you!!! It works!!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.