covariance_markers_alg.cpp
Go to the documentation of this file.
00001 #include "covariance_markers_alg.h"
00002 
00003 CovarianceMarkersAlgorithm::CovarianceMarkersAlgorithm():
00004 frame_id_("/teo/odom"),
00005 namespace_("/teo"),
00006 counter_(0),
00007 scale_multiplier_(2.0),
00008 scale_multiplier_z_(0.01),
00009 color_alpha_(0.5),
00010 color_red_(0.45),
00011 color_green_(0.95),
00012 color_blue_ (0.65)
00013 {
00014 }
00015 
00016 CovarianceMarkersAlgorithm::~CovarianceMarkersAlgorithm()
00017 {
00018 }
00019 
00020 void CovarianceMarkersAlgorithm::config_update(const Config& new_cfg, uint32_t level)
00021 {
00022   this->lock();
00023 
00024   // save the current configuration
00025   config_ = new_cfg;
00026   
00027   frame_id_  = config_.frame_id;
00028   namespace_ = config_.ns;
00029   scale_multiplier_   = config_.scale_multiplier;
00030   scale_multiplier_z_ = config_.scale_multiplier_z;
00031   color_alpha_ = config_.color_alpha;
00032   color_red_   = config_.color_red;
00033   color_green_ = config_.color_green;
00034   color_blue_  = config_.color_blue;
00035 
00036   this->unlock();
00037 }
00038 
00039 // CovarianceMarkersAlgorithm Public API
00040 void CovarianceMarkersAlgorithm::drawCovariance()
00041 {
00042   current_marker_.header.stamp = ros::Time::now();
00043   current_marker_.header.frame_id = frame_id_;
00044   current_marker_.header.seq = counter_++;
00045   current_marker_.ns = namespace_;
00046   current_marker_.id = counter_;
00047   current_marker_.type = visualization_msgs::Marker::SPHERE;
00048   current_marker_.action = visualization_msgs::Marker::ADD;
00049 
00050   current_marker_.pose.position.x = odom_.pose.pose.position.x;
00051   current_marker_.pose.position.y = odom_.pose.pose.position.y;
00052   current_marker_.pose.position.z = odom_.pose.pose.position.z;
00053 
00054   ROS_DEBUG("CovarianceMarkersAlgorithm::drawCovariance x: %f y: %f z: %f",
00055                                                     odom_.pose.pose.position.x,
00056                                                     odom_.pose.pose.position.y,
00057                                                     odom_.pose.pose.position.z);
00058 
00059   Eigen::Matrix3f covs;
00060   covs(0) = odom_.pose.covariance[0]; // xx
00061   covs(1) = odom_.pose.covariance[1]; // xy
00062   covs(2) = odom_.pose.covariance[2]; // xz
00063   covs(3) = odom_.pose.covariance[6]; // yy
00064   covs(4) = odom_.pose.covariance[7]; // yx
00065   covs(5) = odom_.pose.covariance[8]; // yz
00066   covs(6) = odom_.pose.covariance[12]; // zx
00067   covs(7) = odom_.pose.covariance[13]; // zy
00068   covs(8) = odom_.pose.covariance[14]; // zz
00069 
00070   ROS_DEBUG("CovarianceMarkersAlgorithm::drawCovariance cov: %f %f %f %f",
00071                                               covs(0),covs(1),covs(2),covs(3));
00072 
00073   Eigen::SelfAdjointEigenSolver<Eigen::Matrix3f> eig(covs);
00074 
00075   const Eigen::Vector3f& eigValues  (eig.eigenvalues());
00076   const Eigen::Matrix3f& eigVectors (eig.eigenvectors());
00077 
00078   // de moment nomes considerem rotacio en z
00079   float angle = (atan2(eigVectors(1, 0), eigVectors(0, 0)));
00080 
00081   double length_x = sqrt(eigValues[0]);
00082   double length_y = sqrt(eigValues[1]);
00083   double length_z = sqrt(eigValues[2]);
00084 
00085   ROS_DEBUG("CovarianceMarkersAlgorithm::drawCovariance lengths: %f %f %f",
00086                                                    length_x,length_y,length_z);
00087 
00088   current_marker_.scale.x = length_x*scale_multiplier_;
00089   current_marker_.scale.y = length_y*scale_multiplier_;
00090   current_marker_.scale.z = length_z*scale_multiplier_z_;
00091 
00092   current_marker_.color.a = color_alpha_;
00093   current_marker_.color.r = color_red_;
00094   current_marker_.color.g = color_green_;
00095   current_marker_.color.b = color_blue_;
00096 
00097   current_marker_.pose.orientation.w = cos(angle*0.5);
00098   current_marker_.pose.orientation.z = sin(angle*0.5);
00099 
00100   marker_array_.markers.push_back(current_marker_);
00101 }
00102 
00103 visualization_msgs::MarkerArray CovarianceMarkersAlgorithm::marker_array()
00104 {
00105   return marker_array_;
00106 }
00107 
00108 // http://geus.wordpress.com/2011/09/15/how-to-represent-a-3d-normal-function-with-ros-rviz/
00109 
00110 
00111   // #getting the distribution eigen vectors and values
00112   // (eigValues,eigVectors) = numpy.linalg.eig (covMat)
00113 
00114   // #painting the eigen vectors
00115   // id=1
00116   // for v in eigVectors:
00117   //   m=markerVector(id, v*eigValues[id-1], mean)
00118   //   id=id+1
00119   //   points_pub.publish(m)
00120 
00121   // #building the rotation matrix
00122   // eigx_n=PyKDL.Vector(eigVectors[0,0],eigVectors[0,1],eigVectors[0,2])
00123   // eigy_n=-PyKDL.Vector(eigVectors[1,0],eigVectors[1,1],eigVectors[1,2])
00124   // eigz_n=PyKDL.Vector(eigVectors[2,0],eigVectors[2,1],eigVectors[2,2])
00125   // eigx_n.Normalize()
00126   // eigy_n.Normalize()
00127   // eigz_n.Normalize()
00128   //   rot = PyKDL.Rotation (eigx_n,eigy_n,eigz_n)
00129   // quat = rot.GetQuaternion ()
00130 
00131 
00132 
00133 // https://code.ros.org/trac/ros-pkg/ticket/4857
00134 
00135 
00136   // void drawCovariance(const Eigen::Vector2f& mean, const Eigen::Matrix2f& covMatrix)
00137   //   {
00138   //     tempMarker.pose.position.x = mean[0];
00139   //     tempMarker.pose.position.y = mean[1];
00140 
00141   //     Eigen::SelfAdjointEigenSolver<Eigen::Matrix2f> eig(covMatrix);
00142 
00143   //     const Eigen::Vector2f& eigValues (eig.eigenvalues());
00144   //     const Eigen::Matrix2f& eigVectors (eig.eigenvectors());
00145 
00146   //     float angle = (atan2(eigVectors(1, 0), eigVectors(0, 0)));
00147 
00148   //     tempMarker.type = visualization_msgs::Marker::CYLINDER;
00149 
00150   //     double lengthMajor = sqrt(eigValues[0]);
00151   //     double lengthMinor = sqrt(eigValues[1]);
00152 
00153   //     tempMarker.scale.x = lengthMajor;
00154   //     tempMarker.scale.y = lengthMinor;
00155   //     tempMarker.scale.z = 0.001;
00156 
00157   //     tempMarker.pose.orientation.w = cos(angle*0.5);
00158   //     tempMarker.pose.orientation.z = sin(angle*0.5);
00159 
00160   //     tempMarker.id = idCounter++;
00161   //     markerArray.markers.push_back(tempMarker);
00162   //   }


iri_covariance_markers
Author(s): Marti Morta, mmorta at iri.upc.edu
autogenerated on Fri Dec 6 2013 22:11:39