00001 /* 00002 * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA) 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 #ifndef COB_FRAME_TRACKER_H 00019 #define COB_FRAME_TRACKER_H 00020 00021 #include <string> 00022 #include <vector> 00023 #include <math.h> 00024 #include <algorithm> 00025 #include <ros/ros.h> 00026 00027 #include <std_msgs/String.h> 00028 #include <std_msgs/Float64.h> 00029 #include <std_msgs/Float64MultiArray.h> 00030 #include <sensor_msgs/JointState.h> 00031 #include <geometry_msgs/Twist.h> 00032 #include <std_srvs/Trigger.h> 00033 #include <cob_srvs/SetString.h> 00034 00035 #include <actionlib/server/simple_action_server.h> 00036 #include <cob_frame_tracker/FrameTrackerConfig.h> 00037 #include <cob_frame_tracker/FrameTrackingAction.h> 00038 00039 #include <tf/transform_listener.h> 00040 #include <tf/transform_datatypes.h> 00041 00042 #include <kdl_parser/kdl_parser.hpp> 00043 #include <kdl/chainiksolvervel_pinv.hpp> 00044 #include <kdl/chainfksolvervel_recursive.hpp> 00045 #include <kdl/frames.hpp> 00046 #include <kdl/jntarray.hpp> 00047 #include <kdl/jntarrayvel.hpp> 00048 00049 #include <control_toolbox/pid.h> 00050 #include <dynamic_reconfigure/server.h> 00051 #include <dynamic_reconfigure/Reconfigure.h> 00052 #include <boost/thread/mutex.hpp> 00053 00054 00055 typedef actionlib::SimpleActionServer<cob_frame_tracker::FrameTrackingAction> SAS_FrameTrackingAction_t; 00056 00057 struct HoldTf 00058 { 00059 tf::StampedTransform transform_tf; 00060 bool hold; 00061 }; 00062 00063 00064 class CobFrameTracker 00065 { 00066 public: 00067 CobFrameTracker() 00068 { 00069 ht_.hold = false; 00070 } 00071 00072 ~CobFrameTracker() 00073 { 00074 jntToCartSolver_vel_.reset(); 00075 as_.reset(); 00076 reconfigure_server_.reset(); 00077 } 00078 00079 bool initialize(); 00080 void run(const ros::TimerEvent& event); 00081 00082 void jointstateCallback(const sensor_msgs::JointState::ConstPtr& msg); 00083 00084 bool startTrackingCallback(cob_srvs::SetString::Request& request, cob_srvs::SetString::Response& response); 00085 bool startLookatCallback(cob_srvs::SetString::Request& request, cob_srvs::SetString::Response& response); 00086 bool stopCallback(std_srvs::Trigger::Request& request, std_srvs::Trigger::Response& response); 00087 00088 bool getTransform(const std::string& from, const std::string& to, tf::StampedTransform& stamped_tf); 00089 00090 void publishZeroTwist(); 00091 void publishTwist(ros::Duration period, bool do_publish = true); 00092 void publishHoldTwist(const ros::Duration& period); 00093 00095 void goalCB(); 00096 void preemptCB(); 00097 void action_success(); 00098 void action_abort(); 00099 00100 private: 00101 HoldTf ht_; 00102 00103 double update_rate_; 00104 ros::Timer timer_; 00105 00106 bool tracking_; 00107 bool tracking_goal_; 00108 bool lookat_; 00109 std::string chain_base_link_; 00110 std::string chain_tip_link_; 00111 std::string lookat_focus_frame_; 00112 std::string tracking_frame_; // the frame tracking the target (i.e. chain_tip or lookat_focus) 00113 std::string target_frame_; // the frame to be tracked 00114 00115 double max_vel_lin_; 00116 double max_vel_rot_; 00117 00118 std::vector<std::string> joints_; 00119 unsigned int dof_; 00120 00121 bool movable_trans_; 00122 bool movable_rot_; 00123 00124 control_toolbox::Pid pid_controller_trans_x_; 00125 control_toolbox::Pid pid_controller_trans_y_; 00126 control_toolbox::Pid pid_controller_trans_z_; 00127 00128 control_toolbox::Pid pid_controller_rot_x_; 00129 control_toolbox::Pid pid_controller_rot_y_; 00130 control_toolbox::Pid pid_controller_rot_z_; 00131 00133 KDL::Chain chain_; 00134 KDL::JntArray last_q_; 00135 KDL::JntArray last_q_dot_; 00136 boost::shared_ptr<KDL::ChainFkSolverVel_recursive> jntToCartSolver_vel_; 00137 00138 tf::TransformListener tf_listener_; 00139 00140 ros::Subscriber jointstate_sub_; 00141 ros::Publisher twist_pub_; 00142 ros::Publisher error_pub_; 00143 00144 ros::ServiceServer start_tracking_server_; 00145 ros::ServiceServer start_lookat_server_; 00146 ros::ServiceServer stop_server_; 00147 ros::ServiceClient reconfigure_client_; 00148 00150 std::string action_name_; 00151 boost::shared_ptr<SAS_FrameTrackingAction_t> as_; 00152 00153 cob_frame_tracker::FrameTrackingFeedback action_feedback_; 00154 cob_frame_tracker::FrameTrackingResult action_result_; 00155 00156 boost::recursive_mutex reconfig_mutex_; 00157 boost::shared_ptr< dynamic_reconfigure::Server<cob_frame_tracker::FrameTrackerConfig> > reconfigure_server_; 00158 void reconfigureCallback(cob_frame_tracker::FrameTrackerConfig& config, uint32_t level); 00159 00161 int checkStatus(); 00162 bool checkInfinitesimalTwist(const KDL::Twist current); 00163 bool checkCartDistanceViolation(const double dist, const double rot); 00164 bool checkTwistViolation(const KDL::Twist current, const KDL::Twist target); 00165 00166 int checkServiceCallStatus(); 00167 00168 bool stop_on_goal_; 00169 double tracking_duration_; 00170 ros::Time tracking_start_time_; 00171 00172 bool enable_abortion_checking_; 00173 double cart_min_dist_threshold_lin_; 00174 double cart_min_dist_threshold_rot_; 00175 double twist_dead_threshold_lin_; 00176 double twist_dead_threshold_rot_; 00177 double twist_deviation_threshold_lin_; 00178 double twist_deviation_threshold_rot_; 00179 00180 KDL::Twist current_twist_; 00181 KDL::Twist target_twist_; 00182 00183 double cart_distance_; 00184 double rot_distance_; 00185 00186 unsigned int abortion_counter_; 00187 unsigned int max_abortions_; 00188 }; 00189 00190 #endif