hri_skeletons.cpp
Go to the documentation of this file.
1 // Copyright 2021 PAL Robotics S.L.
2 // Copyright 2008, Willow Garage, Inc.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the Willow Garage, Inc., PAL Robotics S.L. nor the
15 // names of its contributors may be used to endorse or promote products
16 // derived from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "hri_skeletons.hpp"
31 
32 #include <OGRE/OgreSceneManager.h>
33 #include <OGRE/OgreSceneNode.h>
34 #include <rviz/display_context.h>
38 #include <rviz/robot/robot.h>
39 #include <rviz/robot/robot_link.h>
41 #include <urdf/model.h>
42 
43 #include <QTimer>
44 
45 namespace rviz {
47  const std::string& link_name,
48  const std::string& text,
49  HumansModelDisplay* display) {
50  display->setStatus(level, QString::fromStdString(link_name),
51  QString::fromStdString(text));
52 }
53 
55  : Display(), has_new_transforms_(false), time_since_last_transform_(0.0f) {
57  new Property("Visual Enabled", true,
58  "Whether to display the visual representation of the robot.",
59  this, SLOT(updateVisualVisible()));
60 
62  "Collision Enabled", false,
63  "Whether to display the collision representation of the robot.", this,
64  SLOT(updateCollisionVisible()));
65 
67  new FloatProperty("Update Interval", 0,
68  "Interval at which to update the links, in seconds. "
69  "0 means to update every update cycle.",
70  this);
71 
73 
75  "Alpha", 1, "Amount of transparency to apply to the links.", this,
76  SLOT(updateAlpha()));
77  alpha_property_->setMin(0.0);
78  alpha_property_->setMax(1.0);
79 
81  new StringProperty("TF Prefix", "",
82  "Robot Model normally assumes the link name is the "
83  "same as the tf frame name. "
84  " This option allows you to set a prefix. Mainly "
85  "useful for multi-robot situations.",
86  this, SLOT(updateTfPrefix()));
87 
88  idsSub_ = update_nh_.subscribe("/humans/bodies/tracked", 1,
90 
91  pluginEnabled_ = true;
92 }
93 
95  if (initialized()) {
96  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
97  it != humans_.end(); it++)
98  it->second = nullptr;
99  }
100 }
101 
105  updateAlpha();
106 }
107 
109  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
110  it != humans_.end(); it++)
111  if(it->second != nullptr)
112  it->second->setAlpha(alpha_property_->getFloat());
114 }
115 
117  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
118  it != humans_.end(); it++)
119  if(it->second != nullptr)
120  it->second->setVisualVisible(visual_enabled_property_->getValue().toBool());
122 }
123 
125  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
126  it != humans_.end(); it++)
127  if(it->second != nullptr)
128  it->second->setVisualVisible(
131 }
132 
134  clearStatuses();
136 }
137 
139  std::map<std::string, rviz::RobotPtr>::iterator it) {
141 
142  std::string description = "human_description_" + (it->first);
143  std::string content;
144 
145  try {
147  content)) // In content we get the string
148  // representing the urdf model
149  {
150  std::string loc;
152  update_nh_.getParam(loc, content);
153  }
154  else {
155  clear();
157  QString("Parameter [%1] does not exist, and was not found by "
158  "searchParam()")
159  .arg(QString::fromStdString(description)));
160  return;
161  }
162  }
163  } catch (const ros::InvalidNameException& e) {
164  clear();
166  QString("Invalid parameter name: %1.\n%2")
167  .arg(QString::fromStdString(description), e.what()));
168  return;
169  }
170 
171  if (content.empty()) {
172  clear();
173  setStatus(StatusProperty::Warn, "URDF", "URDF is empty");
174  return;
175  }
176 
177  std::string robot_description = content;
178 
179  urdf::Model descr;
180  if (!descr.initString(robot_description)) {
181  clear();
182  setStatus(StatusProperty::Warn, "URDF", "Failed to parse URDF model");
183  return;
184  }
185 
186  it->second = RobotPtr((new Robot(scene_node_, context_, "Human: " + it->first, this)));
187 
188  setStatus(StatusProperty::Ok, "URDF", "URDFs parsed OK");
189  it->second->load(descr);
190 
191  it->second->update(
193  boost::bind(linkUpdaterStatusFunction, _1, _2, _3, this),
195 }
196 
198  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
199  it != humans_.end(); it++)
200  if(it->second != nullptr)
201  it->second->setVisible(true);
202  pluginEnabled_ = true;
203 }
204 
206  // robot_->setVisible(false);
207  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
208  it != humans_.end(); it++)
209  if(it->second != nullptr)
210  it->second->setVisible(false);
211  pluginEnabled_ = false;
212  //clear();
213 }
214 
215 void HumansModelDisplay::update(float wall_dt, float /*ros_dt*/) {
216  time_since_last_transform_ += wall_dt;
217  float rate = update_rate_property_->getFloat();
218  bool update = rate < 0.0001f || time_since_last_transform_ >= rate;
219 
220  if (has_new_transforms_ || update) {
221  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin(); it != humans_.end(); it++){
222  if(it->second != nullptr){
223  it->second->update(TFLinkUpdater(
225  boost::bind(linkUpdaterStatusFunction, _1, _2, _3, this),
227  }
228  }
230 
231  has_new_transforms_ = false;
233  }
234 }
235 
237 
239  // robot_->clear();
240  for (std::map<std::string, rviz::RobotPtr>::iterator it = humans_.begin();
241  it != humans_.end(); it++)
242  if(it->second != nullptr)
243  it->second->clear();
244  clearStatuses();
245  robot_description_.clear();
246 }
247 
249  Display::reset();
250  has_new_transforms_ = true;
251 }
252 
253 void HumansModelDisplay::idsCallback(const hri_msgs::IdsListConstPtr& msg) {
254  if (ros::ok() && pluginEnabled_) {
255  ids_ = msg->ids;
256 
257  // Check for bodies that are no more in the list
258  // Remove them from the map0
259 
260  std::map<std::string, rviz::RobotPtr>::iterator itH;
261  for (itH = humans_.begin(); itH != humans_.end();) {
262  if (std::find(ids_.begin(), ids_.end(), itH->first) == ids_.end()) {
263  if(itH->second)
264  itH->second = nullptr;
265  humans_.erase((itH++)->first);
266  } else
267  ++itH;
268  }
269 
270  // Check for new faces
271  // Create a bounding box message and insert it in the map
272 
273 
274  for (const auto& id : ids_) {
275  auto human = humans_.find(id);
276  if (human == humans_.end()) {
277  std::string human_description = "human_description_"+id;
278  if(update_nh_.hasParam(human_description)){
279  auto ins = humans_.insert(std::pair<std::string, rviz::RobotPtr>(id, nullptr));
280  ROS_WARN("Initializing robot");
281  if (ins.second) initializeRobot(ins.first); // Maybe I could remove this
282  }
283  }
284  }
285  }
286 }
287 
288 } // namespace rviz
289 
rviz::HumansModelDisplay::onDisable
void onDisable() override
Definition: hri_skeletons.cpp:205
rviz::HumansModelDisplay::update
void update(float wall_dt, float ros_dt) override
Definition: hri_skeletons.cpp:215
rviz::HumansModelDisplay::initializeRobot
void initializeRobot(std::map< std::string, RobotPtr >::iterator it)
Definition: hri_skeletons.cpp:138
rviz::TFLinkUpdater
rviz::DisplayContext::queueRender
virtual void queueRender()=0
rviz::HumansModelDisplay::updateTfPrefix
void updateTfPrefix()
Definition: hri_skeletons.cpp:133
hri_skeletons.hpp
rviz::HumansModelDisplay::clear
void clear()
Definition: hri_skeletons.cpp:238
rviz::Display::initialized
bool initialized() const
rviz::HumansModelDisplay::tf_prefix_property_
StringProperty * tf_prefix_property_
Definition: hri_skeletons.hpp:106
rviz::HumansModelDisplay::updateCollisionVisible
void updateCollisionVisible()
Definition: hri_skeletons.cpp:124
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
rviz::Display::clearStatuses
virtual void clearStatuses()
rviz::HumansModelDisplay::collision_enabled_property_
Property * collision_enabled_property_
Definition: hri_skeletons.hpp:103
rviz::StatusProperty::Level
Level
rviz::FloatProperty::setMax
void setMax(float max)
description
description
urdf::Model
rviz::Property::getValue
virtual QVariant getValue() const
ros::ok
ROSCPP_DECL bool ok()
rviz::FloatProperty::setMin
void setMin(float min)
float_property.h
f
f
rviz::Display
rviz::linkUpdaterStatusFunction
void linkUpdaterStatusFunction(StatusProperty::Level level, const std::string &link_name, const std::string &text, RobotModelDisplay *display)
rviz::FloatProperty
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
rviz::HumansModelDisplay::alpha_property_
FloatProperty * alpha_property_
Definition: hri_skeletons.hpp:105
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::HumansModelDisplay::idsSub_
ros::Subscriber idsSub_
Definition: hri_skeletons.hpp:112
rviz
rviz::Property::Property
Property(const QString &name, const QVariant &default_value, const QString &description, P *parent, Func &&changed_slot)
rviz::Display::scene_node_
Ogre::SceneNode * scene_node_
rviz::StringProperty
rviz::StringProperty::getStdString
std::string getStdString()
rviz::StatusProperty::Ok
Ok
rviz::StatusProperty::Warn
Warn
model.h
ros::NodeHandle::searchParam
bool searchParam(const std::string &key, std::string &result) const
rviz::HumansModelDisplay::time_since_last_transform_
float time_since_last_transform_
Definition: hri_skeletons.hpp:98
ros::NodeHandle::hasParam
bool hasParam(const std::string &key) const
ROS_WARN
#define ROS_WARN(...)
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
urdf::Model::initString
URDF_EXPORT bool initString(const std::string &xmlstring)
rviz::DisplayContext::getFrameManager
virtual FrameManager * getFrameManager() const=0
rviz::HumansModelDisplay::visual_enabled_property_
Property * visual_enabled_property_
Definition: hri_skeletons.hpp:102
rviz::HumansModelDisplay::humans_
std::map< std::string, RobotPtr > humans_
Definition: hri_skeletons.hpp:108
rviz::HumansModelDisplay::fixedFrameChanged
void fixedFrameChanged() override
Definition: hri_skeletons.cpp:236
rviz::HumansModelDisplay::onEnable
void onEnable() override
Loads a URDF from the ros-param named by our "Robot Description" property, iterates through the links...
Definition: hri_skeletons.cpp:197
rviz::Display::context_
DisplayContext * context_
rviz::HumansModelDisplay::robot_description_
std::string robot_description_
Definition: hri_skeletons.hpp:100
rviz::HumansModelDisplay::idsCallback
void idsCallback(const hri_msgs::IdsListConstPtr &msg)
Definition: hri_skeletons.cpp:253
property.h
rviz::HumansModelDisplay::pluginEnabled_
bool pluginEnabled_
Definition: hri_skeletons.hpp:92
class_list_macros.hpp
rviz::HumansModelDisplay::updateAlpha
void updateAlpha()
Definition: hri_skeletons.cpp:108
rviz::HumansModelDisplay::update_rate_property_
FloatProperty * update_rate_property_
Definition: hri_skeletons.hpp:104
ros::InvalidNameException
rviz::HumansModelDisplay::HumansModelDisplay
HumansModelDisplay()
Definition: hri_skeletons.cpp:54
rviz::Display::reset
virtual void reset()
rviz::HumansModelDisplay::onInitialize
void onInitialize() override
Definition: hri_skeletons.cpp:102
rviz::HumansModelDisplay::has_new_transforms_
bool has_new_transforms_
Definition: hri_skeletons.hpp:94
rviz::HumansModelDisplay::updateVisualVisible
void updateVisualVisible()
Definition: hri_skeletons.cpp:116
rviz::Robot
rviz::HumansModelDisplay::reset
void reset() override
Definition: hri_skeletons.cpp:248
rviz::RobotPtr
std::shared_ptr< Robot > RobotPtr
Definition: hri_skeletons.hpp:52
string_property.h
rviz::HumansModelDisplay
Uses a robot xml description to display the pieces of a robot at the transforms broadcast by rosTF.
Definition: hri_skeletons.hpp:61
rviz::HumansModelDisplay::ids_
std::vector< std::string > ids_
Definition: hri_skeletons.hpp:111
rviz::Display::update_nh_
ros::NodeHandle update_nh_
robot.h
rviz::HumansModelDisplay::~HumansModelDisplay
~HumansModelDisplay() override
Definition: hri_skeletons.cpp:94
display_context.h


hri_rviz
Author(s): Lorenzo Ferrini, Séverin Lemaignan
autogenerated on Fri Oct 20 2023 02:53:21