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.
Is there any way to have the publisher outside the void loop?