ambient_sound_display.cpp
Go to the documentation of this file.
1 #include <OGRE/OgreSceneNode.h>
2 #include <OGRE/OgreSceneManager.h>
3 
5 
8 #include <rviz/properties/property_manager.h>
9 #include <rviz/frame_manager.h>
10 
11 #include "ambient_sound_visual.h"
12 
13 #include "ambient_sound_display.h"
14 
15 namespace jsk_rviz_plugins
16 {
17 
18  // BEGIN_TUTORIAL
19  // The constructor must have no arguments, so we can't give the
20  // constructor the parameters it needs to fully initialize.
22  : Display()
23  , scene_node_( NULL )
24  , messages_received_( 0 )
25  , color_( .8, .2, .8 ) // Default color is bright purple.
26  , alpha_( 1.0 ) // Default alpha is completely opaque.
27  , width_(0.1)
28  , scale_(0.1)
29  {
30  }/*}}}*/
31 
32  // After the parent rviz::Display::initialize() does its own setup, it
33  // calls the subclass's onInitialize() function. This is where we
34  // instantiate all the workings of the class.
36  {
37  // Make an Ogre::SceneNode to contain all our visuals.
38  scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
39 
40  // Set the default history length and resize the ``visuals_`` array.
41  setHistoryLength( 1 );
42 
43  // A tf::MessageFilter listens to ROS messages and calls our
44  // callback with them when they can be matched up with valid tf
45  // transform data.
46  tf_filter_ =
47  new tf::MessageFilter<jsk_hark_msgs::HarkPower>( *vis_manager_->getTFClient(),
48  "", 100, update_nh_ );
50  tf_filter_->registerCallback( boost::bind( &AmbientSoundDisplay::incomingMessage,
51  this, _1 ));
52 
53  // FrameManager has some built-in functions to set the status of a
54  // Display based on callbacks from a tf::MessageFilter. These work
55  // fine for this simple display.
56  vis_manager_->getFrameManager()
57  ->registerFilterForTransformStatusCheck( tf_filter_, this );
58  }/*}}}*/
59 
61  {
62  unsubscribe();
63  clear();
64  for( size_t i = 0; i < visuals_.size(); i++ )
65  {
66  delete visuals_[ i ];
67  }
68 
69  delete tf_filter_;
70  }/*}}}*/
71 
72  // Clear the visuals by deleting their objects.
74  {
75  for( size_t i = 0; i < visuals_.size(); i++ )
76  {
77  delete visuals_[ i ];
78  visuals_[ i ] = NULL;
79  }
80  tf_filter_->clear();
82  setStatus( rviz::status_levels::Warn, "Topic", "No messages received" );
83  }/*}}}*/
84 
85  void AmbientSoundDisplay::setTopic( const std::string& topic )/*{{{*/
86  {
87  unsubscribe();
88  clear();
89  topic_ = topic;
90  subscribe();
91 
92  // Broadcast the fact that the variable has changed.
93  propertyChanged( topic_property_ );
94 
95  // Make sure rviz renders the next time it gets a chance.
96  causeRender();
97  }/*}}}*/
98 
99  void AmbientSoundDisplay::setColor( const rviz::Color& color )/*{{{*/
100  {
101  color_ = color;
102 
103  propertyChanged( color_property_ );
105  causeRender();
106  }/*}}}*/
107 
109  {
110  alpha_ = alpha;
111 
112  propertyChanged( alpha_property_ );
114  causeRender();
115  }/*}}}*/
116 
117  void AmbientSoundDisplay::setWidth( float width )/*{{{*/
118  {
119  width_ = width;
120 
121  propertyChanged( width_property_ );
123  causeRender();
124  }/*}}}*/
125 
127  {
128  scale_ = scale;
129 
130  propertyChanged( scale_property_ );
132  causeRender();
133  }
134  /*}}}*/
135 
136  void AmbientSoundDisplay::setBias( float bias )/*{{{*/
137  {
138  bias_ = bias;
139 
140  propertyChanged( bias_property_ );
142  causeRender();
143  }/*}}}*/
144 
145  void AmbientSoundDisplay::setGrad( float grad )/*{{{*/
146  {
147  grad_ = grad;
148 
149  propertyChanged( grad_property_ );
151  causeRender();
152  }/*}}}*/
153 
154  // Set the current color and alpha values for each visual.
156  {
157  for( size_t i = 0; i < visuals_.size(); i++ )
158  {
159  if( visuals_[ i ] )
160  {
161  visuals_[ i ]->setColor( color_.r_, color_.g_, color_.b_, alpha_ );
162  }
163  }
164  }/*}}}*/
165 
166  // Set the number of past visuals to show.
167  void AmbientSoundDisplay::setHistoryLength( int length )/*{{{*/
168  {
169  // Don't let people enter invalid values.
170  if( length < 1 )
171  {
172  length = 1;
173  }
174  // If the length is not changing, we don't need to do anything.
175  if( history_length_ == length )
176  {
177  return;
178  }
179 
180  // Set the actual variable.
182  propertyChanged( history_length_property_ );
183 
184  // Create a new array of visual pointers, all NULL.
185  std::vector<AmbientSoundVisual*> new_visuals( history_length_, (AmbientSoundVisual*)0 );
186 
187  // Copy the contents from the old array to the new.
188  // (Number to copy is the minimum of the 2 vector lengths).
189  size_t copy_len =
190  (new_visuals.size() > visuals_.size()) ?
191  visuals_.size() : new_visuals.size();
192  for( size_t i = 0; i < copy_len; i++ )
193  {
194  int new_index = (messages_received_ - i) % new_visuals.size();
195  int old_index = (messages_received_ - i) % visuals_.size();
196  new_visuals[ new_index ] = visuals_[ old_index ];
197  visuals_[ old_index ] = NULL;
198  }
199 
200  // Delete any remaining old visuals
201  for( size_t i = 0; i < visuals_.size(); i++ ) {
202  delete visuals_[ i ];
203  }
204 
205  // We don't need to create any new visuals here, they are created as
206  // needed when messages are received.
207 
208  // Put the new vector into the member variable version and let the
209  // old one go out of scope.
210  visuals_.swap( new_visuals );
211  }/*}}}*/
212 
214  {
215  // If we are not actually enabled, don't do it.
216  if ( !isEnabled() )
217  {
218  return;
219  }
220 
221  // Try to subscribe to the current topic name (in ``topic_``). Make
222  // sure to catch exceptions and set the status to a descriptive
223  // error message.
224  try
225  {
227  setStatus( rviz::status_levels::Ok, "Topic", "OK" );
228  }
229  catch( ros::Exception& e )
230  {
231  setStatus( rviz::status_levels::Error, "Topic",
232  std::string( "Error subscribing: " ) + e.what() );
233  }
234  }/*}}}*/
235 
237  {
238  sub_.unsubscribe();
239  }/*}}}*/
240 
242  {
243  subscribe();
244  }/*}}}*/
245 
247  {
248  unsubscribe();
249  clear();
250  }/*}}}*/
251 
252  // When the "Fixed Frame" changes, we need to update our
253  // tf::MessageFilter and erase existing visuals.
255  {
257  clear();
258  }/*}}}*/
259 
260  // This is our callback to handle an incoming message.
261  void AmbientSoundDisplay::incomingMessage( const jsk_hark_msgs::HarkPower::ConstPtr& msg )/*{{{*/
262  {
264 
265  // Each display can have multiple status lines. This one is called
266  // "Topic" and says how many messages have been received in this case.
267  std::stringstream ss;
268  ss << messages_received_ << " messages received";
269  setStatus( rviz::status_levels::Ok, "Topic", ss.str() );
270 
271  // Here we call the rviz::FrameManager to get the transform from the
272  // fixed frame to the frame in the header of this Imu message. If
273  // it fails, we can't do anything else so we return.
274  Ogre::Quaternion orientation;
275  Ogre::Vector3 position;
276  if( !vis_manager_->getFrameManager()->getTransform( msg->header.frame_id,
277  msg->header.stamp,
278  position, orientation ))
279  {
280  ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'",
281  msg->header.frame_id.c_str(), fixed_frame_.c_str() );
282  return;
283  }
284 
285  // We are keeping a circular buffer of visual pointers. This gets
286  // the next one, or creates and stores it if it was missing.
288  if( visual == NULL )
289  {
290  visual = new AmbientSoundVisual( vis_manager_->getSceneManager(), scene_node_ );
292  }
293 
294  // Now set or update the contents of the chosen visual.
295  visual->setFramePosition( position );
296  visual->setFrameOrientation( orientation );
297  visual->setColor( color_.r_, color_.g_, color_.b_, alpha_ );
298  visual->setWidth( width_ );
299  visual->setScale( scale_ );
300  visual->setBias ( bias_ );
301  visual->setGrad ( grad_ );
302  visual->setMessage( msg );
303  }/*}}}*/
304 
305  // Override rviz::Display's reset() function to add a call to clear().
307  {
308  Display::reset();
309  clear();
310  }/*}}}*/
311 
312  // Override createProperties() to build and configure a Property
313  // object for each user-editable property. ``property_manager_``,
314  // ``property_prefix_``, and ``parent_category_`` are all initialized before
315  // this is called.
317  {
319  property_manager_->createProperty<rviz::ROSTopicStringProperty>( "Topic",
320  property_prefix_,
321  boost::bind( &AmbientSoundDisplay::getTopic, this ),
322  boost::bind( &AmbientSoundDisplay::setTopic, this, _1 ),
323  parent_category_,
324  this );
325  setPropertyHelpText( topic_property_, "jsk_hark_msgs::HarkPower topic to subscribe to." );
326  rviz::ROSTopicStringPropertyPtr topic_prop = topic_property_.lock();
327  topic_prop->setMessageType( ros::message_traits::datatype<jsk_hark_msgs::HarkPower>() );
328 
330  property_manager_->createProperty<rviz::ColorProperty>( "Color",
331  property_prefix_,
332  boost::bind( &AmbientSoundDisplay::getColor, this ),
333  boost::bind( &AmbientSoundDisplay::setColor, this, _1 ),
334  parent_category_,
335  this );
336  setPropertyHelpText( color_property_, "Color to draw the acceleration arrows." );
337 
339  property_manager_->createProperty<rviz::FloatProperty>( "Alpha",
340  property_prefix_,
341  boost::bind( &AmbientSoundDisplay::getAlpha, this ),
342  boost::bind( &AmbientSoundDisplay::setAlpha, this, _1 ),
343  parent_category_,
344  this );
345  setPropertyHelpText( alpha_property_, "0 is fully transparent, 1.0 is fully opaque." );
346 
348  property_manager_->createProperty<rviz::IntProperty>( "History Length",
349  property_prefix_,
350  boost::bind( &AmbientSoundDisplay::getHistoryLength, this ),
351  boost::bind( &AmbientSoundDisplay::setHistoryLength, this, _1 ),
352  parent_category_,
353  this );
354  setPropertyHelpText( history_length_property_, "Number of prior measurements to display." );
355 
356  width_property_ =
357  property_manager_->createProperty<rviz::FloatProperty>( "Width",
358  property_prefix_,
359  boost::bind( &AmbientSoundDisplay::getWidth, this ),
360  boost::bind( &AmbientSoundDisplay::setWidth, this, _1 ),
361  parent_category_,
362  this );
363  setPropertyHelpText( width_property_, "Width of line" );
364 
365  scale_property_ =
366  property_manager_->createProperty<rviz::FloatProperty>( "Scale",
367  property_prefix_,
368  boost::bind( &AmbientSoundDisplay::getScale, this ),
369  boost::bind( &AmbientSoundDisplay::setScale, this, _1 ),
370  parent_category_,
371  this );
372  setPropertyHelpText( scale_property_, "Scale of line" );
373 
374  grad_property_ =
375  property_manager_->createProperty<rviz::FloatProperty>( "Grad",
376  property_prefix_,
377  boost::bind( &AmbientSoundDisplay::getGrad, this ),
378  boost::bind( &AmbientSoundDisplay::setGrad, this, _1 ),
379  parent_category_,
380  this );
381  setPropertyHelpText( grad_property_, "Graduation" );
382 
383  bias_property_ =
384  property_manager_->createProperty<rviz::FloatProperty>( "Bias",
385  property_prefix_,
386  boost::bind( &AmbientSoundDisplay::getBias, this ),
387  boost::bind( &AmbientSoundDisplay::setBias, this, _1 ),
388  parent_category_,
389  this );
390  setPropertyHelpText( bias_property_, "Bias to subtract" );
391  }/*}}}*/
392 
393 } // end namespace jsk_rviz_plugin
394 
395 // Tell pluginlib about this class. It is important to do this in
396 // global scope, outside our package's namespace.
399 
jsk_rviz_plugins::AmbientSoundDisplay::updateColorAndAlpha
void updateColorAndAlpha()
Definition: ambient_sound_display.cpp:155
jsk_rviz_plugins::AmbientSoundDisplay::setColor
void setColor(const rviz::Color &color)
Definition: ambient_sound_display.cpp:99
rviz::Display::isEnabled
bool isEnabled() const
jsk_rviz_plugins::AmbientSoundDisplay::setAlpha
void setAlpha(float alpha)
Definition: ambient_sound_display.cpp:108
jsk_rviz_plugins::AmbientSoundDisplay::onDisable
virtual void onDisable()
Definition: ambient_sound_display.cpp:246
jsk_rviz_plugins::AmbientSoundDisplay::unsubscribe
void unsubscribe()
Definition: ambient_sound_display.cpp:236
msg
msg
NULL
#define NULL
contact_state_marker.scale
scale
Definition: contact_state_marker.py:91
jsk_rviz_plugins::AmbientSoundVisual::setBias
void setBias(float b)
Definition: ambient_sound_visual.cpp:114
jsk_rviz_plugins::AmbientSoundDisplay::onEnable
virtual void onEnable()
Definition: ambient_sound_display.cpp:241
jsk_rviz_plugins::AmbientSoundDisplay::createProperties
virtual void createProperties()
Definition: ambient_sound_display.cpp:316
jsk_rviz_plugins::AmbientSoundDisplay::getWidth
float getWidth() const
Definition: ambient_sound_display.h:49
jsk_rviz_plugins::AmbientSoundDisplay::alpha_property_
rviz::FloatPropertyWPtr alpha_property_
Definition: ambient_sound_display.h:107
jsk_rviz_plugins::AmbientSoundDisplay::onInitialize
virtual void onInitialize()
Definition: ambient_sound_display.cpp:35
jsk_rviz_plugins::AmbientSoundDisplay::width_
float width_
Definition: ambient_sound_display.h:102
jsk_rviz_plugins::AmbientSoundDisplay::subscribe
void subscribe()
Definition: ambient_sound_display.cpp:213
jsk_rviz_plugins::AmbientSoundDisplay::sub_
message_filters::Subscriber< jsk_hark_msgs::HarkPower > sub_
Definition: ambient_sound_display.h:93
jsk_rviz_plugins::AmbientSoundVisual::setColor
void setColor(float r, float g, float b, float a)
Definition: ambient_sound_visual.cpp:100
frame_manager.h
jsk_rviz_plugins::AmbientSoundDisplay::messages_received_
int messages_received_
Definition: ambient_sound_display.h:95
tf::MessageFilter::connectInput
void connectInput(F &f)
jsk_rviz_plugins::AmbientSoundDisplay::color_property_
rviz::ColorPropertyWPtr color_property_
Definition: ambient_sound_display.h:105
jsk_rviz_plugins::AmbientSoundDisplay::history_length_property_
rviz::IntPropertyWPtr history_length_property_
Definition: ambient_sound_display.h:108
jsk_rviz_plugins::AmbientSoundDisplay::getScale
float getScale() const
Definition: ambient_sound_display.h:52
jsk_rviz_plugins::AmbientSoundDisplay::fixedFrameChanged
virtual void fixedFrameChanged()
Definition: ambient_sound_display.cpp:254
ros::Exception
rviz::Display::fixed_frame_
QString fixed_frame_
jsk_rviz_plugins::AmbientSoundDisplay::scale_
float scale_
Definition: ambient_sound_display.h:102
jsk_rviz_plugins::AmbientSoundDisplay::bias_property_
rviz::FloatPropertyWPtr bias_property_
Definition: ambient_sound_display.h:111
jsk_rviz_plugins::AmbientSoundDisplay::setTopic
void setTopic(const std::string &topic)
Definition: ambient_sound_display.cpp:85
jsk_rviz_plugins::AmbientSoundDisplay::getBias
float getBias() const
Definition: ambient_sound_display.h:55
message_filters::Subscriber::unsubscribe
void unsubscribe()
jsk_rviz_plugins::AmbientSoundDisplay::grad_
float grad_
Definition: ambient_sound_display.h:102
jsk_rviz_plugins::AmbientSoundDisplay::topic_property_
rviz::ROSTopicStringPropertyWPtr topic_property_
Definition: ambient_sound_display.h:106
rviz::ColorProperty
visualization_manager.h
jsk_rviz_plugins::AmbientSoundDisplay::setGrad
void setGrad(float grad)
Definition: ambient_sound_display.cpp:145
rviz::Display
jsk_rviz_plugins::AmbientSoundDisplay::getGrad
float getGrad() const
Definition: ambient_sound_display.h:58
rviz::FloatProperty
class_list_macros.h
jsk_rviz_plugins::AmbientSoundDisplay::setWidth
void setWidth(float width)
Definition: ambient_sound_display.cpp:117
jsk_rviz_plugins::AmbientSoundDisplay::alpha_
float alpha_
Definition: ambient_sound_display.h:100
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
jsk_rviz_plugins::AmbientSoundDisplay::clear
void clear()
Definition: ambient_sound_display.cpp:73
jsk_rviz_plugins::AmbientSoundDisplay::color_
rviz::Color color_
Definition: ambient_sound_display.h:98
jsk_rviz_plugins::AmbientSoundDisplay::visuals_
std::vector< AmbientSoundVisual * > visuals_
Definition: ambient_sound_display.h:87
jsk_rviz_plugins::AmbientSoundDisplay
Definition: ambient_sound_display.h:21
jsk_rviz_plugins::AmbientSoundDisplay::getAlpha
float getAlpha()
Definition: ambient_sound_display.h:43
jsk_rviz_plugins::AmbientSoundDisplay::scene_node_
Ogre::SceneNode * scene_node_
Definition: ambient_sound_display.h:90
jsk_rviz_plugins::AmbientSoundDisplay::incomingMessage
void incomingMessage(const jsk_hark_msgs::HarkPower::ConstPtr &msg)
Definition: ambient_sound_display.cpp:261
jsk_rviz_plugins::AmbientSoundVisual::setMessage
void setMessage(const jsk_hark_msgs::HarkPower::ConstPtr &msg)
Definition: ambient_sound_visual.cpp:47
ROS_DEBUG
#define ROS_DEBUG(...)
rviz::Color::b_
float b_
jsk_rviz_plugins::AmbientSoundDisplay::scale_property_
rviz::FloatPropertyWPtr scale_property_
Definition: ambient_sound_display.h:110
ambient_sound_visual.h
jsk_rviz_plugins::AmbientSoundDisplay::tf_filter_
tf::MessageFilter< jsk_hark_msgs::HarkPower > * tf_filter_
Definition: ambient_sound_display.h:94
jsk_rviz_plugins::AmbientSoundDisplay::setHistoryLength
void setHistoryLength(int history_length)
Definition: ambient_sound_display.cpp:167
jsk_rviz_plugins::AmbientSoundDisplay::bias_
float bias_
Definition: ambient_sound_display.h:102
jsk_rviz_plugins::AmbientSoundVisual::setGrad
void setGrad(float g)
Definition: ambient_sound_visual.cpp:118
jsk_rviz_plugins::AmbientSoundDisplay::width_property_
rviz::FloatPropertyWPtr width_property_
Definition: ambient_sound_display.h:109
rviz::Display::scene_manager_
Ogre::SceneManager * scene_manager_
rviz::Color::r_
float r_
_1
boost::arg< 1 > _1
jsk_rviz_plugins::AmbientSoundVisual
Definition: ambient_sound_visual.h:32
contact_state_marker.alpha
alpha
Definition: contact_state_marker.py:90
jsk_rviz_plugins::AmbientSoundVisual::setWidth
void setWidth(float w)
Definition: ambient_sound_visual.cpp:106
jsk_rviz_plugins::AmbientSoundVisual::setFramePosition
void setFramePosition(const Ogre::Vector3 &position)
Definition: ambient_sound_visual.cpp:83
jsk_rviz_plugins::AmbientSoundDisplay::history_length_
int history_length_
Definition: ambient_sound_display.h:101
width
width
message_filters::Subscriber::subscribe
void subscribe()
transform_listener.h
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_rviz_plugins::PictogramArrayDisplay, rviz::Display)
jsk_rviz_plugins::AmbientSoundDisplay::reset
virtual void reset()
Definition: ambient_sound_display.cpp:306
jsk_rviz_plugins::AmbientSoundDisplay::~AmbientSoundDisplay
virtual ~AmbientSoundDisplay()
Definition: ambient_sound_display.cpp:60
rviz::Color::g_
float g_
jsk_rviz_plugins::AmbientSoundVisual::setScale
void setScale(float s)
Definition: ambient_sound_visual.cpp:110
tf::MessageFilter::setTargetFrame
void setTargetFrame(const std::string &target_frame)
tf::MessageFilter< jsk_hark_msgs::HarkPower >
length
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
property.h
jsk_rviz_plugins::AmbientSoundDisplay::setScale
void setScale(float scale)
Definition: ambient_sound_display.cpp:126
jsk_rviz_plugins::AmbientSoundDisplay::AmbientSoundDisplay
AmbientSoundDisplay()
Definition: ambient_sound_display.cpp:21
jsk_rviz_plugins::AmbientSoundDisplay::topic_
std::string topic_
Definition: ambient_sound_display.h:99
rviz::Display::reset
virtual void reset()
tf::MessageFilter::clear
void clear()
jsk_rviz_plugins::AmbientSoundDisplay::getColor
const rviz::Color & getColor()
Definition: ambient_sound_display.h:40
jsk_rviz_plugins::AmbientSoundDisplay::getHistoryLength
int getHistoryLength() const
Definition: ambient_sound_display.h:46
jsk_rviz_plugins::AmbientSoundDisplay::grad_property_
rviz::FloatPropertyWPtr grad_property_
Definition: ambient_sound_display.h:112
jsk_rviz_plugins
Definition: __init__.py:1
ambient_sound_display.h
rviz::Color
jsk_rviz_plugins::AmbientSoundDisplay::setBias
void setBias(float bias)
Definition: ambient_sound_display.cpp:136
rviz::Display::update_nh_
ros::NodeHandle update_nh_
jsk_rviz_plugins::AmbientSoundVisual::setFrameOrientation
void setFrameOrientation(const Ogre::Quaternion &orientation)
Definition: ambient_sound_visual.cpp:90
jsk_rviz_plugins::AmbientSoundDisplay::getTopic
const std::string & getTopic()
Definition: ambient_sound_display.h:37
rviz::IntProperty


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Fri Dec 13 2024 03:49:56