Skip to content

Commit

Permalink
Allow to turn on/off lights (#1343)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <ahcorde@gmail.com>
Co-authored-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
ahcorde and chapulina committed Mar 15, 2022
1 parent b45f60f commit 30f8b9e
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
# as protobuf could be find transitively by any dependency
set(protobuf_MODULE_COMPATIBLE TRUE)

ign_find_package(sdformat11 REQUIRED VERSION 11.2.1)
ign_find_package(sdformat11 REQUIRED VERSION 11.4.0)
set(SDF_VER ${sdformat11_VERSION_MAJOR})

#--------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,14 @@ msgs::Light ignition::gazebo::convert(const sdf::Light &_in)
out.set_spot_inner_angle(_in.SpotInnerAngle().Radian());
out.set_spot_outer_angle(_in.SpotOuterAngle().Radian());
out.set_spot_falloff(_in.SpotFalloff());

// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
auto header = out.mutable_header()->add_data();
header->set_key("isLightOn");
std::string *value = header->add_value();
*value = std::to_string(_in.LightOn());

if (_in.Type() == sdf::LightType::POINT)
out.set_type(msgs::Light_LightType_POINT);
else if (_in.Type() == sdf::LightType::SPOT)
Expand Down Expand Up @@ -597,6 +605,25 @@ sdf::Light ignition::gazebo::convert(const msgs::Light &_in)
out.SetSpotInnerAngle(math::Angle(_in.spot_inner_angle()));
out.SetSpotOuterAngle(math::Angle(_in.spot_outer_angle()));
out.SetSpotFalloff(_in.spot_falloff());

// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
bool isLightOn = true;
for (int i = 0; i < _in.header().data_size(); ++i)
{
for (int j = 0;
j < _in.header().data(i).value_size(); ++j)
{
if (_in.header().data(i).key() ==
"isLightOn")
{
isLightOn = ignition::math::parseInt(
_in.header().data(i).value(0));
}
}
}
out.SetLightOn(isLightOn);

if (_in.type() == msgs::Light_LightType_POINT)
out.SetType(sdf::LightType::POINT);
else if (_in.type() == msgs::Light_LightType_SPOT)
Expand Down
29 changes: 27 additions & 2 deletions src/gui/plugins/component_inspector/ComponentInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ void ignition::gazebo::setData(QStandardItem *_item, const msgs::Light &_data)
lightType = 2;
}

bool isLightOn = true;
for (int i = 0; i < _data.header().data_size(); ++i)
{
for (int j = 0;
j < _data.header().data(i).value_size(); ++j)
{
if (_data.header().data(i).key() ==
"isLightOn")
{
isLightOn = ignition::math::parseInt(
_data.header().data(i).value(0));
}
}
}

_item->setData(QString("Light"),
ComponentsModel::RoleNames().key("dataType"));
_item->setData(QList({
Expand All @@ -176,7 +191,8 @@ void ignition::gazebo::setData(QStandardItem *_item, const msgs::Light &_data)
QVariant(_data.spot_outer_angle()),
QVariant(_data.spot_falloff()),
QVariant(_data.intensity()),
QVariant(lightType)
QVariant(lightType),
QVariant(isLightOn)
}), ComponentsModel::RoleNames().key("data"));
}

Expand Down Expand Up @@ -989,7 +1005,8 @@ void ComponentInspector::OnLight(
double _attRange, double _attLinear, double _attConstant,
double _attQuadratic, bool _castShadows, double _directionX,
double _directionY, double _directionZ, double _innerAngle,
double _outerAngle, double _falloff, double _intensity, int _type)
double _outerAngle, double _falloff, double _intensity, int _type,
bool _isLightOn)
{
std::function<void(const ignition::msgs::Boolean &, const bool)> cb =
[](const ignition::msgs::Boolean &/*_rep*/, const bool _result)
Expand All @@ -999,6 +1016,14 @@ void ComponentInspector::OnLight(
};

ignition::msgs::Light req;

// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
auto header = req.mutable_header()->add_data();
header->set_key("isLightOn");
std::string *value = header->add_value();
*value = std::to_string(_isLightOn);

req.set_name(this->dataPtr->entityName);
req.set_id(this->dataPtr->entity);
ignition::msgs::Set(req.mutable_diffuse(),
Expand Down
3 changes: 2 additions & 1 deletion src/gui/plugins/component_inspector/ComponentInspector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,15 @@ namespace gazebo
/// \param[in] _falloff Falloff of the spotlight
/// \param[in] _intensity Intensity of the light
/// \param[in] _type light type
/// \param[in] _isLightOn is light on
public: Q_INVOKABLE void OnLight(
double _rSpecular, double _gSpecular, double _bSpecular,
double _aSpecular, double _rDiffuse, double _gDiffuse,
double _bDiffuse, double _aDiffuse, double _attRange,
double _attLinear, double _attConstant, double _attQuadratic,
bool _castShadows, double _directionX, double _directionY,
double _directionZ, double _innerAngle, double _outerAngle,
double _falloff, double _intensity, int _type);
double _falloff, double _intensity, int _type, bool _isLightOn);

/// \brief Callback in Qt thread when physics' properties change.
/// \param[in] _stepSize step size
Expand Down
6 changes: 4 additions & 2 deletions src/gui/plugins/component_inspector/ComponentInspector.qml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ Rectangle {
_rDiffuse, _gDiffuse, _bDiffuse, _aDiffuse,
_attRange, _attLinear, _attConstant, _attQuadratic,
_castShadows, _directionX, _directionY, _directionZ,
_innerAngle, _outerAngle, _falloff, _intensity, _type) {
_innerAngle, _outerAngle, _falloff, _intensity, _type,
_isLightOn) {
ComponentInspector.OnLight(_rSpecular, _gSpecular, _bSpecular, _aSpecular,
_rDiffuse, _gDiffuse, _bDiffuse, _aDiffuse,
_attRange, _attLinear, _attConstant, _attQuadratic,
_castShadows, _directionX, _directionY, _directionZ,
_innerAngle, _outerAngle, _falloff, _intensity, _type)
_innerAngle, _outerAngle, _falloff, _intensity, _type,
_isLightOn)
}

