pr2_base_trajectory_action.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 
00037 #ifndef JOINT_TRAJECTORY_ACTION_CONTROLLER_H__
00038 #define JOINT_TRAJECTORY_ACTION_CONTROLLER_H__
00039 
00040 #include <vector>
00041 
00042 #include <boost/scoped_ptr.hpp>
00043 #include <boost/thread/recursive_mutex.hpp>
00044 #include <boost/thread/condition.hpp>
00045 #include <ros/node_handle.h>
00046 
00047 #include <actionlib/server/action_server.h>
00048 
00049 #include <geometry_msgs/Twist.h>
00050 #include <nav_msgs/Odometry.h>
00051 #include <trajectory_msgs/JointTrajectory.h>
00052 #include <pr2_controllers_msgs/JointTrajectoryAction.h>
00053 
00054 class BaseTrajectoryActionController
00055 {
00056   typedef actionlib::ActionServer<pr2_controllers_msgs::JointTrajectoryAction> JTAS;
00057   typedef JTAS::GoalHandle GoalHandle;
00058 
00059   // dummy class
00060   class JointState {
00061   public:
00062     std::string name;
00063     double position_, velocity_;
00064     double max_abs_velocity_;
00065     double commanded_velocity_;
00066     double commanded_effort_;// same unit as velocity
00067   };
00068 
00069 public:
00070 
00071   BaseTrajectoryActionController(const ros::NodeHandle& n);
00072   ~BaseTrajectoryActionController();
00073 
00074   bool init();
00075 
00076   void starting();
00077   void update();
00078 
00079 private:
00080   bool use_pid;
00081   JointState x_joint_,y_joint_,z_joint_;
00082   ros::Time last_time_, robot_time_;
00083   std::vector<JointState*> joints_;
00084   double goal_time_constraint_;
00085   double goal_threshold_;
00086   double stopped_velocity_tolerance_;
00087   std::vector<double> goal_constraints_;
00088   std::vector<double> trajectory_constraints_;
00089 
00090   ros::NodeHandle node_;
00091 
00092   void commandCB(const trajectory_msgs::JointTrajectory::ConstPtr &msg);
00093   ros::Publisher pub_command_;
00094 
00095   void odomCB(const nav_msgs::Odometry::ConstPtr& msg);
00096   ros::Subscriber sub_odom_;
00097 
00098   boost::scoped_ptr<JTAS> action_server_;
00099   void goalCB(GoalHandle gh);
00100   void cancelCB(GoalHandle gh);
00101   ros::Timer goal_handle_timer_;
00102 
00103   boost::shared_ptr<GoalHandle> active_goal_;
00104 
00105   void commandTrajectory(const trajectory_msgs::JointTrajectory::ConstPtr &traj, boost::shared_ptr<GoalHandle> gh = boost::shared_ptr<GoalHandle>((GoalHandle*)NULL));
00106 
00107   // coef[0] + coef[1]*t + ... + coef[5]*t^5
00108   struct Spline
00109   {
00110     std::vector<double> coef;
00111 
00112     Spline() : coef(6, 0.0) {}
00113   };
00114 
00115   struct Segment
00116   {
00117     double start_time;
00118     double duration;
00119     std::vector<Spline> splines;
00120 
00121     boost::shared_ptr<GoalHandle> gh;
00122   };
00123   typedef std::vector<Segment> SpecifiedTrajectory;
00124 
00125   boost::shared_ptr<const SpecifiedTrajectory> current_trajectory_;
00126 
00127   // Holds the trajectory that we are currently following.  The mutex
00128   // guarding current_trajectory_ is locked from within realtime, so
00129   // it may only be locked for a bounded duration.
00130   //boost::shared_ptr<const SpecifiedTrajectory> current_trajectory_;
00131   //boost::recursive_mutex current_trajectory_lock_RT_;
00132 
00133   std::vector<double> q, qd, qdd;  // Preallocated in init
00134   std::vector<double> e, ed, edd;  // Preallocated in init
00135 
00136   // Samples, but handling time bounds.  When the time is past the end
00137   // of the spline duration, the position is the last valid position,
00138   // and the derivatives are all 0.
00139   static void sampleSplineWithTimeBounds(const std::vector<double>& coefficients, double duration, double time,
00140                                          double& position, double& velocity, double& acceleration);
00141 };
00142 
00143 
00144 #endif


pr2_base_trajectory_action
Author(s): saito
autogenerated on Wed Sep 16 2015 10:31:51