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

Ros-Arduino subscriber

asked 2016-03-10 01:58:28 -0600

Reza1984 gravatar image

updated 2016-03-10 02:09:15 -0600

Hi

I would like to send a message to arduino serial monitor, here is my code

#include <ros.h>
#include <sensor_msgs/LaserScan.h>

void Callback_laser(const sensor_msgs::LaserScan& scan)
{
   Serial.print("Hello = :");
   Serial.println(scan.ranges[0]);
}

   ros::NodeHandle nh;
   ros::Subscriber<sensor_msgs::LaserScan> laser_subscriber("scan", &Callback_laser);

void setup() {

  nh.initNode();
  nh.subscribe(laser_subscriber);

 Serial.begin(9600);

}

void loop() {

  nh.spinOnce();
  delay(10);
}

but nothing will appear in serial monitor!

Could you please tell me what is the problem with my code?

Thanks in advance for your help.

edit retag flag offensive close merge delete

Comments

When I put Serial.print("Hello = :"); inside void loop() I can see the message, but I cannot see any message when it's inside callback

Reza1984 gravatar image Reza1984  ( 2016-03-10 02:12:26 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-03-10 02:04:56 -0600

updated 2016-03-10 02:12:00 -0600

I have never used rosserial_arduino, but I used arduinos before, have you initialized the object Serial?

Somewhere inside the setup() function should be a line like:

Serial.begin(9600);

Hope this helps

UPDATE

Forget about what I said before. I don't think using Serial.print is a good idea as it might interfere with ROS's communication. Have you tried blinking the LED instead?

digitalWrite(13, HIGH-digitalRead(13));   // blink the led

You should see the LED blinking quite fast

edit flag offensive delete link more

Comments

Thanks, I forgot to add it here. I edited my question.

Reza1984 gravatar image Reza1984  ( 2016-03-10 02:09:53 -0600 )edit

I put that command in callback the LED was just off but when put in void loop() it start blinking. Seems the callback function is not executed.

Reza1984 gravatar image Reza1984  ( 2016-03-10 02:23:30 -0600 )edit
0

answered 2016-03-14 10:32:12 -0600

alee gravatar image

updated 2016-03-14 10:33:28 -0600

What Arduino are you using? Depending on your Arduino, it might only have one serial port and as @martin-peris said, Serial might interfere with rosserial if you only have one line. On a Mega with 4 lines it might work, but definitely not on an Uno with only one line.

edit flag offensive delete link more

Comments

Thanks, the main issue was the Arduino couldn't read large messages with high publishing rate. Instead of sending sensor_msgs::LaserScan I sent std_msgs::Float32 message with low publish rate and could see the messages in Arduino Uno.

Reza1984 gravatar image Reza1984  ( 2016-03-15 02:23:36 -0600 )edit

Can you make a new answer and accept it as correct?

alee gravatar image alee  ( 2016-03-15 09:48:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-10 01:58:28 -0600

Seen: 1,116 times

Last updated: Mar 14 '16