Skip to content

Commit

Permalink
Light Commands via topic (#1222)
Browse files Browse the repository at this point in the history
Signed-off-by: William Lew <[email protected]>
  • Loading branch information
WilliamLewww authored Dec 28, 2021
1 parent 0810a6b commit fe335f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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
19 changes: 19 additions & 0 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
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 light config
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

0 comments on commit fe335f9

Please sign in to comment.