jntarray.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (C) 2007 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  2. // Version: 1.0
  3. // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  4. // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  5. // URL: http://www.orocos.org/kdl
  6. // This library is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 2.1 of the License, or (at your option) any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #ifndef KDL_JNTARRAY_HPP
  18. #define KDL_JNTARRAY_HPP
  19. #include "frames.hpp"
  20. #include "jacobian.hpp"
  21. namespace KDL
  22. {
  23. /**
  24. * @brief This class represents an fixed size array containing
  25. * joint values of a KDL::Chain.
  26. *
  27. * \warning An object constructed with the default constructor provides
  28. * a valid, but inert, object. Many of the member functions will do
  29. * the correct thing and have no affect on this object, but some
  30. * member functions can _NOT_ deal with an inert/empty object. These
  31. * functions will assert() and exit the program instead. The intended use
  32. * case for the default constructor (in an RTT/OCL setting) is outlined in
  33. * code below - the default constructor plus the resize() function allow
  34. * use of JntArray objects whose size is set within a configureHook() call
  35. * (typically based on a size determined from a property).
  36. \code
  37. class MyTask : public RTT::TaskContext
  38. {
  39. JntArray j;
  40. MyTask()
  41. {} // invokes j's default constructor
  42. bool configureHook()
  43. {
  44. unsigned int size = some_property.rvalue();
  45. j.resize(size)
  46. ...
  47. }
  48. void updateHook()
  49. {
  50. ** use j here
  51. }
  52. };
  53. \endcode
  54. */
  55. class JntArray
  56. {
  57. private:
  58. unsigned int size;
  59. double* data;
  60. public:
  61. /** Construct with _no_ data array
  62. * @post NULL == data
  63. * @post 0 == rows()
  64. * @warning use of an object constructed like this, without
  65. * a resize() first, may result in program exit! See class
  66. * documentation.
  67. */
  68. JntArray();
  69. /**
  70. * Constructor of the joint array
  71. *
  72. * @param size size of the array, this cannot be changed
  73. * afterwards.
  74. * @pre 0 < size
  75. * @post NULL != data
  76. * @post 0 < rows()
  77. * @post all elements in data have 0 value
  78. */
  79. JntArray(unsigned int size);
  80. /** Copy constructor
  81. * @note Will correctly copy an empty object
  82. */
  83. JntArray(const JntArray& arg);
  84. ~JntArray();
  85. /** Resize the array
  86. * @warning This causes a dynamic allocation (and potentially
  87. * also a dynamic deallocation). This _will_ negatively affect
  88. * real-time performance!
  89. *
  90. * @post newSize == rows()
  91. * @post NULL != data
  92. * @post all elements in data have 0 value
  93. */
  94. void resize(unsigned int newSize);
  95. JntArray& operator = ( const JntArray& arg);
  96. /**
  97. * get_item operator for the joint array
  98. *
  99. *
  100. * @return the joint value at position i, starting from 0
  101. * @pre 0 != size (ie non-default constructor or resize() called)
  102. */
  103. double operator[](unsigned int i) const;
  104. /**
  105. * set_item operator
  106. *
  107. * @return reference to the joint value at position i,starting
  108. *from zero.
  109. * @pre 0 != size (ie non-default constructor or resize() called)
  110. */
  111. double& operator[](unsigned int i);
  112. /**
  113. * access operator for the joint array. Use pointer here to allow
  114. * access to sequential joint angles (required for ndof joints)
  115. *
  116. *
  117. * @return the joint value at position i, NULL if i is outside the valid range
  118. */
  119. double* operator()(unsigned int i);
  120. /**
  121. * Returns the number of rows (size) of the array
  122. *
  123. */
  124. unsigned int rows()const;
  125. /**
  126. * Returns the number of columns of the array, always 1.
  127. */
  128. unsigned int columns()const;
  129. /**
  130. * Function to add two joint arrays, all the arguments must
  131. * have the same size: A + B = C. This function is
  132. * aliasing-safe, A or B can be the same array as C.
  133. *
  134. * @param src1 A
  135. * @param src2 B
  136. * @param dest C
  137. */
  138. friend void Add(const JntArray& src1,const JntArray& src2,JntArray& dest);
  139. /**
  140. * Function to subtract two joint arrays, all the arguments must
  141. * have the same size: A - B = C. This function is
  142. * aliasing-safe, A or B can be the same array as C.
  143. *
  144. * @param src1 A
  145. * @param src2 B
  146. * @param dest C
  147. */
  148. friend void Subtract(const JntArray& src1,const JntArray& src2,JntArray& dest);
  149. /**
  150. * Function to multiply all the array values with a scalar
  151. * factor: A*b=C. This function is aliasing-safe, A can be the
  152. * same array as C.
  153. *
  154. * @param src A
  155. * @param factor b
  156. * @param dest C
  157. */
  158. friend void Multiply(const JntArray& src,const double& factor,JntArray& dest);
  159. /**
  160. * Function to divide all the array values with a scalar
  161. * factor: A/b=C. This function is aliasing-safe, A can be the
  162. * same array as C.
  163. *
  164. * @param src A
  165. * @param factor b
  166. * @param dest C
  167. */
  168. friend void Divide(const JntArray& src,const double& factor,JntArray& dest);
  169. /**
  170. * Function to multiply a KDL::Jacobian with a KDL::JntArray
  171. * to get a KDL::Twist, it should not be used to calculate the
  172. * forward velocity kinematics, the solver classes are built
  173. * for this purpose.
  174. * J*q = t
  175. *
  176. * @param jac J
  177. * @param src q
  178. * @param dest t
  179. * @post dest==Twist::Zero() if 0==src.rows() (ie src is empty)
  180. */
  181. friend void MultiplyJacobian(const Jacobian& jac, const JntArray& src, Twist& dest);
  182. /**
  183. * Function to set all the values of the array to 0
  184. *
  185. * @param array
  186. */
  187. friend void SetToZero(JntArray& array);
  188. /**
  189. * Function to check if two arrays are the same with a
  190. *precision of eps
  191. *
  192. * @param src1
  193. * @param src2
  194. * @param eps default: epsilon
  195. * @return true if each element of src1 is within eps of the same
  196. * element in src2, or if both src1 and src2 have no data (ie 0==rows())
  197. */
  198. friend bool Equal(const JntArray& src1,const JntArray& src2,double eps);
  199. friend bool operator==(const JntArray& src1,const JntArray& src2);
  200. //friend bool operator!=(const JntArray& src1,const JntArray& src2);
  201. };
  202. bool Equal(const JntArray&,const JntArray&, double = epsilon);
  203. bool operator==(const JntArray& src1,const JntArray& src2);
  204. //bool operator!=(const JntArray& src1,const JntArray& src2);
  205. }
  206. #endif