Ask Your Question
0

ROS Arduino creating more than one subscriber (function)

asked 2019-10-03 11:36:12 -0500

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

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-10-04 03:06:07 -0500

aPonza gravatar image

error: redefinition of ‘ros::Subscriber<std_msgs::empty> sub

means you are violating the One Definition Rule (reusing the same name for a different object, with the same declaration as well) in these lines:

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 );

so a simple solution would be to instead have

ros::Subscriber<std_msgs::Empty> sub_led1 ("toggle_led1", &led1);
ros::Subscriber<std_msgs::Empty> sub_led2 ("toggle_led2", &led2);
ros::Subscriber<std_msgs::Empty> sub_led3 ("toggle_led3", &led3);
ros::Subscriber<std_msgs::Empty> sub_all ("toggle_all", &all);
ros::Subscriber<std_msgs::Empty> sub_light ("toggle_light", &light);

This is not a ROS issue but a C++ one, you should check some C++ books and the Publisher Subscriber tutorial among the others.

edit flag offensive delete link more

Comments

Thank you this is what I needed

cyotmhnd gravatar imagecyotmhnd ( 2019-10-04 11:13:00 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-10-03 11:36:12 -0500

Seen: 9 times

Last updated: Oct 04