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

Error while publishing encoder ticks in Arduino

asked 2016-09-11 13:53:16 -0500

Kishore Kumar gravatar image

updated 2016-09-13 04:57:35 -0500

I am using Arduino Uno to publish Rotary Encoder ticks. I have used the following code to publish the encoder values to ROS and ended up in the error

#include <ros.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Int32.h>
#include <std_msgs/Int64.h>
#include <std_msgs/Float32.h>

int val;
 int encoder0PinA = 2;
 int encoder0PinB = 3;
 int encoder0PinZ = 4;
 int lwheel = 0;
 int encoder0PinALast = LOW;
 int n = LOW;

ros::NodeHandle  nh;

std_msgs::Int16 lwheelMsg;
ros::Publisher lwheelPub("lwheel", &lwheelMsg);   /

 void setup() {
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);


void loop() {

   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
     } else {
     Serial.print (lwheel*0.97);
     Serial.print ("mm/");
   encoder0PinALast = n;


[WARN] [WallTime: 1473618880.299270] Serial Port read returned short (expected 2 bytes, received 1 instead).
[WARN] [WallTime: 1473618880.300057] Serial Port read failure:

I found some fixes which told to change the baud rate, i tried them but didn't help. Someone please help me to fix the issue and direct me to correct ROS Arduino node to publish encoder ticks to ROS with interupt.

edit retag flag offensive close merge delete


Don't use Serial.print() and rosserial: . Feel free to comment on that issue if this helps.

ahendrix gravatar image ahendrix  ( 2016-09-11 21:08:26 -0500 )edit

Thanks, that fixed the issue. But i am just getting "0" in ros topic. When i run the plain Arduino code to visualize the result in serial monitor i am getting perfect results. still there is a communication problem b/w Arduino & ROS

Kishore Kumar gravatar image Kishore Kumar  ( 2016-09-12 13:38:50 -0500 )edit

I don't see anywhere in your code where you actually put the lwheel encoder value into the message.

ahendrix gravatar image ahendrix  ( 2016-09-12 20:46:30 -0500 )edit

1wheel=lwheel. there some problem in pasting "L", My actual code is lwheel in all the places of 1wheel, i have edited now but still it looks like "1". It is the font style.

Kishore Kumar gravatar image Kishore Kumar  ( 2016-09-13 04:56:07 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2016-09-13 16:22:10 -0500

ahendrix gravatar image

You never assign a value to lwheelMsg, so it's always 0.

edit flag offensive delete link more


Thank you, i have fixed it

Kishore Kumar gravatar image Kishore Kumar  ( 2016-09-13 23:02:04 -0500 )edit

Hi, I want to do same thing but unfortunately ı did not solve my problem yet. If you don't mind, would you please share the arduino and ros code with me?

Mekateng gravatar image Mekateng  ( 2020-02-24 15:25:54 -0500 )edit

Question Tools

1 follower


Asked: 2016-09-11 13:53:16 -0500

Seen: 995 times

Last updated: Sep 13 '16