Reading message failed in rosserial
I have used the code and circuit given below to glow the LED when the value from analog sensor exceeds the threshold value. and i received the error as "[WARN] [WallTime: 1422707498.360818] Serial Port read returned short (expected 77 bytes, received 19 instead) " and some times LED is glowing when the value from sensor is less than threshold value.
I use: Operating system : Ubuntu 14.04, ROS indigo, Analog sensor : potentiometer, Board : Arduino UNO.
#include <ros.h>
#include <ros/time.h>
#include <sensor_msgs/Range.h>
#include <std_msgs/Empty.h>
ros::NodeHandle nh;
void messageCb( const sensor_msgs::Range& range_msg)
{
if (range_msg.range >= 700) {
digitalWrite(13, HIGH-digitalRead(13));
}
else {
digitalWrite(13, LOW-digitalRead(13));
}
}
ros::Subscriber<sensor_msgs::Range> sub("range_data", &messageCb );
sensor_msgs::Range range_msg;
ros::Publisher pub_range( "range_data", &range_msg);
const int analog_pin = 0;
unsigned long range_timer;
float getRange(int pin_num){
int sample;
sample = analogRead(pin_num);
return (sample);
}
char frameid[] = "/ir_ranger";
void setup()
{
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
nh.initNode();
nh.advertise(pub_range);
range_timer = millis();
}
void loop()
{
if ( range_timer + 50 < millis() )
{
range_msg.range = getRange(analog_pin);
range_msg.header.stamp = nh.now();
pub_range.publish(&range_msg);
range_timer = millis();
}
nh.spinOnce();
}