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

#include <FlashLightPlugin.hh>

Inheritance diagram for smacc2::FlashLightPlugin:
Inheritance graph
Collaboration diagram for smacc2::FlashLightPlugin:
Collaboration graph

Public Member Functions

 FlashLightPlugin ()
 Constructor. More...
 
virtual ~FlashLightPlugin ()
 Destructor. More...
 
void Load (gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf) override
 

Protected Member Functions

virtual void OnUpdate ()
 Called by the world update start event. More...
 
virtual bool TurnOn (const std::string &_lightName) final
 Turn on a flash light specified by the light name If more than one link have lights with the identical name, the first appearing light in the list will be updated. More...
 
virtual bool TurnOn (const std::string &_lightName, const std::string &_linkName) final
 Turn on a flash light specified by the name and its link. More...
 
virtual bool TurnOnAll () final
 Turn on all flash lights. More...
 
virtual bool TurnOff (const std::string &_lightName) final
 Turn off a flash light specified by the name If more than one link have lights with the identical name, the first appearing light in the list will be updated. More...
 
virtual bool TurnOff (const std::string &_lightName, const std::string &_linkName) final
 Turn off a flash light specified by the name. More...
 
virtual bool TurnOffAll () final
 Turn off all flash lights. More...
 
virtual bool ChangeDuration (const std::string &_lightName, const std::string &_linkName, const double _duration, const int _index) final
 Change the duration of a specific block of the flashlight. If the index is a negative number, it updates all the blocks. More...
 
virtual bool ChangeDuration (const std::string &_lightName, const std::string &_linkName, const double _duration) final
 Change the duration of all the blocks of the flashlight. More...
 
virtual bool ChangeInterval (const std::string &_lightName, const std::string &_linkName, const double _interval, const int _index) final
 Change the interval of a specific block of the flashlight. If the index is a negative number, it updates all the blocks. More...
 
virtual bool ChangeInterval (const std::string &_lightName, const std::string &_linkName, const double _interval) final
 Change the interval of all the blocks of the flashlight. More...
 
virtual bool ChangeColor (const std::string &_lightName, const std::string &_linkName, const ignition::math::Color &_color, const int _index) final
 Change the color of a specific block of the flashlight. If the index is a negative number, it updates all the blocks. More...
 
virtual bool ChangeColor (const std::string &_lightName, const std::string &_linkName, const ignition::math::Color &_color) final
 Change the color of all the blocks of the flashlight. More...
 
virtual std::shared_ptr< FlashLightSettingCreateSetting (const sdf::ElementPtr &_sdf, const gazebo::physics::ModelPtr &_model, const gazebo::common::Time &_currentTime, gazebo_ros::Node::SharedPtr node)
 Create an object of setting. More...
 
virtual void InitSettingBySpecificData (std::shared_ptr< FlashLightSetting > &_setting)
 Initialize the additional part of an object of setting. More...
 

Private Attributes

std::unique_ptr< FlashLightPluginPrivatedataPtr
 Pointer to private data. More...
 
rclcpp::Subscription< std_msgs::msg::Int8 >::SharedPtr cmdledsubscription
 

Detailed Description

Definition at line 164 of file FlashLightPlugin.hh.

Constructor & Destructor Documentation

◆ FlashLightPlugin()

smacc2::FlashLightPlugin::FlashLightPlugin ( )

Constructor.

Definition at line 597 of file FlashLightPlugin.cc.

597 : gazebo::ModelPlugin(),
598 dataPtr(new FlashLightPluginPrivate)
599{
600 // Create a node
601 this->dataPtr->node = gazebo::transport::NodePtr(new gazebo::transport::Node());
602 this->dataPtr->node->Init();
603
604 // advertise the topic to update lights
605 // this->dataPtr->pubLight
606 // = this->dataPtr->node->Advertise<gazebo::msgs::Light>("~/light/modify");
607
608 // this->dataPtr->pubLight->WaitForConnection();
609}
std::unique_ptr< FlashLightPluginPrivate > dataPtr
Pointer to private data.

References dataPtr.

◆ ~FlashLightPlugin()

smacc2::FlashLightPlugin::~FlashLightPlugin ( )
virtual

Destructor.

Definition at line 612 of file FlashLightPlugin.cc.

