marker_visualization.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2011, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Sebastian Klose
00036  *********************************************************************/
00037 
00038 #include <imu_filter/marker_visualization.h>
00039 
00040 namespace imu_filter {
00041 
00042         MarkerVisualization::MarkerVisualization( size_t bufferSize, const std::string & imuFrameId ) : 
00043                 maxMarkers_( bufferSize ),
00044                 imuFrameId_( imuFrameId ),
00045                 nextId_( 0 )
00046         {
00047                 // create publisher for the markers
00048                 ros::NodeHandle n;
00049                 publisher_ = n.advertise<visualization_msgs::Marker>( "imutest_markers", 10 );
00050 
00051                 gravityArrow_.header.frame_id = imuFrameId_;
00052                 gravityArrow_.header.stamp = ros::Time::now();
00053                 gravityArrow_.ns = "imu_arrows";
00054                 gravityArrow_.action = visualization_msgs::Marker::ADD;
00055                 gravityArrow_.type = visualization_msgs::Marker::ARROW;
00056                 gravityArrow_.id = 0;
00057                 gravityArrow_.color.a = 0.7;
00058                 gravityArrow_.color.r = 0.0;
00059                 gravityArrow_.color.g = 0.9;
00060                 gravityArrow_.color.b = 0.1;
00061                 gravityArrow_.points.resize( 2 );
00062                 gravityArrow_.points[ 0 ].x = 0.0; gravityArrow_.points[ 0 ].y = 0.0; gravityArrow_.points[ 0 ].z = 0.0;
00063                 gravityArrow_.points[ 1 ].x = 0.0; gravityArrow_.points[ 1 ].y = 0.0; gravityArrow_.points[ 1 ].z = 0.05*EARTH_GRAVITY;
00064                 gravityArrow_.scale.x = 0.02;
00065                 gravityArrow_.scale.y = 0.04;
00066 
00067                 velocityArrow_.header.frame_id = imuFrameId_;
00068                 velocityArrow_.header.stamp = ros::Time::now();
00069                 velocityArrow_.ns = "imu_arrows";
00070                 velocityArrow_.action = visualization_msgs::Marker::ADD;
00071                 velocityArrow_.type = visualization_msgs::Marker::ARROW;
00072                 velocityArrow_.id = 1;
00073                 velocityArrow_.color.a = 0.7;
00074                 velocityArrow_.color.r = 0.0;
00075                 velocityArrow_.color.g = 0.1;
00076                 velocityArrow_.color.b = 0.9;
00077                 velocityArrow_.points.resize( 2 );
00078                 velocityArrow_.points[ 0 ].x = 0.0; velocityArrow_.points[ 0 ].y = 0.0; velocityArrow_.points[ 0 ].z = 0.0;
00079                 velocityArrow_.points[ 1 ].x = 0.0; velocityArrow_.points[ 1 ].y = 0.0; velocityArrow_.points[ 1 ].z = 0.0;
00080                 velocityArrow_.scale.x = 0.02;
00081                 velocityArrow_.scale.y = 0.04;
00082 
00083                 accArrow_.header.frame_id = imuFrameId_;
00084                 accArrow_.header.stamp = ros::Time::now();
00085                 accArrow_.ns = "imu_arrows";
00086                 accArrow_.action = visualization_msgs::Marker::ADD;
00087                 accArrow_.type = visualization_msgs::Marker::ARROW;
00088                 accArrow_.id = 2;
00089                 accArrow_.color.a = 0.9;
00090                 accArrow_.color.r = 0.9;
00091                 accArrow_.color.g = 0.0;
00092                 accArrow_.color.b = 0.0;
00093                 accArrow_.points.resize( 2 );
00094                 accArrow_.points[ 0 ].x = 0.0; velocityArrow_.points[ 0 ].y = 0.0; velocityArrow_.points[ 0 ].z = 0.0;
00095                 accArrow_.points[ 1 ].x = 0.0; velocityArrow_.points[ 1 ].y = 0.0; velocityArrow_.points[ 1 ].z = 0.0;
00096                 accArrow_.scale.x = 0.02;
00097                 accArrow_.scale.y = 0.04;
00098 
00099                 measAcc_.header.frame_id = imuFrameId_;
00100                 measAcc_.header.stamp = ros::Time::now();
00101                 measAcc_.ns = "imu_arrows";
00102                 measAcc_.action = visualization_msgs::Marker::ADD;
00103                 measAcc_.type = visualization_msgs::Marker::ARROW;
00104                 measAcc_.id = 3;
00105                 measAcc_.color.a = 0.9;
00106                 measAcc_.color.r = 0.9;
00107                 measAcc_.color.g = 0.0;
00108                 measAcc_.color.b = 0.9;
00109                 measAcc_.points.resize( 2 );
00110                 measAcc_.points[ 0 ].x = 0.0; velocityArrow_.points[ 0 ].y = 0.0; velocityArrow_.points[ 0 ].z = 0.0;
00111                 measAcc_.points[ 1 ].x = 0.0; velocityArrow_.points[ 1 ].y = 0.0; velocityArrow_.points[ 1 ].z = 0.0;
00112                 measAcc_.scale.x = 0.02;
00113                 measAcc_.scale.y = 0.04;
00114         }
00115 
00116         MarkerVisualization::~MarkerVisualization()
00117         {
00118         }
00119 
00120         void MarkerVisualization::addImuMarker( const ImuState & state, const Eigen::Vector3d & acceleration )
00121         {
00122                 marker_.push_back( visualization_msgs::Marker() );
00123 
00124                 visualization_msgs::Marker & m = marker_.back();
00125 
00126                 m.header.frame_id = imuFrameId_;
00127                 m.header.stamp = ros::Time::now();
00128 
00129                 m.ns = "imu_markers";
00130                 m.action = visualization_msgs::Marker::ADD;
00131                 m.type = visualization_msgs::Marker::SPHERE;
00132                 m.id = nextId();
00133                 m.color.a = 0.5;
00134                 m.color.r = 0.7;
00135                 m.color.g = 0.3;
00136                 m.color.b = 0.1;
00137 
00138                 this->updateMarkerPose( m, state );
00139 
00140                 //      dump( m );
00141 
00142                 if( maxMarkers_ && marker_.size() > maxMarkers_ ){
00143                         marker_.erase( marker_.begin() );
00144                 }
00145         }
00146 
00147         void MarkerVisualization::publish() const
00148         {
00149                 Container::const_iterator it = marker_.begin(); 
00150                 Container::const_iterator end = marker_.end();
00151 
00152                 while( it != end ){
00153                         publisher_.publish( *it );
00154                         it++;
00155                 }
00156 
00157                 publisher_.publish( gravityArrow_ );
00158                 publisher_.publish( velocityArrow_ );
00159                 publisher_.publish( accArrow_ );
00160                 publisher_.publish( measAcc_ );
00161         }
00162 
00163 
00164         void MarkerVisualization::updateMarkerPose( visualization_msgs::Marker & m,
00165                                                                                            const ImuState & state )
00166 
00167         {
00168                 Eigen::Quaterniond q = state.orientation();
00169                 m.pose.orientation.w = q.w();
00170                 m.pose.orientation.x = q.x();
00171                 m.pose.orientation.y = q.y();
00172                 m.pose.orientation.z = q.z();
00173 
00174                 m.pose.position.x = state.position().x();
00175                 m.pose.position.y = state.position().y();
00176                 m.pose.position.z = state.position().z();
00177 
00178                 m.scale.x = sqrt(state.covariance()( 4, 4 ));
00179                 m.scale.y = sqrt(state.covariance()( 5, 5 ));
00180                 m.scale.z = sqrt(state.covariance()( 6, 6 ));
00181                 
00182 
00183                 // update the velocity vector
00184                 Eigen::Vector3d velocity = state.velocity();
00185                 velocity += state.position();
00186                 velocityArrow_.points[ 0 ].x = state.position().x();
00187                 velocityArrow_.points[ 0 ].y = state.position().y();
00188                 velocityArrow_.points[ 0 ].z = state.position().z();
00189                 velocityArrow_.points[ 1 ].x = velocity.x();
00190                 velocityArrow_.points[ 1 ].y = velocity.y();
00191                 velocityArrow_.points[ 1 ].z = velocity.z();
00192 
00193                 gravityArrow_.points[ 0 ].x = state.position().x();
00194                 gravityArrow_.points[ 0 ].y = state.position().y();
00195                 gravityArrow_.points[ 0 ].z = state.position().z();
00196                 gravityArrow_.points[ 1 ].x = state.position().x() + 0.1 * state.gravity().x();
00197                 gravityArrow_.points[ 1 ].y = state.position().y() + 0.1 * state.gravity().y();
00198                 gravityArrow_.points[ 1 ].z = state.position().z() + 0.1 * state.gravity().z();
00199         }
00200 
00201         void MarkerVisualization::dump( visualization_msgs::Marker & m )
00202         {
00203                 std::cout << m << std::endl;
00204         }
00205 
00206         void MarkerVisualization::setAcceleration( const Eigen::Vector3d & acc )
00207         {
00208                 accArrow_.points[ 1 ].x = 0.1 * acc.x();
00209                 accArrow_.points[ 1 ].y = 0.1 * acc.y();
00210                 accArrow_.points[ 1 ].z = 0.1 * acc.z();
00211         }
00212 
00213         void MarkerVisualization::setMeasuredAcc( const Eigen::Vector3d & acc )
00214         {
00215                 measAcc_.points[ 1 ].x =0.1 * acc.x();
00216                 measAcc_.points[ 1 ].y =0.1 * acc.y();
00217                 measAcc_.points[ 1 ].z =0.1 * acc.z();
00218         }
00219 
00220         int MarkerVisualization::nextId()
00221         {
00222                 nextId_++;
00223                 if( maxMarkers_ ){
00224                         nextId_ %= maxMarkers_;
00225                         nextId_++;
00226                 }
00227                 return nextId_;
00228         }
00229 
00230 }


imu_filter
Author(s): Sebastian Klose
autogenerated on Thu Dec 12 2013 11:24:42