Skip to content

Light Commands via topic #1222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 28, 2021
23 changes: 23 additions & 0 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ class ignition::gazebo::systems::UserCommandsPrivate
/// \return True if successful.
public: bool LightService(const msgs::Light &_req, msgs::Boolean &_res);

/// \brief Callback for light subscription
/// \param[in] _msg Light message
public: void OnCmdLight(const msgs::Light &_msg);

/// \brief Callback for pose service
/// \param[in] _req Request containing pose update of an entity.
/// \param[out] _res True if message successfully received and queued.
Expand Down Expand Up @@ -602,6 +606,10 @@ void UserCommands::Configure(const Entity &_entity,
ignmsg << "Light configuration service on [" << lightService << "]"
<< std::endl;

std::string lightTopic{"/world/" + validWorldName + "/light_config"};
this->dataPtr->node.Subscribe(lightTopic, &UserCommandsPrivate::OnCmdLight,
this->dataPtr.get());

// Physics service
std::string physicsService{"/world/" + validWorldName + "/set_physics"};
this->dataPtr->node.Advertise(physicsService,
Expand Down Expand Up @@ -754,6 +762,21 @@ bool UserCommandsPrivate::LightService(const msgs::Light &_req,
return true;
}

//////////////////////////////////////////////////
void UserCommandsPrivate::OnCmdLight(const msgs::Light &_msg)
{
auto msg = _msg.New();
msg->CopyFrom(_msg);
auto cmd = std::make_unique<LightCommand>(msg, this->iface);

// Push to pending
{
std::lock_guard<std::mutex> lock(this->pendingMutex);
this->pendingCmds.push_back(std::move(cmd));
}
}


//////////////////////////////////////////////////
bool UserCommandsPrivate::PoseService(const msgs::Pose &_req,
msgs::Boolean &_res)
Expand Down
23 changes: 21 additions & 2 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ TEST_F(UserCommandsTest, Light)
{
// Start server
ServerConfig serverConfig;
const auto sdfFile = ignition::common::joinPaths(
std::string(PROJECT_SOURCE_PATH), "test", "worlds", "lights_render.sdf");
const auto sdfFile = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/lights_render.sdf";
serverConfig.SetSdfFile(sdfFile);

Server server(serverConfig);
Expand Down Expand Up @@ -919,6 +919,25 @@ TEST_F(UserCommandsTest, Light)
EXPECT_NEAR(1.5, spotLightComp->Data().SpotInnerAngle().Radian(), 0.1);
EXPECT_NEAR(0.3, spotLightComp->Data().SpotOuterAngle().Radian(), 0.1);
EXPECT_NEAR(0.9, spotLightComp->Data().SpotFalloff(), 0.1);

// Test light_config topic
const std::string lightTopic = "/world/lights_command/light_config";

msgs::Light lightMsg;
lightMsg.set_name("spot");
ignition::msgs::Set(lightMsg.mutable_diffuse(),
ignition::math::Color(1.0f, 1.0f, 1.0f, 1.0f));

// Publish joint trajectory
auto pub = node.Advertise<msgs::Light>(lightTopic);
pub.Publish(lightMsg);

server.Run(true, 100, false);
// Sleep for a small duration to allow Run thread to start
IGN_SLEEP_MS(10);

EXPECT_EQ(math::Color(1.0f, 1.0f, 1.0f, 1.0f),
spotLightComp->Data().Diffuse());
}

/////////////////////////////////////////////////
Expand Down