19 #include <gazebo/rendering/Scene.hh> 31 const double _g,
const double _b,
const double _a)
33 static std_msgs::ColorRGBA color;
47 if (_color == color.second)
53 return std::numeric_limits<uint8_t>::max();
59 for (uint8_t i = 0u; i < this->
kColors.size() - 1; ++i)
61 for (uint8_t j = 0u; j < this->
kColors.size() - 1; ++j)
66 for (uint8_t k = 0u; k < this->
kColors.size() - 1; ++k)
96 std::lock_guard<std::mutex> lock(this->
mutex);
105 sdf::ElementPtr _sdf)
107 GZ_ASSERT(_parent !=
nullptr,
"Received NULL model pointer");
109 this->
scene = _parent->GetScene();
111 GZ_ASSERT(this->
scene !=
nullptr,
"NULL scene");
147 for (
auto colorIndex : {
"color_1",
"color_2",
"color_3"})
149 if (!_sdf->HasElement(colorIndex))
155 auto color = _sdf->GetElement(colorIndex)->Get<std::string>();
156 std::transform(color.begin(), color.end(), color.begin(), ::tolower);
159 if (color !=
"red" && color !=
"green" &&
160 color !=
"blue" && color !=
"yellow" && color !=
"off")
162 ROS_ERROR(
"Invalid color [%s]", color.c_str());
174 if (!_sdf->HasElement(
"visuals"))
180 auto visualsElem = _sdf->GetElement(
"visuals");
181 if (!visualsElem->HasElement(
"visual"))
187 auto visualElem = visualsElem->GetElement(
"visual");
190 std::string visualName = visualElem->Get<std::string>();
192 visualElem = visualElem->GetNextElement();
196 if (_sdf->HasElement(
"shuffle"))
201 if (!_sdf->HasElement(
"ros_shuffle_topic"))
203 ROS_ERROR(
"<ros_shuffle_topic> missing");
206 (
"ros_shuffle_topic")->Get<std::string>();
210 if (!_sdf->HasElement(
"gz_colors_topic"))
217 (
"gz_colors_topic")->Get<std::string>();
220 if (_sdf->HasElement(
"robot_namespace"))
221 this->
ns = _sdf->GetElement(
"robot_namespace")->Get<std::string>();
234 auto visualPtr = this->
scene->GetVisual(visualName);
236 this->
visuals.push_back(visualPtr);
238 ROS_ERROR(
"Unable to find [%s] visual", visualName.c_str());
242 std::lock_guard<std::mutex> lock(this->
mutex);
254 #if GAZEBO_MAJOR_VERSION >= 8 255 ignition::math::Color gazeboColor(color.r, color.g, color.b, color.a);
257 gazebo::common::Color gazeboColor(color.r, color.g, color.b, color.a);
260 for (
auto visual : this->
visuals)
262 visual->SetAmbient(gazeboColor);
263 visual->SetDiffuse(gazeboColor);
277 std::string colorSeq =
"";
278 for (
size_t i = 0; i < 3; ++i)
281 ROS_INFO_NAMED(
"LightBuoyPlugin",
"Pattern is %s", colorSeq.c_str());
static std_msgs::ColorRGBA CreateColor(const double _r, const double _g, const double _b, const double _a)
Creates a std_msgs::ColorRGBA message from 4 doubles.
std::vector< gazebo::rendering::VisualPtr > visuals
Pointer to the visual elements to modify.
void ChangePatternTo(gazebo::ConstLightBuoyColorsPtr &_msg)
Gazebo callback for changing light to a specific color pattern.
#define ROS_INFO_NAMED(name,...)
gazebo::transport::SubscriberPtr colorSub
void Load(gazebo::rendering::VisualPtr _parent, sdf::ElementPtr _sdf)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool shuffleEnabled
Whether shuffle is enabled via a ROS topic or not.
Visual plugin for changing the color of some visual elements using ROS messages. This plugin accepts ...
ROSCPP_DECL bool isInitialized()
std::string rosShuffleTopic
ROS topic.
std::vector< std::string > visualNames
Collection of visual names.
bool ParseSDF(sdf::ElementPtr _sdf)
Parse all SDF parameters.
void InitializeAllPatterns()
Initialize all color sequences.
ros::NodeHandle nh
ROS Node handle.
static uint8_t IndexFromColor(const std::string &_color)
Return the index of the color from its string.
std::string ns
ROS namespace.
std::pair< std_msgs::ColorRGBA, std::string > Colors_t
void Update()
Display the next color in the sequence, or start over if at the end.
static const std::array< Colors_t, 5 > kColors
List of the color options (red, green, blue, yellow and no color) with their string name for logging...
Pattern_t pattern
The color pattern.
gazebo::transport::NodePtr gzNode
std::string gzColorsTopic
gazebo topic.
gazebo::rendering::ScenePtr scene
Pointer to the scene node.
size_t allPatternsIdx
The index pointing to one of the potential color sequences.
std::mutex mutex
Locks state and pattern member variables.
GZ_REGISTER_VISUAL_PLUGIN(GazeboRosVideo)
uint8_t state
Track current index in pattern.
gazebo::event::ConnectionPtr updateConnection
Connects to rendering update event.
std::vector< Pattern_t > allPatterns
All color sequences.
ros::Subscriber changePatternSub
Subscriber to generate and display a new color sequence.
gazebo::common::Time nextUpdateTime
Next time where the plugin should be updated.
void ChangePattern(const std_msgs::Empty::ConstPtr &_msg)
ROS callback for generating a new color pattern.