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

Rosserial/Arduino Publishers and Subscribers

asked 2011-07-19 06:47:58 -0500

UAV gravatar image

updated 2011-07-20 04:59:25 -0500

We have been working with arduino and rosserial, and we were wondering if it is possible to both send and receive information through the arduino so that it can communicate with the computer, instead of only sending information to or receiving information from the computer.

If so, does anyone have an example code we could see to point us in the right direction?

Thanks!

edit retag flag offensive close merge delete

Comments

Is there any way to have the publisher outside the void loop?

subarashi gravatar image subarashi  ( 2020-06-29 03:33:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
7

answered 2011-07-19 15:40:15 -0500

fergs gravatar image

updated 2011-07-20 05:28:50 -0500

Yes, you can have many publishers and subscribers at the same time -- the current implementation supports up to 25 of each on the Arduino (you'll probably run out of bandwidth before you hit that though).

The following can be found under ros_lib/examples folder as float64_test.pde:

/* 
 * rosserial::std_msgs::Float64 Test
 * Receives a Float64 input, subtracts 1.0, and publishes it
 */

#include <ros.h>
#include <std_msgs/Float64.h>

float x; 

ros::NodeHandle nh;

ROS_CALLBACK(messageCb, std_msgs::Float64, msg)
  x = msg.data - 1.0;
  digitalWrite(13, HIGH-digitalRead(13));   // blink the led
}

std_msgs::Float64 test;
ros::Subscriber s("your_topic", &msg, &messageCb);
ros::Publisher p("my_topic", &test);

void setup()
{
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.advertise(p);
  nh.subscribe(s);
}

void loop()
{
  test.data = x;
  p.publish( &test );
  nh.spinOnce();
  delay(10);
}

Here we subscribe to "your_topic", and publish to "my_topic". The publisher publishes at ~100hz, and the value is always the last message received - 1.0.

edit flag offensive delete link more

Comments

Thanks! This is very helpful! And on another note, apparently we were way over-thinking this...
UAV gravatar image UAV  ( 2011-07-20 05:37:53 -0500 )edit
Yep, we've tried to make this as easy as possible, while maintaining the feel of roscpp (as much as possible, some limitations on Arduino have forced things like the ROS_CALLBACK macro).
fergs gravatar image fergs  ( 2011-07-21 08:30:11 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2011-07-19 06:47:58 -0500

Seen: 12,620 times

Last updated: Jun 29 '20