ros-drivers / rosserial

A ROS client library for small, embedded devices, such as Arduino. See: http://wiki.ros.org/rosserial
523 stars 525 forks source link

rosserial on Arduino UNO #97

Open rflmota opened 10 years ago

rflmota commented 10 years ago

I've this sketch developed with rosserial_arduino:

#include <Wire.h>
#include <Magabot.h>

#include <ros.h>
#include <magabot/Status.h>
#include <magabot/Actuation.h>

#define N_SONARS 5
#define N_BUMPERS 4
#define N_IR 3
#define N_ENCODERS 2
#define PUB_FREQUENCY 20

void callback_actuation(const magabot::Actuation &msg);
void update_status(magabot::Status *status_msg);

Magabot robot;

int encoders[N_ENCODERS] = {0, 0};

ros::NodeHandle nh;
magabot::Status status_msg;
ros::Publisher status_pub("/magabot/Status", &status_msg);
ros::Subscriber<magabot::Actuation> actuation_sub("/magabot/Actuation", callback_actuation);

long publisher_timer;

void callback_actuation(const magabot::Actuation &actuation_msg)
{
    if(actuation_msg.set_velocity) 
        robot.actuateMotors(actuation_msg.velocity.left_wheel, actuation_msg.velocity.right_wheel);

    if(actuation_msg.set_leds)
        robot.actuateLEDs(actuation_msg.color.red, actuation_msg.color.green, actuation_msg.color.blue);
}

void update_status(magabot::Status *status_msg)
{
    robot.readBattery();
    status_msg->battery = robot.batteryRead;

    robot.readSonars();
    for(int i = 0; i < N_SONARS; i++)
        status_msg->sonars[i] = robot.sonarRead[i].read();

    robot.readBumpers();
    for(int i = 0; i < N_BUMPERS; i++)
        status_msg->bumpers[i] = robot.bumperRead[i];

    robot.readIR();
    for(int i = 0; i < N_IR; i++)
        status_msg->ir[i] = robot.irRead[i];

    robot.readClicks();
    status_msg->encoders.left_wheel = -robot.leftClicks;
    status_msg->encoders.right_wheel = robot.rightClicks;

    status_msg->timestamp = nh.now();

}

void setup() 
{
    nh.getHardware()->setBaud(115200); //or what ever baud you want
    nh.initNode();
    nh.spinOnce();
    nh.subscribe(actuation_sub);
    nh.spinOnce();
    nh.advertise(status_pub);
    nh.spinOnce();
}

void loop() 
{
    robot.update();

    update_status(&status_msg);

    nh.spinOnce();

    status_pub.publish(&status_msg);

    nh.spinOnce();

    delay(1);

}

The problem is that I'm trying to running it with rosserial_python serial_node and sometimes it works and sometime it doesn't... Normally it just works when I finish uploading the sketch to the Arduino board... The other times I just keep getting the error message:

[ERROR] [WallTime: 1392394316.596957] Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino

I've tried to running with rosserial_server serial_node, it appears to work besides I got the following warnings:

[ INFO] [1392394377.353974076]: Opening serial port.
[ INFO] [1392394377.354752380]: Starting session.
[ WARN] [1392394378.356651677]: Sync with device lost.
[ WARN] [1392394379.359455852]: Sync with device lost.
[ INFO] [1392394379.369414154]: Attached client is using protocol VER2 (hydro)
[ INFO] [1392394379.379951872]: waitForService: Service [/message_info] has not been advertised, waiting...
[ WARN] [1392394384.391269182]: Timed out waiting for message_info service to become available.
[ WARN] [1392394384.392973826]: Failed to call message_info service. Proceeding without full message definition.
[ WARN] [1392394384.393186438]: Advertising on topic [/magabot/Status] with an empty message definition.  Some tools (e.g. rosbag) may not work correctly.

I'm using the following messages definition:

bool set_velocity
Velocity velocity
bool set_leds
Color color
uint16 battery
uint16[5] sonars
bool[4] bumpers
uint16[3] ir
Encoders encoders
time timestamp

. . . which are compounded by the following messages :

uint8 red
uint8 green
uint8 blue
int16 left_wheel
int16 right_wheel
int16 left_wheel
int16 right_wheel

I must add I'm using the latest version of ROS Hydro w/ the latest version of rosserial checked out from this repository...

Can someone help me solving this issue?!

Thanks in advance ! ;)

tonybaltovski commented 10 years ago

I don't believe that the nh.spinOnce() should be in the setup.

rflmota commented 10 years ago

Hi @tonybaltovski , First of all, thank you for your attention... Regarding your suggestion, I already tried deleting the 'nh.spinOnce()' calls from setup() but the problems persist...

tonybaltovski commented 10 years ago

No worries. Try increasing the delay in the Arduino loop?

rflmota commented 10 years ago

Already tried that too, and still giving me the same problems... :s

aakarm commented 10 years ago

Hey! Did you find any solution to your problem of "Unable to sync device"?? I have been having the same problem and I have seen that whenever I call a function from the loop, it gives me that error. I have tried including spinOnce() throughout the code (except for the Setup(). )

When I remove that function, the sync works perfectly. I have tried searching on a lot of other similar problems but could not find an answer. Please do let me know if you were able to get a solution to it. Thanks!

aakarm commented 10 years ago

@rflmota , forgot to tag you in that post.

isura commented 10 years ago

Maybe this will help: http://answers.ros.org/question/164191/rosserial-arduino-cant-connect-arduino-micro/

PaulBouchier commented 10 years ago

Someone should add this tip to the wiki Paul On Nov 10, 2014 10:48 PM, "Isura Ranatunga" notifications@github.com wrote:

Maybe this will help:

http://answers.ros.org/question/164191/rosserial-arduino-cant-connect-arduino-micro/

— Reply to this email directly or view it on GitHub https://github.com/ros-drivers/rosserial/issues/97#issuecomment-62505084 .

Kukanani commented 9 years ago

I still am having this issue after following all steps listed above. I'm using an Arduino Uno and Indigo, with a fresh reinstall of the rosserial_arduino package, and having just re-ran make_libraries.

hendriyawanachmad commented 9 years ago

I'm facing same problem, but somehow when I'm using launch file from rosserial_server [roslaunch rosserial_server serial.launch port:=/dev/ttyUSB0] there is no error and can monitor the message using rostopic echo as well. maybe you can try this one.

hendriyawan

Kukanani commented 9 years ago

I am using ROS indigo, but actually rolled back just the Arduino to Hydro code and the arduino works again for me.

jonmgonzales commented 8 years ago

I had a similar issue recently, where my arduino would sometimes sync just fine, and other times when it couldn't sync. i resolve the issue by adding a delay(1000); command right after the nh.init(), nh.subscribe(...), nh.advertise(...) commands in the setup loop