eband_visualization.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2010, 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: Christian Connette
00036  *********************************************************************/
00037 
00038 
00039 #include <eband_local_planner/eband_visualization.h>
00040 
00041 
00042 namespace eband_local_planner{
00043 
00044 
00045   EBandVisualization::EBandVisualization() : initialized_(false) {}
00046 
00047 
00048   EBandVisualization::EBandVisualization(ros::NodeHandle& pn, costmap_2d::Costmap2DROS* costmap_ros)
00049   {
00050     initialize(pn, costmap_ros);
00051   }
00052 
00053 
00054   EBandVisualization::~EBandVisualization() {}
00055 
00056 
00057   void EBandVisualization::initialize(ros::NodeHandle& pn, costmap_2d::Costmap2DROS* costmap_ros)
00058   {
00059     // check if visualization already initialized
00060     if(!initialized_)
00061     {
00062       // read parameters from parameter server
00063       pn.param("marker_lifetime", marker_lifetime_, 0.5);
00064 
00065       // advertise topics
00066       one_bubble_pub_ = pn.advertise<visualization_msgs::Marker>("eband_visualization", 1);
00067       // although we want to publish MarkerArrays we have to advertise Marker topic first -> rviz searchs relative to this
00068       bubble_pub_ = pn.advertise<visualization_msgs::MarkerArray>("eband_visualization_array", 1);
00069 
00070       // copy adress of costmap and Transform Listener (handed over from move_base) -> to get robot shape
00071       costmap_ros_ = costmap_ros;
00072 
00073       initialized_ = true;
00074     }
00075     else
00076     {
00077       ROS_WARN("Trying to initialize already initialized visualization, doing nothing.");
00078     }
00079   }
00080 
00081   
00082   void EBandVisualization::reconfigure(
00083     eband_local_planner::EBandPlannerConfig& config)
00084   {
00085     marker_lifetime_ = config.marker_lifetime;
00086   }
00087 
00088 
00089   void EBandVisualization::publishBand(std::string marker_name_space, std::vector<Bubble> band)
00090   {
00091     // check if visualization was initialized
00092     if(!initialized_)
00093     {
00094       ROS_ERROR("Visualization not yet initialized, please call initialize() before using visualization");
00095       return;
00096     }
00097 
00098     visualization_msgs::MarkerArray eband_msg;
00099     eband_msg.markers.resize(band.size());
00100 
00101     visualization_msgs::MarkerArray eband_heading_msg;
00102     eband_heading_msg.markers.resize(band.size());
00103     std::string marker_heading_name_space = marker_name_space;
00104     marker_heading_name_space.append("_heading");
00105 
00106     // convert elastic band to msg
00107     for(int i = 0; i < ((int) band.size()); i++)
00108     {
00109       // convert bubbles in eband to marker msg
00110       bubbleToMarker(band[i], eband_msg.markers[i], marker_name_space, i, green);
00111 
00112       // convert bubbles in eband to marker msg
00113       // bubbleHeadingToMarker(band[i], eband_heading_msg.markers[i], marker_heading_name_space, i, green);
00114     }
00115 
00116     // publish
00117     bubble_pub_.publish(eband_msg);
00118     //bubble_pub_.publish(eband_heading_msg);
00119   }
00120 
00121 
00122   void EBandVisualization::publishBubble(std::string marker_name_space, int marker_id, Bubble bubble)
00123   {
00124     // check if visualization was initialized
00125     if(!initialized_)
00126     {
00127       ROS_ERROR("Visualization not yet initialized, please call initialize() before using visualization");
00128       return;
00129     }
00130 
00131     visualization_msgs::Marker bubble_msg;
00132 
00133     // convert bubble to marker msg
00134     bubbleToMarker(bubble, bubble_msg, marker_name_space, marker_id, green);
00135 
00136     // publish
00137     one_bubble_pub_.publish(bubble_msg);
00138   }
00139 
00140 
00141   void EBandVisualization::publishBubble(std::string marker_name_space, int marker_id, Color marker_color, Bubble bubble)
00142   {
00143     // check if visualization was initialized
00144     if(!initialized_)
00145     {
00146       ROS_ERROR("Visualization not yet initialized, please call initialize() before using visualization");
00147       return;
00148     }
00149 
00150     visualization_msgs::Marker bubble_msg;
00151 
00152     // convert bubble to marker msg
00153     bubbleToMarker(bubble, bubble_msg, marker_name_space, marker_id, marker_color);
00154 
00155     // publish
00156     one_bubble_pub_.publish(bubble_msg);
00157   }
00158 
00159 
00160   void EBandVisualization::publishForceList(std::string marker_name_space, std::vector<geometry_msgs::WrenchStamped> forces, std::vector<Bubble> band)
00161   {
00162     // check if visualization was initialized
00163     if(!initialized_)
00164     {
00165       ROS_ERROR("Visualization not yet initialized, please call initialize() before using visualization");
00166       return;
00167     }
00168 
00169     visualization_msgs::MarkerArray forces_msg;
00170     forces_msg.markers.resize(forces.size());
00171 
00172     //before converting to msg - check whether internal, external or resulting forces - switch color
00173     Color marker_color = green;
00174     if(marker_name_space.compare("internal_forces") == 0)
00175       marker_color = blue;
00176 
00177     if(marker_name_space.compare("external_forces") == 0)
00178       marker_color = red;
00179 
00180     if(marker_name_space.compare("resulting_forces") == 0)
00181       marker_color = green;
00182 
00183     for(int i = 0; i < ((int) forces.size()); i++)
00184     {
00185       // convert wrenches in force list into marker msg
00186       forceToMarker(forces[i], band[i].center.pose, forces_msg.markers[i], marker_name_space, i, marker_color);
00187     }
00188 
00189     // publish
00190     bubble_pub_.publish(forces_msg);
00191   }
00192 
00193   void EBandVisualization::publishForce(std::string marker_name_space, int id, Color marker_color, geometry_msgs::WrenchStamped force, Bubble bubble)
00194   {
00195     // check if visualization was initialized
00196     if(!initialized_)
00197     {
00198       ROS_ERROR("Visualization not yet initialized, please call initialize() before using visualization");
00199       return;
00200     }
00201 
00202     visualization_msgs::Marker force_msg;
00203 
00204     // convert wrenches in force list into marker msg
00205     forceToMarker(force, bubble.center.pose, force_msg, marker_name_space, id, marker_color);
00206 
00207     // publish
00208     one_bubble_pub_.publish(force_msg);
00209   }
00210 
00211 
00212   void EBandVisualization::bubbleToMarker(Bubble bubble, visualization_msgs::Marker& marker, std::string marker_name_space, int marker_id, Color marker_color)
00213   {
00214     geometry_msgs::Pose2D tmp_pose2d;
00215 
00216     // header
00217     marker.header.stamp = ros::Time::now();
00218     marker.header.frame_id = bubble.center.header.frame_id;
00219 
00220     // identifier and cmds
00221     marker.ns = marker_name_space;
00222     marker.id = marker_id;
00223     marker.type = visualization_msgs::Marker::SPHERE;
00224     marker.action = visualization_msgs::Marker::ADD;
00225 
00226     // body
00227     marker.pose = bubble.center.pose;
00228     // get theta-angle to display as elevation
00229     PoseToPose2D(bubble.center.pose, tmp_pose2d);
00230     marker.pose.position.z = 0;//tmp_pose2d.theta * getCircumscribedRadius(*costmap_ros_);
00231     // scale ~ diameter --> is 2x expansion ~ radius
00232     marker.scale.x = 2.0*bubble.expansion;
00233     marker.scale.y = 2.0*bubble.expansion;
00234     marker.scale.z = 2.0*bubble.expansion;
00235 
00236     // color (rgb)
00237     marker.color.r = 0.0f;
00238     marker.color.g = 0.0f;
00239     marker.color.b = 0.0f;
00240     switch(marker_color)
00241     {
00242       case red: { marker.color.r = 1.0f; break; }
00243       case green:       { marker.color.g = 1.0f; break; }
00244       case blue:        { marker.color.b = 1.0f; break; }
00245     }
00246     // transparency (alpha value < 1 : displays marker transparent)
00247     marker.color.a = 0.75;
00248 
00249     // lifetime of this marker
00250     marker.lifetime = ros::Duration(marker_lifetime_);
00251   }
00252 
00253 
00254   void EBandVisualization::bubbleHeadingToMarker(Bubble bubble, visualization_msgs::Marker& marker, std::string marker_name_space, int marker_id, Color marker_color)
00255   {
00256     geometry_msgs::Pose2D tmp_pose2d;
00257 
00258     // header
00259     marker.header.stamp = ros::Time::now();
00260     marker.header.frame_id = bubble.center.header.frame_id;
00261 
00262     // identifier and cmds
00263     marker.ns = marker_name_space;
00264     marker.id = marker_id;
00265     marker.type = visualization_msgs::Marker::ARROW;
00266     marker.action = visualization_msgs::Marker::ADD;
00267 
00268     // body
00269     marker.pose = bubble.center.pose;
00270     // get theta-angle to display as elevation
00271     PoseToPose2D(bubble.center.pose, tmp_pose2d);
00272     marker.pose.position.z = tmp_pose2d.theta * getCircumscribedRadius(*costmap_ros_);
00273     // scale ~ diameter --> is 2x expansion ~ radius
00274     marker.scale.x = 0.9;
00275     marker.scale.y = 0.45;
00276     marker.scale.z = 0.45;
00277 
00278     // color (rgb)
00279     marker.color.r = 0.0f;
00280     marker.color.g = 0.0f;
00281     marker.color.b = 0.0f;
00282     switch(marker_color)
00283     {
00284       case red: { marker.color.r = 1.0f; break; }
00285       case green:       { marker.color.g = 1.0f; break; }
00286       case blue:        { marker.color.b = 1.0f; break; }
00287     }
00288     // transparency (alpha value < 1 : displays marker transparent)
00289     marker.color.a = 1.0;
00290 
00291     // lifetime of this marker
00292     marker.lifetime = ros::Duration(marker_lifetime_);
00293   }
00294 
00295 
00296   void EBandVisualization::forceToMarker(geometry_msgs::WrenchStamped wrench, geometry_msgs::Pose wrench_origin, visualization_msgs::Marker& marker, std::string marker_name_space, int marker_id, Color marker_color)
00297   {
00298     geometry_msgs::Pose2D tmp_pose2d;
00299 
00300     // header
00301     marker.header.stamp = ros::Time::now();
00302     marker.header.frame_id = wrench.header.frame_id;
00303 
00304     // identifier and cmds
00305     marker.ns = marker_name_space;
00306     marker.id = marker_id;
00307     marker.type = visualization_msgs::Marker::ARROW;
00308     marker.action = visualization_msgs::Marker::ADD;
00309 
00310     // body - origin
00311     marker.pose.position = wrench_origin.position;
00312     // get theta-angle to display as elevation
00313     PoseToPose2D(wrench_origin, tmp_pose2d);
00314     marker.pose.position.z = tmp_pose2d.theta * getCircumscribedRadius(*costmap_ros_);
00315 
00316     // body - orientation of vector (calc. quaternion that transform xAxis into force-vector)
00317     // check if force different from zero
00318     if( (wrench.wrench.force.x != 0) || (wrench.wrench.force.y != 0) || (wrench.wrench.torque.z != 0) )
00319     {
00320       // find AxisAngle Representation then convert to Quaternion
00321       // (Axis = normalize(vec1) cross normalize(vec2) // cos(Angle) = normalize(vec1) dot normalize(vec2))
00322       Eigen::Vector3d x_axis(1.0, 0.0, 0.0);
00323       Eigen::Vector3d target_vec( wrench.wrench.force.x, wrench.wrench.force.y, wrench.wrench.torque.z / getCircumscribedRadius(*costmap_ros_) );
00324       Eigen::Vector3d rotation_axis(1.0, 0.0, 0.0);
00325       double rotation_angle = 0.0;
00326       // get Axis orthogonal to both vectors ()
00327       x_axis.normalize(); // unneccessary but just in case
00328       target_vec.normalize();
00329       if(!(x_axis == target_vec))
00330       {
00331         // vector not identical - cross-product defined
00332         rotation_axis = x_axis.cross(target_vec);
00333         rotation_angle = x_axis.dot(target_vec);
00334         rotation_angle = acos(rotation_angle);
00335       }
00336       // create AngleAxis representation
00337       rotation_axis.normalize(); // normalize vector -> otherwise AngleAxis will be invalid!
00338       const double s = sin(rotation_angle/2);
00339       const double c = cos(rotation_angle/2);
00340       Eigen::Quaterniond rotate_quat(c, s*rotation_axis.x(), s*rotation_axis.y(), s*rotation_axis.z());
00341 
00342       // transform quaternion back from Eigen to ROS
00343       tf::Quaternion orientation_tf;
00344       geometry_msgs::Quaternion orientation_msg;
00345       tf::quaternionEigenToTF(rotate_quat, orientation_tf);
00346       tf::quaternionTFToMsg(orientation_tf, orientation_msg);
00347 
00348       // finally set orientation of marker
00349       marker.pose.orientation = orientation_msg;
00350 
00351       // scale ~ diameter --> is 2x expansion ~ radius
00352       double scale = sqrt( (wrench.wrench.force.x * wrench.wrench.force.x) + (wrench.wrench.force.y * wrench.wrench.force.y) +
00353           ( (wrench.wrench.torque.z/getCircumscribedRadius(*costmap_ros_))*(wrench.wrench.torque.z/getCircumscribedRadius(*costmap_ros_)) ) );
00354       marker.scale.x = scale; //1.0;
00355       marker.scale.y = scale; //1.0;
00356       marker.scale.z = scale; //1.0;
00357 
00358       // color (rgb)
00359       marker.color.r = 0.0f;
00360       marker.color.g = 0.0f;
00361       marker.color.b = 0.0f;
00362       switch(marker_color)
00363       {
00364         case red:       { marker.color.r = 1.0f; break; }
00365         case green:     { marker.color.g = 1.0f; break; }
00366         case blue:      { marker.color.b = 1.0f; break; }
00367       }
00368       // transparency (alpha value < 1 : displays marker transparent)
00369       marker.color.a = 1.25;
00370     }
00371     else
00372     {
00373       // force on this bubble is zero -> make marker invisible
00374       marker.pose.orientation = wrench_origin.orientation; // just take orientation of bubble as dummy-value
00375 
00376       // scale
00377       marker.scale.x = 0.0;
00378       marker.scale.y = 0.0;
00379       marker.scale.z = 0.0;
00380 
00381       // color (rgb)
00382       marker.color.r = 0.0f;
00383       marker.color.g = 0.0f;
00384       marker.color.b = 0.0f;
00385 
00386       // transparency (alpha value < 1 : displays marker transparent)
00387       marker.color.a = 0.0;
00388     }
00389 
00390     // lifetime of this marker
00391     marker.lifetime = ros::Duration(marker_lifetime_);
00392   }
00393 
00394 };
00395 


eband_local_planner
Author(s): Christian Connette, Bhaskara Marthi, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 18:50:52