613{
614}

Member Function Documentation

◆ ChangeColor() [1/2]

bool smacc2::FlashLightPlugin::ChangeColor ( const std::string &  _lightName,
const std::string &  _linkName,
const ignition::math::Color &  _color 
)
finalprotectedvirtual

Change the color of all the blocks of the flashlight.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_colorThe new color to set
Returns
True if the specified light is found.

Definition at line 918 of file FlashLightPlugin.cc.

922{
923 return this->ChangeColor(_lightName, _linkName, _color, -1);
924}
virtual bool ChangeColor(const std::string &_lightName, const std::string &_linkName, const ignition::math::Color &_color, const int _index) final
Change the color of a specific block of the flashlight. If the index is a negative number,...

References ChangeColor().

Here is the call graph for this function:

◆ ChangeColor() [2/2]

bool smacc2::FlashLightPlugin::ChangeColor ( const std::string &  _lightName,
const std::string &  _linkName,
const ignition::math::Color &  _color,
const int  _index 
)
finalprotectedvirtual

Change the color of a specific block of the flashlight. If the index is a negative number, it updates all the blocks.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_colorThe new color to set
[in]_indexThe index to the block to update
Returns
True if the specified light is found.

Definition at line 892 of file FlashLightPlugin.cc.

896{
897 std::shared_ptr<FlashLightSetting> setting
898 = this->dataPtr->SettingByLightNameAndLinkName(_lightName, _linkName);
899
900 if (setting)
901 {
902 if (_index >= 0)
903 {
904 setting->SetColor(_color, _index);
905 }
906 else
907 {
908 setting->SetColor(_color);
909 }
910 return true;
911 }
912
913 gzerr << "light <" + _lightName + "> does not exist." << std::endl;
914 return false;
915}

References dataPtr.

Referenced by ChangeColor().

Here is the caller graph for this function:

◆ ChangeDuration() [1/2]

bool smacc2::FlashLightPlugin::ChangeDuration ( const std::string &  _lightName,
const std::string &  _linkName,
const double  _duration 
)
finalprotectedvirtual

Change the duration of all the blocks of the flashlight.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_durationThe new duration time to set
Returns
True if the specified light is found.

Definition at line 848 of file FlashLightPlugin.cc.

852{
853 return this->ChangeDuration(_lightName, _linkName, _duration, -1);
854}
virtual bool ChangeDuration(const std::string &_lightName, const std::string &_linkName, const double _duration, const int _index) final
Change the duration of a specific block of the flashlight. If the index is a negative number,...

References ChangeDuration().

Here is the call graph for this function:

◆ ChangeDuration() [2/2]

bool smacc2::FlashLightPlugin::ChangeDuration ( const std::string &  _lightName,
const std::string &  _linkName,
const double  _duration,
const int  _index 
)
finalprotectedvirtual

Change the duration of a specific block of the flashlight. If the index is a negative number, it updates all the blocks.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_durationThe new duration time to set
[in]_indexThe index to the block to update
Returns
True if the specified light is found.

Definition at line 822 of file FlashLightPlugin.cc.

826{
827 std::shared_ptr<FlashLightSetting> setting
828 = this->dataPtr->SettingByLightNameAndLinkName(_lightName, _linkName);
829
830 if (setting)
831 {
832 if (_index >= 0)
833 {
834 setting->SetDuration(_duration, _index);
835 }
836 else
837 {
838 setting->SetDuration(_duration);
839 }
840 return true;
841 }
842
843 gzerr << "light <" + _lightName + "> does not exist." << std::endl;
844 return false;
845}

References dataPtr.

Referenced by ChangeDuration().

Here is the caller graph for this function:

◆ ChangeInterval() [1/2]

bool smacc2::FlashLightPlugin::ChangeInterval ( const std::string &  _lightName,
const std::string &  _linkName,
const double  _interval 
)
finalprotectedvirtual

Change the interval of all the blocks of the flashlight.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_intervalThe new interval time to set
Returns
True if the specified light is found.

Definition at line 883 of file FlashLightPlugin.cc.

887{
888 return this->ChangeInterval(_lightName, _linkName, _interval, -1);
889}
virtual bool ChangeInterval(const std::string &_lightName, const std::string &_linkName, const double _interval, const int _index) final
Change the interval of a specific block of the flashlight. If the index is a negative number,...

