Skip to content

Commit 0cdf1ee

Browse files
rhaschkewjwwood
authored andcommitted
generalize WrenchVisual class (#982)
* whitespace normalization in wrench_display.* + wrench_visual.* * generalized WrenchStampedVisual::setMessage() - interface changed to receive a Wrench& only - renamed setMessage() to setWrench() * alternative API: WrenchStampedVisual::setWrench(OgreVector3 force, OgreVector3 torque) * expose SceneNode::setVisible() * separate scene nodes for force and torque marker ... to allow hiding of arrows if they become shorter than wide * retain API compatibility * retain ABI compatibility
1 parent 5bda797 commit 0cdf1ee

File tree

4 files changed

+254
-214
lines changed

4 files changed

+254
-214
lines changed

src/rviz/default_plugin/wrench_display.cpp

Lines changed: 106 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -18,162 +18,159 @@
1818
namespace rviz
1919
{
2020

21-
WrenchStampedDisplay::WrenchStampedDisplay()
22-
{
23-
force_color_property_ =
24-
new rviz::ColorProperty( "Force Color", QColor( 204, 51, 51 ),
21+
WrenchStampedDisplay::WrenchStampedDisplay()
22+
{
23+
force_color_property_ =
24+
new rviz::ColorProperty( "Force Color", QColor( 204, 51, 51 ),
2525
"Color to draw the force arrows.",
2626
this, SLOT( updateColorAndAlpha() ));
2727

28-
torque_color_property_ =
29-
new rviz::ColorProperty( "Torque Color", QColor( 204, 204, 51),
28+
torque_color_property_ =
29+
new rviz::ColorProperty( "Torque Color", QColor( 204, 204, 51),
3030
"Color to draw the torque arrows.",
3131
this, SLOT( updateColorAndAlpha() ));
3232

33-
alpha_property_ =
33+
alpha_property_ =
3434
new rviz::FloatProperty( "Alpha", 1.0,
3535
"0 is fully transparent, 1.0 is fully opaque.",
3636
this, SLOT( updateColorAndAlpha() ));
3737

38-
force_scale_property_ =
38+
force_scale_property_ =
3939
new rviz::FloatProperty( "Force Arrow Scale", 2.0,
4040
"force arrow scale",
4141
this, SLOT( updateColorAndAlpha() ));
4242

43-
torque_scale_property_ =
43+
torque_scale_property_ =
4444
new rviz::FloatProperty( "Torque Arrow Scale", 2.0,
4545
"torque arrow scale",
46-
this, SLOT( updateColorAndAlpha() ));
46+
this, SLOT( updateColorAndAlpha() ));
4747

48-
width_property_ =
48+
width_property_ =
4949
new rviz::FloatProperty( "Arrow Width", 0.5,
5050
"arrow width",
5151
this, SLOT( updateColorAndAlpha() ));
5252

5353

54-
history_length_property_ =
55-
new rviz::IntProperty( "History Length", 1,
54+
history_length_property_ =
55+
new rviz::IntProperty( "History Length", 1,
5656
"Number of prior measurements to display.",
5757
this, SLOT( updateHistoryLength() ));
5858

59-
history_length_property_->setMin( 1 );
60-
history_length_property_->setMax( 100000 );
61-
}
59+
history_length_property_->setMin( 1 );
60+
history_length_property_->setMax( 100000 );
61+
}
6262

63-
void WrenchStampedDisplay::onInitialize()
64-
{
65-
MFDClass::onInitialize();
66-
updateHistoryLength( );
67-
}
63+
void WrenchStampedDisplay::onInitialize()
64+
{
65+
MFDClass::onInitialize();
66+
updateHistoryLength( );
67+
}
68+
69+
WrenchStampedDisplay::~WrenchStampedDisplay()
70+
{
71+
}
72+
73+
// Override rviz::Display's reset() function to add a call to clear().
74+
void WrenchStampedDisplay::reset()
75+
{
76+
MFDClass::reset();
77+
visuals_.clear();
78+
}
6879

69-
WrenchStampedDisplay::~WrenchStampedDisplay()
80+
void WrenchStampedDisplay::updateColorAndAlpha()
81+
{
82+
float alpha = alpha_property_->getFloat();
83+
float force_scale = force_scale_property_->getFloat();
84+
float torque_scale = torque_scale_property_->getFloat();
85+
float width = width_property_->getFloat();
86+
Ogre::ColourValue force_color = force_color_property_->getOgreColor();
87+
Ogre::ColourValue torque_color = torque_color_property_->getOgreColor();
88+
89+
for( size_t i = 0; i < visuals_.size(); i++ )
7090
{
91+
visuals_[i]->setForceColor( force_color.r, force_color.g, force_color.b, alpha );
92+
visuals_[i]->setTorqueColor( torque_color.r, torque_color.g, torque_color.b, alpha );
93+
visuals_[i]->setForceScale( force_scale );
94+
visuals_[i]->setTorqueScale( torque_scale );
95+
visuals_[i]->setWidth( width );
7196
}
97+
}
98+
99+
// Set the number of past visuals to show.
100+
void WrenchStampedDisplay::updateHistoryLength()
101+
{
102+
visuals_.rset_capacity(history_length_property_->getInt());
103+
}
72104

73-
// Override rviz::Display's reset() function to add a call to clear().
74-
void WrenchStampedDisplay::reset()
105+
bool validateFloats( const geometry_msgs::WrenchStamped& msg )
106+
{
107+
return rviz::validateFloats(msg.wrench.force) && rviz::validateFloats(msg.wrench.torque) ;
108+
}
109+
110+
// This is our callback to handle an incoming message.
111+
void WrenchStampedDisplay::processMessage( const geometry_msgs::WrenchStamped::ConstPtr& msg )
112+
{
113+
if( !validateFloats( *msg ))
75114
{
76-
MFDClass::reset();
77-
visuals_.clear();
115+
setStatus( rviz::StatusProperty::Error, "Topic", "Message contained invalid floating point values (nans or infs)" );
116+
return;
78117
}
79118

80-
void WrenchStampedDisplay::updateColorAndAlpha()
119+
// Here we call the rviz::FrameManager to get the transform from the
120+
// fixed frame to the frame in the header of this Imu message. If
121+
// it fails, we can't do anything else so we return.
122+
Ogre::Quaternion orientation;
123+
Ogre::Vector3 position;
124+
if( !context_->getFrameManager()->getTransform( msg->header.frame_id,
125+
msg->header.stamp,
126+
position, orientation ))
81127
{
82-
float alpha = alpha_property_->getFloat();
83-
float force_scale = force_scale_property_->getFloat();
84-
float torque_scale = torque_scale_property_->getFloat();
85-
float width = width_property_->getFloat();
86-
Ogre::ColourValue force_color = force_color_property_->getOgreColor();
87-
Ogre::ColourValue torque_color = torque_color_property_->getOgreColor();
88-
89-
for( size_t i = 0; i < visuals_.size(); i++ )
90-
{
91-
visuals_[i]->setForceColor( force_color.r, force_color.g, force_color.b, alpha );
92-
visuals_[i]->setTorqueColor( torque_color.r, torque_color.g, torque_color.b, alpha );
93-
visuals_[i]->setForceScale( force_scale );
94-
visuals_[i]->setTorqueScale( torque_scale );
95-
visuals_[i]->setWidth( width );
96-
}
128+
ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'",
129+
msg->header.frame_id.c_str(), qPrintable( fixed_frame_ ));
130+
return;
97131
}
98132

99-
// Set the number of past visuals to show.
100-
void WrenchStampedDisplay::updateHistoryLength()
133+
if ( position.isNaN() )
101134
{
102-
visuals_.rset_capacity(history_length_property_->getInt());
135+
ROS_ERROR_THROTTLE(1.0, "Wrench position contains NaNs. Skipping render as long as the position is invalid");
136+
return;
103137
}
104138

105-
bool validateFloats( const geometry_msgs::WrenchStamped& msg )
139+
// We are keeping a circular buffer of visual pointers. This gets
140+
// the next one, or creates and stores it if the buffer is not full
141+
boost::shared_ptr<WrenchStampedVisual> visual;
142+
if( visuals_.full() )
106143
{
107-
return rviz::validateFloats(msg.wrench.force) && rviz::validateFloats(msg.wrench.torque) ;
144+
visual = visuals_.front();
108145
}
109-
110-
// This is our callback to handle an incoming message.
111-
void WrenchStampedDisplay::processMessage( const geometry_msgs::WrenchStamped::ConstPtr& msg )
146+
else
112147
{
113-
if( !validateFloats( *msg ))
114-
{
115-
setStatus( rviz::StatusProperty::Error, "Topic", "Message contained invalid floating point values (nans or infs)" );
116-
return;
117-
}
118-
119-
// Here we call the rviz::FrameManager to get the transform from the
120-
// fixed frame to the frame in the header of this Imu message. If
121-
// it fails, we can't do anything else so we return.
122-
Ogre::Quaternion orientation;
123-
Ogre::Vector3 position;
124-
if( !context_->getFrameManager()->getTransform( msg->header.frame_id,
125-
msg->header.stamp,
126-
position, orientation ))
127-
{
128-
ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'",
129-
msg->header.frame_id.c_str(), qPrintable( fixed_frame_ ));
130-
return;
131-
}
132-
133-
if ( position.isNaN() )
134-
{
135-
ROS_ERROR_THROTTLE(1.0, "Wrench position contains NaNs. Skipping render as long as the position is invalid");
136-
return;
137-
}
138-
139-
// We are keeping a circular buffer of visual pointers. This gets
140-
// the next one, or creates and stores it if the buffer is not full
141-
boost::shared_ptr<WrenchStampedVisual> visual;
142-
if( visuals_.full() )
143-
{
144-
visual = visuals_.front();
145-
}
146-
else
147-
{
148-
visual.reset(new WrenchStampedVisual( context_->getSceneManager(), scene_node_ ));
149-
}
150-
151-
// Now set or update the contents of the chosen visual.
152-
visual->setMessage( msg );
153-
visual->setFramePosition( position );
154-
visual->setFrameOrientation( orientation );
155-
float alpha = alpha_property_->getFloat();
156-
float force_scale = force_scale_property_->getFloat();
157-
float torque_scale = torque_scale_property_->getFloat();
158-
float width = width_property_->getFloat();
159-
Ogre::ColourValue force_color = force_color_property_->getOgreColor();
160-
Ogre::ColourValue torque_color = torque_color_property_->getOgreColor();
161-
visual->setForceColor( force_color.r, force_color.g, force_color.b, alpha );
162-
visual->setTorqueColor( torque_color.r, torque_color.g, torque_color.b, alpha );
163-
visual->setForceScale( force_scale );
164-
visual->setTorqueScale( torque_scale );
165-
visual->setWidth( width );
166-
167-
// And send it to the end of the circular buffer
168-
visuals_.push_back(visual);
148+
visual.reset(new WrenchStampedVisual( context_->getSceneManager(), scene_node_ ));
169149
}
170150

151+
// Now set or update the contents of the chosen visual.
152+
visual->setWrench( msg->wrench );
153+
visual->setFramePosition( position );
154+
visual->setFrameOrientation( orientation );
155+
float alpha = alpha_property_->getFloat();
156+
float force_scale = force_scale_property_->getFloat();
157+
float torque_scale = torque_scale_property_->getFloat();
158+
float width = width_property_->getFloat();
159+
Ogre::ColourValue force_color = force_color_property_->getOgreColor();
160+
Ogre::ColourValue torque_color = torque_color_property_->getOgreColor();
161+
visual->setForceColor( force_color.r, force_color.g, force_color.b, alpha );
162+
visual->setTorqueColor( torque_color.r, torque_color.g, torque_color.b, alpha );
163+
visual->setForceScale( force_scale );
164+
visual->setTorqueScale( torque_scale );
165+
visual->setWidth( width );
166+
167+
// And send it to the end of the circular buffer
168+
visuals_.push_back(visual);
169+
}
170+
171171
} // end namespace rviz
172172

173173
// Tell pluginlib about this class. It is important to do this in
174174
// global scope, outside our package's namespace.
175175
#include <pluginlib/class_list_macros.h>
176176
PLUGINLIB_EXPORT_CLASS( rviz::WrenchStampedDisplay, rviz::Display )
177-
178-
179-

src/rviz/default_plugin/wrench_display.h

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,55 @@
1010

1111
namespace Ogre
1212
{
13-
class SceneNode;
13+
class SceneNode;
1414
}
1515

1616
namespace rviz
1717
{
18-
class ColorProperty;
19-
class ROSTopicStringProperty;
20-
class FloatProperty;
21-
class IntProperty;
18+
class ColorProperty;
19+
class ROSTopicStringProperty;
20+
class FloatProperty;
21+
class IntProperty;
2222
}
2323

2424
namespace rviz
2525
{
2626

27-
class WrenchStampedVisual;
27+
class WrenchStampedVisual;
2828

29-
class WrenchStampedDisplay: public rviz::MessageFilterDisplay<geometry_msgs::WrenchStamped>
30-
{
29+
class WrenchStampedDisplay: public rviz::MessageFilterDisplay<geometry_msgs::WrenchStamped>
30+
{
3131
Q_OBJECT
32-
public:
33-
// Constructor. pluginlib::ClassLoader creates instances by calling
34-
// the default constructor, so make sure you have one.
35-
WrenchStampedDisplay();
36-
virtual ~WrenchStampedDisplay();
32+
public:
33+
// Constructor. pluginlib::ClassLoader creates instances by calling
34+
// the default constructor, so make sure you have one.
35+
WrenchStampedDisplay();
36+
virtual ~WrenchStampedDisplay();
3737

38-
protected:
39-
// Overrides of public virtual functions from the Display class.
40-
virtual void onInitialize();
41-
virtual void reset();
38+
protected:
39+
// Overrides of public virtual functions from the Display class.
40+
virtual void onInitialize();
41+
virtual void reset();
4242

43-
private Q_SLOTS:
44-
// Helper function to apply color and alpha to all visuals.
45-
void updateColorAndAlpha();
46-
void updateHistoryLength();
43+
private Q_SLOTS:
44+
// Helper function to apply color and alpha to all visuals.
45+
void updateColorAndAlpha();
46+
void updateHistoryLength();
4747

48-
private:
49-
// Function to handle an incoming ROS message.
50-
void processMessage( const geometry_msgs::WrenchStamped::ConstPtr& msg );
48+
private:
49+
// Function to handle an incoming ROS message.
50+
void processMessage( const geometry_msgs::WrenchStamped::ConstPtr& msg );
5151

52-
// Storage for the list of visuals par each joint intem
53-
// Storage for the list of visuals. It is a circular buffer where
54-
// data gets popped from the front (oldest) and pushed to the back (newest)
55-
boost::circular_buffer<boost::shared_ptr<WrenchStampedVisual> > visuals_;
52+
// Storage for the list of visuals par each joint intem
53+
// Storage for the list of visuals. It is a circular buffer where
54+
// data gets popped from the front (oldest) and pushed to the back (newest)
55+
boost::circular_buffer<boost::shared_ptr<WrenchStampedVisual> > visuals_;
5656

57-
// Property objects for user-editable properties.
58-
rviz::ColorProperty *force_color_property_, *torque_color_property_;
59-
rviz::FloatProperty *alpha_property_, *force_scale_property_, *torque_scale_property_, *width_property_;
60-
rviz::IntProperty *history_length_property_;
61-
};
57+
// Property objects for user-editable properties.
58+
rviz::ColorProperty *force_color_property_, *torque_color_property_;
59+
rviz::FloatProperty *alpha_property_, *force_scale_property_, *torque_scale_property_, *width_property_;
60+
rviz::IntProperty *history_length_property_;
61+
};
6262
} // end namespace rviz_plugin_tutorials
6363

6464
#endif // WRENCHSTAMPED_DISPLAY_H

0 commit comments

Comments
 (0)