Skip to content

Commit

Permalink
Add Light Usercommand and include Light parameters in the componentIn…
Browse files Browse the repository at this point in the history
…spector (#482)

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
ahcorde and chapulina authored Feb 17, 2021
1 parent 5e0c773 commit 44a6713
Show file tree
Hide file tree
Showing 18 changed files with 2,343 additions and 12 deletions.
12 changes: 12 additions & 0 deletions examples/standalone/light_control/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

find_package(ignition-transport9 QUIET REQUIRED OPTIONAL_COMPONENTS log)
set(IGN_TRANSPORT_VER ${ignition-transport9_VERSION_MAJOR})

find_package(ignition-gazebo4 REQUIRED)
set(IGN_GAZEBO_VER ${ignition-gazebo4_VERSION_MAJOR})

add_executable(light_control light_control.cc)
target_link_libraries(light_control
ignition-transport${IGN_TRANSPORT_VER}::core
ignition-gazebo${IGN_GAZEBO_VER}::ignition-gazebo${IGN_GAZEBO_VER})
179 changes: 179 additions & 0 deletions examples/standalone/light_control/light_control.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <chrono>
#include <iostream>

#include <ignition/gazebo/components/Light.hh>
#include <ignition/gazebo/components/LightCmd.hh>
#include <ignition/msgs.hh>
#include <ignition/transport.hh>

using namespace std::chrono_literals;

// Create a transport node.
ignition::transport::Node node;

bool result;
// timeout used for services
constexpr unsigned int timeout = 5000;

std::mt19937 twister(std::time(0));
std::uniform_real_distribution<double> distr(0, 1000);
constexpr double epsilon = 0.1;

float directionX = 0.5;
float directionY = 0.2;
float directionZ = -0.9;

void createLight()
{
ignition::msgs::Boolean rep;
//! [create light]
ignition::msgs::EntityFactory entityFactoryRequest;

entityFactoryRequest.mutable_light()->set_name("point");
entityFactoryRequest.mutable_light()->set_range(4);
entityFactoryRequest.mutable_light()->set_attenuation_linear(0.5);
entityFactoryRequest.mutable_light()->set_attenuation_constant(0.2);
entityFactoryRequest.mutable_light()->set_attenuation_quadratic(0.01);
entityFactoryRequest.mutable_light()->set_cast_shadows(false);
entityFactoryRequest.mutable_light()->set_type(ignition::msgs::Light::POINT);
ignition::msgs::Set(
entityFactoryRequest.mutable_light()->mutable_direction(),
ignition::math::Vector3d(directionX, directionY, directionZ));
ignition::msgs::Set(entityFactoryRequest.mutable_light()->mutable_pose(),
ignition::math::Pose3d(0.0, 0, 3.0, 0.0, 0.0, 0.0));
//! [create light]

bool executedEntityFactory = node.Request("/world/empty/create",
entityFactoryRequest, timeout, rep, result);
if (executedEntityFactory)
{
if (result)
std::cout << "Light was created : [" << rep.data() << "]" << std::endl;
else
{
std::cout << "Service call failed" << std::endl;
return;
}
}
else
std::cerr << "Service call timed out" << std::endl;
}

void createSphere()
{
auto modelStr = R"(
<?xml version="1.0" ?>
<sdf version='1.7'>
<model name='sphere'>
<link name='link'>
<pose>0 0 0.5 0 0 0</pose>
<visual name='visual'>
<geometry><sphere><radius>1</radius></sphere></geometry>
</visual>
<collision name='collision'>
<geometry><sphere><radius>1</radius></sphere></geometry>
</collision>
</link>
</model>
</sdf>)";

ignition::msgs::EntityFactory req;
ignition::msgs::Boolean res;
req.set_sdf(modelStr);

bool executed = node.Request("/world/empty/create",
req, timeout, res, result);
if (executed)
{
if (result)
std::cout << "Sphere was created : [" << res.data() << "]" << std::endl;
else
{
std::cout << "Service call failed" << std::endl;
return;
}
}
else
std::cerr << "Service call timed out" << std::endl;
}

//////////////////////////////////////////////////
int main(int argc, char **argv)
{
ignition::msgs::Boolean rep;
ignition::msgs::Light lightRequest;
auto lightConfigService = "/world/empty/light_config";

createSphere();
createLight();

while (1)
{
float r, g, b;
double m = 0;
//! [random numbers]
while (m < epsilon)
{
r = distr(twister);
g = distr(twister);
b = distr(twister);
m = std::sqrt(r*r + b*b + g*g);
}
r /= m;
b /= m;
b /= m;
//! [random numbers]

//! [modify light]
lightRequest.set_name("point");
lightRequest.set_range(4);
lightRequest.set_attenuation_linear(0.5);
lightRequest.set_attenuation_constant(0.2);
lightRequest.set_attenuation_quadratic(0.01);
lightRequest.set_cast_shadows(false);
lightRequest.set_type(ignition::msgs::Light::POINT);
// direction field only affects spot / directional lights
ignition::msgs::Set(lightRequest.mutable_direction(),
ignition::math::Vector3d(directionX, directionY, directionZ));
ignition::msgs::Set(lightRequest.mutable_pose(),
ignition::math::Pose3d(0.0, -1.5, 3.0, 0.0, 0.0, 0.0));
ignition::msgs::Set(lightRequest.mutable_diffuse(),
ignition::math::Color(r, g, b, 1));
ignition::msgs::Set(lightRequest.mutable_specular(),
ignition::math::Color(r, g, b, 1));
//! [modify light]
bool executed = node.Request(lightConfigService, lightRequest, timeout,
rep, result);
std::cout << "Service called: [" << r << ", " << g << ", " << b << "]"
<< std::endl;

if (executed)
{
if (result)
std::cout << "Response: [" << rep.data() << "]" << std::endl;
else
std::cout << "Service call failed" << std::endl;
}
else
std::cerr << "Service call timed out" << std::endl;

std::this_thread::sleep_for(1s);
}
}
48 changes: 48 additions & 0 deletions include/ignition/gazebo/components/LightCmd.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GAZEBO_COMPONENTS_LIGHTCMD_HH_
#define IGNITION_GAZEBO_COMPONENTS_LIGHTCMD_HH_

#include <sdf/Light.hh>

#include <ignition/gazebo/config.hh>
#include <ignition/gazebo/Export.hh>
#include <ignition/gazebo/components/Factory.hh>
#include <ignition/gazebo/components/Component.hh>
#include <ignition/gazebo/components/Serialization.hh>

#include <ignition/msgs/light.pb.h>

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component type that contains commanded light of an
/// entity in the world frame represented by msgs::Light.
using LightCmd = Component<ignition::msgs::Light,
class LightCmdTag, serializers::MsgSerializer>;
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.LightCmd",
LightCmd)
}
}
}
}
#endif
Loading

0 comments on commit 44a6713

Please sign in to comment.