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

ROS - Arduino subscriber question

asked 2015-08-06 09:49:20 -0500

oneevening gravatar image

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 rosserial_python serial_node.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);
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-06-13 21:35:59 -0500

ecdeise gravatar image

maybe this?

rostopic pub red std_msgs/UInt16 1000 --once

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-06 09:49:20 -0500

Seen: 5,229 times

Last updated: Jun 13 '17