imarker.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2013, 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: Acorn Pooley
00036 *********************************************************************/
00037 
00038 // This code goes with the interactivity tutorial
00039 //    http://moveit.ros.org/wiki/index.php/Groovy/InteractiveRobot/InteractivityTutorial
00040 
00041 #include "imarker.h"
00042 #include "pose_string.h"
00043 #include <eigen_conversions/eigen_msg.h>
00044 
00045 
00046 /* default callback which just prints the current pose */
00047 void IMarker::printFeedback(
00048     const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00049 {
00050   ROS_INFO_STREAM(feedback->marker_name.c_str()
00051     << "is now at :"
00052     << PoseString(feedback->pose));
00053 }
00054 
00055 
00056 /* create 1 cylinder as part of a 3-cylinder axis (used to visualize pose
00057  * manipulation) */
00058 visualization_msgs::Marker IMarker::makeAxisCyl( IMarker::Axis axis )
00059 {
00060   visualization_msgs::Marker marker;
00061   double scale = imarker_.scale * 0.4;
00062 
00063   marker.type = visualization_msgs::Marker::CYLINDER;
00064   marker.scale.x = scale * 0.15;
00065   marker.scale.y = scale * 0.15;
00066   marker.scale.z = scale;
00067   marker.pose.orientation.w = 1.0;
00068   marker.color.r = 0.0;
00069   marker.color.g = 0.0;
00070   marker.color.b = 0.0;
00071   marker.color.a = 1.0;
00072 
00073   switch( axis )
00074   {
00075   case X:
00076     marker.pose.position.x = 0.5 * scale;
00077     marker.pose.orientation.y = 1.0;
00078     marker.color.r = 0.5;
00079     break;
00080   case Y:
00081     marker.pose.position.y = 0.5 * scale;
00082     marker.pose.orientation.x = 1.0;
00083     marker.color.g = 0.5;
00084     break;
00085   case Z:
00086   default:
00087     marker.pose.position.z = 0.5 * scale;
00088     marker.color.b = 0.5;
00089     break;
00090   }
00091 
00092   return marker;
00093 }
00094 
00095 /* create a positional control no marker */
00096 void IMarker::makeBallControl()
00097 {
00098   visualization_msgs::InteractiveMarkerControl control;
00099   control.always_visible = true;
00100   imarker_.controls.push_back( control );
00101 }
00102 
00103 /* create an orientation/position control with an axis marker */
00104 void IMarker::makeAxisControl()
00105 {
00106   visualization_msgs::InteractiveMarkerControl control;
00107   control.always_visible = true;
00108   control.markers.push_back( makeAxisCyl(X) );
00109   control.markers.push_back( makeAxisCyl(Y) );
00110   control.markers.push_back( makeAxisCyl(Z) );
00111   imarker_.controls.push_back( control );
00112 }
00113 
00114 /* move to new pose */
00115 void IMarker::move(const Eigen::Affine3d& pose)
00116 {
00117   tf::poseEigenToMsg(pose, imarker_.pose);
00118   server_->applyChanges();
00119 }
00120 
00121 /* initialize the marker.  All constructors call this. */
00122 void IMarker::initialize(
00123           interactive_markers::InteractiveMarkerServer& server,
00124           const std::string& name,
00125           const Eigen::Vector3d& position,
00126           const Eigen::Quaterniond& orientation,
00127           const std::string& frame_id,
00128           boost::function<void (const visualization_msgs::InteractiveMarkerFeedbackConstPtr &)> callback,
00129           IMarker::Dof dof)
00130 {
00131   server_ = &server;
00132   imarker_.header.frame_id = frame_id;
00133   tf::pointEigenToMsg(position, imarker_.pose.position);
00134   tf::quaternionEigenToMsg(orientation, imarker_.pose.orientation);
00135   imarker_.scale = 0.3;
00136 
00137   imarker_.name = name;
00138   imarker_.description = name;
00139 
00140   // insert a control with a marker
00141   switch (dof)
00142   {
00143   case POS:
00144     makeBallControl();
00145     break;
00146   case ORIENT:
00147   case BOTH:
00148   default:
00149     makeAxisControl();
00150     break;
00151   }
00152 
00153   visualization_msgs::InteractiveMarkerControl control;
00154 
00155   control.orientation_mode = visualization_msgs::InteractiveMarkerControl::FIXED;
00156 
00157   // add orientation and/or position controls
00158   for (int i=0; i<3; i++)
00159   {
00160     static const char *dirname = "xyz";
00161 
00162     control.orientation.w = 1;
00163     control.orientation.x = 0;
00164     control.orientation.y = 0;
00165     control.orientation.z = 0;
00166     switch(i) {
00167     case 0: control.orientation.x = 1; break;
00168     case 1: control.orientation.y = 1; break;
00169     case 2: control.orientation.z = 1; break;
00170     }
00171 
00172     if (dof == ORIENT || dof == BOTH)
00173     {
00174       control.name = std::string("rotate_") + std::string(1,dirname[i]);
00175       control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00176       imarker_.controls.push_back(control);
00177     }
00178     if (dof == POS || dof == BOTH)
00179     {
00180       control.name = std::string("move_") + std::string(1,dirname[i]);
00181       control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00182       imarker_.controls.push_back(control);
00183     }
00184   }
00185 
00186   // tell the server to show the marker and listen for changes
00187   server_->insert(imarker_);
00188   server_->setCallback(imarker_.name, callback);
00189   server_->applyChanges();
00190 }


pr2_moveit_tutorials
Author(s): Sachin Chitta
autogenerated on Mon Oct 6 2014 11:15:31