0
$\begingroup$

Rosanswers logo

Hi everyone.

My arduino uno is publishing std_msgs/Int16 messages over two topics, one for each wheels encoder ticks. It connects to rosserial on a raspberry pi 4 and data is published just fine, but I see errors/warnings that sometimes interrupt the data. These include:

[INFO] [1597354299.800672]: wrong checksum for topic id and msg

[ERROR] [1597354307.941502]: Mismatched protocol version in packet ('\x00'): lost sync or rosserial_python is from different ros release than the rosserial client

Occasionally, it also looses sync with the arduino:

[ERROR] [1597353144.897853]: Lost sync with device, restarting...

Here is my arduino code:

#include <RedBot.h>
#include <ros.h>
#include <std_msgs/Int16.h>

//ROS
ros::NodeHandle node;
std_msgs::Int16 lWheelMsg;
std_msgs::Int16 rWheelMsg;
ros::Publisher lWheelPub("/lwheel", &lWheelMsg);
ros::Publisher rWheelPub("/rwheel", &rWheelMsg);

//Encoder
const int LEFT_ENCODER_PIN = 9;
const int RIGHT_ENCODER_PIN = 10;
RedBotEncoder encoder = RedBotEncoder(LEFT_ENCODER_PIN, RIGHT_ENCODER_PIN);//left, right
int countsPerRev = 192;   // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev
int16_t lWheelTicks = 0;
int16_t rWheelTicks = 0;

void setup()
{
  //initialize ros
  node.initNode();
  node.advertise(lWheelPub);
  node.advertise(rWheelPub);

  //reset encoders
  encoder.clearEnc(BOTH);

}

void loop() 
{

  //send encoder data over ROS
  lWheelTicks = encoder.getTicks(LEFT);
  rWheelTicks = encoder.getTicks(RIGHT);
  lWheelMsg.data = lWheelTicks;
  rWheelMsg.data = rWheelTicks;
  lWheelPub.publish(&lWheelMsg);
  rWheelPub.publish(&rWheelMsg);

  node.spinOnce();
  delay(200);
}

Any idea what could be causing this? Thanks!


Originally posted by hvak on ROS Answers with karma: 3 on 2020-08-13

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The delay is most likely causing the system to foul up. ROSSerial tries to communicate with the arduino every so often, if it's during this delay then everything falls over.

Instead of delay(200); use millis(); to check current arduino clock time. If it has been at least 200 ms since the last message, then publish the data. Check out this article on how to use this (https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing).

As a rough guide:

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

//ROS
ros::NodeHandle node;
std_msgs::Int16 msg;
ros::Publisher msgPub("my_topicl", &msg);

unsigned long pubPeriod = 200; // milliseconds between publishing data
unsigned long previousTime;
int seq = 0;

void setup()
{
  //initialize ros
  node.initNode();
  node.advertise(msgPub);

  previousTime = 0;
}

void loop()
{
  
  if (previousTime + pubPeriod <= millis()){
    // Do actions after pubPeriod since last published message

    msg.data = seq;
    msgPub.publish(&msg);

    previousTime = millis();
    seq++;
  }
  nh.spinOnce();
  delay(1);

}

Hope this helps.


Originally posted by Andy West with karma: 81 on 2020-08-16

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by hvak on 2020-08-21:
This solved my issue, thank you!

Comment by Andy West on 2020-08-22:
Yay! Glad to be of help.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.