SMACC2
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
smacc2::FlashLightSetting Class Reference

Internal data class to hold individual flash light settings. A setting for each flash light is separately stored in a FlashLightSetting class, which takes care of dynamic specifications such as duration and interval. More...

#include <FlashLightPlugin.hh>

Inheritance diagram for smacc2::FlashLightSetting:
Inheritance graph
Collaboration diagram for smacc2::FlashLightSetting:
Collaboration graph

Public Member Functions

 FlashLightSetting (const sdf::ElementPtr &_sdf, const gazebo::physics::ModelPtr &_model, const gazebo::common::Time &_currentTime, gazebo_ros::Node::SharedPtr node)
 Constructor. Initialize the setting by the data given to the base plugin. More...
 
virtual ~FlashLightSetting ()
 Destructor. More...
 
virtual void InitPubLight (const gazebo::transport::PublisherPtr &_pubLight) final
 Set the publisher and send an initial light command. More...
 
virtual void UpdateLightInEnv (const gazebo::common::Time &_currentTime) final
 Update the light based on the given time. More...
 
virtual const std::string Name () const final
 Getter of name. More...
 
virtual const gazebo::physics::LinkPtr Link () const final
 Getter of link. More...
 
virtual void SwitchOn () final
 Switch on (enable the flashlight). More...
 
virtual void SwitchOff () final
 Switch off (disable the flashlight). More...
 
virtual void SetDuration (const double _duration, const int _index) final
 Set the duration time for the specified block. More...
 
virtual void SetDuration (const double _duration) final
 Set the duration time for all the blocks. More...
 
virtual void SetInterval (const double _interval, const int _index) final
 Set the interval time for the specified block. More...
 
virtual void SetInterval (const double _interval) final
 Set the interval time for all the blocks. More...
 
virtual void SetColor (const ignition::math::Color &_color, const int _index) final
 Set the color for the specified block. More...
 
virtual void SetColor (const ignition::math::Color &_color) final
 Set the color for all the blocks. More...
 
virtual unsigned int BlockCount () final
 Get the number of blocks. More...
 
virtual bool RemoveBlock (const int _index) final
 Remove a specified block. More...
 
virtual void InsertBlock (const double _duration, const double _interval, const ignition::math::Color &_color, const int _index) final
 Insert a block. Create a block with specified parameters. If the index is out of range, the block will be appended at the end of the list. More...
 

Protected Member Functions

virtual void Flash ()
 Flash the light This function is internally used to update the light in the environment. More...
 
virtual void Dim ()
 Dim the light This function is internally used to update the light in the environment. More...
 
virtual ignition::math::Color CurrentColor () final
 Get the current color of the light. This is to be used by an inheriting class of FlashLightSetting class. More...
 

Private Attributes

std::unique_ptr< FlashLightSettingPrivatedataPtr
 Pointer to private data. More...
 

Detailed Description

Internal data class to hold individual flash light settings. A setting for each flash light is separately stored in a FlashLightSetting class, which takes care of dynamic specifications such as duration and interval.

Definition at line 48 of file FlashLightPlugin.hh.

Constructor & Destructor Documentation

◆ FlashLightSetting()

smacc2::FlashLightSetting::FlashLightSetting ( const sdf::ElementPtr &  _sdf,
const gazebo::physics::ModelPtr &  _model,
const gazebo::common::Time &  _currentTime,
gazebo_ros::Node::SharedPtr  node 
)

Constructor. Initialize the setting by the data given to the base plugin.

Parameters
[in]_sdfSDF data for the setting.
[in]_modelThe Model pointer holding the light to control.
[in]_currentTimeThe current time point.

Definition at line 184 of file FlashLightPlugin.cc.

