Ask Your Question
0

How can i send a message from ROS to arduino using rosserial?

asked 2018-08-06 08:02:50 -0600

Prateek gravatar image

updated 2018-08-06 12:05:25 -0600

jayess gravatar image

I want LED 13 on my Arduino Uno board to glow when I pass the number 100 as command line argument from ROS. Here is my arduino code:

#include <ros.h>
#include <Arduino.h>
#include <std_msgs/Int8.h>

ros::NodeHandle  nh;

void ledGlow(const std_msgs::Int8& val){

  if(val.data==100){
  digitalWrite(13, HIGH);
  }
  else{
  digitalWrite(13, LOW);
  }
}

ros::Subscriber<std_msgs::Int8> led("led/start", &ledGlow);

void setup() { 
  pinMode(13, OUTPUT);
  nh.initNode();  
  nh.subscribe(led);
}

void loop() {  
  nh.spinOnce();
  delay(10);
}

I am giving the following commands in the terminal:

  1. roscore
  2. rostopic pub led/start std_msgs/Int8 100

After giving the second command LED 13 doesn't glow, also RX led on arduino board doesn't blink.

edit retag flag offensive close merge delete

Comments

Try adding serial output to your Arduino program to determine if the ledGlow callback is being called, if yes check what value is in the message

VictorLamoine gravatar imageVictorLamoine ( 2018-08-06 08:53:38 -0600 )edit

I tried adding Serial.println("Hello"); inside the callback function but I'm not getting any output on serial output screen. Also, when I try printing something inside the loop, I get garbage value on serial output screen. Please help! @VictorLamoine

Prateek gravatar imagePrateek ( 2018-08-07 01:00:17 -0600 )edit

Make sure to adjust the baudrate of the serial terminal

VictorLamoine gravatar imageVictorLamoine ( 2018-08-07 01:52:10 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-08-07 01:16:31 -0600

fvd gravatar image

updated 2018-08-08 09:13:01 -0600

1) You need to start up your node by running "rosrun your_package your_node" or an equivalent roslaunch command. I assume you're doing that and that the code you quoted is not complete.

2) If your node lives in its own namespace "my_node_ns", then it would try to subscribe to "/my_node_ns/led/start". Make sure to publish to the right topic.

3) Debug by:
- adding ROS_INFO("test"); or similar in your functions so you know they are being called.
- Using rostopic list /led/start --verbose to check if your node is subscribing correctly to the topic.
- Checking your serial ports and USB connection. It seems like a problem with your Arduino connection, not the ROS side.

edit flag offensive delete link more

Comments

Thanks a lot :)

Prateek gravatar imagePrateek ( 2018-08-07 01:51:21 -0600 )edit

Sure. Please update what the problem was when you solved it.

fvd gravatar imagefvd ( 2018-08-08 02:22:43 -0600 )edit
1

answered 2018-08-07 01:32:46 -0600

Are you running the rosserial node as described in this tutorial http://wiki.ros.org/rosserial_arduino... that converts the rostopic message and sends it down the serial port. Don't use serial print in your code it will interfere with that communication

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-08-06 07:48:23 -0600

Seen: 1,360 times

Last updated: Aug 08 '18