ultrasonic.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/Range.h>
00003 #include <std_msgs/Float32.h>
00004 #include <string>
00005 #include <utility>
00006 
00007 // vrep bounds for ultrasonic sensor indexing
00008 #define VREP_LOWER_BOUND 1
00009 #define VREP_UPPER_BOUND 16
00010 #define VREP_END VREP_UPPER_BOUND - VREP_LOWER_BOUND + 1
00011 // parameters for sensor defined in vrep
00012 #define VREP_ANGLE_DETECTION 0.5236 // in [rad]
00013 #define VREP_MIN_DIST_DETECTION 0.0500
00014 #define VREP_MAX_DIST_DETECTION 1.0
00015 
00016 // robot's index in ROS ecosystem (if multiple instances)
00017 std::string tf_prefix;  
00018 
00027 void ultrasonicSensorData_cb(const ros::Publisher& publisher, std::size_t sensor_n, const std_msgs::Float32::ConstPtr& msg)
00028 {
00029   if(msg->data <= VREP_MAX_DIST_DETECTION && msg->data >= VREP_MIN_DIST_DETECTION) {
00030     sensor_msgs::Range range_msg;
00031 
00032     // data
00033     range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
00034     range_msg.field_of_view = VREP_ANGLE_DETECTION;
00035     range_msg.min_range = VREP_MIN_DIST_DETECTION;
00036     range_msg.max_range = VREP_MAX_DIST_DETECTION;
00037     range_msg.range = msg->data;
00038 
00039     // header
00040     range_msg.header.stamp = ros::Time::now(); // current time of data collection
00041     range_msg.header.frame_id = tf_prefix + "/ultrasonicSensors/sensor" + std::to_string(sensor_n);
00042 
00043     publisher.publish(std::move(range_msg));
00044   }
00045 
00046 }
00047 
00048 int main(int argc, char **argv)
00049 {
00050   ros::init(argc, argv, "ultrasonicSensor");
00051   ros::NodeHandle n("~"); // we want relative namespace
00052 
00053   // init parameters
00054   // resolve `tf_prefix` and `vrep_index` in parent namespaces
00055   int vrep_no = 0; // robot's index in vrep (if multiple instances)
00056 
00057   std::string tf_prefix_path;
00058   if (n.searchParam("tf_prefix", tf_prefix_path))
00059   {
00060     n.getParam(tf_prefix_path, tf_prefix);
00061   }
00062 
00063   std::string vrep_index_path;
00064   if (n.searchParam("vrep_index", vrep_index_path))
00065   {
00066     n.getParam(vrep_index_path, vrep_no);
00067   }
00068 
00069   // will publish the ultrasonic mesages to the rest of the stack
00070   std::vector<ros::Publisher> hal_publishers;
00071   hal_publishers.reserve(VREP_END);
00072   for (int i = 0; i < VREP_END; ++i)
00073   {
00074     hal_publishers.push_back(
00075       n.advertise<sensor_msgs::Range>(
00076         "sensor" + std::to_string(i) + "/Range", 
00077         1000));
00078   }
00079 
00080   // subscribe to to all ultrasonics from vrep (16)
00081   std::vector<ros::Subscriber> vrep_subscribers;
00082   vrep_subscribers.reserve(VREP_END);
00083 
00084   auto it = hal_publishers.cbegin();
00085   std::size_t vrep_i = VREP_LOWER_BOUND;
00086   for (; it != hal_publishers.cend(); ++vrep_i, ++it)
00087   {
00088     // prepare function for this sensor
00089     boost::function<void(const std_msgs::Float32::ConstPtr&)> callback = 
00090       std::bind(ultrasonicSensorData_cb, *it, std::distance(hal_publishers.cbegin(), it), std::placeholders::_1);
00091     // subscribe each callback
00092     vrep_subscribers.push_back(
00093       n.subscribe(
00094         "/vrep/i" + std::to_string(vrep_no) + "_Pioneer_p3dx_ultrasonicSensor" + std::to_string(vrep_i),
00095         1000,
00096         std::move(callback)
00097       ));
00098   }
00099 
00100   ROS_INFO("HAL(VREP): UltrasonicSensor node initialized");
00101 
00102   // runs event loop
00103   ros::spin();
00104 
00105   return 0;
00106 }


p3dx_hal_vrep
Author(s):
autogenerated on Sat Jun 8 2019 20:22:32