pr2_gripper_trajectory_controller_handler.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00037 #ifndef _PR2_GRIPPER_TRAJECTORY_CONTROLLER_HANDLER_H_
00038 #define _PR2_GRIPPER_TRAJECTORY_CONTROLLER_HANDLER_H_
00039 
00040 #include <trajectory_execution_monitor/trajectory_controller_handler.h>
00041 #include <actionlib/client/simple_action_client.h>
00042 #include <actionlib/client/simple_client_goal_state.h>
00043 #include <pr2_controllers_msgs/Pr2GripperCommandAction.h>
00044 
00046 class Pr2GripperTrajectoryControllerHandler : public trajectory_execution_monitor::TrajectoryControllerHandler {
00047 
00048 public:
00049   
00050   static const double GRIPPER_OPEN = 0.086;
00051   static const double GRIPPER_CLOSED = 0.0;
00052   static const double DEFAULT_GRIPPER_OBJECT_PRESENCE_THRESHOLD = 0.0021;
00053 
00054   Pr2GripperTrajectoryControllerHandler(const std::string& group_name, 
00055                                         const std::string& controller_name) : 
00056     TrajectoryControllerHandler(group_name, controller_name),
00057     pr2_gripper_action_client_(controller_name, true)
00058   {
00059     while(ros::ok() && !pr2_gripper_action_client_.waitForServer(ros::Duration(5.0))){
00060       ROS_INFO_STREAM("Waiting for the pr2_gripper action for group " << group_name << " on the topic " << controller_name << " to come up");
00061     }
00062   }
00063 
00065   bool enableOvershoot( double max_overshoot_velocity_epsilon,
00066                         ros::Duration min_overshoot_time,
00067                         ros::Duration max_overshoot_time )
00068   {
00069     return false;
00070   }
00071 
00073   bool executeTrajectory(const trajectory_msgs::JointTrajectory& trajectory,
00074                          boost::shared_ptr<trajectory_execution_monitor::TrajectoryRecorder>& recorder,
00075                          const trajectory_execution_monitor::TrajectoryFinishedCallbackFunction& traj_callback)
00076   {
00077     recorder_ = recorder;
00078     trajectory_finished_callback_ = traj_callback;
00079 
00080     initializeRecordedTrajectory(trajectory);
00081     
00082     pr2_controllers_msgs::Pr2GripperCommandGoal gripper_command;
00083       gripper_command.command.max_effort = 10000;
00084 
00085     double jval = trajectory.points[0].positions[0];
00086     if(jval != 0.0) {
00087       ROS_INFO_STREAM("Commanding gripper open");
00088       gripper_command.command.position = GRIPPER_OPEN;
00089     } else {
00090       ROS_INFO_STREAM("Commanding gripper closed");
00091       gripper_command.command.position = GRIPPER_CLOSED;
00092     }
00093     
00094     pr2_gripper_action_client_.sendGoal(gripper_command,
00095                                         boost::bind(&Pr2GripperTrajectoryControllerHandler::controllerDoneCallback, this, _1, _2),
00096                                         boost::bind(&Pr2GripperTrajectoryControllerHandler::controllerActiveCallback, this),
00097                                         boost::bind(&Pr2GripperTrajectoryControllerHandler::controllerFeedbackCallback, this, _1));
00098 
00099     recorder_->registerCallback(group_controller_combo_name_, 
00100                                 boost::bind(&Pr2GripperTrajectoryControllerHandler::addNewStateToRecordedTrajectory, this, _1, _2, _3));
00101     return true;
00102   }
00103 
00104   void cancelExecution() {
00105   }
00106 
00107   void controllerDoneCallback(const actionlib::SimpleClientGoalState& state,
00108                               const pr2_controllers_msgs::Pr2GripperCommandResultConstPtr& result)
00109   {
00110     ROS_INFO_STREAM("Gripper controller is done with state " << state.toString());
00111 
00112     if( controller_state_ == trajectory_execution_monitor::TrajectoryControllerStates::EXECUTING ||
00113         controller_state_ == trajectory_execution_monitor::TrajectoryControllerStates::OVERSHOOTING )
00114     {
00115       if(state == actionlib::SimpleClientGoalState::SUCCEEDED)
00116       {
00117         controller_state_ = trajectory_execution_monitor::TrajectoryControllerStates::SUCCESS;
00118       }
00119       else
00120       {
00121         ROS_WARN_STREAM("Failed state is " << state.toString() );
00122         controller_state_ = trajectory_execution_monitor::TrajectoryControllerStates::EXECUTION_FAILURE;
00123       }
00124 
00125       // We don't record overshoot on the gripper
00126       done();
00127     }
00128   }
00129 
00130   void controllerActiveCallback() 
00131   {
00132     ROS_DEBUG_STREAM("Controller went active");
00133   }
00134 
00135   void controllerFeedbackCallback(const pr2_controllers_msgs::Pr2GripperCommandFeedbackConstPtr& feedback)
00136   {
00137     ROS_DEBUG_STREAM("Got feedback");
00138   }
00139     
00140 
00141 protected:
00142 
00143   actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> pr2_gripper_action_client_;
00144 }; 
00145 
00146 #endif


trajectory_execution_monitor
Author(s): Gil Jones
autogenerated on Thu Dec 12 2013 11:08:00