/*
Expand Down
37 changes: 36 additions & 1 deletion src/gui/plugins/component_inspector/Light.qml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ Rectangle {
// Loaded item for intensity
property var intensityItem: {}

// Loaded item for isLightOn
property var isLightOnItem: {}

// Send new light data to C++
function sendLight() {
// TODO(anyone) There's a loss of precision when these values get to C++
Expand All @@ -123,7 +126,8 @@ Rectangle {
outerAngleItem.value,
falloffItem.value,
intensityItem.value,
model.data[20]
model.data[20],
isLightOnItem.checked
);
}

Expand Down Expand Up @@ -285,6 +289,37 @@ Rectangle {
id: grid
width: parent.width

RowLayout {
Rectangle {
color: "transparent"
height: 40
Layout.preferredWidth: isOnText.width + indentation*3

Text {
id : isOnText
text: ' Turn on/off'
leftPadding: 5
color: Material.theme == Material.Light ? "#444444" : "#bbbbbb"
font.pointSize: 12
anchors.centerIn: parent
}
}
Item {
Layout.fillWidth: true
height: 40

Loader {
id: isOnLoader
anchors.fill: parent
property double numberValue: model.data[21]
sourceComponent: ignSwitch
onLoaded: {
isLightOnItem = isOnLoader.item
}
}
}
}

RowLayout {
Rectangle {
color: "transparent"
Expand Down
32 changes: 28 additions & 4 deletions src/rendering/RenderUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2131,11 +2131,35 @@ void RenderUtilPrivate::UpdateLights(
auto l = std::dynamic_pointer_cast<rendering::Light>(node);
if (l)
{
if (!ignition::math::equal(
l->Intensity(),
static_cast<double>(light.second.intensity())))
// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
bool isLightOn = true;
for (int i = 0; i < light.second.header().data_size(); ++i)
{
for (int j = 0;
j < light.second.header().data(i).value_size(); ++j)
{
if (light.second.header().data(i).key() ==
"isLightOn")
{
isLightOn = ignition::math::parseInt(
light.second.header().data(i).value(0));
}
}
}

if (isLightOn)
{
if (!ignition::math::equal(
l->Intensity(),
static_cast<double>(light.second.intensity())))
{
l->SetIntensity(light.second.intensity());
}
}
else
{
l->SetIntensity(light.second.intensity());
l->SetIntensity(0);
}
if (light.second.has_diffuse())
{
Expand Down
21 changes: 21 additions & 0 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,28 @@ class LightCommand : public UserCommandBase
public: std::function<bool(const msgs::Light &, const msgs::Light &)>
lightEql { [](const msgs::Light &_a, const msgs::Light &_b)
{
// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
auto getIsLightOn = [](const msgs::Light &_light) -> bool
{
bool isLightOn = true;
for (int i = 0; i < _light.header().data_size(); ++i)
{
for (int j = 0;
j < _light.header().data(i).value_size(); ++j)
{
if (_light.header().data(i).key() ==
"isLightOn")
{
isLightOn = ignition::math::parseInt(
_light.header().data(i).value(0));
}
}
}
return isLightOn;
};
return
getIsLightOn(_a) == getIsLightOn(_b) &&
_a.type() == _b.type() &&
_a.name() == _b.name() &&
math::equal(
Expand Down
8 changes: 8 additions & 0 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,14 @@ TEST_F(UserCommandsTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Light))
req.set_attenuation_constant(0.6f);
req.set_attenuation_quadratic(0.001f);
req.set_cast_shadows(true);

// todo(ahcorde) Use the field is_light_off in light.proto from
// Garden on.
auto header = req.mutable_header()->add_data();
header->set_key("isLightOn");
std::string *value = header->add_value();
*value = std::to_string(true);

EXPECT_TRUE(node.Request(service, req, timeout, res, result));
EXPECT_TRUE(result);
EXPECT_TRUE(res.data());
Expand Down

0 comments on commit 30f8b9e

Please sign in to comment.