20 #include <boost/algorithm/string/replace.hpp> 21 #include <gazebo/physics/physics.hh> 22 #include <gazebo/transport/Node.hh> 23 #include <gazebo/transport/Publisher.hh> 48 std::string globalTopicName =
"~/";
49 globalTopicName += this->
parentSensor->Name() +
"/" + this->GetHandle() +
"/" + topicName;
50 boost::replace_all(globalTopicName,
"::",
"/");
52 return globalTopicName;
60 std::dynamic_pointer_cast<sensors::RaySensor>(_parent);
63 gzthrow(
"ProximityRayPlugin requires a Ray Sensor as its parent");
66 this->
world = physics::get_world(worldName);
67 this->
node = transport::NodePtr(
new transport::Node());
68 this->
node->Init(worldName);
70 if (_sdf->HasElement(
"time_delay"))
72 double time_delay = _sdf->Get<
double>(
"time_delay");
74 gzdbg <<
"Setting update rate of parent sensor to " << 1.0/time_delay <<
" Hz\n";
77 gzdbg <<
"Using update rate of parent sensor: " << this->
parentSensor->UpdateRate() <<
" Hz\n";
80 if (_sdf->HasElement(
"output_state_topic"))
82 this->
stateTopic = _sdf->Get<std::string>(
"output_state_topic");
91 if (_sdf->HasElement(
"output_change_topic"))
124 if (_sdf->HasElement(
"use_link_frame"))
126 this->useLinkFrame = _sdf->Get<
bool>(
"use_link_frame");
128 if (this->useLinkFrame)
130 std::string linkName = this->
parentSensor->ParentName();
131 this->
link = boost::dynamic_pointer_cast<physics::Link>(this->
world->GetEntity(linkName));
146 std::lock_guard<std::mutex> lock(this->
mutex);
147 msgs::Set(this->
stateMsg.mutable_stamp(), this->
world->GetSimTime());
167 bool stateChanged =
false;
173 std::vector<double> ranges;
178 for (
unsigned int i = 0; i<ranges.size(); i++){
179 double range = ranges[i];
182 objectDetected =
true;
187 if (objectDetected) {
188 if (!this->objectDetected) {
189 gzdbg <<
"Object detected\n";
192 this->objectDetected =
true;
196 if (this->objectDetected) {
197 gzdbg <<
"Object no longer detected\n";
200 this->objectDetected =
false;
event::ConnectionPtr newLaserScansConnection
The connection tied to ProximityRayPlugin::OnNewLaserScans()
physics::WorldPtr world
Pointer to world.
bool useLinkFrame
Convert sensor ranges to parent link frame?
virtual bool ProcessScan()
Process the scan data and update state.
virtual void OnNewLaserScans()
Update callback.
void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)
Load the plugin.
physics::LinkPtr link
Pointer to parent link.
transport::NodePtr node
Pointer to this node for publishing.
A Ray Sensor Plugin which makes it act as a proximity sensor.
msgs::Header stateMsg
State message.
double sensingRangeMin
Minimum sensing range in meters.
bool objectDetected
Sensor detection state.
std::mutex mutex
Mutex to protect interruptionMsg.
sensors::RaySensorPtr parentSensor
The parent sensor.
std::string Topic(std::string topicName) const
Generate a scoped topic name from a local one.
std::string stateTopic
Topic name for state message.
transport::PublisherPtr stateChangePub
Publisher for the sensor state change.
virtual ~ProximityRayPlugin()
Destructor.
double sensingRangeMax
Maximum sensing range in meters.
transport::PublisherPtr statePub
Publisher for the sensor state.
std::string stateChangeTopic
Topic name for state change message.