189 : dataPtr(new FlashLightSettingPrivate)
190{
191 // Get the light name.
192 std::string lightId;
193 if (_sdf->HasElement("id"))
194 {
195 lightId = _sdf->Get<std::string>("id");
196 }
197 else
198 {
199 gzerr << "Parameter <id> is missing." << std::endl;
200 }
201 int posDelim = lightId.rfind("/");
202 this->dataPtr->name = lightId.substr(posDelim+1, lightId.length());
203
204 // link which holds this light
205 this->dataPtr->link = this->dataPtr->FindLinkForLight(
206 _model, this->dataPtr->name,
207 lightId.substr(0, posDelim));
208
209 if (_sdf->HasElement("block"))
210 {
211 sdf::ElementPtr sdfBlock = _sdf->GetElement("block");
212 while (sdfBlock)
213 {
214 auto block = std::make_shared<Block>();
215 // duration
216 if (sdfBlock->HasElement("duration"))
217 {
218 block->duration = sdfBlock->Get<double>("duration");
219 }
220 else
221 {
222 //gzerr << "Parameter <duration> is missing in a block." << std::endl;
223 block->duration = std::numeric_limits<double>::max();
224
225 }
226 // interval
227 if (sdfBlock->HasElement("interval"))
228 {
229 block->interval = sdfBlock->Get<double>("interval");
230 }
231 else
232 {
233 //gzerr << "Parameter <interval> is missing in a block." << std::endl;
234 block->interval = std::numeric_limits<double>::max();
235
236 }
237 // color
238 if (sdfBlock->HasElement("color"))
239 {
240 block->color = sdfBlock->Get<ignition::math::Color>("color");
241 }
242 else
243 {
244 block->color.Reset();
245 }
246
247 this->dataPtr->blocks.push_back(block);
248 sdfBlock = sdfBlock->GetNextElement("block");
249 }
250 }
251 else
252 {
253 auto block = std::make_shared<Block>();
254 // duration
255 if (_sdf->HasElement("duration"))
256 {
257 block->duration = _sdf->Get<double>("duration");
258 }
259 else
260 {
261 // gzerr << "Parameter <duration> is missing." << std::endl;
262 block->duration = std::numeric_limits<double>::max();
263
264 }
265 // interval
266 if (_sdf->HasElement("interval"))
267 {
268 block->interval = _sdf->Get<double>("interval");
269 }
270 else
271 {
272 // gzerr << "Parameter <interval> is missing." << std::endl;
273 block->interval = std::numeric_limits<double>::max();
274
275 }
276 // color
277 if (_sdf->HasElement("color"))
278 {
279 block->color = _sdf->Get<ignition::math::Color>("color");
280 }
281 else
282 {
283 block->color.Reset();
284 }
285
286 this->dataPtr->blocks.push_back(block);
287 }
288
289 // start time
290 this->dataPtr->startTime = _currentTime;
291
292 // If link is not nullptr, the light exists.
293 if (this->dataPtr->link)
294 {
295
296
297 // void LightCmd(const std_msgs::msg::Int8::SharedPtr msg)
298 // {
299 // gzmsg << "Turning on light\n";
300
301 // if (msg->data == 1)
302 // {
303 // gzmsg << "Turning on light\n";
304 // this->TurnOnAll();
305 // // lightMsg.set_range(15.0);
306 // }
307 // else if (msg->data == 0)
308 // {
309 // gzmsg << "Turning off light\n";
310 // this->TurnOffAll();
311 // // lightMsg.set_range(0.0);
312 // }
313 // }
314 // range
315 if (this->dataPtr->link->GetSDF()->HasElement("light"))
316 {
317 auto sdfLight = this->dataPtr->link->GetSDF()->GetElement("light");
318 while (sdfLight)
319 {
320 if (sdfLight->Get<std::string>("name") == this->dataPtr->name)
321 {
322 this->dataPtr->range
323 = sdfLight->GetElement("attenuation")->Get<double>("range");
324 break;
325 }
326 sdfLight = sdfLight->GetNextElement("light");
327 }
328 this->dataPtr->lightExists = true;
329 }
330 }
331}
std::unique_ptr< FlashLightSettingPrivate > dataPtr
Pointer to private data.

References dataPtr.

◆ ~FlashLightSetting()

smacc2::FlashLightSetting::~FlashLightSetting ( )
virtual

Destructor.

Definition at line 334 of file FlashLightPlugin.cc.

335{
336}

Member Function Documentation

◆ BlockCount()

unsigned int smacc2::FlashLightSetting::BlockCount ( )
finalvirtual

