Skip to content

Commit

Permalink
Fix gzclient starting with black screen (#3121)
Browse files Browse the repository at this point in the history
* World: advertise scene info as ignition service
* Scene: use ignition service for scene info

This should be more reliable than the ~/request topic
over gazebo::transport, but if it fails then fall back
on the current method.

* ScenePrivate: store requestMsg as unique_ptr
* Fix more compiler warnings

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
scpeters authored Oct 15, 2021
1 parent 04fd631 commit 720b52d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 20 deletions.
33 changes: 31 additions & 2 deletions gazebo/physics/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,19 @@ void World::Load(sdf::ElementPtr _sdf)
<< std::endl;
}

std::string sceneInfoService("/scene_info");
if (!this->dataPtr->ignNode.Advertise(sceneInfoService,
&World::SceneInfoService, this))
{
gzerr << "Error advertising service [" << sceneInfoService << "]"
<< std::endl;
}

std::string shadowCasterMaterialNameService("/shadow_caster_material_name");
if (!this->dataPtr->ignNode.Advertise(shadowCasterMaterialNameService,
&World::ShadowCasterMaterialNameService, this))
{
gzerr << "Error advertising service [" <<
gzerr << "Error advertising service [" <<
shadowCasterMaterialNameService << "]" << std::endl;
}

Expand All @@ -313,7 +321,7 @@ void World::Load(sdf::ElementPtr _sdf)
if (!this->dataPtr->ignNode.Advertise(shadowCasterRenderBackFacesService,
&World::ShadowCasterRenderBackFacesService, this))
{
gzerr << "Error advertising service [" <<
gzerr << "Error advertising service [" <<
shadowCasterRenderBackFacesService << "]" << std::endl;
}

Expand Down Expand Up @@ -3385,6 +3393,27 @@ bool World::PluginInfoService(const ignition::msgs::StringMsg &_req,
return false;
}

//////////////////////////////////////////////////
bool World::SceneInfoService(msgs::Scene &_res)
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->receiveMutex);

// Copy implementation from ProcessRequestMsgs
this->dataPtr->sceneMsg.clear_model();
this->dataPtr->sceneMsg.clear_light();
this->BuildSceneMsg(this->dataPtr->sceneMsg, this->dataPtr->rootElement);

_res = this->dataPtr->sceneMsg;

for (auto road : this->dataPtr->roads)
{
// this causes the roads to publish road msgs.
road->Init();
}

return true;
}

//////////////////////////////////////////////////
bool World::ShadowCasterMaterialNameService(ignition::msgs::StringMsg &_res)
{
Expand Down
7 changes: 6 additions & 1 deletion gazebo/physics/World.hh
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,18 @@ namespace gazebo
private: bool PluginInfoService(const ignition::msgs::StringMsg &_request,
ignition::msgs::Plugin_V &_plugins);

/// \brief Callback for "<this_name>/scene_info" service.
/// \param[out] _response Message containing scene info
/// \return True if the info was successfully obtained.
private: bool SceneInfoService(msgs::Scene &_response);

/// \brief Callback for "<this_name>/shadow_caster_material_name" service.
/// \param[out] _response Message containing shadow caster material name
/// \return True if the info was successfully obtained.
private: bool ShadowCasterMaterialNameService(
ignition::msgs::StringMsg &_response);

/// \brief Callback for "<this_name>/shadow_caster_render_back_faces"
/// \brief Callback for "<this_name>/shadow_caster_render_back_faces"
/// service.
/// \param[out] _response Message containing shadow caster render back
/// faces
Expand Down
4 changes: 4 additions & 0 deletions gazebo/physics/dart/DARTJointPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ namespace gazebo

mFuncs.clear();

#if DART_VERSION_AT_LEAST(6, 10, 0)
this->dtJoint->setLimitEnforcement(true);
#else
this->dtJoint->setPositionLimitEnforced(true);
#endif
}

/// \brief Return true if DART Joint is initialized
Expand Down
46 changes: 34 additions & 12 deletions gazebo/rendering/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ Scene::Scene(const std::string &_name, const bool _enableVisualizations,
this->dataPtr->transparent = false;
this->dataPtr->wireframe = false;

this->dataPtr->requestMsg = NULL;
this->dataPtr->enableVisualizations = _enableVisualizations;
this->dataPtr->node = transport::NodePtr(new transport::Node());
this->dataPtr->node->Init(_name);
Expand Down Expand Up @@ -226,7 +225,7 @@ Scene::Scene(const std::string &_name, const bool _enableVisualizations,
else
{
gzerr << "Service call[" << serviceName << "] timed out" << std::endl;
}
}
}

{
Expand All @@ -248,7 +247,7 @@ Scene::Scene(const std::string &_name, const bool _enableVisualizations,
else
{
gzerr << "Service call[" << serviceName << "] timed out" << std::endl;
}
}
}
}

Expand Down Expand Up @@ -355,8 +354,7 @@ Scene::~Scene()
{
this->Clear();

delete this->dataPtr->requestMsg;
this->dataPtr->requestMsg = NULL;
this->dataPtr->requestMsg.reset(nullptr);
delete this->dataPtr->receiveMutex;
this->dataPtr->receiveMutex = NULL;

Expand Down Expand Up @@ -454,9 +452,20 @@ void Scene::Init()
this->dataPtr->worldVisual));
this->dataPtr->originVisual->Load();

