Skip to content

Commit

Permalink
World: add gz-transport /world_control subscriber
Browse files Browse the repository at this point in the history
Subscribe to WorldControl messages over gz-transport
(ZeroMQ) in addition to gazebo_transport (boost asio),
since gz-transport does a better job closing sockets.

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
scpeters committed Feb 23, 2024
1 parent 9442fa6 commit 299fdb2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
39 changes: 27 additions & 12 deletions gazebo/physics/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ void World::Load(sdf::ElementPtr _sdf)
&World::OnFactoryMsg, this);
this->dataPtr->controlSub = this->dataPtr->node->Subscribe("~/world_control",
&World::OnControl, this);
{
// Also subscribe to WorldControl messages over ZeroMQ-based gz-transport
std::string worldControlTopic("/world_control");
if (!this->dataPtr->ignNode.Subscribe(worldControlTopic,
&World::OnWorldControl, this))
{
gzerr << "Error advertising topic [" << worldControlTopic << "]\n";
}
}
this->dataPtr->playbackControlSub = this->dataPtr->node->Subscribe(
"~/playback_control", &World::OnPlaybackControl, this);

Expand Down Expand Up @@ -1609,43 +1618,49 @@ void World::OnFactoryMsg(ConstFactoryPtr &_msg)
//////////////////////////////////////////////////
void World::OnControl(ConstWorldControlPtr &_data)
{
if (_data->has_pause())
this->SetPaused(_data->pause());
this->OnWorldControl(*_data);
}

//////////////////////////////////////////////////
void World::OnWorldControl(const msgs::WorldControl &_data)
{
if (_data.has_pause())
this->SetPaused(_data.pause());

if (_data->has_step())
if (_data.has_step())
this->OnStep();

if (_data->has_multi_step())
if (_data.has_multi_step())
{
// stepWorld is a blocking call so set stepInc directly so that world stats
// will still be published
this->SetPaused(true);
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->worldUpdateMutex);
this->dataPtr->stepInc = _data->multi_step();
this->dataPtr->stepInc = _data.multi_step();
}

if (_data->has_seed())
if (_data.has_seed())
{
ignition::math::Rand::Seed(_data->seed());
this->dataPtr->physicsEngine->SetSeed(_data->seed());
ignition::math::Rand::Seed(_data.seed());
this->dataPtr->physicsEngine->SetSeed(_data.seed());
}

if (_data->has_reset())
if (_data.has_reset())
{
this->dataPtr->needsReset = true;

if (_data->reset().has_all() && _data->reset().all())
if (_data.reset().has_all() && _data.reset().all())
{
this->dataPtr->resetAll = true;
}
else
{
this->dataPtr->resetAll = false;

if (_data->reset().has_time_only() && _data->reset().time_only())
if (_data.reset().has_time_only() && _data.reset().time_only())
this->dataPtr->resetTimeOnly = true;

if (_data->reset().has_model_only() && _data->reset().model_only())
if (_data.reset().has_model_only() && _data.reset().model_only())
this->dataPtr->resetModelOnly = true;
}
}
Expand Down
8 changes: 7 additions & 1 deletion gazebo/physics/World.hh
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,16 @@ namespace gazebo
/// \brief Step callback.
private: void OnStep();

/// \brief Called when a world control message is received.
/// \brief Called when a world control message is received on the
/// gazebo_transport topic using boost asio.
/// \param[in] _data The world control message.
private: void OnControl(ConstWorldControlPtr &_data);

/// \brief Called when a world control message is received on the
/// gz-transport topic using ZeroMQ.
/// \param[in] _data The world control message.
private: void OnWorldControl(const msgs::WorldControl &_data);

/// \brief Called when log playback control message is received.
/// \param[in] _data The log playback control message.
private: void OnPlaybackControl(ConstLogPlaybackControlPtr &_data);
Expand Down

0 comments on commit 299fdb2

Please sign in to comment.