Get the number of blocks.

Returns
The number of blocks the object currently has.

Definition at line 514 of file FlashLightPlugin.cc.

515{
516 return this->dataPtr->blocks.size();
517}

References dataPtr.

◆ CurrentColor()

ignition::math::Color smacc2::FlashLightSetting::CurrentColor ( )
finalprotectedvirtual

Get the current color of the light. This is to be used by an inheriting class of FlashLightSetting class.

Returns
the color for the current block which the object is using. It returns Black if there is no update about color.

Definition at line 591 of file FlashLightPlugin.cc.

592{
593 return this->dataPtr->blocks[this->dataPtr->currentBlockIndex]->color;
594}

References dataPtr.

Referenced by smacc2::LedSetting::Flash().

Here is the caller graph for this function:

◆ Dim()

void smacc2::FlashLightSetting::Dim ( )
protectedvirtual

Dim the light This function is internally used to update the light in the environment.

Reimplemented in smacc2::LedSetting.

Definition at line 577 of file FlashLightPlugin.cc.

578{
579 // Set the range to zero.
580 this->dataPtr->msg.set_range(0.0);
581 // Send the message.
582 if (this->dataPtr->lightExists)
583 {
584 //this->dataPtr->pubLight->Publish(this->dataPtr->msg);
585 }
586 // Update the state.
587 this->dataPtr->flashing = false;
588}

References dataPtr.

Referenced by smacc2::LedSetting::Dim(), and UpdateLightInEnv().

Here is the caller graph for this function:

◆ Flash()

void smacc2::FlashLightSetting::Flash ( )
protectedvirtual

Flash the light This function is internally used to update the light in the environment.

Reimplemented in smacc2::LedSetting.

Definition at line 554 of file FlashLightPlugin.cc.

555{
556 // Set the range to the default value.
557 this->dataPtr->msg.set_range(this->dataPtr->range);
558 // set the color of light.
559 if (this->dataPtr->blocks[this->dataPtr->currentBlockIndex]->color
560 != ignition::math::Color::Black)
561 {
562 msgs::Set(this->dataPtr->msg.mutable_diffuse(),
563 this->dataPtr->blocks[this->dataPtr->currentBlockIndex]->color);
564 msgs::Set(this->dataPtr->msg.mutable_specular(),
565 this->dataPtr->blocks[this->dataPtr->currentBlockIndex]->color);
566 }
567 // Send the message.
568 if (this->dataPtr->lightExists)
569 {
570 //this->dataPtr->pubLight->Publish(this->dataPtr->msg);
571 }
572 // Update the state.
573 this->dataPtr->flashing = true;
574}

References dataPtr.

Referenced by smacc2::LedSetting::Flash(), and UpdateLightInEnv().

Here is the caller graph for this function:

◆ InitPubLight()

void smacc2::FlashLightSetting::InitPubLight ( const gazebo::transport::PublisherPtr &  _pubLight)
finalvirtual

Set the publisher and send an initial light command.

Parameters
[in]_pubLightThe publisher to send a message

Definition at line 339 of file FlashLightPlugin.cc.

341{
342 // The PublisherPtr
343 this->dataPtr->pubLight = _pubLight;
344
345 if (this->dataPtr->lightExists)
346 {
347 // Make a message
348 this->dataPtr->msg.set_name(
349 this->dataPtr->link->GetScopedName() + "::" + this->dataPtr->name);
350 this->dataPtr->msg.set_range(this->dataPtr->range);
351 }
352}

References dataPtr.

◆ InsertBlock()

void smacc2::FlashLightSetting::InsertBlock ( const double  _duration,
const double  _interval,
const ignition::math::Color &  _color,
const int  _index 
)
finalvirtual

Insert a block. Create a block with specified parameters. If the index is out of range, the block will be appended at the end of the list.

Parameters
[in]_durationThe duration for the block.
[in]_intervalThe interval for the block.
[in]_colorThe color for the block.
[in]_indexThe index of the block to be inserted into the list.

Definition at line 533 of file FlashLightPlugin.cc.

