follow_joint_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 _FOLLOW_JOINT_TRAJECTORY_CONTROLLER_HANDLER_H_
00038 #define _FOLLOW_JOINT_TRAJECTORY_CONTROLLER_HANDLER_H_
00039 
00040 #include <trajectory_execution_monitor/trajectory_controller_handler.h>
00041 #include <control_msgs/FollowJointTrajectoryAction.h>
00042 
00043 #include <actionlib/client/simple_action_client.h>
00044 #include <actionlib/client/simple_client_goal_state.h>
00045 
00047 class FollowJointTrajectoryControllerHandler : public trajectory_execution_monitor::TrajectoryControllerHandler {
00048 
00049 public:
00050   
00051   FollowJointTrajectoryControllerHandler(const std::string& group_name, 
00052                                          const std::string& controller_name) : 
00053     TrajectoryControllerHandler(group_name, controller_name),
00054     follow_joint_trajectory_action_client_(controller_name, true)
00055   {
00056     while(ros::ok() && !follow_joint_trajectory_action_client_.waitForServer(ros::Duration(5.0))){
00057       ROS_INFO_STREAM("Waiting for the follow joint trajectory action for group " << group_name << " on the topic " << controller_name << " to come up");
00058     }
00059   }
00060 
00061   bool executeTrajectory(const trajectory_msgs::JointTrajectory& trajectory,
00062                          boost::shared_ptr<trajectory_execution_monitor::TrajectoryRecorder>& recorder,
00063                          const trajectory_execution_monitor::TrajectoryFinishedCallbackFunction& traj_callback)
00064   {
00065     recorder_ = recorder;
00066     trajectory_finished_callback_ = traj_callback;
00067 
00068     initializeRecordedTrajectory(trajectory);
00069 
00070     control_msgs::FollowJointTrajectoryGoal goal;
00071     goal.trajectory = trajectory;
00072 
00073     follow_joint_trajectory_action_client_.sendGoal(goal,
00074                                                     boost::bind(&FollowJointTrajectoryControllerHandler::controllerDoneCallback, this, _1, _2),
00075                                                     boost::bind(&FollowJointTrajectoryControllerHandler::controllerActiveCallback, this),
00076                                                     boost::bind(&FollowJointTrajectoryControllerHandler::controllerFeedbackCallback, this, _1));
00077     recorder_->registerCallback(group_controller_combo_name_, 
00078                                 boost::bind(&FollowJointTrajectoryControllerHandler::addNewStateToRecordedTrajectory, this, _1, _2, _3));
00079     return true;
00080   }
00081 
00082   void cancelExecution() {
00083   }
00084 
00085   void controllerDoneCallback(const actionlib::SimpleClientGoalState& state,
00086                               const control_msgs::FollowJointTrajectoryResultConstPtr& result)
00087   {
00088     ROS_INFO_STREAM("Controller is done with state " << state.toString() );
00089 
00090     if( controller_state_ == trajectory_execution_monitor::TrajectoryControllerStates::EXECUTING ||
00091         controller_state_ == trajectory_execution_monitor::TrajectoryControllerStates::OVERSHOOTING )
00092     {
00093       if(state == actionlib::SimpleClientGoalState::SUCCEEDED)
00094       {
00095         controller_state_ = trajectory_execution_monitor::TrajectoryControllerStates::SUCCESS;
00096       }
00097       else
00098       {
00099         ROS_WARN_STREAM("Failed state is " << state.toString() << " code " << result->error_code);
00100         controller_state_ = trajectory_execution_monitor::TrajectoryControllerStates::EXECUTION_FAILURE;
00101       }
00102 
00103       // record overshoot
00104       if( controller_state_==trajectory_execution_monitor::TrajectoryControllerStates::SUCCESS )
00105       {
00106         if( monitor_overshoot_ )
00107         {
00108           initializeOvershootTrajectory();
00109         }
00110         else
00111         {
00112           done();
00113         }
00114       }
00115       else
00116       {
00117         ROS_WARN_STREAM("Controller returned an error.  Not recording the overshoot.");
00118         done();
00119       }
00120     }
00121   }
00122 
00123   void controllerActiveCallback() 
00124   {
00125     ROS_DEBUG_STREAM("Controller went active");
00126   }
00127 
00128   void controllerFeedbackCallback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback)
00129   {
00130     ROS_INFO_STREAM("Got feedback");
00131   }
00132     
00133 
00134 protected:
00135   actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> follow_joint_trajectory_action_client_;
00136 }; 
00137 
00138 #endif


trajectory_execution_monitor
Author(s): Gil Jones
autogenerated on Fri Dec 6 2013 21:09:24