00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <string>
00031 #include <map>
00032 #include <vector>
00033 #include <sbpl/config.h>
00034 #include <angles/angles.h>
00035 #include <kdl_parser/kdl_parser.hpp>
00036 #include <kdl/jntarray.hpp>
00037 #include <kdl/chainfksolverpos_recursive.hpp>
00038 #include <kdl/chain.hpp>
00039 #include <kdl/frames.hpp>
00040 #include <kdl/chainiksolver.hpp>
00041 #include <kdl/chainiksolverpos_nr.hpp>
00042 #include <kdl/chainiksolvervel_pinv_givens.hpp>
00043 #include <kdl/treefksolverpos_recursive.hpp>
00044 #include <urdf/model.h>
00045 #include <ros/ros.h>
00046 #include <pr2_arm_kinematics/pr2_arm_ik_solver.h>
00047
00048 using namespace std;
00049
00050 #ifndef _SBPL_ARM_MODEL_
00051 #define _SBPL_ARM_MODEL_
00052
00057 struct ArmJoint
00058 {
00059 bool continuous;
00060 double min;
00061 double max;
00062 std::string name;
00063 };
00064
00069 struct ArmLink
00070 {
00071 short unsigned int radius_c;
00072 short unsigned int length_c;
00073 int ind_chain;
00074 int ind_jnt1;
00075 int ind_jnt2;
00076 double length;
00077 double radius;
00078 std::string name;
00079 };
00080
00081
00082 class SBPLArmModel{
00083
00084 public:
00085
00090 SBPLArmModel(FILE* arm_file);
00091
00093 ~SBPLArmModel();
00094
00096 int num_joints_;
00097
00099 int num_links_;
00100
00102 double resolution_;
00103
00105 bool initKDLChain(const std::string &fKDL);
00106
00108 bool initKDLChainFromParamServer();
00109
00111 bool getArmDescription(FILE* fCfg);
00112
00114 double getAxisAngle(const double R1[3][3], const double R2[3][3]);
00115
00117 void RPY2Rot(double roll, double pitch, double yaw, double Rot[3][3]);
00118
00120 bool computeFK(const std::vector<double> angles, int frame_num, KDL::Frame *frame_out);
00121
00123 bool computeFK(const std::vector<double> angles, int f_num, std::vector<double> &xyzrpy);
00124
00128 bool computeFK(const std::vector<double> angles, int frame_num, std::vector<double> &xyzrpy, int sol_num);
00129
00132 bool computeEndEffPose(const std::vector<double> angles, double R_target[3][3], double *x, double *y, double *z, double *axis_angle);
00133
00135 bool computeIK(const std::vector<double> pose, const std::vector<double> start, std::vector<double> &solution);
00136
00138 bool getJointPositions(const std::vector<double> angles, const double R_target[3][3], std::vector<std::vector<double> > &links, double *axis_angle);
00139
00140 bool getJointPositions(const std::vector<double> angles, std::vector<std::vector<double> > &links, KDL::Frame &f_out);
00141
00143 bool computePlanningJointPos(const std::vector<double> angles, double *x, double *y, double *z);
00144
00145 bool getPlanningJointPose(const std::vector<double> angles, std::vector<double> &pose);
00146
00148 bool getPlanningJointPose(const std::vector<double> angles, double R_target[3][3], std::vector<double> &pose, double *axis_angle);
00149
00151 bool checkJointLimits(std::vector<double> angles, bool verbose);
00152
00154 void printArmDescription(FILE* fOut);
00155
00157 int getMaxLinkRadius();
00158
00160 short unsigned int getLinkRadiusCells(int link);
00161
00162 double getLinkRadius(int link);
00163
00165 short unsigned int getPlanningJointRadius();
00166
00168 void setResolution(double resolution);
00169
00173 void setRefFrameTransform(KDL::Frame f, std::string &name);
00174
00175 void getArmChainRootLinkName(std::string &name);
00176
00178 std::vector<std::vector<double> > getCollisionCuboids();
00179
00181 void setDebugFile(FILE* file);
00182
00184 void getJointLimits(int joint_num, double* min, double *max);
00185
00186 private:
00187
00189 std::string robot_description_;
00190
00192 std::string chain_root_name_;
00193
00195 std::string chain_tip_name_;
00196
00198 std::string reference_frame_;
00199
00201 urdf::Model robot_model_;
00202
00204 KDL::Tree kdl_tree_;
00205
00207 int planning_joint_;
00208
00210 std::string planning_joint_name_;
00211
00213 short unsigned int max_radius_;
00214
00216 std::vector<ArmJoint> joints_;
00217
00219 std::vector<ArmLink> links_;
00220
00222 std::vector<int> joint_indeces_;
00223
00224 KDL::Frame transform_;
00225
00226 KDL::Frame transform_inverse_;
00227
00228 std::vector<std::vector<double> > collision_cuboids_;
00229
00230 int num_collision_cuboids_;
00231
00232 FILE* fOut_;
00233
00234
00235 void parseKDLTree();
00236 int num_kdl_joints_;
00237 std::map<std::string, std::string> joint_segment_mapping_;
00238 std::map<std::string, std::string> segment_joint_mapping_;
00239 std::vector<std::string> kdl_number_to_urdf_name_;
00240 std::map<std::string, int> urdf_name_to_kdl_number_;
00242 KDL::JntArray jnt_pos_in_;
00243 KDL::JntArray jnt_pos_out_;
00244 KDL::Frame p_out_;
00245
00246 KDL::Chain chain_;
00247 KDL::ChainFkSolverPos_recursive *fk_solver_;
00248
00249 KDL::Chain ik_chain_;
00250 KDL::ChainFkSolverPos_recursive *ik_fk_solver_;
00251 KDL::ChainIkSolverPos_NR *ik_solver_;
00252 KDL::ChainIkSolverVel_pinv_givens *ik_solver_vel_;
00253 KDL::JntArray ik_jnt_pos_in_;
00254 KDL::JntArray ik_jnt_pos_out_;
00255
00256 pr2_arm_kinematics::PR2ArmIKSolver* pr2_arm_ik_solver_;
00257
00259 double frameToAxisAngle(const KDL::Frame frame, const double R_target[3][3]);
00260
00262 void getRPY(double Rot[3][3], double* roll, double* pitch, double* yaw, int solution_number);
00263 };
00264
00265
00266 inline short unsigned int SBPLArmModel::getLinkRadiusCells(int link)
00267 {
00268 if(link < num_links_)
00269 return links_[link].radius_c;
00270 return 0;
00271 }
00272
00273 inline double SBPLArmModel::getLinkRadius(int link)
00274 {
00275 if(link < num_links_)
00276 return links_[link].radius;
00277 return 0;
00278 }
00279
00280 inline int SBPLArmModel::getMaxLinkRadius()
00281 {
00282 return max_radius_;
00283 }
00284
00285 inline std::vector<std::vector<double> > SBPLArmModel::getCollisionCuboids()
00286 {
00287 return collision_cuboids_;
00288 }
00289
00290 #endif