rosserial, multiple callbacks and interrptions?
Hello, I am new to rosserial. The objective of this program is to make a LED (pin 13) blink with a Period=5s, and then exit this blinking.
Thus, after connecting ROS to Arduino via USB, I apply a 'toggle_msg" to initiate the process (which runs perfectly), and my will is that the process ends when I apply a "end_msg" message. Each message has its own callback function as you can see.On the ROS terminal I apply each message this way:
rostopic pub toggle_led std_msgs/Empty --once
rostopic pub end_led std_msgs/Empty --once
The problem is that I do not know how to exit the while loop in the first callback, and the process really never ends. How can I do this?
The code is here:
# include <ros.h>
# include <std_msgs/Empty.h>
int k=0;
ros::NodeHandle nh;
void messageCb( const std_msgs::Empty& toggle_msg){
while(k==0){
digitalWrite(13, HIGH-digitalRead(13));
delay(5000);
}
}
void messageCb1( const std_msgs::Empty& end_msg){
k=1;
delay(1000);
}
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
ros::Subscriber<std_msgs::Empty> sub1("end_led", &messageCb1 );
void setup()
{
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
nh.subscribe(sub1);
}
void loop()
{
nh.spinOnce();
delay(1);
}
THANKS!!!
Welcome! Can you please update your question with a copy and paste of your code using the preformatted text (
101010
) button? This will make the question self-contained and the link that you gave says that.ino
files can't be previewed.Thanks, I added that