References ChangeInterval().

Here is the call graph for this function:

◆ ChangeInterval() [2/2]

bool smacc2::FlashLightPlugin::ChangeInterval ( const std::string &  _lightName,
const std::string &  _linkName,
const double  _interval,
const int  _index 
)
finalprotectedvirtual

Change the interval of a specific block of the flashlight. If the index is a negative number, it updates all the blocks.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
[in]_intervalThe new interval time to set
[in]_indexThe index to the block to update
Returns
True if the specified light is found.

Definition at line 857 of file FlashLightPlugin.cc.

861{
862 std::shared_ptr<FlashLightSetting> setting
863 = this->dataPtr->SettingByLightNameAndLinkName(_lightName, _linkName);
864
865 if (setting)
866 {
867 if (_index >= 0)
868 {
869 setting->SetInterval(_interval, _index);
870 }
871 else
872 {
873 setting->SetInterval(_interval);
874 }
875 return true;
876 }
877
878 gzerr << "light <" + _lightName + "> does not exist." << std::endl;
879 return false;
880}

References dataPtr.

Referenced by ChangeInterval().

Here is the caller graph for this function:

◆ CreateSetting()

std::shared_ptr< FlashLightSetting > smacc2::FlashLightPlugin::CreateSetting ( const sdf::ElementPtr &  _sdf,
const gazebo::physics::ModelPtr &  _model,
const gazebo::common::Time &  _currentTime,
gazebo_ros::Node::SharedPtr  node 
)
protectedvirtual

Create an object of setting.

NOTE: This function is internally called in Load() of the base class. If a child class of FlashLightPlugin has also an inherited class of FlashLightSetting, this function must be overridden so that dataPtr deals with objects of the appropriate setting class.

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

Reimplemented in smacc2::LedPlugin.

Definition at line 928 of file FlashLightPlugin.cc.

933{
934 return std::make_shared<FlashLightSetting>(_sdf, _model, _currentTime, node);
935}

Referenced by Load().

Here is the caller graph for this function:

◆ InitSettingBySpecificData()

void smacc2::FlashLightPlugin::InitSettingBySpecificData ( std::shared_ptr< FlashLightSetting > &  _setting)
protectedvirtual

Initialize the additional part of an object of setting.

NOTE: This function is internally called in Load() of the base class. If a child class of FlashLightPlugin has also an inherited class of FlashLightSetting, this function must be overridden so that the object can be initialized with necessary data. Also, the overridden function must call the original function in it.

Parameters
[in]_settingA pointer to the setting object.

Reimplemented in smacc2::LedPlugin.

Definition at line 938 of file FlashLightPlugin.cc.

940{
941 _setting->InitPubLight(this->dataPtr->pubLight);
942}

References dataPtr.

Referenced by smacc2::LedPlugin::InitSettingBySpecificData(), and Load().

Here is the caller graph for this function:

◆ Load()

void smacc2::FlashLightPlugin::Load ( gazebo::physics::ModelPtr  _parent,
sdf::ElementPtr  _sdf 
)
override

Definition at line 617 of file FlashLightPlugin.cc.

