Error while publishing encoder ticks in Arduino
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() {
Serial.begin(57600);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
nh.initNode();
nh.advertise(lwheelPub);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
lwheel--;
} else {
lwheel++;
}
Serial.print (lwheel*0.97);
Serial.print ("mm/");
lwheelPub.publish(&lwheelMsg);
}
encoder0PinALast = n;
nh.spinOnce();
}
ERROR
[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.
Don't use Serial.print() and rosserial: https://github.com/ros-drivers/rosser... . Feel free to comment on that issue if this helps.
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
I don't see anywhere in your code where you actually put the lwheel encoder value into the message.
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.