GraspPoseControllerHandler.cpp
Go to the documentation of this file.
00001 /*
00002  * GraspPoseControllerHandler.cpp
00003  *
00004  *  Created on: Oct 10, 2012
00005  *      Author: jnicho
00006  */
00007 
00008 /*
00009  * Software License Agreement (BSD License)
00010  *
00011  * Copyright (c) 2011, Southwest Research Institute
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  *       * Redistributions of source code must retain the above copyright
00018  *       notice, this list of conditions and the following disclaimer.
00019  *       * Redistributions in binary form must reproduce the above copyright
00020  *       notice, this list of conditions and the following disclaimer in the
00021  *       documentation and/or other materials provided with the distribution.
00022  *       * Neither the name of the Southwest Research Institute, nor the names
00023  *       of its contributors may be used to endorse or promote products derived
00024  *       from this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <object_manipulation_tools/controller_utils/GraspPoseControllerHandler.h>
00040 
00041 static const double GRASP_COMMAND_EXECUTION_TIMEOUT = 4.0f;
00042 
00043 GraspPoseControllerHandler::GraspPoseControllerHandler(const std::string& group_name,const std::string& controller_name)
00044 :TrajectoryControllerHandler(group_name, controller_name),
00045  grasp_posture_execution_action_client_(controller_name, true)
00046 {
00047         while(ros::ok() && !grasp_posture_execution_action_client_.waitForServer(ros::Duration(5.0)))
00048         {
00049           ROS_INFO_STREAM("Waiting for the grasp_posture_execution action for group " << group_name << " on the topic " << controller_name << " to come up");
00050         }
00051 }
00052 
00053 bool GraspPoseControllerHandler::executeTrajectory(
00054                 const trajectory_msgs::JointTrajectory& trajectory,
00055             boost::shared_ptr<trajectory_execution_monitor::TrajectoryRecorder>& recorder,
00056             const trajectory_execution_monitor::TrajectoryFinishedCallbackFunction& traj_callback)
00057 {
00058 
00059         recorder_ = recorder;
00060         trajectory_finished_callback_ = traj_callback;
00061 
00062         initializeRecordedTrajectory(trajectory);
00063 
00064         object_manipulation_msgs::GraspHandPostureExecutionGoal goal;
00065         int graspCode = (int)trajectory.points[0].positions[0];
00066         std::string graspMoveName;
00067 
00068         switch(graspCode)
00069         {
00070         case object_manipulation_msgs::GraspHandPostureExecutionGoal::GRASP:
00071 
00072                 ROS_INFO_STREAM("Grasp Controller should be commanding grasp");
00073                 graspMoveName = "Grasp";
00074                 goal.goal = object_manipulation_msgs::GraspHandPostureExecutionGoal::GRASP;
00075                 break;
00076 
00077         case object_manipulation_msgs::GraspHandPostureExecutionGoal::PRE_GRASP:
00078 
00079                 ROS_INFO_STREAM("Grasp Controller should be commanding pre-grasp");
00080                 graspMoveName = "Pre-grasp";
00081                 goal.goal = object_manipulation_msgs::GraspHandPostureExecutionGoal::PRE_GRASP;
00082                 break;
00083 
00084         case object_manipulation_msgs::GraspHandPostureExecutionGoal::RELEASE:
00085 
00086                 ROS_INFO_STREAM("Grasp Controller should be commanding release");
00087                 graspMoveName = "Release";
00088                 goal.goal = object_manipulation_msgs::GraspHandPostureExecutionGoal::RELEASE;
00089                 break;
00090 
00091         default:
00092                 ROS_INFO_STREAM("Grasp Controller received unknown command, exiting ");
00093                 return false;
00094         }
00095 
00096         grasp_posture_execution_action_client_.sendGoal(goal,
00097                                                                                                   boost::bind(&GraspPoseControllerHandler::controllerDoneCallback, this, _1, _2),
00098                                                                                                   boost::bind(&GraspPoseControllerHandler::controllerActiveCallback, this),
00099                                                                                                   boost::bind(&GraspPoseControllerHandler::controllerFeedbackCallback, this, _1));
00100 
00101         //grasp_posture_execution_action_client_.sendGoal(goal);
00102         recorder_->registerCallback(group_controller_combo_name_,
00103                                                           boost::bind(&GraspPoseControllerHandler::addNewStateToRecordedTrajectory
00104                                                                           , this, _1, _2, _3));
00105 
00106         // waiting for result
00107 //      bool success = grasp_posture_execution_action_client_.waitForResult(ros::Duration(GRASP_COMMAND_EXECUTION_TIMEOUT));
00108 //      if(success)
00109 //      {
00110 //              ROS_INFO_STREAM("Grasp Controller: "<< graspMoveName <<" completed");
00111 //      }
00112 //      else
00113 //      {
00114 //              ROS_ERROR_STREAM("Grasp Controller: "<< graspMoveName <<" request timeout, exiting");
00115 //      }
00116 
00117         //done();
00118 
00119         return true;
00120 }
00121 
00122 void GraspPoseControllerHandler::cancelExecution()
00123 {
00124 
00125 }
00126 
00127 void GraspPoseControllerHandler::controllerDoneCallback(const actionlib::SimpleClientGoalState& state,
00128                 const object_manipulation_msgs::GraspHandPostureExecutionResultConstPtr& result)
00129 {
00130 
00131         bool success = (state == actionlib::SimpleClientGoalState::SUCCEEDED);
00132 
00133         // setting controller state and passing result to callback
00134         trajectory_execution_monitor::TrajectoryControllerState controllerState;
00135         controllerState = (success) ? trajectory_execution_monitor::TrajectoryControllerStates::SUCCESS :
00136                 trajectory_execution_monitor::TrajectoryControllerStates::EXECUTION_FAILURE;
00137 
00138         // finalizing and storing results
00139         controller_state_ = controllerState;
00140         recorder_->deregisterCallback(group_controller_combo_name_);
00141 
00142         ROS_INFO_STREAM("Grasp Controller is done with state " <<
00143                         (success?"SUCCESS":"FAULT"));
00144 
00145         done();
00146 }
00147 
00148 void GraspPoseControllerHandler::controllerActiveCallback()
00149 {
00150         ROS_DEBUG_STREAM("Grasp Controller went active");
00151 }
00152 
00153 void GraspPoseControllerHandler::controllerFeedbackCallback(const object_manipulation_msgs::GraspHandPostureExecutionFeedbackConstPtr& feedback)
00154 {
00155         ROS_INFO_STREAM("Grasp Controller got feedback");
00156 }
00157 


object_manipulation_tools
Author(s): Jnicho
autogenerated on Mon Oct 6 2014 00:56:17