00001
00020 #ifndef __FRAME
00021
00022 #define __FRAME
00023
00024 #include <vector>
00025
00026 #include "configuration.h"
00027 #include "vecmath.h"
00028 #include "datapairs.h"
00029 #include "interfaces.h"
00030
00031 namespace robotLibPbD {
00032
00039 class CFrame : public CCopyInterface
00040 {
00041 protected:
00042 enum {
00043 FRAME_X,
00044 FRAME_Y,
00045 FRAME_Z,
00046 FRAME_RX,
00047 FRAME_RY,
00048 FRAME_RZ,
00049 FRAME_DOFS
00050 };
00051
00052 std::vector<double> dofs_min, dofs_max;
00053 bool _isData;
00054
00055 CMatrix tmpMatrix;
00056 bool _isValid, _isUpdated, _isLocked;
00057 CFrame *base;
00058 CMatrix pose;
00059 CMatrix relativePose;
00060 std::string name, baseName;
00061 int baseType;
00062 int frameType;
00063 double time;
00064
00065 std::vector<CFrame*> parents, childs;
00066 public:
00067 unsigned long int counter, baseCounter;
00068
00069 enum
00070 {
00071 BASE_NORMAL,
00072 BASE_GEOMETRY,
00073 BASE_COMBINED
00074 };
00075
00076 enum
00077 {
00078 FRAME_POSITION,
00079 FRAME_VELOCITY
00080 };
00081
00082
00083 CFrame();
00084 CFrame(char* name);
00085 virtual ~CFrame();
00086
00087 virtual CMatrix getRelativeToBase();
00088 virtual void getRelativeToBase(CMatrix &mat);
00089
00090 std::vector<CFrame*>& getParents() { return parents; };
00091 void removeParent(CFrame *parent);
00092 void addParent(CFrame *parent);
00093 int getParentId(CFrame *parent) { for (unsigned int i=0; i<parents.size(); i++) if (parents[i] == parent) return (int)i; return -1; };
00094
00095 bool isData() { return _isData; };
00096 bool setData(bool value) { _isData = value; return value;};
00097 bool isValid() { return _isValid; };
00098 bool isUpdated() { return _isUpdated; };
00099 void lock() { _isLocked = true; };
00100 void unlock() { _isLocked = false; };
00101
00102 virtual void setName(const char* str);
00103 virtual char* getName() { return (char*) name.c_str(); };
00104 virtual void setBaseName(const char* str) { baseName = str; };
00105 virtual char* getBaseName() { return (char*) baseName.c_str(); };
00106 virtual bool hasName(char* str);
00107
00108 virtual void setBaseType(unsigned int type) { baseType = type; };
00109 virtual unsigned int getBaseType() { return baseType; };
00110
00111 virtual void setRelativePose(const CMatrix &value);
00112
00113 virtual void setFrameType(unsigned int type) { frameType = type; };
00114 virtual unsigned int getFrameType() { return frameType; };
00115
00116 virtual CFrame* getBase();
00117 virtual void setBase(CFrame* base);
00118 virtual CMatrix getPose() { return pose; };
00119 virtual void getPose(CMatrix &pose) { pose = this->pose; };
00120 inline virtual void setPose(const CMatrix &pose) { this->pose = pose; invalidate(); };
00121 inline virtual void setPoseNoInvalidation(const CMatrix &pose) { this->pose = pose;};
00122
00123 virtual CFrame* getByName(char* str);
00124 virtual void update();
00125 virtual void invalidate();
00126 virtual void invalidateAll();
00127
00128 void getDofs(std::vector<unsigned int> &dofs);
00129 void getDofs(std::vector<double> &dofs_min, std::vector<double> &dofs_max);
00130 void setDofs(const std::vector<double> &dofs_min, const std::vector<double> &dofs_max);
00131
00132
00133 virtual void setTime(double time) { this->time = time; };
00134 virtual double getTime() { return time; };
00135 virtual void* getCopy();
00136 };
00137
00138 class CFrameInterface
00139 {
00140 protected:
00141 CFrame* frame;
00142 public:
00143 CFrameInterface() { frame = NULL; };
00144 virtual void setFrame(CFrame* frame) { this->frame = frame; };
00145 virtual CFrame* getFrame() { return this->frame; };
00146 virtual void invalidate() { if (frame != NULL) frame->invalidate(); };
00147 virtual void invalidateAll() { if (frame != NULL) frame->invalidateAll(); };
00148 virtual std::string getFrameAsXml();
00149 };
00150
00151
00154 class CFrameReference : public CFrame
00155 {
00156 protected:
00157 CFrame *reference;
00158 public:
00159 CFrameReference(CFrame *reference);
00160 CFrameReference();
00161
00162 void setPose(const CMatrix &pose) { relativePose = this->pose = pose; };
00163 CMatrix getRelativeToBase();
00164 void getRelativeToBase(CMatrix &pose);
00165 };
00166
00169 class CFrameCombination : public CFrame
00170 {
00171 protected:
00172 CFrame *baseOrientation;
00173
00174 std::string baseOrientationName;
00175 public:
00176
00177 CFrameCombination();
00178 virtual ~CFrameCombination();
00179
00180 CMatrix getRelativeToBase();
00181 void getRelativeToBase(CMatrix &pose);
00182
00183 virtual void setBaseOrientationName(const char* str) { baseOrientationName = str; };
00184 virtual char* getBaseOrientationName() { return (char*) baseOrientationName.c_str(); };
00185
00186 virtual CFrame* getBaseOrientation();
00187 virtual void setBaseOrientation(CFrame* base);
00188
00189 virtual void invalidate();
00190 virtual void invalidateAll();
00191
00192 virtual void* getCopy();
00193 };
00194
00195
00196 class CFrameContainer
00197 {
00198 public:
00199 std::vector<CFrame*> frames;
00200
00201 CFrameContainer() { frames.reserve(1000); };
00202 virtual ~CFrameContainer() { clear(); };
00203
00204 void clear();
00205
00206 unsigned int size() { return frames.size(); };
00207 int add(CFrame* newFrame);
00208 bool getFrameByName(const char* name, CFrame* &frame);
00209 int getFrameByName(const char* name, bool create = false);
00210 bool setBase(char* frame, char* base);
00213 void invalidate(bool time = false);
00214 bool xmlToFrame(CFrame *frame, TiXmlElement* frameNode, bool create = false);
00215 bool xmlToFrame(CFrame *frame, TiXmlElement* frameNode, DataPairs &additionalData, bool create = false);
00216 bool xmlToFrameCombination(CFrameCombination *frame, TiXmlElement* frameNode, DataPairs &additionalData, bool create = false);
00217
00218 void updateBaseLinks();
00219 void updateBaseLinks(std::vector<CFrame*> &frames);
00220 void updateBaseLinks(CFrame* frame);
00221 void loadFromFile(const char* filename);
00222 void loadFromFile(const char* filename, DataPairs &additionalData, CConfiguration &config, bool loadAll);
00223
00224 CFrame* getFrame(unsigned int id) { if (id < frames.size()) return frames[id]; return NULL; };
00225 CFrame* getFrame(const char* name) { int id = getFrameByName(name, false); if (id < 0) return NULL; return frames[id]; };
00226
00227 std::vector<CFrame*> getFrames() { return frames; };
00228 int compareBase(CFrame* first, CFrame* second);
00229
00230 void checkBaseFrames();
00231
00232 static bool isRelativeTo(CFrame* first, CFrame* relative);
00233
00234 void resolve(std::vector<std::string> &in, std::vector<CFrame*> &out);
00235 };
00236
00237
00238
00241 class CDh
00242 {
00243 public:
00244 std::string ivModel;
00245 double speedFactor;
00246 bool rotationalDof;
00247 bool useAxis;
00248 CVec axis;
00249 double rot_z;
00250 double trans_z;
00251 double rot_x;
00252 double trans_x;
00253 double angle;
00254 double sgn;
00255 int id;
00256 CDh();
00257 double min, max;
00258 double getAngle();
00259 void setAngle(double angle);
00260 void set(double rot_z, double trans_z, double rot_x, double trans_x);
00261 };
00262
00263
00266 class CKinematicChain
00267 {
00268 protected:
00269 std::string name;
00270 CMatrix tmpMatrix;
00271 public:
00272 void setName(std::string value) { this->name = value; };
00273 std::string getName() { return this->name; };
00274 CFrame* lastPoseBuffer;
00275
00276 CDh *dhParameters;
00277
00278 CFrame **frames;
00279 int length;
00280
00281 double totalArmLength;
00282
00283 CKinematicChain();
00284 virtual ~CKinematicChain();
00285
00288 void update();
00289 CDh& getDhParameters(unsigned int id);
00290 CFrame* getFrame(unsigned int id);
00291
00292 CFrame* getLastFrame();
00295 CFrame* getByName(char* str);
00296
00299
00300
00303 void setLength(int len);
00304 unsigned int getLength() { return (unsigned int) length; };
00305
00308 void loadFromXml(CConfiguration &config, TiXmlElement* kinChainsNode, CFrameContainer &container);
00309
00310 void invalidate();
00311 };
00312
00317 class CKinematicChainContainer
00318 {
00319 public:
00320 int length;
00321
00322 CKinematicChain *chain;
00323
00324 unsigned int getLength() { return (unsigned int) length; };
00325
00326 CKinematicChainContainer();
00327 virtual ~CKinematicChainContainer();
00328
00329 void update();
00330 void loadFromXml(CConfiguration &config, TiXmlElement* kinChainsNode, CFrameContainer &container);
00331 };
00332
00333 inline CMatrix CFrameReference::getRelativeToBase()
00334 {
00335 if (_isValid || _isLocked)
00336 {
00337 return relativePose;
00338 }
00339 _isValid = true;
00340 reference->getRelativeToBase(relativePose);
00341 pose = relativePose;
00342 return relativePose;
00343 }
00344
00345 inline void CFrameReference::getRelativeToBase(CMatrix &mat)
00346 {
00347 if (_isValid || _isLocked)
00348 {
00349 mat = relativePose;
00350 return;
00351 }
00352 _isValid = true;
00353
00354 reference->getRelativeToBase(relativePose);
00355 mat = pose = relativePose;
00356 }
00357
00358
00359 inline CMatrix CFrameCombination::getRelativeToBase()
00360 {
00361 if (_isValid || _isLocked)
00362 {
00363 return relativePose;
00364 }
00365
00366 _isValid = true;
00367
00368 if (getBaseOrientation() != NULL)
00369 {
00370 getBaseOrientation()->getRelativeToBase(relativePose);
00371 relativePose.a[12] = 0.0;
00372 relativePose.a[13] = 0.0;
00373 relativePose.a[14] = 0.0;
00374 }
00375 else
00376 relativePose.unity();
00377
00378 if (getBase() != NULL)
00379 {
00380 getBase()->getRelativeToBase(tmpMatrix);
00381 relativePose.a[12] = tmpMatrix.a[12];
00382 relativePose.a[13] = tmpMatrix.a[13];
00383 relativePose.a[14] = tmpMatrix.a[14];
00384 }
00385
00386 relativePose.mul(relativePose, pose);
00387
00388 return relativePose;
00389 }
00390
00391 inline void CFrameCombination::getRelativeToBase(CMatrix &mat)
00392 {
00393 if (_isValid || _isLocked)
00394 {
00395 mat = relativePose;
00396 return;
00397 }
00398
00399 _isValid = true;
00400
00401 if (getBaseOrientation() != NULL)
00402 {
00403 getBaseOrientation()->getRelativeToBase(relativePose);
00404 relativePose.a[12] = 0.0;
00405 relativePose.a[13] = 0.0;
00406 relativePose.a[14] = 0.0;
00407 }
00408 else
00409 relativePose.unity();
00410
00411 if (getBase() != NULL)
00412 {
00413 getBase()->getRelativeToBase(tmpMatrix);
00414
00415 relativePose.a[12] = tmpMatrix.a[12];
00416 relativePose.a[13] = tmpMatrix.a[13];
00417 relativePose.a[14] = tmpMatrix.a[14];
00418 }
00419
00420 relativePose.mul(relativePose, pose);
00421
00422 mat = relativePose;
00423 }
00424
00425 inline void CKinematicChainContainer::update()
00426 {
00427 for (int i=0; i<length; i++)
00428 chain[i].update();
00429 }
00430
00431 inline void CKinematicChain::update()
00432 {
00433
00434
00435
00436 frames[0]->invalidate();
00437 for (int i=0; i<length; i++)
00438 {
00439 tmpMatrix.setDh(dhParameters[i]);
00440 frames[i]->setPoseNoInvalidation(tmpMatrix);
00441 }
00442 }
00443
00444
00445 inline void CKinematicChain::invalidate()
00446 {
00447 frames[0]->invalidate();
00448
00449
00450 }
00451
00452
00453 inline void CFrameContainer::invalidate(bool time)
00454 {
00455 for (unsigned int i=0; i<frames.size(); i++)
00456 {
00457 if (time)
00458 {
00459 frames[i]->unlock();
00460 }
00461 frames[i]->invalidate();
00462 }
00463 }
00464
00465
00466 inline void CFrameCombination::invalidate()
00467 {
00468
00469 _isValid = false;
00470
00471
00472
00473
00474
00475
00476
00477 for (unsigned int i=0; i<parents.size(); i++)
00478 parents[i]->invalidate();
00479 }
00480
00481
00482 inline CFrame* CFrame::getBase()
00483 {
00484 return base;
00485 }
00486
00487
00488 inline void CFrame::getRelativeToBase(CMatrix &mat)
00489 {
00490 if (_isValid || _isLocked)
00491 {
00492 mat = relativePose;
00493 return;
00494 }
00495
00496 if (getBase() != NULL)
00497 {
00498 getBase()->getRelativeToBase(tmpMatrix);
00499 relativePose.mulNoAlloc(tmpMatrix, pose);
00500 } else relativePose = pose;
00501
00502 _isValid = true;
00503
00504 mat = relativePose;
00505 }
00506
00507
00508 inline CMatrix CFrame::getRelativeToBase()
00509 {
00510
00511 if (_isValid || _isLocked)
00512 {
00513 return relativePose;
00514 }
00515
00516 if (getBase() != NULL)
00517 {
00518 getBase()->getRelativeToBase(tmpMatrix);
00519 relativePose.mulNoAlloc(tmpMatrix, pose);
00520 } else relativePose = pose;
00521
00522 _isValid = true;
00523
00524 return relativePose;
00525 }
00526 };
00527
00528 #endif