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
00031
00032
00033
00034
00037 #ifndef PLANNING_MODELS_KINEMATIC_MODEL_
00038 #define PLANNING_MODELS_KINEMATIC_MODEL_
00039
00040 #include <geometric_shapes/shapes.h>
00041
00042 #include <urdf/model.h>
00043 #include <LinearMath/btTransform.h>
00044 #include <boost/thread/shared_mutex.hpp>
00045
00046 #include <iostream>
00047 #include <vector>
00048 #include <string>
00049 #include <map>
00050 #include <boost/bimap.hpp>
00051
00053 namespace planning_models
00054 {
00055
00058 class KinematicModel
00059 {
00060 public:
00062 class JointModel;
00063
00065 class LinkModel;
00066
00068 class AttachedBodyModel;
00069
00070 struct MultiDofConfig
00071 {
00072 MultiDofConfig(std::string n) : name(n) {
00073 }
00074
00075 ~MultiDofConfig() {
00076 }
00077
00079 std::string name;
00080
00082 std::string type;
00083
00085 std::string parent_frame_id;
00086
00088 std::string child_frame_id;
00089
00091 std::map<std::string, std::string> name_equivalents;
00092 };
00093
00095 class JointModel
00096 {
00097 friend class KinematicModel;
00098 public:
00099 JointModel(const std::string& name);
00100
00101 JointModel(const JointModel* joint);
00102
00103 void initialize(const std::vector<std::string>& local_names,
00104 const MultiDofConfig* multi_dof_config = NULL);
00105
00106 virtual ~JointModel(void);
00107
00108 typedef boost::bimap< std::string, std::string > js_type;
00109
00110 const std::string& getName() const
00111 {
00112 return name_;
00113 }
00114
00115 const LinkModel* getParentLinkModel() const
00116 {
00117 return parent_link_model_;
00118 }
00119
00120 const LinkModel* getChildLinkModel() const
00121 {
00122 return child_link_model_;
00123 }
00124
00125 const std::string& getParentFrameId() const
00126 {
00127 return parent_frame_id_;
00128 }
00129
00130 const std::string& getChildFrameId() const
00131 {
00132 return child_frame_id_;
00133 }
00134
00135 const js_type& getJointStateEquivalents() const
00136 {
00137 return joint_state_equivalents_;
00138 }
00139
00140 const std::map<unsigned int, std::string>& getComputatationOrderMapIndex() const
00141 {
00142 return computation_order_map_index_;
00143 }
00145 std::string getEquiv(const std::string name) const;
00146
00148 std::pair<double, double> getVariableBounds(std::string variable) const;
00149
00151 void setVariableBounds(std::string variable, double low, double high);
00152
00153 const std::map<std::string, std::pair<double, double> >& getAllVariableBounds() const {
00154 return joint_state_bounds_;
00155 }
00156
00157 bool hasVariable(const std::string var) const
00158 {
00159 return(joint_state_equivalents_.right.find(var) != joint_state_equivalents_.right.end());
00160 }
00161
00162 virtual btTransform computeTransform(const std::vector<double>& joint_values) const = 0;
00163
00164 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const = 0;
00165
00166 private:
00167
00169 std::string name_;
00170
00172 LinkModel *parent_link_model_;
00173
00175 LinkModel *child_link_model_;
00176
00177
00178 js_type joint_state_equivalents_;
00179
00180
00181 std::map<std::string, std::pair<double, double> > joint_state_bounds_;
00182
00183
00184 std::map<unsigned int, std::string> computation_order_map_index_;
00185
00187 std::string parent_frame_id_;
00188
00190 std::string child_frame_id_;
00191 };
00192
00194 class FixedJointModel : public JointModel
00195 {
00196 public:
00197
00198 FixedJointModel(const std::string name, const MultiDofConfig* multi_dof_config) :
00199 JointModel(name)
00200 {
00201 std::vector<std::string> local_names;
00202 initialize(local_names, multi_dof_config);
00203 }
00204
00205 FixedJointModel(const FixedJointModel* joint): JointModel(joint)
00206 {
00207 }
00208
00209 virtual btTransform computeTransform(const std::vector<double>& joint_values) const {
00210 btTransform ident;
00211 ident.setIdentity();
00212 return ident;
00213 }
00214
00215 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const {
00216 std::vector<double> ret;
00217 return ret;
00218 }
00219
00220 };
00221
00223 class PlanarJointModel : public JointModel
00224 {
00225 public:
00226
00227 PlanarJointModel(const std::string& name, const MultiDofConfig* multi_dof_config);
00228
00229 PlanarJointModel(const PlanarJointModel* joint): JointModel(joint)
00230 {
00231 }
00232
00233 virtual btTransform computeTransform(const std::vector<double>& joint_values) const;
00234
00235 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const;
00236
00237 };
00238
00240 class FloatingJointModel : public JointModel
00241 {
00242 public:
00243
00244 FloatingJointModel(const std::string& name, const MultiDofConfig* multi_dof_config);
00245
00246 FloatingJointModel(const FloatingJointModel* joint) : JointModel(joint)
00247 {
00248 }
00249
00250 virtual btTransform computeTransform(const std::vector<double>& joint_values) const;
00251
00252 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const;
00253
00254 };
00255
00257 class PrismaticJointModel : public JointModel
00258 {
00259 public:
00260
00261 PrismaticJointModel(const std::string& name, const MultiDofConfig* multi_dof_config);
00262
00263 PrismaticJointModel(const PrismaticJointModel* joint) : JointModel(joint){
00264 axis_ = joint->axis_;
00265 }
00266
00267 btVector3 axis_;
00268
00269 virtual btTransform computeTransform(const std::vector<double>& joint_values) const;
00270
00271 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const;
00272
00273 };
00274
00276 class RevoluteJointModel : public JointModel
00277 {
00278 public:
00279
00280 RevoluteJointModel(const std::string& name, const MultiDofConfig* multi_dof_config);
00281
00282 RevoluteJointModel(const RevoluteJointModel* joint) : JointModel(joint){
00283 axis_ = joint->axis_;
00284 continuous_ = joint->continuous_;
00285 }
00286
00287 btVector3 axis_;
00288 bool continuous_;
00289
00290 virtual btTransform computeTransform(const std::vector<double>& joint_values) const;
00291
00292 virtual std::vector<double> computeJointStateValues(const btTransform& transform) const;
00293 };
00294
00296 class LinkModel
00297 {
00298 friend class KinematicModel;
00299 public:
00300
00301 LinkModel(const KinematicModel* kinematic_model);
00302
00303 LinkModel(const LinkModel* link_model);
00304
00305 ~LinkModel(void);
00306
00307 const std::string& getName() const {
00308 return name_;
00309 }
00310
00311 const JointModel* getParentJointModel() const {
00312 return parent_joint_model_;
00313 }
00314
00315 const std::vector<JointModel*>& getChildJointModels() const {
00316 return child_joint_models_;
00317 }
00318
00319 const btTransform& getJointOriginTransform() const {
00320 return joint_origin_transform_;
00321 }
00322
00323 const btTransform& getCollisionOriginTransform() const {
00324 return collision_origin_transform_;
00325 }
00326
00327 const shapes::Shape* getLinkShape() const {
00328 return shape_;
00329 }
00330
00331 const std::vector<AttachedBodyModel*>& getAttachedBodyModels() const {
00332 return attached_body_models_;
00333 }
00334
00335 private:
00336
00338 void clearAttachedBodyModels();
00339
00343 void replaceAttachedBodyModels(std::vector<AttachedBodyModel*>& attached_body_vector);
00344
00345 void clearLinkAttachedBodyModel(const std::string& att_name);
00346
00347 void addAttachedBodyModel(AttachedBodyModel* attached_body_model);
00348
00350 std::string name_;
00351
00353 const KinematicModel* kinematic_model_;
00354
00356 JointModel *parent_joint_model_;
00357
00359 std::vector<JointModel*> child_joint_models_;
00360
00362 btTransform joint_origin_transform_;
00363
00365 btTransform collision_origin_transform_;
00366
00368 shapes::Shape *shape_;
00369
00371 std::vector<AttachedBodyModel*> attached_body_models_;
00372 };
00373
00377 class AttachedBodyModel
00378 {
00379 public:
00380
00381 AttachedBodyModel(const LinkModel *link,
00382 const std::string& id,
00383 const std::vector<btTransform>& attach_trans,
00384 const std::vector<std::string>& touch_links,
00385 std::vector<shapes::Shape*>& shapes);
00386
00387 ~AttachedBodyModel(void);
00388
00389 const std::string& getName() const
00390 {
00391 return id_;
00392 }
00393
00394 const LinkModel* getAttachedLinkModel() const
00395 {
00396 return attached_link_model_;
00397 }
00398
00399 const std::vector<shapes::Shape*>& getShapes() const
00400 {
00401 return shapes_;
00402 }
00403
00404 const std::vector<btTransform>& getAttachedBodyFixedTransforms() const
00405 {
00406 return attach_trans_;
00407 }
00408
00409 const std::vector<std::string>& getTouchLinks() const
00410 {
00411 return touch_links_;
00412 }
00413
00414 private:
00415
00417 const LinkModel *attached_link_model_;
00418
00420 std::vector<shapes::Shape*> shapes_;
00421
00423 std::vector<btTransform> attach_trans_;
00424
00426 std::vector<std::string> touch_links_;
00427
00429 std::string id_;
00430 };
00431
00432 class JointModelGroup
00433 {
00434 friend class KinematicModel;
00435 public:
00436
00437 JointModelGroup(
00438 const std::string& groupName,
00439 const std::vector<const JointModel*> &groupJoints);
00440 ~JointModelGroup(void);
00441
00442 const std::string& getName() const
00443 {
00444 return name_;
00445 }
00446
00448 bool hasJointModel(const std::string &joint) const;
00449
00451 const JointModel* getJointModel(const std::string &joint);
00452
00453 const std::vector<const JointModel*>& getJointModels() const
00454 {
00455 return joint_model_vector_;
00456 }
00457
00458 const std::vector<std::string>& getJointModelNames() const
00459 {
00460 return joint_model_name_vector_;
00461 }
00462
00463 const std::vector<const LinkModel*>& getUpdatedLinkModels() const
00464 {
00465 return updated_link_model_vector_;
00466 }
00467
00468 const std::vector<const JointModel*>& getJointRoots() const
00469 {
00470 return joint_roots_;
00471 }
00472
00473 private:
00474
00476 std::string name_;
00477
00479 std::vector<std::string> joint_model_name_vector_;
00480
00482 std::vector<const JointModel*> joint_model_vector_;
00483
00485 std::map<std::string, const JointModel*> joint_model_map_;
00486
00488 std::vector<const JointModel*> joint_roots_;
00489
00491 std::vector<const LinkModel*> updated_link_model_vector_;
00492
00493 };
00494
00496 KinematicModel(const KinematicModel &source);
00497
00499 KinematicModel(const urdf::Model &model,
00500 const std::map< std::string, std::vector<std::string> > &groups,
00501 const std::vector<MultiDofConfig>& multi_dof_configs);
00502
00504 ~KinematicModel(void);
00505
00506 void copyFrom(const KinematicModel &source);
00507
00508 void defaultState(void);
00509
00511 const std::string& getName(void) const;
00512
00514 const LinkModel* getLinkModel(const std::string &link) const;
00515
00517 bool hasLinkModel(const std::string &name) const;
00518
00520 void getLinkModelNames(std::vector<std::string> &links) const;
00521
00523 void getChildLinkModels(const LinkModel* parent, std::vector<const LinkModel*> &links) const;
00524
00526 const JointModel* getJointModel(const std::string &joint) const;
00527
00529 bool hasJointModel(const std::string &name) const;
00530
00533 const std::vector<JointModel*>& getJointModels() const
00534 {
00535 return joint_model_vector_;
00536 }
00539 const std::vector<LinkModel*>& getLinkModels() const
00540 {
00541 return link_model_vector_;
00542 }
00543
00546 void getJointModelNames(std::vector<std::string> &joints) const;
00547
00549 const JointModel* getRoot(void) const;
00550
00552 void exclusiveLock(void) const;
00553
00555 void exclusiveUnlock(void) const;
00556
00558 void sharedLock(void) const;
00559
00561 void sharedUnlock(void) const;
00562
00564 void printModelInfo(std::ostream &out = std::cout) const;
00565
00566 bool hasModelGroup(const std::string& group) const;
00567
00568 const JointModelGroup* getModelGroup(const std::string& name) const
00569 {
00570 if(!hasModelGroup(name)) return NULL;
00571 return joint_model_group_map_.find(name)->second;
00572 }
00573
00574 const std::map<std::string, JointModelGroup*>& getJointModelGroupMap() const
00575 {
00576 return joint_model_group_map_;
00577 }
00578
00579 void getModelGroupNames(std::vector<std::string>& getModelGroupNames) const;
00580
00581 void clearAllAttachedBodyModels();
00582
00583 void clearLinkAttachedBodyModels(const std::string& link_name);
00584
00585 void clearLinkAttachedBodyModel(const std::string& link_name, const std::string& att_name);
00586
00587 void replaceAttachedBodyModels(const std::string& link_name, std::vector<AttachedBodyModel*>& attached_body_vector);
00588
00589 void addAttachedBodyModel(const std::string& link_name, AttachedBodyModel* att_body_model);
00590
00591 private:
00592
00594 mutable boost::shared_mutex lock_;
00595
00597 std::string model_name_;
00598
00600 std::map<std::string, LinkModel*> link_model_map_;
00601
00603 std::map<std::string, JointModel*> joint_model_map_;
00604
00606 std::vector<JointModel*> joint_model_vector_;
00607
00609 std::vector<LinkModel*> link_model_vector_;
00610
00612 JointModel *root_;
00613
00614 std::map<std::string, JointModelGroup*> joint_model_group_map_;
00615
00616 void buildGroups(const std::map< std::string, std::vector<std::string> > &groups);
00617 JointModel* buildRecursive(LinkModel *parent, const urdf::Link *link,
00618 const std::vector<MultiDofConfig>& multi_dof_configs);
00619 JointModel* constructJointModel(const urdf::Joint *urdfJointModel, const urdf::Link *child_link,
00620 const std::vector<MultiDofConfig>& multi_dof_configs);
00621 LinkModel* constructLinkModel(const urdf::Link *urdfLink);
00622 shapes::Shape* constructShape(const urdf::Geometry *geom);
00623
00624 JointModel* copyJointModel(const JointModel *joint);
00625 JointModel* copyRecursive(LinkModel *parent, const LinkModel *link);
00626
00627 };
00628
00629 }
00630
00631 #endif