marker_control.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 // author: Adam Leeper
00031 
00032 // **** 10 ***** 20 ****** 30 ****** 40 ****** 50 ****** 60 ****** 70 ****** 80 ****** 90 ***** 100 ***** 110 ***** 120
00033 
00034 #include <ros/ros.h>
00035 #include <math.h>
00036 
00037 #include <arm_navigation_msgs/GetStateValidity.h>
00038 
00039 #include <interactive_markers/interactive_marker_server.h>
00040 #include <interactive_markers/menu_handler.h>
00041 
00042 #include <object_manipulator/tools/mechanism_interface.h>
00043 #include <object_manipulator/tools/msg_helpers.h>
00044 #include <object_manipulator/tools/arm_configurations.h>
00045 #include <object_manipulator/tools/vector_tools.h>
00046 #include <object_manipulator/tools/service_action_wrappers.h>
00047 
00048 #include <std_srvs/Empty.h>
00049 
00050 #include <actionlib/client/simple_action_client.h>
00051 #include <actionlib/server/simple_action_server.h>
00052 
00053 #include <boost/thread.hpp>
00054 
00055 #include <tf/transform_listener.h>
00056 #include <tf/transform_broadcaster.h>
00057 
00058 #include <pr2_wrappers/gripper_controller.h>
00059 #include <pr2_wrappers/torso_client.h>
00060 #include <pr2_wrappers/base_client.h>
00061 #include <pr2_wrappers/tuck_arms_client.h>
00062 #include <pr2_wrappers/plugs_client.h>
00063 #include <point_cloud_server/StoreCloudAction.h>
00064 
00065 #include <pr2_object_manipulation_msgs/GetNavPoseAction.h>
00066 #include <pr2_object_manipulation_msgs/GetGripperPoseAction.h>
00067 
00068 #include <interactive_marker_helpers/interactive_marker_helpers.h>
00069 #include <pr2_marker_control/cloud_handler.h>
00070 
00071 #include <boost/thread/thread.hpp>
00072 
00074 class PR2MarkerControl
00075 {
00076 public:
00077 
00079   PR2MarkerControl();
00080 
00082   virtual ~PR2MarkerControl() { switchToJoint();  }
00083 
00084 // **** 10 ***** 20 ****** 30 ****** 40 ****** 50 ****** 60 ****** 70 ****** 80 ****** 90 ***** 100 ***** 110 ***** 120
00085 
00087   void fastUpdate();
00088 
00090   void slowUpdate();
00091 
00093   double getJointPosition( std::string name, const arm_navigation_msgs::RobotState& robot_state);
00094 
00096   geometry_msgs::PoseStamped toolToWrist(const geometry_msgs::PoseStamped &ps);
00097 
00099   geometry_msgs::PoseStamped wristToTool(const geometry_msgs::PoseStamped &ps);
00100 
00102   void initAllMarkers(bool apply_immediately = false)
00103   {
00104     initControlMarkers();
00105     initMeshMarkers();
00106 
00107     if(apply_immediately)
00108     {
00109       ROS_INFO_STREAM( "Re-initializing all markers!" );
00110       server_.applyChanges();
00111     }
00112   }
00113 
00115   void initControlMarkers();
00116 
00118   void initMeshMarkers();
00119 
00121   void cancelBaseMovement() {
00122       base_goal_pose_.header.stamp = ros::Time::now();
00123       base_client_.cancelGoals();
00124   }
00125 
00126 
00127 protected:
00128 
00130   void switchToCartesian();
00131 
00133   void switchToJoint();
00134 
00136   bool checkStateValidity(std::string arm_name);
00137 
00139   void gripperToggleModeCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00140 
00141   void snapshotCB() { snapshot_client_.refresh( nh_.resolveName("snapshot_input_topic", true) ); }
00142 
00143   void gripperToggleFixedCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00144 
00145   void dualGripperToggleFixedCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00146 
00147   void gripperToggleControlCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00148 
00149   void dualGripperToggleControlCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00150 
00151   void gripperResetControlCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00152 
00153   void dualGripperResetControlCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00154 
00155   void startDualGrippers( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, bool active );
00156 
00157 
00158   void updateGripper( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int arm_id );
00159 
00160   void updateDualGripper( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00161 
00163   void gripperButtonCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, std::string action);
00164 
00165   void upperArmButtonCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int id);
00166 
00167 
00168   void baseButtonCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00169   {
00170     control_state_.base_on_ ^= true;
00171     initControlMarkers();
00172   }
00173 
00174   void refreshPosture( const std::string &arm_name )
00175   {
00176     std::vector<double> arm_angles(7);
00177     for( size_t i = 0; i < arm_angles.size(); i++) arm_angles[i] = 9999;
00178     mechanism_.sendCartesianPostureCommand(arm_name, arm_angles);
00179   }
00180 
00181   void tuckArmsCB( bool tuck_left, bool tuck_right) 
00182   { 
00183     switchToJoint();
00184     tuck_arms_client_.tuckArms( tuck_right, tuck_left, false );  
00185   }
00186 
00187   typedef enum {PLUGS_PLUGIN, PLUGS_UNPLUG, PLUGS_CANCEL} plugs_cmd_t_;
00188   void plugsCB( plugs_cmd_t_ cmd )
00189   {
00190           switchToJoint();
00191           if (cmd!=PLUGS_CANCEL) {
00192                   plugs_client_.plug_unplug( (cmd==PLUGS_PLUGIN) ? true : false, false );
00193           } else {
00194                   plugs_client_.cancel();
00195           }
00196 
00197   }
00198 
00199   void updatePosture( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int arm_id );
00200 
00201   void projectorMenuCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00202 
00203   void targetPointMenuCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00204 
00205   void centerHeadCB()
00206   {
00207     geometry_msgs::PointStamped ps;
00208     ps.header.frame_id = "/base_link";    
00209     ps.point.x = 1.0;
00210     mechanism_.pointHeadAction(ps, head_pointing_frame_, false);
00211   }
00212 
00213   // Moved to graveyard
00214   //void controllerSelectMenuCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback );
00215 
00216   void updateTorso( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback);
00217 
00218   void updateBase( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback);
00219 
00220   void torsoMenuCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback);
00221 
00222   void gripperClosureCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, const float &command);
00223 
00224   void plugCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, const bool &plug);
00225 
00226   void updateHeadGoal( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int arm_id );
00227 
00228   void commandGripperPose(const geometry_msgs::PoseStamped &ps, int arm_id, bool use_offset);
00229 
00230   void requestGripperPose(  const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int arm_id);
00231 
00232   void processGripperPoseFeedback(  const pr2_object_manipulation_msgs::GetGripperPoseFeedbackConstPtr &result,
00233                                     const std::string &arm_name);
00234 
00235   void processGripperPoseResult(  const actionlib::SimpleClientGoalState& state,
00236                                   const pr2_object_manipulation_msgs::GetGripperPoseResultConstPtr &result,
00237                                   const std::string &arm_name);
00238 
00239   void moveArm( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback,
00240                 const std::string &position, bool planner);
00241 
00242   void moveArmThread(std::string arm_name, std::string position, bool collision, bool planner);
00243 
00244   void initMenus();
00245 
00246   void requestNavGoal(  const bool & collision_aware );
00247 
00248   void processNavGoal(  const actionlib::SimpleClientGoalState& state,
00249                         const pr2_object_manipulation_msgs::GetNavPoseResultConstPtr &result,
00250                         const bool &collision_aware );
00251 
00252   void requestBasePoseEstimate( );
00253 
00254 
00255   void processBasePoseEstimate( const actionlib::SimpleClientGoalState& state,
00256                                 const pr2_object_manipulation_msgs::GetNavPoseResultConstPtr &result );
00257   
00259   void sendLastNavGoal();
00260   
00261   void clearLocalCostmap();
00262 
00263   void inHandObjectRightCallback( const sensor_msgs::PointCloud2ConstPtr &cloud);
00264 
00265   void inHandObjectLeftCallback( const sensor_msgs::PointCloud2ConstPtr &cloud);
00266 
00267 protected:
00268 
00269   ros::NodeHandle nh_;
00270   ros::NodeHandle pnh_;
00271   ros::Timer spin_timer_;
00272   ros::Timer slow_sync_timer_;
00273   interactive_markers::InteractiveMarkerServer server_;
00274   //ros::Publisher pub_cloud_snapshot_;
00275 
00277   interactive_markers::MenuHandler menu_arms_;
00278   interactive_markers::MenuHandler menu_head_;
00279   interactive_markers::MenuHandler menu_torso_;
00280   interactive_markers::MenuHandler menu_laser_;
00281   interactive_markers::MenuHandler menu_grippers_;
00282   interactive_markers::MenuHandler menu_dual_grippers_;
00283   interactive_markers::MenuHandler menu_gripper_close_;
00284   interactive_markers::MenuHandler menu_base_;
00285 
00287   interactive_markers::MenuHandler::EntryHandle tuck_handle_;
00288   interactive_markers::MenuHandler::EntryHandle joint_handle_;
00289   interactive_markers::MenuHandler::EntryHandle jtranspose_handle_;
00290   interactive_markers::MenuHandler::EntryHandle posture_handle_;
00291   interactive_markers::MenuHandler::EntryHandle projector_handle_;
00292   interactive_markers::MenuHandler::EntryHandle head_target_handle_;
00293   interactive_markers::MenuHandler::EntryHandle gripper_view_facing_handle_;
00294   interactive_markers::MenuHandler::EntryHandle gripper_6dof_handle_;
00295   interactive_markers::MenuHandler::EntryHandle gripper_fixed_control_handle_;
00296   interactive_markers::MenuHandler::EntryHandle dual_gripper_fixed_control_handle_;
00297   interactive_markers::MenuHandler::EntryHandle gripper_edit_control_handle_;
00298   interactive_markers::MenuHandler::EntryHandle dual_gripper_edit_control_handle_;
00299   interactive_markers::MenuHandler::EntryHandle gripper_reset_control_handle_;
00300   interactive_markers::MenuHandler::EntryHandle dual_gripper_reset_control_handle_;
00301 
00302   tf::TransformListener tfl_;
00303   tf::TransformBroadcaster tfb_;
00304 
00305   std::vector<tf::Transform> pose_offset_;
00306   tf::Transform dual_pose_offset_;
00307   geometry_msgs::Pose tool_frame_offset_;
00308   geometry_msgs::PoseStamped head_goal_pose_;
00309 
00310   geometry_msgs::PoseStamped dual_grippers_frame_;
00311   std::vector<tf::Transform> dual_gripper_offsets_;
00312 
00313   std::string manipulator_base_frame_;
00314 
00315   geometry_msgs::PoseStamped base_goal_pose_;
00316 
00318   struct GripperState{
00319     GripperState() : on_(false), view_facing_(false), edit_control_(false), torso_frame_(false) {}
00320 
00321     bool on_;
00322     bool view_facing_;
00323     bool edit_control_;
00324     bool torso_frame_;
00325   };
00326 
00328   struct ControlState{
00329     ControlState() : posture_r_(false), posture_l_(false), torso_on_(false), head_on_(false),
00330                      projector_on_(false), init_head_goal_(false), base_on_(false) {}
00331 
00332     void print()
00333     {
00334       ROS_DEBUG_NAMED("control_state", "gripper: on[%d|%d][%d], edit[%d|%d][%d], torso[%d|%d]",
00335                       l_gripper_.on_, r_gripper_.on_, dual_grippers_.on_, l_gripper_.edit_control_, r_gripper_.edit_control_, dual_grippers_.edit_control_, l_gripper_.torso_frame_, r_gripper_.torso_frame_);
00336       ROS_DEBUG_NAMED("control_state", "posture[%d|%d] torso[%d] base[%d] head[%d] projector[%d]",
00337                       posture_l_, posture_r_, torso_on_, base_on_, head_on_, projector_on_ );
00338     }
00339 
00340     bool posture_r_;
00341     bool posture_l_;
00342     bool torso_on_;
00343     bool head_on_;
00344     bool projector_on_;
00345     bool init_head_goal_;
00346     bool base_on_;
00347     bool planar_only_;
00348     GripperState dual_grippers_;
00349     GripperState r_gripper_;
00350     GripperState l_gripper_;
00351   };
00352 
00353   ControlState control_state_;
00354   pr2_wrappers::GripperController gripper_client_;
00355   pr2_wrappers::TorsoClient torso_client_;
00356   pr2_wrappers::BaseClient base_client_;
00357   pr2_wrappers::TuckArmsClient tuck_arms_client_;
00358   pr2_wrappers::PlugsClient plugs_client_;
00359 
00360   boost::mutex planner_lock_;
00361 
00362   object_manipulator::MechanismInterface mechanism_;
00363 
00364   bool use_state_validator_;
00365   bool use_right_arm_;
00366   bool use_left_arm_;
00367   double gripper_control_linear_deadband_, gripper_control_angular_deadband_;
00368   double update_period_;
00369   double cartesian_clip_distance_;
00370   double cartesian_clip_angle_;
00371 
00372   bool transparent_;
00373 
00374   double max_direct_nav_radius_;
00375 
00376   bool in_collision_r_, in_collision_l_;
00377 
00378   boost::shared_ptr< boost::thread > sys_thread_;
00379 
00381   object_manipulator::ServiceWrapper<arm_navigation_msgs::GetStateValidity> check_state_validity_client_;
00382   object_manipulator::ServiceWrapper<std_srvs::Empty> collider_node_reset_srv_;
00383   CloudHandler snapshot_client_;
00384   CloudHandler object_cloud_left_;
00385   CloudHandler object_cloud_right_;
00386 
00388   ros::Subscriber object_cloud_left_sub_;
00389   ros::Subscriber object_cloud_right_sub_;
00390 
00392   object_manipulator::ActionWrapper<pr2_object_manipulation_msgs::GetNavPoseAction> base_pose_client_;
00393 
00395   object_manipulator::ActionWrapper<pr2_object_manipulation_msgs::GetGripperPoseAction> gripper_pose_client_;
00396 
00398   int interface_number_;
00400   int task_number_;
00401 
00402   std::string head_pointing_frame_;
00403 
00405   std::string move_base_node_name_;
00406 
00408   bool using_3d_nav_;
00409 
00410   //stuff for the rmrc controller
00411   bool alignedOdomValid_;
00412   boost::thread alignedOdomThread_;
00413   boost::mutex alignedOdomMutex_;
00414   tf::StampedTransform alignedOdom_;
00415 
00417 
00418   std::string l_gripper_type_, r_gripper_type_;
00419 
00420   void publishAlignedOdom() {
00421     while ( true ) {
00422       boost::this_thread::sleep(boost::posix_time::milliseconds(100));
00423       {
00424         boost::mutex::scoped_lock lock(alignedOdomMutex_);
00425         if ( alignedOdomValid_ ) {
00426           alignedOdom_.stamp_ = ros::Time::now();
00427           tfb_.sendTransform(alignedOdom_);
00428         }
00429       }
00430     }
00431   }
00432 
00433 };
00434 
00435 
00436 


pr2_marker_control
Author(s): Adam Leeper
autogenerated on Mon Oct 6 2014 12:48:08