ROS Arduino creating more than one subscriber (function)
Hello, So the issue im facing is easy to explain but im having very hard executing it cuz im a newbie in ROS.
http://wiki.ros.org/rosserial_arduino...
now as you see this example only executes one function from what I understood that if publish a message with the word "toggle_led" it will go on and execute the function "messageCb"
what I want is lets say I publish the message "toggle_led1" it will go and execute a function called "led1"
for example:
/*
* rosserial Subscriber Example
* Blinks an LED on callback
*/
#include <ros.h>
#include <std_msgs/Empty.h>
ros::NodeHandle nh;
void led1( const std_msgs::Empty& toggle_msg){
digitalWrite(11, HIGH-digitalRead(11)); // blink the led
}
void led2( const std_msgs::Empty& toggle_msg){
digitalWrite(10, HIGH-digitalRead(10)); // blink the led
}
void led3( const std_msgs::Empty& toggle_msg){
digitalWrite(9, HIGH-digitalRead(9)); // blink the led
}
void all( const std_msgs::Empty& toggle_msg){
digitalWrite(9, HIGH-digitalRead(9)); // blink the led
digitalWrite(10, HIGH-digitalRead(10));
digitalWrite(11, HIGH-digitalRead(11));
}
void light( const std_msgs::Empty& toggle_msg){
digitalWrite(9, HIGH-digitalRead(9));
digitalWrite(10, LOW-digitalRead(10));
digitalWrite(11, LOW-digitalRead(11));
delay(1000)
digitalWrite(9, LOW-digitalRead(9));
digitalWrite(10, HIGH-digitalRead(10));
digitalWrite(11, LOW-digitalRead(11));
delay(2000)
digitalWrite(9, LOW-digitalRead(9));
digitalWrite(10, LOW-digitalRead(10));
digitalWrite(11, HIGH-digitalRead(11));
delay(3000)
}
ros::Subscriber<std_msgs::Empty> sub("toggle_led1", &led1 );
ros::Subscriber<std_msgs::Empty> sub("toggle_led2", &led2 );
ros::Subscriber<std_msgs::Empty> sub("toggle_led3", &led3 );
ros::Subscriber<std_msgs::Empty> sub("toggle_all", &all );
ros::Subscriber<std_msgs::Empty> sub("toggle_light", &light );
void setup()
{
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(1);
}
this is a test code that I have written but I get this error:
LED_Test.ino:40:37: error: redefinition of ‘ros::Subscriber<std_msgs::Empty> sub’
LED_Test.ino:39:34: note: ‘ros::Subscriber<std_msgs::Empty> sub’ previously declared here
LED_Test.ino:41:37: error: redefinition of ‘ros::Subscriber<std_msgs::Empty> sub’
LED_Test.ino:39:34: note: ‘ros::Subscriber<std_msgs::Empty> sub’ previously declared here
LED_Test.ino:42:37: error: redefinition of ‘ros::Subscriber<std_msgs::Empty> sub’
LED_Test.ino:39:34: note: ‘ros::Subscriber<std_msgs::Empty> sub’ previously declared here
LED_Test.ino:43:37: error: redefinition of ‘ros::Subscriber<std_msgs::Empty> sub’
LED_Test.ino:39:34: note: ‘ros::Subscriber<std_msgs::Empty> sub’ previously declared here
the error is a bit self explanatory but I have no idea how to overcome it I appreciate any help. THANK YOU