Robot Dynamics Library
ReferenceFrame.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
2 // RDL - Robot Dynamics Library
3 // Licensed under the zlib license. See LICENSE for more details.
4 
5 #ifndef __RDL_REFERENCE_FRAME_HPP__
6 #define __RDL_REFERENCE_FRAME_HPP__
7 
12 #include <climits>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
20 
21 namespace RobotDynamics
22 {
67 class ReferenceFrame;
68 class FixedReferenceFrame;
69 typedef std::shared_ptr<ReferenceFrame> ReferenceFramePtr;
70 typedef std::shared_ptr<FixedReferenceFrame> FixedReferenceFramePtr;
71 typedef std::vector<ReferenceFramePtr> ReferenceFramePtrV;
72 typedef std::vector<FixedReferenceFramePtr> FixedReferenceFramePtrV;
85 {
86  public:
91  ReferenceFrame(const ReferenceFrame& referenceFrameToCopy)
92  : frameName(referenceFrameToCopy.frameName)
93  , parentFrame(referenceFrameToCopy.parentFrame)
94  , transformFromParent(referenceFrameToCopy.transformFromParent)
95  , transformToRoot(referenceFrameToCopy.transformToRoot)
96  , inverseTransformToRoot(referenceFrameToCopy.inverseTransformToRoot)
97  , rootFrame(referenceFrameToCopy.rootFrame)
98  , isWorldFrame(referenceFrameToCopy.isWorldFrame)
99  , isBodyFrame(referenceFrameToCopy.isBodyFrame)
100  , movableBodyId(referenceFrameToCopy.movableBodyId)
101  {
102  }
103 
114  // cppcheck-suppress passedByValue
116  unsigned int movableBodyId)
120  , isWorldFrame(false)
123  {
124  if (parentFrame == nullptr)
125  {
126  throw ReferenceFrameException("You are not allowed to create a frame with parentFrame=nullptr. Only a root frame and the world frame may have "
127  "parentFrame=nullptr");
128  }
129 
131 
132  while (root->getParentFrame() != nullptr)
133  {
134  root = root->getParentFrame();
135  }
136 
137  rootFrame = root.get();
138 
139  update();
140  }
141 
146  {
147  }
148 
150  {
151  return rootFrame;
152  }
153 
157  virtual ~ReferenceFrame()
158  {
159  }
160 
168  void update();
169 
175  // cppcheck-suppress passedByValue
177  {
178  transformToPack = getTransformToDesiredFrame(desiredFrame);
179  }
180 
187 
194 
201  {
202  this->transformFromParent = transformFromParent;
203  }
204 
209  void checkReferenceFramesMatch(ReferenceFramePtr referenceFrame) const;
210 
211  void checkReferenceFramesMatch(ReferenceFrame* referenceFrame) const;
212 
218  {
219  return this->transformToRoot;
220  }
221 
227  {
228  return this->inverseTransformToRoot;
229  }
230 
235  inline ReferenceFramePtr getParentFrame()
236  {
237  return this->parentFrame;
238  }
239 
244  inline std::string getName() const
245  {
246  return this->frameName;
247  }
248 
254  static ReferenceFramePtr createARootFrame(const std::string& frameName)
255  {
256  return ReferenceFramePtr(new ReferenceFrame(frameName, false, 0, true));
257  }
258 
264  {
265  return worldFrame;
266  }
267 
273  {
274  return this->transformFromParent;
275  }
276 
282  {
283  return this->transformFromParent.inverse();
284  }
285 
290  inline bool getIsWorldFrame() const
291  {
292  return this->isWorldFrame;
293  }
294 
299  inline unsigned int getMovableBodyId() const
300  {
301  return this->movableBodyId;
302  }
303 
309  inline bool getIsBodyFrame() const
310  {
311  return this->isBodyFrame;
312  }
313 
315  {
316  worldFrame = other.worldFrame;
317  frameName = other.frameName;
318  parentFrame = other.parentFrame;
322  isWorldFrame = other.isWorldFrame;
323  isBodyFrame = other.isBodyFrame;
325  rootFrame = other.rootFrame;
326 
327  return *this;
328  }
329 
330  protected:
338  ReferenceFrame(const std::string& frameName, bool isWorldFrame, unsigned int movableBodyId, bool isBodyFrame)
340  {
341  rootFrame = this;
342  update();
343  }
344 
350  static ReferenceFramePtr createAWorldFrame(const std::string& frameName = "world")
351  {
352  return ReferenceFramePtr(new ReferenceFrame(frameName, true, 0, true));
353  }
354 
356  std::string frameName;
367  bool isBodyFrame;
370  unsigned int movableBodyId;
372 };
373 
375 {
377  void operator=(const FixedReferenceFrame&) = delete;
378 
379  public:
380  // cppcheck-suppress passedByValue
382  unsigned int movableBodyId)
384  {
385  }
386 
388  {
389  }
390 
392  {
393  transformToRoot = parentFrame->getTransformToRoot() * transformFromParent.inverse();
394  return transformToRoot;
395  }
396 
402  {
403  inverseTransformToRoot = transformFromParent * parentFrame->getInverseTransformToRoot();
404  return inverseTransformToRoot;
405  }
406 };
407 } // namespace RobotDynamics
408 
409 #endif // ifndef __RDL_REFERENCE_FRAME_HPP__
Definition: ReferenceFrame.hpp:375
A custom exception for frame operations.
Definition: FrameExceptions.hpp:28
ReferenceFrame object used to tell what frame objects are expressed in. Every ReferenceFrame has a po...
Definition: ReferenceFrame.hpp:85
ReferenceFrame(const ReferenceFrame &referenceFrameToCopy)
Copy constructor.
Definition: ReferenceFrame.hpp:91
std::string frameName
Definition: ReferenceFrame.hpp:356
void operator=(const FixedReferenceFrame &)=delete
ReferenceFramePtr getParentFrame()
get a pointer to this frames parent
Definition: ReferenceFrame.hpp:235
virtual ~ReferenceFrame()
Destructor.
Definition: ReferenceFrame.hpp:157
ReferenceFrame * getRootFrame()
Definition: ReferenceFrame.hpp:149
RobotDynamics::Math::SpatialTransform transformToRoot
Definition: ReferenceFrame.hpp:359
void checkReferenceFramesMatch(ReferenceFramePtr referenceFrame) const
Check if the argument ReferenceFrame equals this.
Definition: ReferenceFrame.cpp:34
FixedReferenceFrame(const FixedReferenceFrame &)=delete
static ReferenceFramePtr worldFrame
Definition: ReferenceFrame.hpp:355
void verifyFramesHaveSameRoot(ReferenceFramePtr frame)
Check if two frames have the same roots.
Definition: ReferenceFrame.cpp:52
ReferenceFrame * rootFrame
Definition: ReferenceFrame.hpp:364
FixedReferenceFrame(const std::string &frameName, ReferenceFramePtr parentFrame, const RobotDynamics::Math::SpatialTransform &transformFromParent, unsigned int movableBodyId)
Definition: ReferenceFrame.hpp:381
static ReferenceFramePtr getWorldFrame()
Get a pointer to the world frame.
Definition: ReferenceFrame.hpp:263
RobotDynamics::Math::SpatialTransform transformFromParent
Definition: ReferenceFrame.hpp:358
std::string getName() const
Get the frame name.
Definition: ReferenceFrame.hpp:244
RobotDynamics::Math::SpatialTransform getTransformToParent()
Get spatial transform this frame to its parent.
Definition: ReferenceFrame.hpp:281
virtual RobotDynamics::Math::SpatialTransform getTransformToRoot()
Get this frames ReferenceFrame::transformToRoot.
Definition: ReferenceFrame.hpp:217
static ReferenceFramePtr createAWorldFrame(const std::string &frameName="world")
Helper method to create a world frame.
Definition: ReferenceFrame.hpp:350
ReferenceFrame & operator=(const ReferenceFrame &other)
Definition: ReferenceFrame.hpp:314
ReferenceFrame()
Empty constructor. All contained ptrs will be initialize to nullptr.
Definition: ReferenceFrame.hpp:145
bool getIsBodyFrame() const
Get boolean telling if this frame is a body frame or not. If it is a body frame, A pointer to this fr...
Definition: ReferenceFrame.hpp:309
unsigned int movableBodyId
Definition: ReferenceFrame.hpp:370
bool isWorldFrame
Definition: ReferenceFrame.hpp:365
std::shared_ptr< FixedReferenceFrame > FixedReferenceFramePtr
Definition: ReferenceFrame.hpp:70
void getTransformToDesiredFrame(RobotDynamics::Math::SpatialTransform &transformToPack, ReferenceFramePtr desiredFrame)
Get the spatial transform from this frame to desiredFrame and store it in transformToPack.
Definition: ReferenceFrame.hpp:176
unsigned int getMovableBodyId() const
Get the ID of the movable body this frame is attached to.
Definition: ReferenceFrame.hpp:299
RobotDynamics::Math::SpatialTransform getTransformFromParent()
Get spatial transform from parent to this frame.
Definition: ReferenceFrame.hpp:272
static ReferenceFramePtr createARootFrame(const std::string &frameName)
Creates a root frame with ReferenceFrame::parentFrame=nullptr.
Definition: ReferenceFrame.hpp:254
RobotDynamics::Math::SpatialTransform inverseTransformToRoot
Definition: ReferenceFrame.hpp:361
RobotDynamics::Math::SpatialTransform getInverseTransformToRoot() override
Get this frames ReferenceFrame::inverseTransformToRoot.
Definition: ReferenceFrame.hpp:401
std::shared_ptr< ReferenceFrame > ReferenceFramePtr
Definition: ReferenceFrame.hpp:68
ReferenceFramePtr parentFrame
Definition: ReferenceFrame.hpp:357
virtual RobotDynamics::Math::SpatialTransform getInverseTransformToRoot()
Get this frames ReferenceFrame::inverseTransformToRoot.
Definition: ReferenceFrame.hpp:226
std::vector< ReferenceFramePtr > ReferenceFramePtrV
Definition: ReferenceFrame.hpp:71
ReferenceFrame(const std::string &frameName, ReferenceFramePtr parentFrame, const RobotDynamics::Math::SpatialTransform &transformFromParent, bool isBodyFrame, unsigned int movableBodyId)
Constructor.
Definition: ReferenceFrame.hpp:115
void update()
Recalculates this frames ReferenceFrame::transformToRoot and ReferenceFrame::inverseTransformToRoot w...
Definition: ReferenceFrame.cpp:23
virtual ~FixedReferenceFrame()
Definition: ReferenceFrame.hpp:387
std::vector< FixedReferenceFramePtr > FixedReferenceFramePtrV
Definition: ReferenceFrame.hpp:72
bool getIsWorldFrame() const
Get a boolean telling if this frame is the world frame.
Definition: ReferenceFrame.hpp:290
void setTransformFromParent(const RobotDynamics::Math::SpatialTransform &transformFromParent)
Set a frames ReferenceFrame::transformToParent. For frames connected by a joint, this needs to be upd...
Definition: ReferenceFrame.hpp:200
RobotDynamics::Math::SpatialTransform getTransformToRoot() override
Get this frames ReferenceFrame::transformToRoot.
Definition: ReferenceFrame.hpp:391
bool isBodyFrame
Definition: ReferenceFrame.hpp:367
ReferenceFrame(const std::string &frameName, bool isWorldFrame, unsigned int movableBodyId, bool isBodyFrame)
Definition: ReferenceFrame.hpp:338
Namespace for all structures of the RobotDynamics library.
Definition: examples.hpp:19
Compact representation of spatial transformations.
Definition: rdl_eigenmath.hpp:412
SpatialTransform inverse() const
Returns inverse of transform.
Definition: rdl_eigenmath.hpp:565