ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I believe that you got some inspiration from this tutorial: http://wiki.ros.org/rosserial_arduino/Tutorials/Blink#The_Code
The line
digitalWrite(13, HIGH-digitalRead(13));
will toggle the LED for you. First, it reads the pin digitalRead(13)
, then it writes it digitalWrite
. The portion that says HIGH-digitalRead(13)
does the alternating part. If digitalRead(13)
is high then your LED will turn off and if it's low your LED will turn on.
HIGH - HIGH = LOW
therefore turn off LEDHIGH - LOW = HIGH
therefore turn on LEDThe callback is the trigger for this behavior. Therefore, all you need to do to get your LED to blink at a high frequency is to publish an std_msgs/Empty
message along the toggle_led
topic at whatever rate you want.
Now, about the delay
s. You should remove them and just do it how the tutorial showed it. Callbacks should be quick so that you can react to the incoming messages. With two delay
s of 100ms each you won't be able to blink the LED at a high frequency.
2 | No.2 Revision |
I believe that you got some inspiration from this tutorial: http://wiki.ros.org/rosserial_arduino/Tutorials/Blink#The_Code
The line
digitalWrite(13, HIGH-digitalRead(13));
will toggle the LED for you. First, it reads the pin digitalRead(13)
, then it writes it digitalWrite
. The portion that says HIGH-digitalRead(13)
does the alternating part. If digitalRead(13)
is high then your LED will turn off and if it's low your LED will turn on.
HIGH - HIGH = LOW
therefore turn off LEDHIGH - LOW = HIGH
therefore turn on LEDThe callback is the trigger for this behavior. Therefore, all you need to do to get your LED to blink at a high frequency is to publish an std_msgs/Empty
message along the toggle_led
topic at whatever rate you want.
Now, about the delay
s. You should remove them and just do it how the tutorial showed it. Callbacks should be quick so that you can react to the incoming messages. With two delay
s of 100ms each you won't be able to blink the LED at a high frequency.
Edit:
I'd be cautious about looping in a callback like so:
void messageCb( const std_msgs::Empty& toggle_msg){
for (int thisInt = 1; thisInt < 2000; thisInt++) {
digitalWrite(8, HIGH-digitalRead(8)); delay(100);
// blink the led
}
}
The reason is that callbacks should be quick in order to react to incoming messages. But, here the loop is running for almost 200s! You're going to be in that function for over 3 minutes not doing anything else, ignoring all subsequent messages. Now, for this use case you're ok because it's trivial. But, for anything of consequence this is going to cause you trouble. Usually, I'd say an action is the way to go, but rosserial_arduino
doesn't support that (as far as I know) so you'll either have to write another node to control the publishing of the message that controls the blinking or find another workaround.
Otherwise, your first code block looks like it works.