ROS - Arduino subscriber question
Hello,
I am quite new to ROS but familiar with Arduino. I managed to run the Arduino through ROS. However, I have some questions that I couldn't find the answers for.
I am initializing ROS by typing "roscore" and then establishing the serial communication between arduino and ros by using the "rosrun rosserialpython serialnode.py /dev/ttyUSB0" command. So far so good. Then I send the following command;
rostopic pub red std_msgs/UInt16 1000 which
which calls the "red" function and uses the input of "1000" for delay time. This also works fine. However, when I type this line, it executes only once and there appears a message on the terminal "publishing and latching message. Press ctrl-C to terminate". Meaning that I need to first execute this with keyboard command to be able to input another command.
First of all, I want to send a command and I want this command to run the related function forever unless I send a new command which calls another function and runs it forever. To be able to do that I need those subscribers to return something when they are called or somehow I need to be able to detect they are called so that I can use a simple if else statement in the main loop which serves what I want. Furthermore, I need to get rid of that "publishing and latching message. Press ctrl-C to terminate" message so that I can send another command without cancelling it.
This is what's in my mind and not sure if this is the right way for that framework but appreciate if you show me the way.
#include <ros.h>
#include <std_msgs/UInt16.h>
#define led_y 12
#define led_r 8
ros::NodeHandle nh;
void yellow(const std_msgs::UInt16& cmd_msg)
{
digitalWrite(led_y,HIGH);
delay(cmd_msg.data);
digitalWrite(led_y,LOW);
delay(cmd_msg.data);
}
void red(const std_msgs::UInt16& cmd_msg)
{
digitalWrite(led_r,HIGH);
delay(cmd_msg.data);
digitalWrite(led_r,LOW);
delay(cmd_msg.data);
}
ros::Subscriber<std_msgs::UInt16> sub("red", red);
ros::Subscriber<std_msgs::UInt16> sub("yellow", yellow);
void setup()
{
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(1);
}
Asked by oneevening on 2015-08-06 09:49:20 UTC
Answers
maybe this?
rostopic pub red std_msgs/UInt16 1000 --once
Asked by ecdeise on 2017-06-13 21:31:58 UTC
Comments