frames.hpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /***************************************************************************
  2. frames.hpp `- description
  3. -------------------------
  4. begin : June 2006
  5. copyright : (C) 2006 Erwin Aertbelien
  6. email : firstname.lastname@mech.kuleuven.be
  7. History (only major changes)( AUTHOR-Description ) :
  8. ***************************************************************************
  9. * This library is free software; you can redistribute it and/or *
  10. * modify it under the terms of the GNU Lesser General Public *
  11. * License as published by the Free Software Foundation; either *
  12. * version 2.1 of the License, or (at your option) any later version. *
  13. * *
  14. * This library is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  17. * Lesser General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU Lesser General Public *
  20. * License along with this library; if not, write to the Free Software *
  21. * Foundation, Inc., 51 Franklin Street, *
  22. * Fifth Floor, Boston, MA 02110-1301, USA. *
  23. * *
  24. ***************************************************************************/
  25. /**
  26. * \file
  27. * \warning
  28. * Efficienty can be improved by writing p2 = A*(B*(C*p1))) instead of
  29. * p2=A*B*C*p1
  30. *
  31. * \par PROPOSED NAMING CONVENTION FOR FRAME-like OBJECTS
  32. *
  33. * \verbatim
  34. * A naming convention of objects of the type defined in this file :
  35. * (1) Frame : F...
  36. * Rotation : R ...
  37. * (2) Twist : T ...
  38. * Wrench : W ...
  39. * Vector : V ...
  40. * This prefix is followed by :
  41. * for category (1) :
  42. * F_A_B : w.r.t. frame A, frame B expressed
  43. * ( each column of F_A_B corresponds to an axis of B,
  44. * expressed w.r.t. frame A )
  45. * in mathematical convention :
  46. * A
  47. * F_A_B == F
  48. * B
  49. *
  50. * for category (2) :
  51. * V_B : a vector expressed w.r.t. frame B
  52. *
  53. * This can also be prepended by a name :
  54. * e.g. : temporaryV_B
  55. *
  56. * With this convention one can write :
  57. *
  58. * F_A_B = F_B_A.Inverse();
  59. * F_A_C = F_A_B * F_B_C;
  60. * V_B = F_B_C * V_C; // both translation and rotation
  61. * V_B = R_B_C * V_C; // only rotation
  62. * \endverbatim
  63. *
  64. * \par CONVENTIONS FOR WHEN USED WITH ROBOTS :
  65. *
  66. * \verbatim
  67. * world : represents the frame ([1 0 0,0 1 0,0 0 1],[0 0 0]')
  68. * mp : represents mounting plate of a robot
  69. * (i.e. everything before MP is constructed by robot manufacturer
  70. * everything after MP is tool )
  71. * tf : represents task frame of a robot
  72. * (i.e. frame in which motion and force control is expressed)
  73. * sf : represents sensor frame of a robot
  74. * (i.e. frame at which the forces measured by the force sensor
  75. * are expressed )
  76. *
  77. * Frame F_world_mp=...;
  78. * Frame F_mp_sf(..)
  79. * Frame F_mp_tf(,.)
  80. *
  81. * Wrench are measured in sensor frame SF, so one could write :
  82. * Wrench_tf = F_mp_tf.Inverse()* ( F_mp_sf * Wrench_sf );
  83. * \endverbatim
  84. *
  85. * \par CONVENTIONS REGARDING UNITS :
  86. * Any consistent series of units can be used, e.g. N,mm,Nmm,..mm/sec
  87. *
  88. * \par Twist and Wrench transformations
  89. * 3 different types of transformations do exist for the twists
  90. * and wrenches.
  91. *
  92. * \verbatim
  93. * 1) Frame * Twist or Frame * Wrench :
  94. * this transforms both the velocity/force reference point
  95. * and the basis to which the twist/wrench are expressed.
  96. * 2) Rotation * Twist or Rotation * Wrench :
  97. * this transforms the basis to which the twist/wrench are
  98. * expressed, but leaves the reference point intact.
  99. * 3) Twist.RefPoint(v_base_AB) or Wrench.RefPoint(v_base_AB)
  100. * this transforms only the reference point. v is expressed
  101. * in the same base as the twist/wrench and points from the
  102. * old reference point to the new reference point.
  103. * \endverbatim
  104. *
  105. * \par Complexity
  106. * Sometimes the amount of work is given in the documentation
  107. * e.g. 6M+3A means 6 multiplications and 3 additions.
  108. *
  109. * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven
  110. *
  111. ****************************************************************************/
  112. #ifndef KDL_FRAMES_H
  113. #define KDL_FRAMES_H
  114. #include "utilities/kdl-config.h"
  115. #include "utilities/utility.h"
  116. /////////////////////////////////////////////////////////////
  117. namespace KDL {
  118. class Vector;
  119. class Rotation;
  120. class Frame;
  121. class Wrench;
  122. class Twist;
  123. class Vector2;
  124. class Rotation2;
  125. class Frame2;
  126. /**
  127. * \brief A concrete implementation of a 3 dimensional vector class
  128. */
  129. class Vector
  130. {
  131. public:
  132. double data[3];
  133. //! Does not initialise the Vector to zero. use Vector::Zero() or SetToZero for that
  134. inline Vector() {data[0]=data[1]=data[2] = 0.0;}
  135. //! Constructs a vector out of the three values x, y and z
  136. inline Vector(double x,double y, double z);
  137. //! Constructs a vector out of an array of three values x, y and z
  138. inline Vector(double* xyz);
  139. //! Constructs a vector out of an array of three values x, y and z
  140. inline Vector(float* xyz);
  141. //! Assignment operator. The normal copy by value semantics.
  142. inline Vector(const Vector& arg);
  143. //! store vector components in array
  144. inline void GetValue(double* xyz) const;
  145. //! Assignment operator. The normal copy by value semantics.
  146. inline Vector& operator = ( const Vector& arg);
  147. //! Access to elements, range checked when NDEBUG is not set, from 0..2
  148. inline double operator()(int index) const;
  149. //! Access to elements, range checked when NDEBUG is not set, from 0..2
  150. inline double& operator() (int index);
  151. //! Equivalent to double operator()(int index) const
  152. double operator[] ( int index ) const
  153. {
  154. return this->operator() ( index );
  155. }
  156. //! Equivalent to double& operator()(int index)
  157. double& operator[] ( int index )
  158. {
  159. return this->operator() ( index );
  160. }
  161. inline double x() const;
  162. inline double y() const;
  163. inline double z() const;
  164. inline void x(double);
  165. inline void y(double);
  166. inline void z(double);
  167. //! Reverses the sign of the Vector object itself
  168. inline void ReverseSign();
  169. //! subtracts a vector from the Vector object itself
  170. inline Vector& operator-=(const Vector& arg);
  171. //! Adds a vector from the Vector object itself
  172. inline Vector& operator +=(const Vector& arg);
  173. //! Multiply by a scalar
  174. inline Vector& operator *=(double arg);
  175. //! Scalar multiplication is defined
  176. inline friend Vector operator*(const Vector& lhs,double rhs);
  177. //! Scalar multiplication is defined
  178. inline friend Vector operator*(double lhs,const Vector& rhs);
  179. //! Scalar division is defined
  180. inline friend Vector operator/(const Vector& lhs,double rhs);
  181. inline friend Vector operator+(const Vector& lhs,const Vector& rhs);
  182. inline friend Vector operator-(const Vector& lhs,const Vector& rhs);
  183. inline friend Vector operator*(const Vector& lhs,const Vector& rhs);
  184. inline friend Vector operator-(const Vector& arg);
  185. inline friend double dot(const Vector& lhs,const Vector& rhs);
  186. //! To have a uniform operator to put an element to zero, for scalar values
  187. //! and for objects.
  188. inline friend void SetToZero(Vector& v);
  189. //! @return a zero vector
  190. inline static Vector Zero();
  191. /** Normalizes this vector and returns it norm
  192. * makes v a unitvector and returns the norm of v.
  193. * if v is smaller than eps, Vector(1,0,0) is returned with norm 0.
  194. * if this is not good, check the return value of this method.
  195. */
  196. double Normalize(double eps=epsilon);
  197. //! @return the norm of the vector
  198. double Norm() const;
  199. //! a 3D vector where the 2D vector v is put in the XY plane
  200. inline void Set2DXY(const Vector2& v);
  201. //! a 3D vector where the 2D vector v is put in the YZ plane
  202. inline void Set2DYZ(const Vector2& v);
  203. //! a 3D vector where the 2D vector v is put in the ZX plane
  204. inline void Set2DZX(const Vector2& v);
  205. //! a 3D vector where the 2D vector v_XY is put in the XY plane of the frame F_someframe_XY.
  206. inline void Set2DPlane(const Frame& F_someframe_XY,const Vector2& v_XY);
  207. //! do not use operator == because the definition of Equal(.,.) is slightly
  208. //! different. It compares whether the 2 arguments are equal in an eps-interval
  209. inline friend bool Equal(const Vector& a,const Vector& b,double eps);
  210. //! return a normalized vector
  211. inline friend Vector Normalize(const Vector& a, double eps);
  212. //! The literal equality operator==(), also identical.
  213. inline friend bool operator==(const Vector& a,const Vector& b);
  214. //! The literal inequality operator!=().
  215. inline friend bool operator!=(const Vector& a,const Vector& b);
  216. friend class Rotation;
  217. friend class Frame;
  218. };
  219. inline Vector Normalize(const Vector&, double eps=epsilon);
  220. /**
  221. \brief represents rotations in 3 dimensional space.
  222. This class represents a rotation matrix with the following
  223. conventions :
  224. \verbatim
  225. Suppose V2 = R*V, (1)
  226. V is expressed in frame B
  227. V2 is expressed in frame A
  228. This matrix R consists of 3 collumns [ X,Y,Z ],
  229. X,Y, and Z contain the axes of frame B, expressed in frame A
  230. Because of linearity expr(1) is valid.
  231. \endverbatim
  232. This class only represents rotational_interpolation, not translation
  233. Two interpretations are possible for rotation angles.
  234. * if you rotate with angle around X frame A to have frame B,
  235. then the result of SetRotX is equal to frame B expressed wrt A.
  236. In code:
  237. \verbatim
  238. Rotation R;
  239. F_A_B = R.SetRotX(angle);
  240. \endverbatim
  241. * Secondly, if you take the following code :
  242. \verbatim
  243. Vector p,p2; Rotation R;
  244. R.SetRotX(angle);
  245. p2 = R*p;
  246. \endverbatim
  247. then the frame p2 is rotated around X axis with (-angle).
  248. Analogue reasonings can be applyd to SetRotY,SetRotZ,SetRot
  249. \par type
  250. Concrete implementation
  251. */
  252. class Rotation
  253. {
  254. public:
  255. double data[9];
  256. inline Rotation() {
  257. *this = Identity();
  258. }
  259. inline Rotation(double Xx,double Yx,double Zx,
  260. double Xy,double Yy,double Zy,
  261. double Xz,double Yz,double Zz);
  262. inline Rotation(const Vector& x,const Vector& y,const Vector& z);
  263. // default copy constructor is sufficient
  264. inline void setValue(float* oglmat);
  265. inline void getValue(float* oglmat) const;
  266. inline Rotation& operator=(const Rotation& arg);
  267. //! Defines a multiplication R*V between a Rotation R and a Vector V.
  268. //! Complexity : 9M+6A
  269. inline Vector operator*(const Vector& v) const;
  270. //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set
  271. inline double& operator()(int i,int j);
  272. //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set
  273. inline double operator() (int i,int j) const;
  274. friend Rotation operator *(const Rotation& lhs,const Rotation& rhs);
  275. //! Sets the value of *this to its inverse.
  276. inline void SetInverse();
  277. //! Gives back the inverse rotation matrix of *this.
  278. inline Rotation Inverse() const;
  279. //! The same as R.Inverse()*v but more efficient.
  280. inline Vector Inverse(const Vector& v) const;
  281. //! The same as R.Inverse()*arg but more efficient.
  282. inline Wrench Inverse(const Wrench& arg) const;
  283. //! The same as R.Inverse()*arg but more efficient.
  284. inline Twist Inverse(const Twist& arg) const;
  285. //! Gives back an identity rotaton matrix
  286. inline static Rotation Identity();
  287. // = Rotations
  288. //! The Rot... static functions give the value of the appropriate rotation matrix back.
  289. inline static Rotation RotX(double angle);
  290. //! The Rot... static functions give the value of the appropriate rotation matrix back.
  291. inline static Rotation RotY(double angle);
  292. //! The Rot... static functions give the value of the appropriate rotation matrix back.
  293. inline static Rotation RotZ(double angle);
  294. //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot..
  295. //! DoRot... functions are only defined when they can be executed more efficiently
  296. inline void DoRotX(double angle);
  297. //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot..
  298. //! DoRot... functions are only defined when they can be executed more efficiently
  299. inline void DoRotY(double angle);
  300. //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot..
  301. //! DoRot... functions are only defined when they can be executed more efficiently
  302. inline void DoRotZ(double angle);
  303. //! Along an arbitrary axes. It is not necessary to normalize rotaxis.
  304. //! returns identity rotation matrix in the case that the norm of rotaxis
  305. //! is to small to be used.
  306. // @see Rot2 if you want to handle this error in another way.
  307. static Rotation Rot(const Vector& rotaxis,double angle);
  308. //! Along an arbitrary axes. rotvec should be normalized.
  309. static Rotation Rot2(const Vector& rotvec,double angle);
  310. // make sure the matrix is a pure rotation (no scaling)
  311. void Ortho();
  312. //! Returns a vector with the direction of the equiv. axis
  313. //! and its norm is angle
  314. Vector GetRot() const;
  315. //! Returns a 2D vector representing the equivalent rotation in the XZ plane that brings the
  316. //! Y axis onto the Matrix Y axis and its norm is angle
  317. Vector2 GetXZRot() const;
  318. /** Returns the rotation angle around the equiv. axis
  319. * @param axis the rotation axis is returned in this variable
  320. * @param eps : in the case of angle == 0 : rot axis is undefined and choosen
  321. * to be +/- Z-axis
  322. * in the case of angle == PI : 2 solutions, positive Z-component
  323. * of the axis is choosen.
  324. * @result returns the rotation angle (between [0..PI] )
  325. */
  326. double GetRotAngle(Vector& axis,double eps=epsilon) const;
  327. //! Gives back a rotation matrix specified with EulerZYZ convention :
  328. //! First rotate around Z with alfa,
  329. //! then around the new Y with beta, then around
  330. //! new Z with gamma.
  331. static Rotation EulerZYZ(double Alfa,double Beta,double Gamma);
  332. //! Gives back the EulerZYZ convention description of the rotation matrix :
  333. //! First rotate around Z with alfa,
  334. //! then around the new Y with beta, then around
  335. //! new Z with gamma.
  336. //!
  337. //! Variables are bound by
  338. //! (-PI <= alfa <= PI),
  339. //! (0 <= beta <= PI),
  340. //! (-PI <= alfa <= PI)
  341. void GetEulerZYZ(double& alfa,double& beta,double& gamma) const;
  342. //! Sets the value of this object to a rotation specified with RPY convention:
  343. //! first rotate around X with roll, then around the
  344. //! old Y with pitch, then around old Z with alfa
  345. static Rotation RPY(double roll,double pitch,double yaw);
  346. //! Gives back a vector in RPY coordinates, variables are bound by
  347. //! -PI <= roll <= PI
  348. //! -PI <= Yaw <= PI
  349. //! -PI/2 <= PITCH <= PI/2
  350. //!
  351. //! convention : first rotate around X with roll, then around the
  352. //! old Y with pitch, then around old Z with alfa
  353. void GetRPY(double& roll,double& pitch,double& yaw) const;
  354. //! Gives back a rotation matrix specified with EulerZYX convention :
  355. //! First rotate around Z with alfa,
  356. //! then around the new Y with beta, then around
  357. //! new X with gamma.
  358. //!
  359. //! closely related to RPY-convention
  360. inline static Rotation EulerZYX(double Alfa,double Beta,double Gamma) {
  361. return RPY(Gamma,Beta,Alfa);
  362. }
  363. //! GetEulerZYX gets the euler ZYX parameters of a rotation :
  364. //! First rotate around Z with alfa,
  365. //! then around the new Y with beta, then around
  366. //! new X with gamma.
  367. //!
  368. //! Range of the results of GetEulerZYX :
  369. //! -PI <= alfa <= PI
  370. //! -PI <= gamma <= PI
  371. //! -PI/2 <= beta <= PI/2
  372. //!
  373. //! Closely related to RPY-convention.
  374. inline void GetEulerZYX(double& Alfa,double& Beta,double& Gamma) const {
  375. GetRPY(Gamma,Beta,Alfa);
  376. }
  377. //! Transformation of the base to which the twist is expressed.
  378. //! Complexity : 18M+12A
  379. //! @see Frame*Twist for a transformation that also transforms
  380. //! the velocity reference point.
  381. inline Twist operator * (const Twist& arg) const;
  382. //! Transformation of the base to which the wrench is expressed.
  383. //! Complexity : 18M+12A
  384. //! @see Frame*Wrench for a transformation that also transforms
  385. //! the force reference point.
  386. inline Wrench operator * (const Wrench& arg) const;
  387. //! Access to the underlying unitvectors of the rotation matrix
  388. inline Vector UnitX() const {
  389. return Vector(data[0],data[3],data[6]);
  390. }
  391. //! Access to the underlying unitvectors of the rotation matrix
  392. inline void UnitX(const Vector& X) {
  393. data[0] = X(0);
  394. data[3] = X(1);
  395. data[6] = X(2);
  396. }
  397. //! Access to the underlying unitvectors of the rotation matrix
  398. inline Vector UnitY() const {
  399. return Vector(data[1],data[4],data[7]);
  400. }
  401. //! Access to the underlying unitvectors of the rotation matrix
  402. inline void UnitY(const Vector& X) {
  403. data[1] = X(0);
  404. data[4] = X(1);
  405. data[7] = X(2);
  406. }
  407. //! Access to the underlying unitvectors of the rotation matrix
  408. inline Vector UnitZ() const {
  409. return Vector(data[2],data[5],data[8]);
  410. }
  411. //! Access to the underlying unitvectors of the rotation matrix
  412. inline void UnitZ(const Vector& X) {
  413. data[2] = X(0);
  414. data[5] = X(1);
  415. data[8] = X(2);
  416. }
  417. //! do not use operator == because the definition of Equal(.,.) is slightly
  418. //! different. It compares whether the 2 arguments are equal in an eps-interval
  419. //! The literal equality operator==(), also identical.
  420. friend bool operator==(const Rotation& a,const Rotation& b);
  421. //! The literal inequality operator!=()
  422. friend bool operator!=(const Rotation& a,const Rotation& b);
  423. friend class Frame;
  424. };
  425. bool operator==(const Rotation& a,const Rotation& b);
  426. /**
  427. \brief represents a frame transformation in 3D space (rotation + translation)
  428. if V2 = Frame*V1 (V2 expressed in frame A, V1 expressed in frame B)
  429. then V2 = Frame.M*V1+Frame.p
  430. Frame.M contains columns that represent the axes of frame B wrt frame A
  431. Frame.p contains the origin of frame B expressed in frame A.
  432. */
  433. class Frame {
  434. public:
  435. Vector p; //!< origine of the Frame
  436. Rotation M; //!< Orientation of the Frame
  437. public:
  438. inline Frame(const Rotation& R,const Vector& V);
  439. //! The rotation matrix defaults to identity
  440. explicit inline Frame(const Vector& V);
  441. //! The position matrix defaults to zero
  442. explicit inline Frame(const Rotation& R);
  443. inline void setValue(float* oglmat);
  444. inline void getValue(float* oglmat) const;
  445. inline Frame() {}
  446. //! The copy constructor. Normal copy by value semantics.
  447. inline Frame(const Frame& arg);
  448. //! Reads data from an double array
  449. //\TODO should be formulated as a constructor
  450. void Make4x4(double* d);
  451. //! Treats a frame as a 4x4 matrix and returns element i,j
  452. //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set
  453. inline double operator()(int i,int j);
  454. //! Treats a frame as a 4x4 matrix and returns element i,j
  455. //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set
  456. inline double operator() (int i,int j) const;
  457. // = Inverse
  458. //! Gives back inverse transformation of a Frame
  459. inline Frame Inverse() const;
  460. //! The same as p2=R.Inverse()*p but more efficient.
  461. inline Vector Inverse(const Vector& arg) const;
  462. //! The same as p2=R.Inverse()*p but more efficient.
  463. inline Wrench Inverse(const Wrench& arg) const;
  464. //! The same as p2=R.Inverse()*p but more efficient.
  465. inline Twist Inverse(const Twist& arg) const;
  466. //! Normal copy-by-value semantics.
  467. inline Frame& operator = (const Frame& arg);
  468. //! Transformation of the base to which the vector
  469. //! is expressed.
  470. inline Vector operator * (const Vector& arg) const;
  471. //! Transformation of both the force reference point
  472. //! and of the base to which the wrench is expressed.
  473. //! look at Rotation*Wrench operator for a transformation
  474. //! of only the base to which the twist is expressed.
  475. //!
  476. //! Complexity : 24M+18A
  477. inline Wrench operator * (const Wrench& arg) const;
  478. //! Transformation of both the velocity reference point
  479. //! and of the base to which the twist is expressed.
  480. //! look at Rotation*Twist for a transformation of only the
  481. //! base to which the twist is expressed.
  482. //!
  483. //! Complexity : 24M+18A
  484. inline Twist operator * (const Twist& arg) const;
  485. //! Composition of two frames.
  486. inline friend Frame operator *(const Frame& lhs,const Frame& rhs);
  487. //! @return the identity transformation Frame(Rotation::Identity(),Vector::Zero()).
  488. inline static Frame Identity();
  489. //! The twist <t_this> is expressed wrt the current
  490. //! frame. This frame is integrated into an updated frame with
  491. //! <samplefrequency>. Very simple first order integration rule.
  492. inline void Integrate(const Twist& t_this,double frequency);
  493. /*
  494. // DH_Craig1989 : constructs a transformationmatrix
  495. // T_link(i-1)_link(i) with the Denavit-Hartenberg convention as
  496. // described in the Craigs book: Craig, J. J.,Introduction to
  497. // Robotics: Mechanics and Control, Addison-Wesley,
  498. // isbn:0-201-10326-5, 1986.
  499. //
  500. // Note that the frame is a redundant way to express the information
  501. // in the DH-convention.
  502. // \verbatim
  503. // Parameters in full : a(i-1),alpha(i-1),d(i),theta(i)
  504. //
  505. // axis i-1 is connected by link i-1 to axis i numbering axis 1
  506. // to axis n link 0 (immobile base) to link n
  507. //
  508. // link length a(i-1) length of the mutual perpendicular line
  509. // (normal) between the 2 axes. This normal runs from (i-1) to
  510. // (i) axis.
  511. //
  512. // link twist alpha(i-1): construct plane perpendicular to the
  513. // normal project axis(i-1) and axis(i) into plane angle from
  514. // (i-1) to (i) measured in the direction of the normal
  515. //
  516. // link offset d(i) signed distance between normal (i-1) to (i)
  517. // and normal (i) to (i+1) along axis i joint angle theta(i)
  518. // signed angle between normal (i-1) to (i) and normal (i) to
  519. // (i+1) along axis i
  520. //
  521. // First and last joints : a(0)= a(n) = 0
  522. // alpha(0) = alpha(n) = 0
  523. //
  524. // PRISMATIC : theta(1) = 0 d(1) arbitrarily
  525. //
  526. // REVOLUTE : theta(1) arbitrarily d(1) = 0
  527. //
  528. // Not unique : if intersecting joint axis 2 choices for normal
  529. // Frame assignment of the DH convention : Z(i-1) follows axis
  530. // (i-1) X(i-1) is the normal between axis(i-1) and axis(i)
  531. // Y(i-1) follows out of Z(i-1) and X(i-1)
  532. //
  533. // a(i-1) = distance from Z(i-1) to Z(i) along X(i-1)
  534. // alpha(i-1) = angle between Z(i-1) to Z(i) along X(i-1)
  535. // d(i) = distance from X(i-1) to X(i) along Z(i)
  536. // theta(i) = angle between X(i-1) to X(i) along X(i)
  537. // \endverbatim
  538. */
  539. static Frame DH_Craig1989(double a,double alpha,double d,double theta);
  540. // DH : constructs a transformationmatrix T_link(i-1)_link(i) with
  541. // the Denavit-Hartenberg convention as described in the original
  542. // publictation: Denavit, J. and Hartenberg, R. S., A kinematic
  543. // notation for lower-pair mechanisms based on matrices, ASME
  544. // Journal of Applied Mechanics, 23:215-221, 1955.
  545. static Frame DH(double a,double alpha,double d,double theta);
  546. //! do not use operator == because the definition of Equal(.,.) is slightly
  547. //! different. It compares whether the 2 arguments are equal in an eps-interval
  548. inline friend bool Equal(const Frame& a,const Frame& b,double eps);
  549. //! The literal equality operator==(), also identical.
  550. inline friend bool operator==(const Frame& a,const Frame& b);
  551. //! The literal inequality operator!=().
  552. inline friend bool operator!=(const Frame& a,const Frame& b);
  553. };
  554. /**
  555. * \brief represents both translational and rotational velocities.
  556. *
  557. * This class represents a twist. A twist is the combination of translational
  558. * velocity and rotational velocity applied at one point.
  559. */
  560. class Twist {
  561. public:
  562. Vector vel; //!< The velocity of that point
  563. Vector rot; //!< The rotational velocity of that point.
  564. public:
  565. //! The default constructor initialises to Zero via the constructor of Vector.
  566. Twist():vel(),rot() {};
  567. Twist(const Vector& _vel,const Vector& _rot):vel(_vel),rot(_rot) {};
  568. inline Twist& operator-=(const Twist& arg);
  569. inline Twist& operator+=(const Twist& arg);
  570. //! index-based access to components, first vel(0..2), then rot(3..5)
  571. inline double& operator()(int i);
  572. //! index-based access to components, first vel(0..2), then rot(3..5)
  573. //! For use with a const Twist
  574. inline double operator()(int i) const;
  575. double operator[] ( int index ) const
  576. {
  577. return this->operator() ( index );
  578. }
  579. double& operator[] ( int index )
  580. {
  581. return this->operator() ( index );
  582. }
  583. inline friend Twist operator*(const Twist& lhs,double rhs);
  584. inline friend Twist operator*(double lhs,const Twist& rhs);
  585. inline friend Twist operator/(const Twist& lhs,double rhs);
  586. inline friend Twist operator+(const Twist& lhs,const Twist& rhs);
  587. inline friend Twist operator-(const Twist& lhs,const Twist& rhs);
  588. inline friend Twist operator-(const Twist& arg);
  589. inline friend double dot(const Twist& lhs,const Wrench& rhs);
  590. inline friend double dot(const Wrench& rhs,const Twist& lhs);
  591. inline friend void SetToZero(Twist& v);
  592. //! @return a zero Twist : Twist(Vector::Zero(),Vector::Zero())
  593. static inline Twist Zero();
  594. //! Reverses the sign of the twist
  595. inline void ReverseSign();
  596. //! Changes the reference point of the twist.
  597. //! The vector v_base_AB is expressed in the same base as the twist
  598. //! The vector v_base_AB is a vector from the old point to
  599. //! the new point.
  600. //!
  601. //! Complexity : 6M+6A
  602. inline Twist RefPoint(const Vector& v_base_AB) const;
  603. //! do not use operator == because the definition of Equal(.,.) is slightly
  604. //! different. It compares whether the 2 arguments are equal in an eps-interval
  605. inline friend bool Equal(const Twist& a,const Twist& b,double eps);
  606. //! The literal equality operator==(), also identical.
  607. inline friend bool operator==(const Twist& a,const Twist& b);
  608. //! The literal inequality operator!=().
  609. inline friend bool operator!=(const Twist& a,const Twist& b);
  610. // = Friends
  611. friend class Rotation;
  612. friend class Frame;
  613. };
  614. /**
  615. * \brief represents both translational and rotational acceleration.
  616. *
  617. * This class represents an acceleration twist. A acceleration twist is
  618. * the combination of translational
  619. * acceleration and rotational acceleration applied at one point.
  620. */
  621. /*
  622. class AccelerationTwist {
  623. public:
  624. Vector trans; //!< The translational acceleration of that point
  625. Vector rot; //!< The rotational acceleration of that point.
  626. public:
  627. //! The default constructor initialises to Zero via the constructor of Vector.
  628. AccelerationTwist():trans(),rot() {};
  629. AccelerationTwist(const Vector& _trans,const Vector& _rot):trans(_trans),rot(_rot) {};
  630. inline AccelerationTwist& operator-=(const AccelerationTwist& arg);
  631. inline AccelerationTwist& operator+=(const AccelerationTwist& arg);
  632. //! index-based access to components, first vel(0..2), then rot(3..5)
  633. inline double& operator()(int i);
  634. //! index-based access to components, first vel(0..2), then rot(3..5)
  635. //! For use with a const AccelerationTwist
  636. inline double operator()(int i) const;
  637. double operator[] ( int index ) const
  638. {
  639. return this->operator() ( index );
  640. }
  641. double& operator[] ( int index )
  642. {
  643. return this->operator() ( index );
  644. }
  645. inline friend AccelerationTwist operator*(const AccelerationTwist& lhs,double rhs);
  646. inline friend AccelerationTwist operator*(double lhs,const AccelerationTwist& rhs);
  647. inline friend AccelerationTwist operator/(const AccelerationTwist& lhs,double rhs);
  648. inline friend AccelerationTwist operator+(const AccelerationTwist& lhs,const AccelerationTwist& rhs);
  649. inline friend AccelerationTwist operator-(const AccelerationTwist& lhs,const AccelerationTwist& rhs);
  650. inline friend AccelerationTwist operator-(const AccelerationTwist& arg);
  651. //inline friend double dot(const AccelerationTwist& lhs,const Wrench& rhs);
  652. //inline friend double dot(const Wrench& rhs,const AccelerationTwist& lhs);
  653. inline friend void SetToZero(AccelerationTwist& v);
  654. //! @return a zero AccelerationTwist : AccelerationTwist(Vector::Zero(),Vector::Zero())
  655. static inline AccelerationTwist Zero();
  656. //! Reverses the sign of the AccelerationTwist
  657. inline void ReverseSign();
  658. //! Changes the reference point of the AccelerationTwist.
  659. //! The vector v_base_AB is expressed in the same base as the AccelerationTwist
  660. //! The vector v_base_AB is a vector from the old point to
  661. //! the new point.
  662. //!
  663. //! Complexity : 6M+6A
  664. inline AccelerationTwist RefPoint(const Vector& v_base_AB) const;
  665. //! do not use operator == because the definition of Equal(.,.) is slightly
  666. //! different. It compares whether the 2 arguments are equal in an eps-interval
  667. inline friend bool Equal(const AccelerationTwist& a,const AccelerationTwist& b,double eps=epsilon);
  668. //! The literal equality operator==(), also identical.
  669. inline friend bool operator==(const AccelerationTwist& a,const AccelerationTwist& b);
  670. //! The literal inequality operator!=().
  671. inline friend bool operator!=(const AccelerationTwist& a,const AccelerationTwist& b);
  672. // = Friends
  673. friend class Rotation;
  674. friend class Frame;
  675. };
  676. */
  677. /**
  678. * \brief represents the combination of a force and a torque.
  679. *
  680. * This class represents a Wrench. A Wrench is the force and torque applied at a point
  681. */
  682. class Wrench
  683. {
  684. public:
  685. Vector force; //!< Force that is applied at the origin of the current ref frame
  686. Vector torque; //!< Torque that is applied at the origin of the current ref frame
  687. public:
  688. //! Does initialise force and torque to zero via the underlying constructor of Vector
  689. Wrench():force(),torque() {};
  690. Wrench(const Vector& _force,const Vector& _torque):force(_force),torque(_torque) {};
  691. // = Operators
  692. inline Wrench& operator-=(const Wrench& arg);
  693. inline Wrench& operator+=(const Wrench& arg);
  694. //! index-based access to components, first force(0..2), then torque(3..5)
  695. inline double& operator()(int i);
  696. //! index-based access to components, first force(0..2), then torque(3..5)
  697. //! for use with a const Wrench
  698. inline double operator()(int i) const;
  699. double operator[] ( int index ) const
  700. {
  701. return this->operator() ( index );
  702. }
  703. double& operator[] ( int index )
  704. {
  705. return this->operator() ( index );
  706. }
  707. //! Scalar multiplication
  708. inline friend Wrench operator*(const Wrench& lhs,double rhs);
  709. //! Scalar multiplication
  710. inline friend Wrench operator*(double lhs,const Wrench& rhs);
  711. //! Scalar division
  712. inline friend Wrench operator/(const Wrench& lhs,double rhs);
  713. inline friend Wrench operator+(const Wrench& lhs,const Wrench& rhs);
  714. inline friend Wrench operator-(const Wrench& lhs,const Wrench& rhs);
  715. //! An unary - operator
  716. inline friend Wrench operator-(const Wrench& arg);
  717. //! Sets the Wrench to Zero, to have a uniform function that sets an object or
  718. //! double to zero.
  719. inline friend void SetToZero(Wrench& v);
  720. //! @return a zero Wrench
  721. static inline Wrench Zero();
  722. //! Reverses the sign of the current Wrench
  723. inline void ReverseSign();
  724. //! Changes the reference point of the wrench.
  725. //! The vector v_base_AB is expressed in the same base as the twist
  726. //! The vector v_base_AB is a vector from the old point to
  727. //! the new point.
  728. //!
  729. //! Complexity : 6M+6A
  730. inline Wrench RefPoint(const Vector& v_base_AB) const;
  731. //! do not use operator == because the definition of Equal(.,.) is slightly
  732. //! different. It compares whether the 2 arguments are equal in an eps-interval
  733. inline friend bool Equal(const Wrench& a,const Wrench& b,double eps);
  734. //! The literal equality operator==(), also identical.
  735. inline friend bool operator==(const Wrench& a,const Wrench& b);
  736. //! The literal inequality operator!=().
  737. inline friend bool operator!=(const Wrench& a,const Wrench& b);
  738. friend class Rotation;
  739. friend class Frame;
  740. };
  741. //! 2D version of Vector
  742. class Vector2
  743. {
  744. double data[2];
  745. public:
  746. //! Does not initialise to Zero().
  747. Vector2() {data[0]=data[1] = 0.0;}
  748. inline Vector2(double x,double y);
  749. inline Vector2(const Vector2& arg);
  750. inline Vector2(double* xyz);
  751. inline Vector2(float* xyz);
  752. inline Vector2& operator = ( const Vector2& arg);
  753. //! Access to elements, range checked when NDEBUG is not set, from 0..1
  754. inline double operator()(int index) const;
  755. //! Access to elements, range checked when NDEBUG is not set, from 0..1
  756. inline double& operator() (int index);
  757. //! store vector components in array
  758. inline void GetValue(double* xy) const;
  759. inline void ReverseSign();
  760. inline Vector2& operator-=(const Vector2& arg);
  761. inline Vector2& operator +=(const Vector2& arg);
  762. inline friend Vector2 operator*(const Vector2& lhs,double rhs);
  763. inline friend Vector2 operator*(double lhs,const Vector2& rhs);
  764. inline friend Vector2 operator/(const Vector2& lhs,double rhs);
  765. inline friend Vector2 operator+(const Vector2& lhs,const Vector2& rhs);
  766. inline friend Vector2 operator-(const Vector2& lhs,const Vector2& rhs);
  767. inline friend Vector2 operator*(const Vector2& lhs,const Vector2& rhs);
  768. inline friend Vector2 operator-(const Vector2& arg);
  769. inline friend void SetToZero(Vector2& v);
  770. //! @return a zero 2D vector.
  771. inline static Vector2 Zero();
  772. /** Normalizes this vector and returns it norm
  773. * makes v a unitvector and returns the norm of v.
  774. * if v is smaller than eps, Vector(1,0,0) is returned with norm 0.
  775. * if this is not good, check the return value of this method.
  776. */
  777. double Normalize(double eps=epsilon);
  778. //! @return the norm of the vector
  779. inline double Norm() const;
  780. //! projects v in its XY plane, and sets *this to these values
  781. inline void Set3DXY(const Vector& v);
  782. //! projects v in its YZ plane, and sets *this to these values
  783. inline void Set3DYZ(const Vector& v);
  784. //! projects v in its ZX plane, and sets *this to these values
  785. inline void Set3DZX(const Vector& v);
  786. //! projects v_someframe in the XY plane of F_someframe_XY,
  787. //! and sets *this to these values
  788. //! expressed wrt someframe.
  789. inline void Set3DPlane(const Frame& F_someframe_XY,const Vector& v_someframe);
  790. //! do not use operator == because the definition of Equal(.,.) is slightly
  791. //! different. It compares whether the 2 arguments are equal in an eps-interval
  792. inline friend bool Equal(const Vector2& a,const Vector2& b,double eps);
  793. friend class Rotation2;
  794. };
  795. //! A 2D Rotation class, for conventions see Rotation. For further documentation
  796. //! of the methods see Rotation class.
  797. class Rotation2
  798. {
  799. double s,c;
  800. //! c,s represent cos(angle), sin(angle), this also represents first col. of rot matrix
  801. //! from outside, this class behaves as if it would store the complete 2x2 matrix.
  802. public:
  803. //! Default constructor does NOT initialise to Zero().
  804. Rotation2() {c=1.0;s=0.0;}
  805. explicit Rotation2(double angle_rad):s(sin(angle_rad)),c(cos(angle_rad)) {}
  806. Rotation2(double ca,double sa):s(sa),c(ca){}
  807. inline Rotation2& operator=(const Rotation2& arg);
  808. inline Vector2 operator*(const Vector2& v) const;
  809. //! Access to elements 0..1,0..1, bounds are checked when NDEBUG is not set
  810. inline double operator() (int i,int j) const;
  811. inline friend Rotation2 operator *(const Rotation2& lhs,const Rotation2& rhs);
  812. inline void SetInverse();
  813. inline Rotation2 Inverse() const;
  814. inline Vector2 Inverse(const Vector2& v) const;
  815. inline void SetIdentity();
  816. inline static Rotation2 Identity();
  817. //! The SetRot.. functions set the value of *this to the appropriate rotation matrix.
  818. inline void SetRot(double angle);
  819. //! The Rot... static functions give the value of the appropriate rotation matrix bac
  820. inline static Rotation2 Rot(double angle);
  821. //! Gets the angle (in radians)
  822. inline double GetRot() const;
  823. //! do not use operator == because the definition of Equal(.,.) is slightly
  824. //! different. It compares whether the 2 arguments are equal in an eps-interval
  825. inline friend bool Equal(const Rotation2& a,const Rotation2& b,double eps);
  826. };
  827. //! A 2D frame class, for further documentation see the Frames class
  828. //! for methods with unchanged semantics.
  829. class Frame2
  830. {
  831. public:
  832. Vector2 p; //!< origine of the Frame
  833. Rotation2 M; //!< Orientation of the Frame
  834. public:
  835. inline Frame2(const Rotation2& R,const Vector2& V);
  836. explicit inline Frame2(const Vector2& V);
  837. explicit inline Frame2(const Rotation2& R);
  838. inline Frame2(void);
  839. inline Frame2(const Frame2& arg);
  840. inline void Make4x4(double* d);
  841. //! Treats a frame as a 3x3 matrix and returns element i,j
  842. //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set
  843. inline double operator()(int i,int j);
  844. //! Treats a frame as a 4x4 matrix and returns element i,j
  845. //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set
  846. inline double operator() (int i,int j) const;
  847. inline void SetInverse();
  848. inline Frame2 Inverse() const;
  849. inline Vector2 Inverse(const Vector2& arg) const;
  850. inline Frame2& operator = (const Frame2& arg);
  851. inline Vector2 operator * (const Vector2& arg);
  852. inline friend Frame2 operator *(const Frame2& lhs,const Frame2& rhs);
  853. inline void SetIdentity();
  854. inline void Integrate(const Twist& t_this,double frequency);
  855. inline static Frame2 Identity() {
  856. Frame2 tmp;
  857. tmp.SetIdentity();
  858. return tmp;
  859. }
  860. inline friend bool Equal(const Frame2& a,const Frame2& b,double eps);
  861. };
  862. inline bool Equal(const Vector&, const Vector&, double = epsilon);
  863. bool Equal(const Rotation&, const Rotation&, double = epsilon);
  864. inline bool Equal(const Frame&, const Frame&, double = epsilon);
  865. inline bool Equal(const Twist&, const Twist&, double = epsilon);
  866. inline bool Equal(const Wrench&, const Wrench&, double = epsilon);
  867. inline bool Equal(const Vector2&, const Vector2&, double = epsilon);
  868. inline bool Equal(const Rotation2&, const Rotation2&, double = epsilon);
  869. inline bool Equal(const Frame2&, const Frame2&, double = epsilon);
  870. IMETHOD Vector diff(const Vector& a,const Vector& b,double dt=1);
  871. IMETHOD Vector diff(const Rotation& R_a_b1,const Rotation& R_a_b2,double dt=1);
  872. IMETHOD Twist diff(const Frame& F_a_b1,const Frame& F_a_b2,double dt=1);
  873. IMETHOD Twist diff(const Twist& a,const Twist& b,double dt=1);
  874. IMETHOD Wrench diff(const Wrench& W_a_p1,const Wrench& W_a_p2,double dt=1);
  875. IMETHOD Vector addDelta(const Vector& a,const Vector&da,double dt=1);
  876. IMETHOD Rotation addDelta(const Rotation& a,const Vector&da,double dt=1);
  877. IMETHOD Frame addDelta(const Frame& a,const Twist& da,double dt=1);
  878. IMETHOD Twist addDelta(const Twist& a,const Twist&da,double dt=1);
  879. IMETHOD Wrench addDelta(const Wrench& a,const Wrench&da,double dt=1);
  880. #ifdef KDL_INLINE
  881. // #include "vector.inl"
  882. // #include "wrench.inl"
  883. //#include "rotation.inl"
  884. //#include "frame.inl"
  885. //#include "twist.inl"
  886. //#include "vector2.inl"
  887. //#include "rotation2.inl"
  888. //#include "frame2.inl"
  889. #include "frames.inl"
  890. #endif
  891. }
  892. #endif