this->dataPtr->requestPub->WaitForConnection();
this->dataPtr->requestMsg = msgs::CreateRequest("scene_info");
this->dataPtr->requestPub->Publish(*this->dataPtr->requestMsg);
// Get scene info from physics::World with ignition transport service
ignition::transport::Node node;
const std::string serviceName = "/scene_info";
std::vector<ignition::transport::ServicePublisher> publishers;
if (!node.ServiceInfo(serviceName, publishers) ||
!node.Request(serviceName, &Scene::OnSceneInfo, this))
{
gzwarn << "Ignition transport [" << serviceName << "] service call failed,"
<< " falling back to gazebo transport [scene_info] request."
<< std::endl;
this->dataPtr->requestPub->WaitForConnection();
this->dataPtr->requestMsg.reset(msgs::CreateRequest("scene_info"));
this->dataPtr->requestPub->Publish(*this->dataPtr->requestMsg);
}

if (!this->dataPtr->isServer)
{
Expand Down Expand Up @@ -2482,6 +2491,21 @@ void Scene::OnScene(ConstScenePtr &_msg)
this->dataPtr->sceneMsgs.push_back(_msg);
}

/////////////////////////////////////////////////
void Scene::OnSceneInfo(const msgs::Scene &_msg, const bool _result)
{
if (_result)
{
std::lock_guard<std::mutex> lock(*this->dataPtr->receiveMutex);
auto msgptr = boost::make_shared<const msgs::Scene>(_msg);
this->dataPtr->sceneMsgs.push_back(msgptr);
}
else
{
gzerr << "Error when calling /scene_info service" << std::endl;
}
}

/////////////////////////////////////////////////
void Scene::OnResponse(ConstResponsePtr &_msg)
{
Expand All @@ -2491,11 +2515,9 @@ void Scene::OnResponse(ConstResponsePtr &_msg)

msgs::Scene sceneMsg;
sceneMsg.ParseFromString(_msg->serialized_data());
boost::shared_ptr<msgs::Scene> sm(new msgs::Scene(sceneMsg));
this->OnSceneInfo(sceneMsg, true);

std::lock_guard<std::mutex> lock(*this->dataPtr->receiveMutex);
this->dataPtr->sceneMsgs.push_back(sm);
this->dataPtr->requestMsg = NULL;
this->dataPtr->requestMsg.reset(nullptr);
}

/////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions gazebo/rendering/Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ namespace gazebo
/// \param[in] _msg The message.
private: void OnScene(ConstScenePtr &_msg);

/// \brief Called when the scene info service replies with the scene
/// message.
/// \param[in] _msg The message.
/// \param[in] _result Flag indicating if service call succeeded.
private: void OnSceneInfo(const msgs::Scene &_msg, const bool _result);

/// \brief Response callback
/// \param[in] _msg The message data.
private: void OnResponse(ConstResponsePtr &_msg);
Expand Down
7 changes: 4 additions & 3 deletions gazebo/rendering/ScenePrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#ifndef GAZEBO_RENDERING_SCENE_PRIVATE_HH_
#define GAZEBO_RENDERING_SCENE_PRIVATE_HH_

#include <condition_variable>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <mutex>
#include <condition_variable>

#include <boost/unordered/unordered_map.hpp>

Expand Down Expand Up @@ -300,7 +301,7 @@ namespace gazebo
public: std::string selectionMode;

/// \brief Keep around our request message.
public: msgs::Request *requestMsg = nullptr;
public: std::unique_ptr<msgs::Request> requestMsg;

/// \brief True if visualizations should be rendered.
public: bool enableVisualizations;
Expand Down
30 changes: 30 additions & 0 deletions test/integration/info_services.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,36 @@ TEST_F(InfoServicesTest, ModelPlugins)
EXPECT_TRUE(g_badElement);
}

/////////////////////////////////////////////////
// Request info about scene
TEST_F(InfoServicesTest, Scene)
{
this->Load("worlds/empty.world");

ignition::transport::Node ignNode;

std::string sceneInfoService("/scene_info");

std::vector<ignition::transport::ServicePublisher> publishers;
ASSERT_TRUE(ignNode.ServiceInfo(sceneInfoService, publishers));

gazebo::msgs::Scene scene;
unsigned int timeout = 5000;
bool result;
EXPECT_TRUE(ignNode.Request(sceneInfoService, timeout, scene, result));
EXPECT_TRUE(result);

EXPECT_EQ("default", scene.name());

ASSERT_EQ(1, scene.model_size());
auto model = scene.model(0);
EXPECT_EQ("ground_plane", model.name());

ASSERT_EQ(1, scene.light_size());
auto light = scene.light(0);
EXPECT_EQ("sun", light.name());
}

/////////////////////////////////////////////////
// Main
int main(int argc, char **argv)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/shadow_caster_render_back_faces.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TEST_F(ShadowCasterRenderBackFacesTest, ShadowCasterBackFaces)
}

/////////////////////////////////////////////////
// \brief The shadow caster will not render back faces; The plane in the world
// \brief The shadow caster will not render back faces; The plane in the world
/// will not cast a shadow
void ShadowCasterRenderBackFacesTest::ShadowCasterNoBackFaces()
{
Expand Down
2 changes: 1 addition & 1 deletion tools/gz_marker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void MarkerCommand::List()
for (auto const &d : data)
{
std::cout << "NAMESPACE " << d.first << std::endl;
for (auto const m : d.second)
for (auto const &m : d.second)
{
uint64_t id = std::get<0>(m);
std::cout << " ID " << id;
Expand Down

0 comments on commit 720b52d

Please sign in to comment.