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

Unable to sync Arduino Mega and RP4 using rosserial

asked 2021-03-28 20:23:48 -0600

Sknet gravatar image


I know there has been many people posting about this issue, but I have tried all of the solutions I have seen so far and I am still unable to establish the connection. Here is the error message I am getting:

Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino

This is the compilation message I am getting:

Sketch uses 11894 bytes (4%) of program storage space. Maximum is 253952 bytes. Global variables use 1192 bytes (14%) of dynamic memory, leaving 7000 bytes for local variables. Maximum is 8192 bytes.

I am trying to publish encoder data from an Arduino Mega through rosserial. Here are some of the things I have tried so far: I have confirmed that this setup works with the rosserial examples. I have switched from odometry messages to Float32 since it seems that the size of these may cause the small memory of the Arduino board to overflow. I have confirmed that the baud rate in both Arduino and RPi are matching, I have tried increasing and lowering the baud rate, I have tried increasing and lowering the buffer size of NodeHandle_<ArduinoHardware>, I have tried changing the cable and board, confirmed that the ports are connected and have sudo permission, and I have tried lowering and increasing the delay in loop(). I have also tried using ros launch to start the node, hard coding baud rate and buffer size in ros.h and ArduinoHardware.h, and trying to set the parameters from the terminal rosrun rosserial_python _port:=/dev/ttyACM0 _baud:=115200. No success...

This is what I currently have:


#include <ros.h>
#include <std_msgs/Float32.h>

ros::NodeHandle_<ArduinoHardware, 10, 15, 128, 256> nh;
std_msgs::Float32 right_wheel_rpm_msg;
std_msgs::Float32 left_wheel_rpm_msg;

long publisher_timer;

    volatile int motor_right_input_A_count = 0; //if the interrupt will change this value, it must be volatile;
    volatile int motor_left_input_A_count = 0;

    const byte motor_right_input_A = 2;
    const byte motor_right_input_B = 3;
    const byte motor_left_input_A = 21;
    const byte motor_left_input_B = 20;

    float motor_cpr = 16.0;
    float right_motor_rpm;
    float left_motor_rpm;
    float right_wheel_rpm;
    float left_wheel_rpm;
    int gear_ratio = 270;

    // ROS publisher
    ros::Publisher right_wheel_msg_pub("right_wheel_rpm", &right_wheel_rpm_msg);
    ros::Publisher left_wheel_msg_pub("left_wheel_rpm", &left_wheel_rpm_msg);

    void setup() { 
      // ROS setup

      // Define the rotary encoder for right motor
      pinMode(motor_right_input_A, INPUT);
      pinMode(motor_right_input_B, INPUT);
      digitalWrite(motor_right_input_A, HIGH); 
      digitalWrite(motor_right_input_B, HIGH);
      attachInterrupt(digitalPinToInterrupt(motor_right_input_A), motor_right_interruptCounter, RISING); 

      // Define the rotary encoder for left motor
      pinMode(motor_left_input_A, INPUT);
      pinMode(motor_left_input_B, INPUT);  
      digitalWrite(motor_left_input_A, HIGH);
      digitalWrite(motor_left_input_B, HIGH);
      attachInterrupt(digitalPinToInterrupt(motor_left_input_A), motor_left_interruptCounter, RISING);

    void loop() {

      right_wheel_rpm = right_motor_rpm / gear_ratio;
      left_wheel_rpm = left_motor_rpm / gear_ratio; = right_wheel_rpm; = left_wheel_rpm;



    void motor_right_interruptCounter()

    void motor_left_interruptCounter()

    void motor_right_measureRPM(){
      unsigned long start_time;
      unsigned long end_time;
      motor_right_input_A_count = 0;
      start_time = millis();

      while(motor_right_input_A_count < 101){

      end_time = millis();
      right_motor_rpm = 60000.0/(end_time - start_time)*motor_right_input_A_count / motor_cpr;

    void motor_left_measureRPM(){
      unsigned long start_time;
      unsigned long end_time;
      motor_left_input_A_count = 0;
      start_time = millis();

      while(motor_left_input_A_count < ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-29 07:36:34 -0600

miura gravatar image

updated 2021-04-03 10:47:26 -0600

I have experienced that re-creating ros_lib solved the corresponding error.

I suggest that you do rosrun rosserial_arduino . with ROS running on RP4 and recreate ros_lib.


Thank you for trying my suggestion. I read the source code.

  while(motor_right_input_A_count < 101){

The above is an infinite loop and it looks like it is not publishing.

void motor_right_interruptCounter()

is not running in parallel with motor_right_measureRPM. ( The same is left side. )

edit flag offensive delete link more


Unfortunately, that did not solve the problem. I tried removing and rebuilding the libraries with roscore running.

Sknet gravatar image Sknet  ( 2021-04-03 08:04:42 -0600 )edit

Yeap, that is correct. I was testing with the motor not moving so it was stuck in the while loop. Thank you!

Sknet gravatar image Sknet  ( 2021-04-07 20:56:38 -0600 )edit

I'm glad I could be of help. If the problem has been solved, please click the correct button.

miura gravatar image miura  ( 2021-04-08 09:23:38 -0600 )edit

Thank you.

miura gravatar image miura  ( 2021-04-09 06:28:59 -0600 )edit

Question Tools

1 follower


Asked: 2021-03-28 20:23:48 -0600

Seen: 335 times

Last updated: Apr 03 '21