ROSSerial fails to sync with teensy device on ROS Noetic

1.8k Views Asked by At

I am using teensy with rosserial + ROS Noetic/Ubuntu 20.04 on RASPI4. The teensy code is implemented with ros_lib on platformio (https://platformio.org/lib/show/5526/...). The program compiles fine and uploads successfully on port /dev/ttyACM0. However, when I do rosrun rossserial_python serial_node.py _port:=/dev/ttyACM0 _baud:=500000 then I get the sync failed error.

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

Things I have already tried:

a) Setting correct baudrate in Serial.begin(500000)

b) Disabling all Serial.begin and Serial.print statements

c) Setting baudrate of ros node nh.gethardware()->setbaud(500000) before nh.init()

d) Increasing the default buffer size to 1024 in ros.h of ros_lib typedef NodeHandle_<arduinohardware, 10,="" 10,="" 1024,="" 1024=""> NodeHandle;

e) Tried the Arduino board instead of Teensy and the problem still remain.

However, nothing has worked so far.

1

There are 1 best solutions below

0
On

The default baud rate for rosserial is 56700. For changing it you will need to do the following

Modify the include statement in the arduino code from

#include <ros.h>

to

#include "ros.h"

Add the following header files alongside the .ino/.pde file

ros.h

#ifndef _ROS_H_
#define _ROS_H_

#include "ros/node_handle.h"
#include "ArduinoHardware.h"

namespace ros
{
  typedef NodeHandle_<ArduinoHardware> NodeHandle;
}

#endif

ArduinoHardware.h

#ifndef ROS_ARDUINO_HARDWARE_H_
#define ROS_ARDUINO_HARDWARE_H_

#if ARDUINO>=100
  #include <Arduino.h>
#else
  #include <WProgram.h>
#endif

#include <HardwareSerial.h>
#define SERIAL_CLASS HardwareSerial

class ArduinoHardware
{
  public:
    ArduinoHardware()
    {
      iostream = &Serial;
      baud_ = 500000;
    }
  
    void setBaud(long baud)
    {
      this->baud_= baud;
    }
  
    int getBaud()
    {
      return baud_;
    }

    void init()
    {
      iostream->begin(baud_);
    }

    int read()
    {
      return iostream->read();
    };

    void write(uint8_t* data, int length)
    {
      for(int i=0; i<length; i++)
      {
        iostream->write(data[i]);
      }
    }

    unsigned long time()
    {
      return millis();
    }

  protected:
    SERIAL_CLASS* iostream;
    long baud_;
};

#endif

In the ArduinoHeader.h file we defined above the iostream & the baud rate to be used is set using the constructor as shown below

ArduinoHardware()
    {
      iostream = &Serial;
      baud_ = 500000;
    }

as you are using teensy with USB the iostream will be Serial. If you are using the pins and not the USB it will vary from Serial1 to Serial8 as mentioned here. The baud rate of the teensy can be varied by varying the baud_ here set to 500000.

This code is referenced from Advanced Configuration for NodeHandle and ArduinoHardware