Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mbed.h"
00008 #include <ros.h>
00009 #include <ros/time.h>
00010 #include <sensor_msgs/Range.h>
00011
00012 ros::NodeHandle nh;
00013
00014 sensor_msgs::Range range_msg;
00015 ros::Publisher pub_range( "/ultrasound", &range_msg);
00016
00017 #if defined(TARGET_LPC1768)
00018 const PinName adc_pin = p20;
00019 #elif defined(TARGET_KL25Z) || defined(TARGET_NUCLEO_F401RE)
00020 const PinName adc_pin = A0;
00021 #else
00022 #error "You need to specify a pin for the sensor"
00023 #endif
00024
00025 char frameid[] = "/ultrasound";
00026
00027 float getRange_Ultrasound(PinName pin_num) {
00028 int val = 0;
00029 for (int i=0; i<4; i++) val += AnalogIn(pin_num).read_u16();
00030 float range = val;
00031 return range /322.519685;
00032 }
00033
00034 Timer t;
00035 int main() {
00036 t.start();
00037 nh.initNode();
00038 nh.advertise(pub_range);
00039
00040 range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
00041 range_msg.header.frame_id = frameid;
00042 range_msg.field_of_view = 0.1;
00043 range_msg.min_range = 0.0;
00044 range_msg.max_range = 6.47;
00045
00046
00047
00048
00049 long range_time=0;
00050
00051 while (1) {
00052
00053
00054
00055 if ( t.read_ms() >= range_time ) {
00056 range_msg.range = getRange_Ultrasound(adc_pin);
00057 range_msg.header.stamp = nh.now();
00058 pub_range.publish(&range_msg);
00059 range_time = t.read_ms() + 50;
00060 }
00061
00062 nh.spinOnce();
00063 }
00064 }
00065