framevel.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*****************************************************************************
  2. * \file
  3. * This file contains the definition of classes for a
  4. * Rall Algebra of (subset of) the classes defined in frames,
  5. * i.e. classes that contain a pair (value,derivative) and define operations on that pair
  6. * this classes are usefull for automatic differentiation ( <-> symbolic diff , <-> numeric diff)
  7. * Defines VectorVel, RotationVel, FrameVel. Look at Frames.h for details on how to work
  8. * with Frame objects.
  9. * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven
  10. *
  11. * \version
  12. * ORO_Geometry V0.2
  13. *
  14. * \par History
  15. * - $log$
  16. *
  17. * \par Release
  18. * $Name: $
  19. ****************************************************************************/
  20. #ifndef KDL_FRAMEVEL_H
  21. #define KDL_FRAMEVEL_H
  22. #include "utilities/utility.h"
  23. #include "utilities/rall1d.h"
  24. #include "utilities/traits.h"
  25. #include "frames.hpp"
  26. namespace KDL {
  27. typedef Rall1d<double> doubleVel;
  28. IMETHOD doubleVel diff(const doubleVel& a,const doubleVel& b,double dt=1.0) {
  29. return doubleVel((b.t-a.t)/dt,(b.grad-a.grad)/dt);
  30. }
  31. IMETHOD doubleVel addDelta(const doubleVel& a,const doubleVel&da,double dt=1.0) {
  32. return doubleVel(a.t+da.t*dt,a.grad+da.grad*dt);
  33. }
  34. IMETHOD void random(doubleVel& F) {
  35. random(F.t);
  36. random(F.grad);
  37. }
  38. IMETHOD void posrandom(doubleVel& F) {
  39. posrandom(F.t);
  40. posrandom(F.grad);
  41. }
  42. }
  43. template <>
  44. struct Traits<KDL::doubleVel> {
  45. typedef double valueType;
  46. typedef KDL::doubleVel derivType;
  47. };
  48. namespace KDL {
  49. class TwistVel;
  50. class VectorVel;
  51. class FrameVel;
  52. class RotationVel;
  53. class VectorVel
  54. // = TITLE
  55. // An VectorVel is a Vector and its first derivative
  56. // = CLASS TYPE
  57. // Concrete
  58. {
  59. public:
  60. Vector p; // position vector
  61. Vector v; // velocity vector
  62. public:
  63. VectorVel():p(),v(){}
  64. VectorVel(const Vector& _p,const Vector& _v):p(_p),v(_v) {}
  65. explicit VectorVel(const Vector& _p):p(_p),v(Vector::Zero()) {}
  66. Vector value() const { return p;}
  67. Vector deriv() const { return v;}
  68. IMETHOD VectorVel& operator = (const VectorVel& arg);
  69. IMETHOD VectorVel& operator = (const Vector& arg);
  70. IMETHOD VectorVel& operator += (const VectorVel& arg);
  71. IMETHOD VectorVel& operator -= (const VectorVel& arg);
  72. IMETHOD static VectorVel Zero();
  73. IMETHOD void ReverseSign();
  74. IMETHOD doubleVel Norm() const;
  75. IMETHOD friend VectorVel operator + (const VectorVel& r1,const VectorVel& r2);
  76. IMETHOD friend VectorVel operator - (const VectorVel& r1,const VectorVel& r2);
  77. IMETHOD friend VectorVel operator + (const Vector& r1,const VectorVel& r2);
  78. IMETHOD friend VectorVel operator - (const Vector& r1,const VectorVel& r2);
  79. IMETHOD friend VectorVel operator + (const VectorVel& r1,const Vector& r2);
  80. IMETHOD friend VectorVel operator - (const VectorVel& r1,const Vector& r2);
  81. IMETHOD friend VectorVel operator * (const VectorVel& r1,const VectorVel& r2);
  82. IMETHOD friend VectorVel operator * (const VectorVel& r1,const Vector& r2);
  83. IMETHOD friend VectorVel operator * (const Vector& r1,const VectorVel& r2);
  84. IMETHOD friend VectorVel operator * (const VectorVel& r1,double r2);
  85. IMETHOD friend VectorVel operator * (double r1,const VectorVel& r2);
  86. IMETHOD friend VectorVel operator * (const doubleVel& r1,const VectorVel& r2);
  87. IMETHOD friend VectorVel operator * (const VectorVel& r2,const doubleVel& r1);
  88. IMETHOD friend VectorVel operator*(const Rotation& R,const VectorVel& x);
  89. IMETHOD friend VectorVel operator / (const VectorVel& r1,double r2);
  90. IMETHOD friend VectorVel operator / (const VectorVel& r2,const doubleVel& r1);
  91. IMETHOD friend void SetToZero(VectorVel& v);
  92. IMETHOD friend bool Equal(const VectorVel& r1,const VectorVel& r2,double eps);
  93. IMETHOD friend bool Equal(const Vector& r1,const VectorVel& r2,double eps);
  94. IMETHOD friend bool Equal(const VectorVel& r1,const Vector& r2,double eps);
  95. IMETHOD friend VectorVel operator - (const VectorVel& r);
  96. IMETHOD friend doubleVel dot(const VectorVel& lhs,const VectorVel& rhs);
  97. IMETHOD friend doubleVel dot(const VectorVel& lhs,const Vector& rhs);
  98. IMETHOD friend doubleVel dot(const Vector& lhs,const VectorVel& rhs);
  99. };
  100. class RotationVel
  101. // = TITLE
  102. // An RotationVel is a Rotation and its first derivative, a rotation vector
  103. // = CLASS TYPE
  104. // Concrete
  105. {
  106. public:
  107. Rotation R; // Rotation matrix
  108. Vector w; // rotation vector
  109. public:
  110. RotationVel():R(),w() {}
  111. explicit RotationVel(const Rotation& R_):R(R_),w(Vector::Zero()){}
  112. RotationVel(const Rotation& R_,const Vector& _w):R(R_),w(_w){}
  113. Rotation value() const { return R;}
  114. Vector deriv() const { return w;}
  115. IMETHOD RotationVel& operator = (const RotationVel& arg);
  116. IMETHOD RotationVel& operator = (const Rotation& arg);
  117. IMETHOD VectorVel UnitX() const;
  118. IMETHOD VectorVel UnitY() const;
  119. IMETHOD VectorVel UnitZ() const;
  120. IMETHOD static RotationVel Identity();
  121. IMETHOD RotationVel Inverse() const;
  122. IMETHOD VectorVel Inverse(const VectorVel& arg) const;
  123. IMETHOD VectorVel Inverse(const Vector& arg) const;
  124. IMETHOD VectorVel operator*(const VectorVel& arg) const;
  125. IMETHOD VectorVel operator*(const Vector& arg) const;
  126. IMETHOD void DoRotX(const doubleVel& angle);
  127. IMETHOD void DoRotY(const doubleVel& angle);
  128. IMETHOD void DoRotZ(const doubleVel& angle);
  129. IMETHOD static RotationVel RotX(const doubleVel& angle);
  130. IMETHOD static RotationVel RotY(const doubleVel& angle);
  131. IMETHOD static RotationVel RotZ(const doubleVel& angle);
  132. IMETHOD static RotationVel Rot(const Vector& rotvec,const doubleVel& angle);
  133. // rotvec has arbitrary norm
  134. // rotation around a constant vector !
  135. IMETHOD static RotationVel Rot2(const Vector& rotvec,const doubleVel& angle);
  136. // rotvec is normalized.
  137. // rotation around a constant vector !
  138. IMETHOD friend RotationVel operator* (const RotationVel& r1,const RotationVel& r2);
  139. IMETHOD friend RotationVel operator* (const Rotation& r1,const RotationVel& r2);
  140. IMETHOD friend RotationVel operator* (const RotationVel& r1,const Rotation& r2);
  141. IMETHOD friend bool Equal(const RotationVel& r1,const RotationVel& r2,double eps);
  142. IMETHOD friend bool Equal(const Rotation& r1,const RotationVel& r2,double eps);
  143. IMETHOD friend bool Equal(const RotationVel& r1,const Rotation& r2,double eps);
  144. IMETHOD TwistVel Inverse(const TwistVel& arg) const;
  145. IMETHOD TwistVel Inverse(const Twist& arg) const;
  146. IMETHOD TwistVel operator * (const TwistVel& arg) const;
  147. IMETHOD TwistVel operator * (const Twist& arg) const;
  148. };
  149. class FrameVel
  150. // = TITLE
  151. // An FrameVel is a Frame and its first derivative, a Twist vector
  152. // = CLASS TYPE
  153. // Concrete
  154. // = CAVEATS
  155. //
  156. {
  157. public:
  158. RotationVel M;
  159. VectorVel p;
  160. public:
  161. FrameVel(){}
  162. explicit FrameVel(const Frame& T_):
  163. M(T_.M),p(T_.p) {}
  164. FrameVel(const Frame& T_,const Twist& _t):
  165. M(T_.M,_t.rot),p(T_.p,_t.vel) {}
  166. FrameVel(const RotationVel& _M,const VectorVel& _p):
  167. M(_M),p(_p) {}
  168. Frame value() const { return Frame(M.value(),p.value());}
  169. Twist deriv() const { return Twist(p.deriv(),M.deriv());}
  170. IMETHOD FrameVel& operator = (const Frame& arg);
  171. IMETHOD FrameVel& operator = (const FrameVel& arg);
  172. IMETHOD static FrameVel Identity();
  173. IMETHOD FrameVel Inverse() const;
  174. IMETHOD VectorVel Inverse(const VectorVel& arg) const;
  175. IMETHOD VectorVel operator*(const VectorVel& arg) const;
  176. IMETHOD VectorVel operator*(const Vector& arg) const;
  177. IMETHOD VectorVel Inverse(const Vector& arg) const;
  178. IMETHOD Frame GetFrame() const;
  179. IMETHOD Twist GetTwist() const;
  180. IMETHOD friend FrameVel operator * (const FrameVel& f1,const FrameVel& f2);
  181. IMETHOD friend FrameVel operator * (const Frame& f1,const FrameVel& f2);
  182. IMETHOD friend FrameVel operator * (const FrameVel& f1,const Frame& f2);
  183. IMETHOD friend bool Equal(const FrameVel& r1,const FrameVel& r2,double eps);
  184. IMETHOD friend bool Equal(const Frame& r1,const FrameVel& r2,double eps);
  185. IMETHOD friend bool Equal(const FrameVel& r1,const Frame& r2,double eps);
  186. IMETHOD TwistVel Inverse(const TwistVel& arg) const;
  187. IMETHOD TwistVel Inverse(const Twist& arg) const;
  188. IMETHOD TwistVel operator * (const TwistVel& arg) const;
  189. IMETHOD TwistVel operator * (const Twist& arg) const;
  190. };
  191. //very similar to Wrench class.
  192. class TwistVel
  193. // = TITLE
  194. // This class represents a TwistVel. This is a velocity and rotational velocity together
  195. {
  196. public:
  197. VectorVel vel;
  198. VectorVel rot;
  199. public:
  200. // = Constructors
  201. TwistVel():vel(),rot() {};
  202. TwistVel(const VectorVel& _vel,const VectorVel& _rot):vel(_vel),rot(_rot) {};
  203. TwistVel(const Twist& p,const Twist& v):vel(p.vel, v.vel), rot( p.rot, v.rot) {};
  204. TwistVel(const Twist& p):vel(p.vel), rot( p.rot) {};
  205. Twist value() const {
  206. return Twist(vel.value(),rot.value());
  207. }
  208. Twist deriv() const {
  209. return Twist(vel.deriv(),rot.deriv());
  210. }
  211. // = Operators
  212. IMETHOD TwistVel& operator-=(const TwistVel& arg);
  213. IMETHOD TwistVel& operator+=(const TwistVel& arg);
  214. // = External operators
  215. IMETHOD friend TwistVel operator*(const TwistVel& lhs,double rhs);
  216. IMETHOD friend TwistVel operator*(double lhs,const TwistVel& rhs);
  217. IMETHOD friend TwistVel operator/(const TwistVel& lhs,double rhs);
  218. IMETHOD friend TwistVel operator*(const TwistVel& lhs,const doubleVel& rhs);
  219. IMETHOD friend TwistVel operator*(const doubleVel& lhs,const TwistVel& rhs);
  220. IMETHOD friend TwistVel operator/(const TwistVel& lhs,const doubleVel& rhs);
  221. IMETHOD friend TwistVel operator+(const TwistVel& lhs,const TwistVel& rhs);
  222. IMETHOD friend TwistVel operator-(const TwistVel& lhs,const TwistVel& rhs);
  223. IMETHOD friend TwistVel operator-(const TwistVel& arg);
  224. IMETHOD friend void SetToZero(TwistVel& v);
  225. // = Zero
  226. static IMETHOD TwistVel Zero();
  227. // = Reverse Sign
  228. IMETHOD void ReverseSign();
  229. // = Change Reference point
  230. IMETHOD TwistVel RefPoint(const VectorVel& v_base_AB);
  231. // Changes the reference point of the TwistVel.
  232. // The VectorVel v_base_AB is expressed in the same base as the TwistVel
  233. // The VectorVel v_base_AB is a VectorVel from the old point to
  234. // the new point.
  235. // Complexity : 6M+6A
  236. // = Equality operators
  237. // do not use operator == because the definition of Equal(.,.) is slightly
  238. // different. It compares whether the 2 arguments are equal in an eps-interval
  239. IMETHOD friend bool Equal(const TwistVel& a,const TwistVel& b,double eps);
  240. IMETHOD friend bool Equal(const Twist& a,const TwistVel& b,double eps);
  241. IMETHOD friend bool Equal(const TwistVel& a,const Twist& b,double eps);
  242. // = Conversion to other entities
  243. IMETHOD Twist GetTwist() const;
  244. IMETHOD Twist GetTwistDot() const;
  245. // = Friends
  246. friend class RotationVel;
  247. friend class FrameVel;
  248. };
  249. IMETHOD bool Equal(const VectorVel&, const VectorVel&, double = epsilon);
  250. IMETHOD bool Equal(const Vector&, const VectorVel&, double = epsilon);
  251. IMETHOD bool Equal(const VectorVel&, const Vector&, double = epsilon);
  252. IMETHOD bool Equal(const RotationVel&, const RotationVel&, double = epsilon);
  253. IMETHOD bool Equal(const Rotation&, const RotationVel&, double = epsilon);
  254. IMETHOD bool Equal(const RotationVel&, const Rotation&, double = epsilon);
  255. IMETHOD bool Equal(const FrameVel&, const FrameVel&, double = epsilon);
  256. IMETHOD bool Equal(const Frame&, const FrameVel&, double = epsilon);
  257. IMETHOD bool Equal(const FrameVel&, const Frame&, double = epsilon);
  258. IMETHOD bool Equal(const TwistVel&, const TwistVel&, double = epsilon);
  259. IMETHOD bool Equal(const Twist&, const TwistVel&, double = epsilon);
  260. IMETHOD bool Equal(const TwistVel&, const Twist&, double = epsilon);
  261. IMETHOD VectorVel diff(const VectorVel& a,const VectorVel& b,double dt=1.0) {
  262. return VectorVel(diff(a.p,b.p,dt),diff(a.v,b.v,dt));
  263. }
  264. IMETHOD VectorVel addDelta(const VectorVel& a,const VectorVel&da,double dt=1.0) {
  265. return VectorVel(addDelta(a.p,da.p,dt),addDelta(a.v,da.v,dt));
  266. }
  267. IMETHOD VectorVel diff(const RotationVel& a,const RotationVel& b,double dt = 1.0) {
  268. return VectorVel(diff(a.R,b.R,dt),diff(a.w,b.w,dt));
  269. }
  270. IMETHOD RotationVel addDelta(const RotationVel& a,const VectorVel&da,double dt=1.0) {
  271. return RotationVel(addDelta(a.R,da.p,dt),addDelta(a.w,da.v,dt));
  272. }
  273. IMETHOD TwistVel diff(const FrameVel& a,const FrameVel& b,double dt=1.0) {
  274. return TwistVel(diff(a.M,b.M,dt),diff(a.p,b.p,dt));
  275. }
  276. IMETHOD FrameVel addDelta(const FrameVel& a,const TwistVel& da,double dt=1.0) {
  277. return FrameVel(
  278. addDelta(a.M,da.rot,dt),
  279. addDelta(a.p,da.vel,dt)
  280. );
  281. }
  282. IMETHOD void random(VectorVel& a) {
  283. random(a.p);
  284. random(a.v);
  285. }
  286. IMETHOD void random(TwistVel& a) {
  287. random(a.vel);
  288. random(a.rot);
  289. }
  290. IMETHOD void random(RotationVel& R) {
  291. random(R.R);
  292. random(R.w);
  293. }
  294. IMETHOD void random(FrameVel& F) {
  295. random(F.M);
  296. random(F.p);
  297. }
  298. IMETHOD void posrandom(VectorVel& a) {
  299. posrandom(a.p);
  300. posrandom(a.v);
  301. }
  302. IMETHOD void posrandom(TwistVel& a) {
  303. posrandom(a.vel);
  304. posrandom(a.rot);
  305. }
  306. IMETHOD void posrandom(RotationVel& R) {
  307. posrandom(R.R);
  308. posrandom(R.w);
  309. }
  310. IMETHOD void posrandom(FrameVel& F) {
  311. posrandom(F.M);
  312. posrandom(F.p);
  313. }
  314. #ifdef KDL_INLINE
  315. #include "framevel.inl"
  316. #endif
  317. } // namespace
  318. #endif