536{
537 auto block = std::make_shared<Block>();
538
539 block->duration = _duration;
540 block->interval = _interval;
541 block->color = _color;
542
543 if (_index < 0 || static_cast<int>(this->dataPtr->blocks.size()) <= _index)
544 {
545 this->dataPtr->blocks.push_back(block);
546 }
547 else
548 {
549 this->dataPtr->blocks.insert(this->dataPtr->blocks.begin() + _index, block);
550 }
551}

References dataPtr.

◆ Link()

const gazebo::physics::LinkPtr smacc2::FlashLightSetting::Link ( ) const
finalvirtual

Getter of link.

Returns
A pointer to the link element.

Definition at line 427 of file FlashLightPlugin.cc.

428{
429 return this->dataPtr->link;
430}

References dataPtr.

Referenced by smacc2::LedSetting::Dim(), smacc2::LedSetting::Flash(), smacc2::LedSetting::InitPubVisual(), and smacc2::LedSetting::LedSetting().

Here is the caller graph for this function:

◆ Name()

const std::string smacc2::FlashLightSetting::Name ( ) const
finalvirtual

Getter of name.

Returns
The name of the light element.

Definition at line 421 of file FlashLightPlugin.cc.

422{
423 return this->dataPtr->name;
424}

References dataPtr.

Referenced by smacc2::LedSetting::InitPubVisual().

Here is the caller graph for this function:

◆ RemoveBlock()

bool smacc2::FlashLightSetting::RemoveBlock ( const int  _index)
finalvirtual

Remove a specified block.

Parameters
[in]_indexIndex to the block to remove
Returns
True if the block exists and it was removed.

Definition at line 520 of file FlashLightPlugin.cc.

521{
522 if (_index < 0 || static_cast<int>(this->dataPtr->blocks.size()) <= _index)
523 {
524 return false;
525 }
526
527 this->dataPtr->blocks.erase(this->dataPtr->blocks.begin() + _index);
528
529 return true;
530}

References dataPtr.

◆ SetColor() [1/2]

void smacc2::FlashLightSetting::SetColor ( const ignition::math::Color &  _color)
finalvirtual

Set the color for all the blocks.

Parameters
[in]_colorNew color to set.

Definition at line 505 of file FlashLightPlugin.cc.

506{
507 for (auto block: this->dataPtr->blocks)
508 {
509 block->color = _color;
510 }
511}

References dataPtr.

◆ SetColor() [2/2]

void smacc2::FlashLightSetting::SetColor ( const ignition::math::Color &  _color,
const int  _index 
)
finalvirtual

Set the color for the specified block.

Parameters
[in]_colorNew color to set.
[in]_indexThe index to the block to update.

Definition at line 491 of file FlashLightPlugin.cc.

493{
494 if (0 <= _index && _index < static_cast<int>(this->dataPtr->blocks.size()))
495 {
496 this->dataPtr->blocks[_index]->color = _color;
497 }
498 else
499 {
500 gzerr << "The given index for block is out of range." << std::endl;
501 }
502}

References dataPtr.

◆ SetDuration() [1/2]

void smacc2::FlashLightSetting::SetDuration ( const double  _duration)
finalvirtual

Set the duration time for all the blocks.

Parameters
[in]_durationNew duration time to set.

Definition at line 460 of file FlashLightPlugin.cc.

461{
462 for (auto block: this->dataPtr->blocks)
463 {
464 block->duration = _duration;
465 }
466}

References dataPtr.

◆ SetDuration() [2/2]

void smacc2::FlashLightSetting::SetDuration ( const double  _duration,
const int  _index 
)
finalvirtual

Set the duration time for the specified block.

Parameters
[in]_durationNew duration time to set.
[in]_indexThe index to the block to update.

Definition at line 447 of file FlashLightPlugin.cc.

448{
449 if (0 <= _index && _index < static_cast<int>(this->dataPtr->blocks.size()))
450 {
451 this->dataPtr->blocks[_index]->duration = _duration;
452 }
453 else
454 {
455 gzerr << "The given index for block is out of range." << std::endl;
456 }
457}

References dataPtr.

◆ SetInterval() [1/2]

void smacc2::FlashLightSetting::SetInterval ( const double  _interval)
finalvirtual

