SMACC2
LedPlugin.cc
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21
22#include <memory>
23#include <vector>
24
25#include <ignition/math/Color.hh>
26
27#include <gazebo/common/Plugin.hh>
28#include <gazebo/msgs/msgs.hh>
29#include <gazebo/physics/physics.hh>
30#include <gazebo/transport/transport.hh>
31#include "LedPlugin.hh"
32
33namespace smacc2
34{
36 {
39 transparency(0.2), defaultEmissiveColor(ignition::math::Color::White),
40 visualExists(false)
41 {
42 }
43
45 public: double transparency;
46
48 public: ignition::math::Color defaultEmissiveColor;
49
51 public: gazebo::transport::PublisherPtr pubVisual;
52
54 public: msgs::Visual msg;
55
57 public: bool visualExists;
58 };
59
61 {
63 public: gazebo::transport::NodePtr node;
64
66 public: gazebo::transport::PublisherPtr pubVisual;
67 };
68}
69
70using namespace gazebo;
71
72namespace smacc2
73{
76 const sdf::ElementPtr &_sdf,
77 const gazebo::physics::ModelPtr &_model,
78 const gazebo::common::Time &_currentTime,
79 gazebo_ros::Node::SharedPtr rosnode)
80 : FlashLightSetting(_sdf, _model, _currentTime, rosnode),
81 dataPtr(new LedSettingPrivate)
82{
83 // check if the visual element exists.
84 this->dataPtr->visualExists = false;
85 msgs::Link msg;
86 this->Link()->FillMsg(msg);
87 for (auto visualMsg : msg.visual())
88 {
89 if (visualMsg.name()
90 == this->Link()->GetScopedName() + "::" + this->Name())
91 {
92 if (visualMsg.has_transparency())
93 {
94 this->dataPtr->transparency = visualMsg.transparency();
95 }
96
97 if (visualMsg.has_material()
98 && visualMsg.material().has_emissive())
99 {
100 this->dataPtr->defaultEmissiveColor
101 = msgs::Convert(visualMsg.material().emissive());
102 }
103
104 this->dataPtr->visualExists = true;
105 break;
106 }
107 }
108}
109
112{
113}
114
116void LedSetting::InitPubVisual(const gazebo::transport::PublisherPtr &_pubVisual)
117{
118 // The PublisherPtr
119 this->dataPtr->pubVisual = _pubVisual;
120
121 if (this->dataPtr->visualExists)
122 {
123 // Make a message
124 this->dataPtr->msg.set_name(
125 this->Link()->GetScopedName() + "::" + this->Name());
126 this->dataPtr->msg.set_parent_name(this->Link()->GetScopedName());
127 uint32_t id;
128 this->Link()->VisualId(this->Name(), id);
129 this->dataPtr->msg.set_id(id);
130 }
131}
132
135{
136 // Call the function of the parent class.
138
139 // Make the appearance brighter.
140 this->dataPtr->msg.set_transparency(0.0);
141 ignition::math::Color color = this->CurrentColor();
142 if (color != ignition::math::Color::Black)
143 {
144 // If the base class is using a specific color rather than the default,
145 // apply it to the visual object.
146 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_diffuse(), color);
147 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_emissive(), color);
148 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_specular(), color);
149 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_ambient(), color);
150 }
151 else
152 {
153 // Otherwise, just apply the default color.
154 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_emissive(),
155 this->dataPtr->defaultEmissiveColor);
156 }
157
158 // Send the message.
159 if (this->dataPtr->visualExists)
160 {
161 // NOTE: this if statement is to make sure that a visual object to update
162 // has been created in the environment before publishing a message.
163 // Otherwise, a duplicate object will be created and the original one will
164 // never be updated.
165 // This problem is solved by the patch (Pull Request # 2983), which has
166 // been merged into gazebo7 as of July 16, 2018. This if statement should be
167 // removed once the patch is forwarded up to gazebo9.
168 if (this->Link()->GetWorld()->SimTime() > 2.0)
169 this->dataPtr->pubVisual->Publish(this->dataPtr->msg);
170 }
171}
172
175{
176 // Call the function of the parent class.
178
179 // Make the appearance darker.
180 this->dataPtr->msg.set_transparency(this->dataPtr->transparency);
181 msgs::Set(this->dataPtr->msg.mutable_material()->mutable_emissive(),
182 ignition::math::Color(0, 0, 0));
183 // Send the message.
184 if (this->dataPtr->visualExists)
185 {
186 // NOTE: this if statement is to make sure that a visual object to update
187 // has been created in the environment before publishing a message.
188 // Otherwise, a duplicate object will be created and the original one will
189 // never be updated.
190 // This problem is solved by the patch (Pull Request # 2983), which has
191 // been merged into gazebo7 as of July 16, 2018. This if statement should be
192 // removed once the patch is forwarded up to gazebo9.
193 if (this->Link()->GetWorld()->SimTime() > 2.0)
194 this->dataPtr->pubVisual->Publish(this->dataPtr->msg);
195 }
196}
197
200{
201 // Create a node
202 this->dataPtr->node = gazebo::transport::NodePtr(new gazebo::transport::Node());
203 this->dataPtr->node->Init();
204
205 // Advertise the topic to update lights
206 this->dataPtr->pubVisual
207 = this->dataPtr->node->Advertise<gazebo::msgs::Visual>("~/visual");
208 // NOTE: it should not call WaitForConnection() since there could be no
209 // subscriber to "~/visual" topic if the render engine is not running (e.g.,
210 // rostest), leading to a hang-up.
211}
212
215{
216}
217
219std::shared_ptr<FlashLightSetting> LedPlugin::CreateSetting(
220 const sdf::ElementPtr &_sdf,
221 const gazebo::physics::ModelPtr &_model,
222 const gazebo::common::Time &_currentTime,
223 gazebo_ros::Node::SharedPtr node)
224{
225 return std::make_shared<LedSetting>(_sdf, _model, _currentTime, node);
226}
227
230 std::shared_ptr<FlashLightSetting> &_setting)
231{
232 // Call the function of the parent class.
234
235 std::dynamic_pointer_cast<LedSetting>(_setting)->InitPubVisual(
236 this->dataPtr->pubVisual);
237}
238
239}
virtual void InitSettingBySpecificData(std::shared_ptr< FlashLightSetting > &_setting)
Initialize the additional part of an object of setting.
Internal data class to hold individual flash light settings. A setting for each flash light is separa...
virtual const std::string Name() const final
Getter of name.
virtual void Dim()
Dim the light This function is internally used to update the light in the environment.
virtual const gazebo::physics::LinkPtr Link() const final
Getter of link.
virtual ignition::math::Color CurrentColor() final
Get the current color of the light. This is to be used by an inheriting class of FlashLightSetting cl...
virtual void Flash()
Flash the light This function is internally used to update the light in the environment.
gazebo::transport::PublisherPtr pubVisual
The pointer to publisher to send a command to the visual.
Definition: LedPlugin.cc:66
gazebo::transport::NodePtr node
The pointer to node for communication.
Definition: LedPlugin.cc:63
virtual std::shared_ptr< FlashLightSetting > CreateSetting(const sdf::ElementPtr &_sdf, const gazebo::physics::ModelPtr &_model, const gazebo::common::Time &_currentTime, gazebo_ros::Node::SharedPtr node)
Create an object of setting.
Definition: LedPlugin.cc:219
LedPlugin()
Constructor.
Definition: LedPlugin.cc:199
virtual ~LedPlugin()
Destructor.
Definition: LedPlugin.cc:214
std::unique_ptr< LedPluginPrivate > dataPtr
Pointer to private data.
Definition: LedPlugin.hh:126
virtual void InitSettingBySpecificData(std::shared_ptr< FlashLightSetting > &_setting)
Initialize the additional part of an object of setting.
Definition: LedPlugin.cc:229
double transparency
The transparency when the light is off.
Definition: LedPlugin.cc:45
msgs::Visual msg
A message holding a Visual message.
Definition: LedPlugin.cc:54
bool visualExists
True if <visual> element exists.
Definition: LedPlugin.cc:57
gazebo::transport::PublisherPtr pubVisual
The pointer to publisher to send a command to update a visual.
Definition: LedPlugin.cc:51
ignition::math::Color defaultEmissiveColor
The emissive color.
Definition: LedPlugin.cc:48
LedSettingPrivate()
Constructor.
Definition: LedPlugin.cc:38
LedSetting(const sdf::ElementPtr &_sdf, const gazebo::physics::ModelPtr &_model, const gazebo::common::Time &_currentTime, gazebo_ros::Node::SharedPtr rosnode)
Constructor.
Definition: LedPlugin.cc:75
virtual void Dim()
Dim the light This function is internally used to update the light in the environment.
Definition: LedPlugin.cc:174
virtual void InitPubVisual(const gazebo::transport::PublisherPtr &_pubVisual) final
Set the publisher and send an initial visual command.
Definition: LedPlugin.cc:116
std::unique_ptr< LedSettingPrivate > dataPtr
Pointer to private data.
Definition: LedPlugin.hh:62
virtual void Flash()
Flash the light This function is internally used to update the light in the environment.
Definition: LedPlugin.cc:134
virtual ~LedSetting()
Destructor.
Definition: LedPlugin.cc:111