Ultrasound.cpp
Go to the documentation of this file.
00001 /*
00002  * rosserial Ultrasound Example
00003  *
00004  * This example is for the Maxbotix Ultrasound rangers.
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;   // (0.0124023437 /4) ; //cvt to meters
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;  // fake
00043     range_msg.min_range = 0.0;
00044     range_msg.max_range = 6.47;
00045 
00046     //pinMode(8,OUTPUT);
00047     //digitalWrite(8, LOW);
00048 
00049     long range_time=0;
00050 
00051     while (1) {
00052 
00053         //publish the adc value every 50 milliseconds
00054         //since it takes that long for the sensor to stablize
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 


rosserial_mbed
Author(s): Gary Servin
autogenerated on Sat Oct 7 2017 03:08:46