Set the interval time for all the blocks.

Parameters
[in]_intervalNew interval time to set.

Definition at line 482 of file FlashLightPlugin.cc.

483{
484 for (auto block: this->dataPtr->blocks)
485 {
486 block->interval = _interval;
487 }
488}

References dataPtr.

◆ SetInterval() [2/2]

void smacc2::FlashLightSetting::SetInterval ( const double  _interval,
const int  _index 
)
finalvirtual

Set the interval time for the specified block.

Parameters
[in]_intervalNew interval time to set.
[in]_indexThe index to the block to update.

Definition at line 469 of file FlashLightPlugin.cc.

470{
471 if (0 <= _index && _index < static_cast<int>(this->dataPtr->blocks.size()))
472 {
473 this->dataPtr->blocks[_index]->interval = _interval;
474 }
475 else
476 {
477 gzerr << "The given index for block is out of range." << std::endl;
478 }
479}

References dataPtr.

◆ SwitchOff()

void smacc2::FlashLightSetting::SwitchOff ( )
finalvirtual

Switch off (disable the flashlight).

Definition at line 439 of file FlashLightPlugin.cc.

440{
441 gzmsg << "switch off" << std::endl;
442
443 this->dataPtr->switchOn = false;
444}

References dataPtr.

◆ SwitchOn()

void smacc2::FlashLightSetting::SwitchOn ( )
finalvirtual

Switch on (enable the flashlight).

Definition at line 433 of file FlashLightPlugin.cc.

434{
435 this->dataPtr->switchOn = true;
436}

References dataPtr.

◆ UpdateLightInEnv()

void smacc2::FlashLightSetting::UpdateLightInEnv ( const gazebo::common::Time &  _currentTime)
finalvirtual

Update the light based on the given time.

Parameters
[in]_currentTimeThe current point of time to update the lights.

Definition at line 355 of file FlashLightPlugin.cc.

356{
357 if (this->dataPtr->switchOn)
358 {
359 this->Flash();
360
361 }
362 else
363 {
364 this->Dim();
365 }
366
367 // int index = this->dataPtr->currentBlockIndex;
368
369 // // Reset the start time so the current time is within the current phase.
370 // if (_currentTime < this->dataPtr->startTime ||
371 // this->dataPtr->startTime
372 // + this->dataPtr->blocks[index]->duration
373 // + this->dataPtr->blocks[index]->interval <= _currentTime)
374 // {
375 // // initialize the start time.
376 // this->dataPtr->startTime = _currentTime;
377 // // proceed to the next block.
378 // index++;
379 // if (index >= static_cast<int>(this->dataPtr->blocks.size()))
380 // {
381 // index = 0;
382 // }
383 // this->dataPtr->currentBlockIndex = index;
384 // }
385
386 // if (this->dataPtr->switchOn)
387 // {
388 // // time to dim
389 // if (_currentTime - this->dataPtr->startTime
390 // > this->dataPtr->blocks[index]->duration)
391 // {
392 // if (this->dataPtr->flashing)
393 // {
394 // this->Dim();
395 // }
396 // }
397 // // time to flash
398 // else
399 // {
400 // // if there is more than one block, it calls Flash() at the beginning of
401 // // every block.
402 // if (this->dataPtr->blocks.size() > 1
403 // && this->dataPtr->startTime == _currentTime)
404 // {
405 // this->Flash();
406 // }
407 // // otherwise, it calls the function only if the light is not lit yet.
408 // else if (!this->dataPtr->flashing)
409 // {
410 // this->Flash();
411 // }
412 // }
413 // }
414 // else if (this->dataPtr->flashing)
415 // {
416 // this->Dim();
417 // }
418}
virtual void Dim()
Dim the light This function is internally used to update the light in the environment.
virtual void Flash()
Flash the light This function is internally used to update the light in the environment.

References dataPtr, Dim(), and Flash().

Here is the call graph for this function:

Member Data Documentation

◆ dataPtr

std::unique_ptr<FlashLightSettingPrivate> smacc2::FlashLightSetting::dataPtr
private

The documentation for this class was generated from the following files: