gazebo_lidar_plugin.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012-2014 Open Source Robotics Foundation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 /*
00018  * Desc: Contact plugin
00019  * Author: Nate Koenig mod by John Hsu
00020  */
00021 
00022 #include "gazebo/physics/physics.hh"
00023 #include "rotors_gazebo_plugins/external/gazebo_lidar_plugin.h"
00024 
00025 #include <gazebo/common/common.hh>
00026 #include <gazebo/common/Plugin.hh>
00027 #include <gazebo/gazebo.hh>
00028 #include <gazebo/physics/physics.hh>
00029 #include "gazebo/transport/transport.hh"
00030 #include "gazebo/msgs/msgs.hh"
00031 
00032 #include <chrono>
00033 #include <cmath>
00034 #include <iostream>
00035 #include <memory>
00036 #include <stdio.h>
00037 #include <boost/algorithm/string.hpp>
00038 
00039 #include "rotors_gazebo_plugins/common.h"
00040 
00041 using namespace gazebo;
00042 using namespace std;
00043 
00044 // Register this plugin with the simulator
00045 GZ_REGISTER_SENSOR_PLUGIN(GazeboLidarPlugin)
00046 
00047 
00048 GazeboLidarPlugin::GazeboLidarPlugin()
00049 {
00050 }
00051 
00052 
00053 GazeboLidarPlugin::~GazeboLidarPlugin()
00054 {
00055   this->newLaserScansConnection.reset();
00056 
00057   this->parentSensor.reset();
00058   this->world.reset();
00059 }
00060 
00061 
00062 void GazeboLidarPlugin::Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
00063 {
00064   if(kPrintOnPluginLoad) {
00065     gzdbg << __FUNCTION__ << "() called." << std::endl;
00066   }
00067 
00068   // Get then name of the parent sensor
00069   this->parentSensor =
00070     std::dynamic_pointer_cast<sensors::RaySensor>(_parent);
00071 
00072   if (!this->parentSensor)
00073     gzthrow("RayPlugin requires a Ray Sensor as its parent");
00074 
00075   this->world = physics::get_world(this->parentSensor->WorldName());
00076 
00077   this->newLaserScansConnection =
00078     this->parentSensor->LaserShape()->ConnectNewLaserScans(
00079       boost::bind(&GazeboLidarPlugin::OnNewLaserScans, this));
00080 
00081   if (_sdf->HasElement("robotNamespace"))
00082     namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
00083   else
00084     gzwarn << "[gazebo_lidar_plugin] Please specify a robotNamespace.\n";
00085 
00086   node_handle_ = transport::NodePtr(new transport::Node());
00087   node_handle_->Init(namespace_);
00088 
00089   const string scopedName = _parent->ParentName();
00090 
00091   string topicName = "~/" + scopedName + "/lidar";
00092   boost::replace_all(topicName, "::", "/");
00093 
00094   lidar_pub_ = node_handle_->Advertise<lidar_msgs::msgs::lidar>(topicName, 10);
00095 }
00096 
00097 
00098 void GazeboLidarPlugin::OnNewLaserScans()
00099 {
00100   lidar_message.set_time_msec(0);
00101   lidar_message.set_min_distance(parentSensor->RangeMin());
00102   lidar_message.set_max_distance(parentSensor->RangeMax());
00103   lidar_message.set_current_distance(parentSensor->Range(0));
00104 
00105   lidar_pub_->Publish(lidar_message);
00106 }


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Thu Apr 18 2019 02:43:43