effort_display.cpp
Go to the documentation of this file.
1 #include <OgreSceneNode.h>
2 #include <OgreSceneManager.h>
3 #include <QTimer>
4 
9 #include <rviz/frame_manager.h>
10 #include <rviz/validate_floats.h>
11 #include <rviz/helpers/tf_prefix.h>
12 
13 #include "effort_visual.h"
14 #include "effort_display.h"
15 
16 #include <urdf/model.h>
17 
18 namespace rviz
19 {
20 JointInfo::JointInfo(const std::string& name, rviz::Property* parent_category)
21 {
22  name_ = name;
23  effort_ = 0;
24  max_effort_ = 0;
25 
26  // info->category_ = new Property( QString::fromStdString( info->name_ ) , QVariant(), "",
27  // joints_category_);
28  category_ = new rviz::Property(QString::fromStdString(name_), true, "", parent_category,
30 
31  effort_property_ = new rviz::FloatProperty("Effort", 0, "Effort value of this joint.", category_);
33 
35  new rviz::FloatProperty("Max Effort", 0, "Max Effort value of this joint.", category_);
37 }
38 
40 {
41 }
42 
44 {
45 }
46 
47 void JointInfo::setEffort(double e)
48 {
50  effort_ = e;
51 }
53 {
55  max_effort_ = m;
56 }
57 
59 {
60  return category_->getValue().toBool();
61 }
62 
63 JointInfo* EffortDisplay::getJointInfo(const std::string& joint)
64 {
65  M_JointInfo::iterator it = joints_.find(joint);
66  if (it == joints_.end())
67  {
68  return nullptr;
69  }
70 
71  return it->second;
72 }
73 
74 JointInfo* EffortDisplay::createJoint(const std::string& joint)
75 {
76  JointInfo* info = new JointInfo(joint, joints_category_);
77  joints_.insert(std::make_pair(joint, info));
78  return info;
79 }
80 
82 {
83  alpha_property_ = new rviz::FloatProperty("Alpha", 1.0, "0 is fully transparent, 1.0 is fully opaque.",
85 
86  width_property_ = new rviz::FloatProperty("Width", 0.02, "Width to drow effort circle", this,
88 
89  scale_property_ = new rviz::FloatProperty("Scale", 1.0, "Scale to drow effort circle", this,
91 
93  new rviz::IntProperty("History Length", 1, "Number of prior measurements to display.", this,
97 
99  new rviz::StringProperty("Robot Description", "robot_description",
100  "Name of the parameter to search for to load the robot "
101  "description.",
103 
105  "TF Prefix", "",
106  "Robot Model normally assumes the link name is the same as the tf frame name. "
107  "This option allows you to set a prefix. Mainly useful for multi-robot situations.",
109 
110  joints_category_ = new rviz::Property("Joints", QVariant(), "", this);
111 }
112 
114 {
116  // skip tf_filter_ (resetting it)
117  delete tf_filter_;
119  std::string(), 1, update_nh_);
120 
121  // but directly process messages
122  sub_.registerCallback(boost::bind(&EffortDisplay::incomingMessage, this, boost::placeholders::_1));
124 }
125 
127 {
128 }
129 
130 // Clear the visuals by deleting their objects.
132 {
133  MFDClass::reset();
134  visuals_.clear();
135 }
136 
138 {
139  clearStatuses();
141 }
142 
144 {
145  clearStatuses();
146  robot_description_.clear();
147 }
148 
149 // Set the current color and alpha values for each visual.
151 {
152  float width = width_property_->getFloat();
153  float scale = scale_property_->getFloat();
154 
155  for (size_t i = 0; i < visuals_.size(); i++)
156  {
157  visuals_[i]->setWidth(width);
158  visuals_[i]->setScale(scale);
159  }
160 }
161 
163 {
164  if (isEnabled())
165  {
166  load();
168  }
169 }
170 
171 // Set the number of past visuals to show.
173 {
174  visuals_.rset_capacity(history_length_property_->getInt());
175 }
176 
178 {
179  // get robot_description
180  std::string content;
181  try
182  {
184  {
185  std::string loc;
187  update_nh_.getParam(loc, content);
188  else
189  {
190  clear();
192  QString("Parameter [%1] does not exist, and was not found by searchParam()")
194  // try again in a second
195  QTimer::singleShot(1000, this, &EffortDisplay::updateRobotDescription);
196  return;
197  }
198  }
199  }
200  catch (const ros::InvalidNameException& e)
201  {
202  clear();
204  QString("Invalid parameter name: %1.\n%2")
205  .arg(robot_description_property_->getString(), e.what()));
206  return;
207  }
208 
209  if (content.empty())
210  {
211  clear();
212  setStatus(rviz::StatusProperty::Error, "URDF", "URDF is empty");
213  return;
214  }
215 
216  if (content == robot_description_)
217  {
218  return;
219  }
220 
221  robot_description_ = content;
222 
224  if (!robot_model_->initString(content))
225  {
226  ROS_ERROR("Unable to parse URDF description!");
227  setStatus(rviz::StatusProperty::Error, "URDF", "Unable to parse robot model description!");
228  return;
229  }
230  setStatus(rviz::StatusProperty::Ok, "URDF", "Robot model parsed Ok");
231  for (std::map<std::string, urdf::JointSharedPtr>::iterator it = robot_model_->joints_.begin();
232  it != robot_model_->joints_.end(); it++)
233  {
234  urdf::JointSharedPtr joint = it->second;
235  if (joint->type == urdf::Joint::REVOLUTE)
236  {
237  std::string joint_name = it->first;
238  urdf::JointLimitsSharedPtr limit = joint->limits;
239  joints_[joint_name] = createJoint(joint_name);
240  joints_[joint_name]->setMaxEffort(limit->effort);
241  }
242  }
243 }
244 
246 {
247  load();
248 }
249 
251 {
252  clear();
253 }
254 
255 // This is our callback to handle an incoming message.
256 void EffortDisplay::processMessage(const sensor_msgs::JointState::ConstPtr& msg)
257 {
258  // Robot model might not be loaded already
259  if (!robot_model_)
260  {
261  return;
262  }
263  // We are keeping a circular buffer of visual pointers. This gets
264  // the next one, or creates and stores it if the buffer is not full
266  if (visuals_.full())
267  {
268  visual = visuals_.front();
269  }
270  else
271  {
272  visual.reset(new EffortVisual(context_->getSceneManager(), scene_node_));
273  }
274  visual->setWidth(width_property_->getFloat());
275  visual->setScale(scale_property_->getFloat());
276 
277  V_string joints;
278  size_t joint_num = msg->name.size();
279  if (joint_num != msg->effort.size())
280  {
282  "Received a joint state msg with different joint names and efforts size!");
283  return;
284  }
285  for (size_t i = 0; i < joint_num; ++i)
286  {
287  const std::string& joint_name = msg->name[i];
288  JointInfo* joint_info = getJointInfo(joint_name);
289  if (!joint_info)
290  continue; // skip joints..
291 
292  // update effort property
293  joint_info->setEffort(msg->effort[i]);
294  joint_info->last_update_ = msg->header.stamp;
295 
296  const urdf::Joint* joint = robot_model_->getJoint(joint_name).get();
297  int joint_type = joint->type;
298  if (joint_type == urdf::Joint::REVOLUTE)
299  {
300  std::string tf_frame_id = concat(tf_prefix_property_->getStdString(), joint->child_link_name);
301  Ogre::Quaternion orientation;
302  Ogre::Vector3 position;
303 
304  // Call rviz::FrameManager to get the transform from the fixed frame to the joint's frame.
305  if (!context_->getFrameManager()->getTransform(tf_frame_id, ros::Time(), position, orientation))
306  {
307  setStatus(rviz::StatusProperty::Error, QString::fromStdString(joint_name),
308  QString("Error transforming from frame '%1' to frame '%2'")
309  .arg(tf_frame_id.c_str(), qPrintable(fixed_frame_)));
310  continue;
311  };
312  tf2::Vector3 axis_joint(joint->axis.x, joint->axis.y, joint->axis.z);
313  tf2::Vector3 axis_z(0, 0, 1);
314  tf2::Quaternion axis_rotation(axis_joint.cross(axis_z), axis_joint.angle(axis_z));
315  if (std::isnan(axis_rotation.x()) || std::isnan(axis_rotation.y()) || std::isnan(axis_rotation.z()))
316  axis_rotation = tf2::Quaternion::getIdentity();
317 
318  tf2::Quaternion axis_orientation(orientation.x, orientation.y, orientation.z, orientation.w);
319  tf2::Quaternion axis_rot = axis_orientation * axis_rotation;
320  Ogre::Quaternion joint_orientation(Ogre::Real(axis_rot.w()), Ogre::Real(axis_rot.x()),
321  Ogre::Real(axis_rot.y()), Ogre::Real(axis_rot.z()));
322  visual->setFramePosition(joint_name, position);
323  visual->setFrameOrientation(joint_name, joint_orientation);
324  visual->setFrameEnabled(joint_name, joint_info->getEnabled());
325 
326  if (!validateFloats(joint_info->getEffort()))
327  {
328  setStatus(rviz::StatusProperty::Error, QString::fromStdString(joint_name),
329  QString("Invalid effort: %1").arg(joint_info->getEffort()));
330  visual->setFrameEnabled(joint_name, false);
331  }
332  else
333  setStatus(rviz::StatusProperty::Ok, QString::fromStdString(joint_name), QString());
334 
335  visual->setEffort(joint_name, joint_info->getEffort(), joint_info->getMaxEffort());
336  }
337  }
338  visuals_.push_back(visual);
339 }
340 
341 } // end namespace rviz
342 
343 // Tell pluginlib about this class. It is important to do this in
344 // global scope, outside our package's namespace.
rviz::JointInfo::setEffort
void setEffort(double e)
Definition: effort_display.cpp:47
rviz::EffortDisplay::EffortDisplay
EffortDisplay()
Definition: effort_display.cpp:81
rviz::Display::isEnabled
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:271
rviz::IntProperty::setMin
void setMin(int min)
Definition: int_property.cpp:52
rviz::JointInfo
Definition: effort_display.h:38
rviz::MessageFilterDisplay< sensor_msgs::JointState >::reset
void reset() override
Definition: message_filter_display.h:125
validate_floats.h
rviz::EffortDisplay::robot_model_
boost::shared_ptr< urdf::Model > robot_model_
Definition: effort_display.h:108
rviz::concat
std::string concat(const std::string &prefix, const std::string &frame)
Definition: tf_prefix.h:35
frame_manager.h
tf2::Quaternion::getIdentity
static const Quaternion & getIdentity()
rviz::DisplayContext::queueRender
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
tf2_ros::MessageFilter< sensor_msgs::JointState >
rviz::EffortDisplay::robot_description_
std::string robot_description_
Definition: effort_display.h:110
boost::shared_ptr< urdf::Model >
rviz::JointInfo::name_
std::string name_
Definition: effort_display.h:63
rviz::JointInfo::last_update_
ros::Time last_update_
Definition: effort_display.h:57
rviz::StatusProperty::Error
@ Error
Definition: status_property.h:46
rviz::EffortDisplay::load
void load()
Definition: effort_display.cpp:177
rviz::JointInfo::getMaxEffort
double getMaxEffort()
Definition: effort_display.h:51
rviz::MessageFilterDisplay< sensor_msgs::JointState >::incomingMessage
void incomingMessage(const typename sensor_msgs::JointState ::ConstPtr &msg)
Incoming message callback. Checks if the message pointer is valid, increments messages_received_,...
Definition: message_filter_display.h:202
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
rviz::Display::clearStatuses
virtual void clearStatuses()
Delete all status children. This is thread-safe.
Definition: display.cpp:212
rviz::EffortDisplay::history_length_property_
rviz::IntProperty * history_length_property_
Definition: effort_display.h:124
int_property.h
rviz::validateFloats
bool validateFloats(const sensor_msgs::CameraInfo &msg)
Definition: camera_display.cpp:72
float_property.h
rviz::EffortDisplay::joints_
M_JointInfo joints_
Definition: effort_display.h:120
rviz::Display::fixed_frame_
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:312
urdf::Model
rviz::Property::getValue
virtual QVariant getValue() const
Return the value of this Property as a QVariant. If the value has never been set, an invalid QVariant...
Definition: property.cpp:150
rviz::JointInfo::JointInfo
JointInfo(const std::string &name, rviz::Property *parent_category)
Definition: effort_display.cpp:20
rviz::JointInfo::getEnabled
bool getEnabled() const
Definition: effort_display.cpp:58
tf_prefix.h
rviz::DisplayContext::getSceneManager
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
rviz::Display
Definition: display.h:63
rviz::FloatProperty
Property specialized to enforce floating point max/min.
Definition: float_property.h:37
rviz::JointInfo::getEffort
double getEffort()
Definition: effort_display.h:46
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::EffortDisplay::visuals_
boost::circular_buffer< boost::shared_ptr< EffortVisual > > visuals_
Definition: effort_display.h:117
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:176
rviz::V_string
std::vector< std::string > V_string
Definition: effort_display.h:72
rviz::EffortDisplay::~EffortDisplay
~EffortDisplay() override
Definition: effort_display.cpp:126
rviz::JointInfo::setMaxEffort
void setMaxEffort(double m)
Definition: effort_display.cpp:52
rviz::JointInfo::max_effort_property_
rviz::FloatProperty * max_effort_property_
Definition: effort_display.h:68
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::FloatProperty::getFloat
virtual float getFloat() const
Definition: float_property.h:79
rviz::EffortDisplay
Definition: effort_display.h:76
rviz::EffortDisplay::scale_property_
rviz::FloatProperty * scale_property_
Definition: effort_display.h:123
rviz::EffortDisplay::reset
void reset() override
Called to tell the display to clear its state.
Definition: effort_display.cpp:131
rviz
Definition: add_display_dialog.cpp:54
rviz::Display::scene_node_
Ogre::SceneNode * scene_node_
The Ogre::SceneNode to hold all 3D scene elements shown by this Display.
Definition: display.h:295
color_property.h
rviz::StringProperty
Property specialized for string values.
Definition: string_property.h:39
rviz::StringProperty::getStdString
std::string getStdString()
Definition: string_property.h:71
rviz::StatusProperty::Ok
@ Ok
Definition: status_property.h:44
rviz::EffortDisplay::updateColorAndAlpha
void updateColorAndAlpha()
Definition: effort_display.cpp:150
rviz::IntProperty::setMax
void setMax(int max)
Definition: int_property.cpp:58
model.h
ros::NodeHandle::searchParam
bool searchParam(const std::string &key, std::string &result) const
rviz::limit
static int limit(int i)
Definition: parse_color.cpp:34
rviz::StringProperty::getString
QString getString()
Definition: string_property.h:75
rviz::EffortDisplay::robot_description_property_
rviz::StringProperty * robot_description_property_
Definition: effort_display.h:126
rviz::JointInfo::updateVisibility
void updateVisibility()
Definition: effort_display.cpp:43
rviz::EffortDisplay::onDisable
void onDisable() override
Derived classes override this to do the actual work of disabling themselves.
Definition: effort_display.cpp:250
message_filters::SimpleFilter::registerCallback
Connection registerCallback(const boost::function< void(P)> &callback)
rviz::EffortVisual
Definition: effort_visual.h:18
rviz::EffortDisplay::tf_prefix_property_
rviz::StringProperty * tf_prefix_property_
Definition: effort_display.h:127
rviz::DisplayContext::getFrameManager
virtual FrameManager * getFrameManager() const =0
Return the FrameManager instance.
rviz::EffortDisplay::joints_category_
rviz::Property * joints_category_
Definition: effort_display.h:128
visualization_manager.h
rviz::EffortDisplay::alpha_property_
rviz::FloatProperty * alpha_property_
Definition: effort_display.h:123
rviz::MessageFilterDisplay< sensor_msgs::JointState >::onInitialize
void onInitialize() override
Definition: message_filter_display.h:105
rviz::FrameManager::getTransform
bool getTransform(const Header &header, Ogre::Vector3 &position, Ogre::Quaternion &orientation)
Return the pose for a header, relative to the fixed frame, in Ogre classes.
Definition: frame_manager.h:125
rviz::JointInfo::max_effort_
double max_effort_
Definition: effort_display.h:64
rviz::EffortDisplay::updateTfPrefix
void updateTfPrefix()
Definition: effort_display.cpp:137
rviz::Display::context_
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz....
Definition: display.h:287
ros::Time
rviz::FloatProperty::setFloat
bool setFloat(float new_value)
Float-typed "SLOT" version of setValue().
Definition: float_property.h:97
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:497
rviz::EffortDisplay::processMessage
void processMessage(const sensor_msgs::JointState::ConstPtr &msg) override
Definition: effort_display.cpp:256
effort_display.h
rviz::MessageFilterDisplay< sensor_msgs::JointState >::tf_filter_
tf2_ros::MessageFilter< sensor_msgs::JointState > * tf_filter_
Definition: message_filter_display.h:232
rviz::EffortDisplay::createJoint
JointInfo * createJoint(const std::string &joint)
Definition: effort_display.cpp:74
rviz::EffortDisplay::onInitialize
void onInitialize() override
Override this function to do subclass-specific initialization.
Definition: effort_display.cpp:113
ROS_ERROR
#define ROS_ERROR(...)
class_list_macros.hpp
ros::InvalidNameException
tf2::Quaternion
rviz::IntProperty::getInt
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:96
rviz::EffortDisplay::clear
void clear()
Definition: effort_display.cpp:143
rviz::EffortDisplay::getJointInfo
JointInfo * getJointInfo(const std::string &joint)
Definition: effort_display.cpp:63
rviz::JointInfo::~JointInfo
~JointInfo() override
Definition: effort_display.cpp:39
rviz::EffortDisplay::width_property_
rviz::FloatProperty * width_property_
Definition: effort_display.h:123
rviz::JointInfo::effort_property_
rviz::FloatProperty * effort_property_
Definition: effort_display.h:67
rviz::JointInfo::category_
rviz::Property * category_
Definition: effort_display.h:66
rviz::EffortDisplay::updateHistoryLength
void updateHistoryLength()
Definition: effort_display.cpp:172
rviz::EffortDisplay::onEnable
void onEnable() override
Derived classes override this to do the actual work of enabling themselves.
Definition: effort_display.cpp:245
effort_visual.h
rviz::DisplayContext::getTF2BufferPtr
std::shared_ptr< tf2_ros::Buffer > getTF2BufferPtr() const
Convenience function: returns getFrameManager()->getTF2BufferPtr().
Definition: display_context.h:98
rviz::EffortDisplay::updateRobotDescription
void updateRobotDescription()
Definition: effort_display.cpp:162
rviz::JointInfo::effort_
double effort_
Definition: effort_display.h:64
rviz::Display::update_nh_
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:300
rviz::IntProperty
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:37
rviz::MessageFilterDisplay< sensor_msgs::JointState >::sub_
message_filters::Subscriber< sensor_msgs::JointState > sub_
Definition: message_filter_display.h:231


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:52