618{
619 // Store the pointers to the model and world
620 this->dataPtr->model = _parent;
621 this->dataPtr->world = _parent->GetWorld();
622
623 auto node = gazebo_ros::Node::Get(_sdf);
624 // Get the current time
625 gazebo::common::Time currentTime = this->dataPtr->world->SimTime();
626
627 // Get the parameters from sdf
628 if (_sdf->HasElement("light"))
629 {
630 sdf::ElementPtr sdfFlashLight = _sdf->GetElement("light");
631 while (sdfFlashLight)
632 {
633 // id required
634 if (sdfFlashLight->HasElement("id"))
635 {
636 // Create an object of setting.
637 std::shared_ptr<FlashLightSetting> setting
638 = this->CreateSetting(
639 sdfFlashLight, this->dataPtr->model, currentTime, node);
640
641 // Initialize the object with the data specific to descendan classes.
642 this->InitSettingBySpecificData(setting);
643
644 // Store the setting to the list
645 this->dataPtr->listFlashLight.push_back(setting);
646 }
647 else
648 {
649 // display an error message
650 gzerr << "id does not exist in <light>" << std::endl;
651 }
652
653 sdfFlashLight = sdfFlashLight->GetNextElement("light");
654 }
655
656 // Turn on/off all the lights if <enable> element is given
657 if (_sdf->HasElement("enable"))
658 {
659 if (_sdf->Get<bool>("enable"))
660 {
661 this->TurnOnAll();
662 }
663 else
664 {
665 this->TurnOffAll();
666 }
667 }
668 // Turn on/off a specific light if <enable> is specifically given.
669 if (_sdf->HasElement("light"))
670 {
671 sdf::ElementPtr sdfFlashLight = _sdf->GetElement("light");
672 while (sdfFlashLight)
673 {
674 if (sdfFlashLight->HasElement("enable"))
675 {
676 std::string lightId = sdfFlashLight->Get<std::string>("id");
677 int posDelim = lightId.rfind("/");
678 std::string lightName = lightId.substr(posDelim+1, lightId.length());
679 std::string linkName = lightId.substr(0, posDelim);
680 if (sdfFlashLight->Get<bool>("enable"))
681 {
682 this->TurnOn(lightName, linkName);
683 }
684 else
685 {
686 this->TurnOff(lightName, linkName);
687 }
688 }
689
690 sdfFlashLight = sdfFlashLight->GetNextElement("light");
691 }
692 }
693
694 // listen to the update event by the World
695 if (!this->dataPtr->listFlashLight.empty())
696 {
697 this->dataPtr->updateConnection = event::Events::ConnectWorldUpdateBegin(
698 std::bind(&FlashLightPlugin::OnUpdate, this));
699 }
700
701 rclcpp::SensorDataQoS qos;
702
703 auto topicname = "/"+ this->dataPtr->model->GetName()+ "/cmdled";
704
705 boost::replace_all(topicname, "::","/");
706
707 gzmsg << "topicname: " << topicname <<"\n";
708
710 node->create_subscription<std_msgs::msg::Int8>(topicname, qos,
711 [this, node]
712 (const std_msgs::msg::Int8::SharedPtr msg)
713 {
714 RCLCPP_INFO(node->get_logger(), "msg received");
715 if(msg->data == 0)
716 this->TurnOffAll();
717 else
718 this->TurnOnAll();
719
720 });
721 }
722}
virtual bool TurnOffAll() final
Turn off all flash lights.
rclcpp::Subscription< std_msgs::msg::Int8 >::SharedPtr cmdledsubscription
virtual bool TurnOnAll() final
Turn on all flash lights.
virtual void OnUpdate()
Called by the world update start event.
virtual bool TurnOn(const std::string &_lightName) final
Turn on a flash light specified by the light name If more than one link have lights with the identica...
virtual bool TurnOff(const std::string &_lightName) final
Turn off a flash light specified by the name If more than one link have lights with the identical nam...
virtual void InitSettingBySpecificData(std::shared_ptr< FlashLightSetting > &_setting)
Initialize the additional part of an object of setting.
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.

References cmdledsubscription, CreateSetting(), dataPtr, InitSettingBySpecificData(), OnUpdate(), TurnOff(), TurnOffAll(), TurnOn(), and TurnOnAll().

Referenced by smacc2::ControllableLed::Load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnUpdate()

void smacc2::FlashLightPlugin::OnUpdate ( )
protectedvirtual

Called by the world update start event.

update the light

Definition at line 725 of file FlashLightPlugin.cc.

726{
727 gazebo::common::Time currentTime = this->dataPtr->world->SimTime();
728
729 for (auto &setting: this->dataPtr->listFlashLight)
730 {
732 setting->UpdateLightInEnv(currentTime);
733 }
734}

References dataPtr.

Referenced by Load().

Here is the caller graph for this function:

◆ TurnOff() [1/2]

bool smacc2::FlashLightPlugin::TurnOff ( const std::string &  _lightName)
finalprotectedvirtual

Turn off a flash light specified by the name If more than one link have lights with the identical name, the first appearing light in the list will be updated.

Parameters
[in]_lightNameThe name of flash light
Returns
True if the specified light is found.

