rosserial Publish and Subscribe to Ultrasonic Ping Range Data?
Hi, I am very new to ROS and I have a few questions as I am trying to learn it all on my own. I've read the tutorials but there are still some things I need some clarification on.
I want to publish ultrasonic sensor data as a range message to a topic. And then I want the same arduino to subscribe to that topic, and use that data to turn the servo motor depending on the distance read from the ultrasonic sensor. This is the code that I have come up inspired ny the rosserial tutorials.
I am coming to the error on arduino ide that states:
" 'const class sensor_msgs::Range' has no member named 'data'. " I am not sure which type of member it should be. Also, if I have any other noticeable errors that did not appear on verification, kindly let me know as I am doing my best to learn ROS. Thank you very much. Best, Matthew.
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <Servo.h>
#include <ros.h>
#include <ros/time.h>
#include <sensor_msgs/Range.h>
Servo servo;
ros::NodeHandle nh;
sensor_msgs::Range range_msg;
long duration;
int distance;
void servo_cb( const sensor_msgs::Range& range_msg){
distance = range_msg.data;
if (distance < 30) {
servo.write(0);
}
else {
servo.write(90);
}
}
ros::Publisher pub_range1("/ultrasound1", &range_msg);
ros::Subscriber<sensor_msgs::Range> sub_range("/ultrasound1", servo_cb);
char frameid[] = "/ultrasound";
void setup()
{
nh.initNode();
nh.advertise(pub_range1);
}
long range_time;
void loop()
{
//publish the adc value every 50 milliseconds
//since it takes that long for the sensor to stabilize
if ( millis() >= range_time ){
range_msg.range = getRange_Ultrasound(14);
pub_range1.publish(&range_msg);
range_time = millis() + 1000;
}
nh.spinOnce();
}
float getRange_Ultrasound(int pin_num)
{
pinMode(pin_num, OUTPUT);
digitalWrite(pin_num, LOW);
delayMicroseconds(2);
digitalWrite(pin_num, HIGH);
delayMicroseconds(10);
digitalWrite(pin_num, LOW);
pinMode(pin_num, INPUT);
duration = pulseIn(pin_num, HIGH);
return duration/58;
}