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

rosserial, multiple callbacks and interrptions?

asked 2018-03-29 17:11:13 -0500

blasrobot gravatar image

updated 2018-03-29 22:56:49 -0500

jayess gravatar image

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!!!

edit retag flag offensive close merge delete

Comments

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.

jayess gravatar image jayess  ( 2018-03-29 21:32:15 -0500 )edit

Thanks, I added that

blasrobot gravatar image blasrobot  ( 2018-03-29 21:58:44 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-04-02 13:54:48 -0500

updated 2018-04-02 13:57:22 -0500

You should not stay in the callbacks - just alter the values of variables to indicate what should happen in the main loop and work out whether it is time to toggle the led each time round if it is blinking

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

int blinking = 0;
int ledState = 0;
long lastTime = 0;
long blinkInterval = 5000;

ros::NodeHandle  nh;

void messageCb(const std_msgs::Empty& toggle_msg){
  blinking = 1;
  lastTime = millis();
  ledState = 1;
}


void messageCb1(const std_msgs::Empty& end_msg){
  blinking = 0;
}

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

  if (blinking){
    long currentTime = millis();
    if (currentTime - lastTime >= blinkInterval){
      ledState = !ledState;
      lastTime = currentTime;
    }
  }else{
    ledState = 0;
  }
  digitalWrite(13, ledState);
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-03-29 17:11:13 -0500

Seen: 341 times

Last updated: Apr 02 '18