gazebo_ros_color.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include <string>
19 
21 
23 void GazeboRosColor::Load(gazebo::rendering::VisualPtr _parent,
24  sdf::ElementPtr _sdf)
25 {
26  GZ_ASSERT(_parent != nullptr, "Received NULL model pointer");
27 
28  // Quit if ROS plugin was not loaded
29  if (!ros::isInitialized())
30  {
31  ROS_FATAL_STREAM_NAMED("gazebo_ros_color_plugin", "A ROS node for Gazebo "
32  "has not been initialized, unable to load plugin. " << "Load the Gazebo "
33  "system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
34  return;
35  }
36 
37  // Load namespace from SDF if available. Otherwise, use the model name.
38  std::string modelName = _parent->GetName();
39  auto delim = modelName.find(":");
40  if (delim != std::string::npos)
41  modelName = modelName.substr(0, delim);
42 
43  std::string ns = modelName;
44  if (_sdf->HasElement("robotNamespace"))
45  ns = _sdf->GetElement("robotNamespace")->Get<std::string>();
46  else
47  {
48  ROS_DEBUG_NAMED("gazebo_ros_color_plugin",
49  "missing <robotNamespace>, defaulting to %s", ns.c_str());
50  }
51 
52  // Load topic from sdf if available
53  std::string topicName = "color";
54  if (_sdf->HasElement("topicName"))
55  topicName = _sdf->GetElement("topicName")->Get<std::string>();
56  else
57  {
58  ROS_INFO_NAMED("gazebo_ros_color_plugin",
59  "missing <topicName>, defaulting to %s", topicName.c_str());
60  }
61 
62  // Copy parent into class for use in callback
63  this->visual = _parent;
64 
65  // Setup node handle and subscriber
66  this->nh = ros::NodeHandle(ns);
67  this->colorSub = this->nh.subscribe(topicName, 1,
69 }
70 
72 void GazeboRosColor::ColorCallback(const std_msgs::ColorRGBAConstPtr &_msg)
73 {
74  // Convert ROS color to gazebo color
75  gazebo::common::Color gazebo_color(_msg->r, _msg->g, _msg->b, _msg->a);
76 
77  // Set parent's color to message color
78  this->visual->SetAmbient(gazebo_color);
79  this->visual->SetDiffuse(gazebo_color);
80 }
81 
82 // Register plugin with gazebo
#define ROS_INFO_NAMED(name,...)
ros::Subscriber colorSub
Subscriber to accept color change requests.
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL bool isInitialized()
void ColorCallback(const std_msgs::ColorRGBAConstPtr &_msg)
gazebo::rendering::VisualPtr visual
Pointer to the visual element to modify.
#define ROS_DEBUG_NAMED(name,...)
Visual plugin for changing the color of some visual elements using ROS messages. This plugin accepts ...
#define ROS_FATAL_STREAM_NAMED(name, args)
void Load(gazebo::rendering::VisualPtr _parent, sdf::ElementPtr _sdf)
GZ_REGISTER_VISUAL_PLUGIN(GazeboRosVideo)
ros::NodeHandle nh
ROS Node handle.


vmrc_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Sun Feb 17 2019 03:14:16