Multiple subscriptions for rosserial on Arduino

1,748 views
Skip to first unread message

Chuck Untulis

unread,
Feb 18, 2012, 2:04:09 AM2/18/12
to hbrob...@googlegroups.com

I have been through the ROS Arduino tutorials and been able to get most of them working including the Servo tutorial. I would like to take that code and expand it by adding a second subscriber for a different servo function like sweep instead of just moving the servo once.

 

When I try to add the second subscriber:

ros::Subscriber<std_msgs::UInt16> sub("once", once_cb);

ros::Subscriber<std_msgs::UInt16> sub("sweep", sweep_cb);

 

Arduino complains ( I think it as about trying to redefine ros::Subscriber, but I am not running Ubuntu at this point).  What is the syntax for defining two or more subscribers.

 

Thanks

 

Chuck

 

Michael Ferguson

unread,
Feb 18, 2012, 2:22:04 AM2/18/12
to hbrob...@googlegroups.com
You have to name the second one differently (both subscribers can't be named "sub"). For reference, this isn't rosserial specific, it's true of all C/C++

-Fergs

--
You received this message because you are subscribed to the Google Groups "HomeBrew Robotics Club" group.
To post to this group, send email to hbrob...@googlegroups.com.
To unsubscribe from this group, send email to hbrobotics+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hbrobotics?hl=en.

Chuck Untulis

unread,
Feb 18, 2012, 12:58:03 PM2/18/12
to hbrob...@googlegroups.com
Great.  Thanks.

Chuck Untulis

unread,
Feb 18, 2012, 2:18:19 PM2/18/12
to hbrob...@googlegroups.com
It worked. Thanks again. I will have to go back to the publish/subscribe documentation to see the syntax of messages.

So there are three things that have to happen:
  • nh.subscribe(<foo>);
  • ros::Subscriber<std_msgs::UInt16> <foo>("<foo>", <foo>_cb);
  • void <foo>_cb( const std_msgs::UInt16& cmd_msg){
  • }
Anything else?

Thanks

Chuck

Michael Ferguson

unread,
Feb 18, 2012, 10:16:40 PM2/18/12
to hbrob...@googlegroups.com
I wouldn't exactly order them that way, but yes.

1. You must define a callback (the function that takes the message and does something).

2. You must then define a subscriber, which is an object that parses raw messages coming over serial, and then sends the decoded message onto the callback.

3. Finally, you must tell the node handle (which processes raw serial bytes) about this subscriber using nh.subscribe().

For each topic you want to subscribe to, you need a new Subscriber (you could actually have two subscribers point to the same callback, if they did exactly the same thing on the exact same message type, although this probably isn't a useful function). Finally, there is only ONE node handle in most cases (if you were to have multiple serial channels you could have multiple node handles, but this is pretty advanced and is not needed/supported in rosserial_arduino)

-Fergs
Reply all
Reply to author
Forward
0 new messages