gazebo_lidar_plugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /*
18  * Desc: Contact plugin
19  * Author: Nate Koenig mod by John Hsu
20  */
21 
22 #include "gazebo/physics/physics.hh"
24 
25 #include <gazebo/common/common.hh>
26 #include <gazebo/common/Plugin.hh>
27 #include <gazebo/gazebo.hh>
28 #include <gazebo/physics/physics.hh>
29 #include "gazebo/transport/transport.hh"
30 #include "gazebo/msgs/msgs.hh"
31 
32 #include <chrono>
33 #include <cmath>
34 #include <iostream>
35 #include <memory>
36 #include <stdio.h>
37 #include <boost/algorithm/string.hpp>
38 
40 
41 using namespace gazebo;
42 using namespace std;
43 
44 // Register this plugin with the simulator
46 
47 
49 {
50 }
51 
52 
54 {
55  this->newLaserScansConnection.reset();
56 
57  this->parentSensor.reset();
58  this->world.reset();
59 }
60 
61 
62 void GazeboLidarPlugin::Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
63 {
64  if(kPrintOnPluginLoad) {
65  gzdbg << __FUNCTION__ << "() called." << std::endl;
66  }
67 
68  // Get then name of the parent sensor
69  this->parentSensor =
70  std::dynamic_pointer_cast<sensors::RaySensor>(_parent);
71 
72  if (!this->parentSensor)
73  gzthrow("RayPlugin requires a Ray Sensor as its parent");
74 
75  this->world = physics::get_world(this->parentSensor->WorldName());
76 
77  this->newLaserScansConnection =
78  this->parentSensor->LaserShape()->ConnectNewLaserScans(
79  boost::bind(&GazeboLidarPlugin::OnNewLaserScans, this));
80 
81  if (_sdf->HasElement("robotNamespace"))
82  namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
83  else
84  gzwarn << "[gazebo_lidar_plugin] Please specify a robotNamespace.\n";
85 
86  node_handle_ = transport::NodePtr(new transport::Node());
87  node_handle_->Init(namespace_);
88 
89  const string scopedName = _parent->ParentName();
90 
91  string topicName = "~/" + scopedName + "/lidar";
92  boost::replace_all(topicName, "::", "/");
93 
94  lidar_pub_ = node_handle_->Advertise<lidar_msgs::msgs::lidar>(topicName, 10);
95 }
96 
97 
99 {
100  lidar_message.set_time_msec(0);
101  lidar_message.set_min_distance(parentSensor->RangeMin());
102  lidar_message.set_max_distance(parentSensor->RangeMax());
103  lidar_message.set_current_distance(parentSensor->Range(0));
104 
105  lidar_pub_->Publish(lidar_message);
106 }
GZ_REGISTER_SENSOR_PLUGIN(GazeboGpsPlugin)
virtual void OnNewLaserScans()
Update callback.
A Gazebo LIDAR plugin.
static const bool kPrintOnPluginLoad
Definition: common.h:41
virtual ~GazeboLidarPlugin()
Destructor.
void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
Load the plugin.


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:03