laser.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/LaserScan.h>
00003 #include <string>
00004 #include <utility>
00005 
00006 // publisher in HAL for laser
00007 ros::Publisher publisher;
00008 
00009 // robot's index in ROS ecosystem (if multiple instances)
00010 std::string tf_prefix;
00011 
00012 // constants for sensor
00013 #define VREP_MIN_DIST_DETECTION 0.0550
00014 #define VREP_MAX_DIST_DETECTION 19.9450
00015 
00016 void laserScanData_cb(const sensor_msgs::LaserScan::ConstPtr& msg)
00017 {
00018   sensor_msgs::LaserScan laser_msg = *msg;
00019 
00020   // include proper ros time and proper tf info
00021   laser_msg.header.stamp = ros::Time::now(); // current time of data collection
00022   laser_msg.header.frame_id = tf_prefix + "/laserSensors/sensor0";
00023 
00024   // set detection ranges
00025   laser_msg.range_min = VREP_MIN_DIST_DETECTION;
00026   laser_msg.range_max = VREP_MAX_DIST_DETECTION;
00027 
00028   // angle_min, angle_man, angle_increment, scan_time, time_increment
00029   // are correctly set in vrep
00030 
00031   publisher.publish(std::move(laser_msg));
00032 }
00033 
00034 int main(int argc, char **argv)
00035 {
00036   ros::init(argc, argv, "laserScan");
00037   ros::NodeHandle n("~"); // we want relative namespace
00038 
00039   // init parameters
00040   // resolve `tf_prefix` and `vrep_index` in parent namespaces
00041   int vrep_no = 0; // robot's index in vrep (if multiple instances)
00042 
00043   std::string tf_prefix_path;
00044   if (n.searchParam("tf_prefix", tf_prefix_path))
00045   {
00046     n.getParam(tf_prefix_path, tf_prefix);
00047   }
00048 
00049   std::string vrep_index_path;
00050   if (n.searchParam("vrep_index", vrep_index_path))
00051   {
00052     n.getParam(vrep_index_path, vrep_no);
00053   }
00054 
00055   // subscribe to laser messages from vrep
00056   ros::Subscriber vrep_subsriber = n.subscribe("/vrep/i" + std::to_string(vrep_no) +
00057     "_Pioneer_p3dx_laserScanner", 1000, laserScanData_cb);
00058 
00059   // will publish the laser mesages to the rest of the stack
00060   // only 1 laserScanner present
00061   publisher = n.advertise<sensor_msgs::LaserScan>("sensor0/LaserScan", 1000);
00062 
00063   ROS_INFO("HAL(VREP): Laser node initialized");
00064 
00065   // runs event loop
00066   ros::spin();
00067 
00068   return 0;
00069 }


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