Definition at line 778 of file FlashLightPlugin.cc.

779{
780 return this->TurnOff(_lightName, "");
781}

References TurnOff().

Referenced by Load(), and TurnOff().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TurnOff() [2/2]

bool smacc2::FlashLightPlugin::TurnOff ( const std::string &  _lightName,
const std::string &  _linkName 
)
finalprotectedvirtual

Turn off a flash light specified by the name.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
Returns
True if the specified light is found.

Definition at line 784 of file FlashLightPlugin.cc.

786{
787 std::shared_ptr<FlashLightSetting> setting
788 = this->dataPtr->SettingByLightNameAndLinkName(_lightName, _linkName);
789
790 if (setting)
791 {
792 setting->SwitchOff();
793 return true;
794 }
795
796 gzerr << "light: [" + _linkName + "/" + _lightName + "] does not exist."
797 << std::endl;
798 return false;
799}

References dataPtr.

◆ TurnOffAll()

bool smacc2::FlashLightPlugin::TurnOffAll ( )
finalprotectedvirtual

Turn off all flash lights.

Returns
True if there is one or more lights to turn off.

Definition at line 802 of file FlashLightPlugin.cc.

803{
804 gzmsg << "msg TurnOffAll" << std::endl;
805 if (this->dataPtr->listFlashLight.empty())
806 {
807 gzerr << "no flash lights exist to turn off." << std::endl;
808 return false;
809 }
810
811 gzmsg << "switch off" << std::endl;
812
813 for (auto &setting: this->dataPtr->listFlashLight)
814 {
815 setting->SwitchOff();
816 }
817
818 return true;
819}

References dataPtr.

Referenced by smacc2::ControllableLed::LightCmd(), and Load().

Here is the caller graph for this function:

◆ TurnOn() [1/2]

bool smacc2::FlashLightPlugin::TurnOn ( const std::string &  _lightName)
finalprotectedvirtual

Turn on a flash light specified by the light name If more than one link have lights with the identical name, the first appearing light in the list will be updated.

Parameters
[in]_lightNameThe name of flash light
Returns
True if the specified light is found.

Definition at line 737 of file FlashLightPlugin.cc.

738{
739 return this->TurnOn(_lightName, "");
740}

References TurnOn().

Referenced by Load(), and TurnOn().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TurnOn() [2/2]

bool smacc2::FlashLightPlugin::TurnOn ( const std::string &  _lightName,
const std::string &  _linkName 
)
finalprotectedvirtual

Turn on a flash light specified by the name and its link.

Parameters
[in]_lightNameThe name of flash light
[in]_linkNameThe name of the link holding the light
Returns
True if the specified light is found.

Definition at line 743 of file FlashLightPlugin.cc.

745{
746 std::shared_ptr<FlashLightSetting> setting
747 = this->dataPtr->SettingByLightNameAndLinkName(_lightName, _linkName);
748
749 if (setting)
750 {
751 setting->SwitchOn();
752 return true;
753 }
754
755 gzerr << "light: [" + _linkName + "/" + _lightName + "] does not exist."
756 << std::endl;
757 return false;
758}

References dataPtr.

◆ TurnOnAll()

bool smacc2::FlashLightPlugin::TurnOnAll ( )
finalprotectedvirtual

Turn on all flash lights.

Returns
True if there is one or more lights to turn on.

Definition at line 761 of file FlashLightPlugin.cc.

762{
763 if (this->dataPtr->listFlashLight.empty())
764 {
765 gzerr << "no flash lights exist to turn on." << std::endl;
766 return false;
767 }
768
769 for (auto &setting: this->dataPtr->listFlashLight)
770 {
771 setting->SwitchOn();
772 }
773
774 return true;
775}

References dataPtr.

Referenced by smacc2::ControllableLed::LightCmd(), and Load().

Here is the caller graph for this function:

Member Data Documentation

◆ cmdledsubscription

rclcpp::Subscription<std_msgs::msg::Int8>::SharedPtr smacc2::FlashLightPlugin::cmdledsubscription
private

Definition at line 307 of file FlashLightPlugin.hh.

Referenced by Load().

◆ dataPtr

std::unique_ptr<FlashLightPluginPrivate> smacc2::FlashLightPlugin::dataPtr
private

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