test_invdyn_jacobian.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Test of kinematic consistency: check if finite differences of velocities, accelerations
  2. // match positions
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <gtest/gtest.h>
  8. #include "Bullet3Common/b3Random.h"
  9. #include "CloneTreeCreator.hpp"
  10. #include "CoilCreator.hpp"
  11. #include "DillCreator.hpp"
  12. #include "RandomTreeCreator.hpp"
  13. #include "BulletInverseDynamics/MultiBodyTree.hpp"
  14. #include "MultiBodyTreeDebugGraph.hpp"
  15. using namespace btInverseDynamics;
  16. #if (defined BT_ID_HAVE_MAT3X) && (defined BT_ID_WITH_JACOBIANS)
  17. // minimal smart pointer to make this work for c++2003
  18. template <typename T>
  19. class ptr {
  20. ptr();
  21. ptr(const ptr&);
  22. public:
  23. ptr(T* p) : m_p(p) {};
  24. ~ptr() { delete m_p; }
  25. T& operator*() { return *m_p; }
  26. T* operator->() { return m_p; }
  27. T*get() {return m_p;}
  28. const T*get() const {return m_p;}
  29. friend bool operator==(const ptr<T>& lhs, const ptr<T>& rhs) { return rhs.m_p == lhs.m_p; }
  30. friend bool operator!=(const ptr<T>& lhs, const ptr<T>& rhs) { return !(rhs.m_p == lhs.m_p);
  31. }
  32. private:
  33. T* m_p;
  34. };
  35. void calculateDotJacUError(const MultiBodyTreeCreator& creator, const int nloops,
  36. double* max_error) {
  37. // tree1 is used as reference to compute dot(Jacobian)*u from acceleration(dot(u)=0)
  38. ptr<MultiBodyTree> tree1(CreateMultiBodyTree(creator));
  39. ASSERT_TRUE(0x0 != tree1);
  40. CloneTreeCreator clone(tree1.get());
  41. // tree2 is used to compute dot(Jacobian)*u using the calculateJacobian function
  42. ptr<MultiBodyTree> tree2(CreateMultiBodyTree(clone));
  43. ASSERT_TRUE(0x0 != tree2);
  44. const int ndofs = tree1->numDoFs();
  45. const int nbodies = tree1->numBodies();
  46. if (ndofs <= 0) {
  47. *max_error = 0;
  48. return;
  49. }
  50. vecx q(ndofs);
  51. vecx u(ndofs);
  52. vecx dot_u(ndofs);
  53. vecx zero(ndofs);
  54. setZero(zero);
  55. double max_lin_error = 0;
  56. double max_ang_error = 0;
  57. for (int loop = 0; loop < nloops; loop++) {
  58. for (int i = 0; i < q.size(); i++) {
  59. q(i) = b3RandRange(-B3_PI, B3_PI);
  60. u(i) = b3RandRange(-B3_PI, B3_PI);
  61. }
  62. EXPECT_EQ(0, tree1->calculateKinematics(q, u, zero));
  63. EXPECT_EQ(0, tree2->calculatePositionAndVelocityKinematics(q, u));
  64. EXPECT_EQ(0, tree2->calculateJacobians(q, u));
  65. for (size_t idx = 0; idx < nbodies; idx++) {
  66. vec3 tmp1, tmp2;
  67. vec3 diff;
  68. EXPECT_EQ(0, tree1->getBodyLinearAcceleration(idx, &tmp1));
  69. EXPECT_EQ(0, tree2->getBodyDotJacobianTransU(idx, &tmp2));
  70. diff = tmp1 - tmp2;
  71. double lin_error = maxAbs(diff);
  72. if (lin_error > max_lin_error) {
  73. max_lin_error = lin_error;
  74. }
  75. EXPECT_EQ(0, tree1->getBodyAngularAcceleration(idx, &tmp1));
  76. EXPECT_EQ(0, tree2->getBodyDotJacobianRotU(idx, &tmp2));
  77. diff = tmp1 - tmp2;
  78. double ang_error = maxAbs(diff);
  79. if (ang_error > max_ang_error) {
  80. max_ang_error = ang_error;
  81. }
  82. }
  83. }
  84. *max_error = max_ang_error > max_lin_error ? max_ang_error : max_lin_error;
  85. }
  86. void calculateJacobianError(const MultiBodyTreeCreator& creator, const int nloops,
  87. double* max_error) {
  88. // tree1 is used as reference to compute the Jacobian from velocities with unit u vectors.
  89. ptr<MultiBodyTree> tree1(CreateMultiBodyTree(creator));
  90. ASSERT_TRUE(0x0 != tree1);
  91. // tree2 is used to compute the Jacobians using the calculateJacobian function
  92. CloneTreeCreator clone(tree1.get());
  93. ptr<MultiBodyTree> tree2(CreateMultiBodyTree(clone));
  94. ASSERT_TRUE(0x0 != tree2);
  95. const int ndofs = tree1->numDoFs();
  96. const int nbodies = tree1->numBodies();
  97. if (ndofs <= 0) {
  98. *max_error = 0;
  99. return;
  100. }
  101. vecx q(ndofs);
  102. vecx zero(ndofs);
  103. setZero(zero);
  104. vecx one(ndofs);
  105. double max_lin_error = 0;
  106. double max_ang_error = 0;
  107. for (int loop = 0; loop < nloops; loop++) {
  108. for (int i = 0; i < q.size(); i++) {
  109. q(i) = b3RandRange(-B3_PI, B3_PI);
  110. }
  111. EXPECT_EQ(0, tree2->calculatePositionKinematics(q));
  112. EXPECT_EQ(0, tree2->calculateJacobians(q));
  113. for (size_t idx = 0; idx < nbodies; idx++) {
  114. mat3x ref_jac_r(3, ndofs);
  115. mat3x ref_jac_t(3, ndofs);
  116. ref_jac_r.setZero();
  117. ref_jac_t.setZero();
  118. // this re-computes all jacobians for every body ...
  119. // but avoids std::vector<eigen matrix> issues
  120. for (int col = 0; col < ndofs; col++) {
  121. setZero(one);
  122. one(col) = 1.0;
  123. EXPECT_EQ(0, tree1->calculatePositionAndVelocityKinematics(q, one));
  124. vec3 vel, omg;
  125. EXPECT_EQ(0, tree1->getBodyLinearVelocity(idx, &vel));
  126. EXPECT_EQ(0, tree1->getBodyAngularVelocity(idx, &omg));
  127. setMat3xElem(0, col, omg(0), &ref_jac_r);
  128. setMat3xElem(1, col, omg(1), &ref_jac_r);
  129. setMat3xElem(2, col, omg(2), &ref_jac_r);
  130. setMat3xElem(0, col, vel(0), &ref_jac_t);
  131. setMat3xElem(1, col, vel(1), &ref_jac_t);
  132. setMat3xElem(2, col, vel(2), &ref_jac_t);
  133. }
  134. mat3x jac_r(3, ndofs);
  135. mat3x jac_t(3, ndofs);
  136. mat3x diff(3, ndofs);
  137. EXPECT_EQ(0, tree2->getBodyJacobianTrans(idx, &jac_t));
  138. EXPECT_EQ(0, tree2->getBodyJacobianRot(idx, &jac_r));
  139. sub(ref_jac_t,jac_t,&diff);
  140. double lin_error = maxAbsMat3x(diff);
  141. if (lin_error > max_lin_error) {
  142. max_lin_error = lin_error;
  143. }
  144. sub(ref_jac_r, jac_r,&diff);
  145. double ang_error = maxAbsMat3x(diff);
  146. if (ang_error > max_ang_error) {
  147. max_ang_error = ang_error;
  148. }
  149. }
  150. }
  151. *max_error = max_ang_error > max_lin_error ? max_ang_error : max_lin_error;
  152. }
  153. void calculateVelocityJacobianError(const MultiBodyTreeCreator& creator, const int nloops,
  154. double* max_error) {
  155. // tree1 is used as reference to compute the velocities directly
  156. ptr<MultiBodyTree> tree1(CreateMultiBodyTree(creator));
  157. ASSERT_TRUE(0x0 != tree1);
  158. // tree2 is used to compute the velocities via jacobians
  159. CloneTreeCreator clone(tree1.get());
  160. ptr<MultiBodyTree> tree2(CreateMultiBodyTree(clone));
  161. ASSERT_TRUE(0x0 != tree2);
  162. const int ndofs = tree1->numDoFs();
  163. const int nbodies = tree1->numBodies();
  164. if (ndofs <= 0) {
  165. *max_error = 0;
  166. return;
  167. }
  168. vecx q(ndofs);
  169. vecx u(ndofs);
  170. double max_lin_error = 0;
  171. double max_ang_error = 0;
  172. for (int loop = 0; loop < nloops; loop++) {
  173. for (int i = 0; i < q.size(); i++) {
  174. q(i) = b3RandRange(-B3_PI, B3_PI);
  175. u(i) = b3RandRange(-B3_PI, B3_PI);
  176. }
  177. EXPECT_EQ(0, tree1->calculatePositionAndVelocityKinematics(q, u));
  178. EXPECT_EQ(0, tree2->calculatePositionKinematics(q));
  179. EXPECT_EQ(0, tree2->calculateJacobians(q));
  180. for (size_t idx = 0; idx < nbodies; idx++) {
  181. vec3 vel1;
  182. vec3 omg1;
  183. vec3 vel2;
  184. vec3 omg2;
  185. mat3x jac_r2(3, ndofs);
  186. mat3x jac_t2(3, ndofs);
  187. EXPECT_EQ(0, tree1->getBodyLinearVelocity(idx, &vel1));
  188. EXPECT_EQ(0, tree1->getBodyAngularVelocity(idx, &omg1));
  189. EXPECT_EQ(0, tree2->getBodyJacobianTrans(idx, &jac_t2));
  190. EXPECT_EQ(0, tree2->getBodyJacobianRot(idx, &jac_r2));
  191. omg2 = jac_r2 * u;
  192. vel2 = jac_t2 * u;
  193. double lin_error = maxAbs(vel1 - vel2);
  194. if (lin_error > max_lin_error) {
  195. max_lin_error = lin_error;
  196. }
  197. double ang_error = maxAbs(omg1 - omg2);
  198. if (ang_error > max_ang_error) {
  199. max_ang_error = ang_error;
  200. }
  201. }
  202. }
  203. *max_error = max_ang_error > max_lin_error ? max_ang_error : max_lin_error;
  204. }
  205. // test nonlinear terms: dot(Jacobian)*u (linear and angular acceleration for dot_u ==0)
  206. // from Jacobian calculation method and pseudo-numerically using via the kinematics method.
  207. TEST(InvDynJacobians, JacDotJacU) {
  208. const int kNumLevels = 5;
  209. #ifdef B3_USE_DOUBLE_PRECISION
  210. const double kMaxError = 1e-12;
  211. #else
  212. const double kMaxError = 5e-5;
  213. #endif
  214. const int kNumLoops = 20;
  215. for (int level = 0; level < kNumLevels; level++) {
  216. const int nbodies = BT_ID_POW(2, level);
  217. CoilCreator coil(nbodies);
  218. double error;
  219. calculateDotJacUError(coil, kNumLoops, &error);
  220. EXPECT_GT(kMaxError, error);
  221. DillCreator dill(level);
  222. calculateDotJacUError(dill, kNumLoops, &error);
  223. EXPECT_GT(kMaxError, error);
  224. }
  225. const int kRandomLoops = 100;
  226. const int kMaxRandomBodies = 128;
  227. for (int loop = 0; loop < kRandomLoops; loop++) {
  228. RandomTreeCreator random(kMaxRandomBodies);
  229. double error;
  230. calculateDotJacUError(random, kNumLoops, &error);
  231. EXPECT_GT(kMaxError, error);
  232. }
  233. }
  234. // Jacobians: linear and angular acceleration for dot_u ==0
  235. // from Jacobian calculation method and pseudo-numerically using via the kinematics method.
  236. TEST(InvDynJacobians, Jacobians) {
  237. const int kNumLevels = 5;
  238. #ifdef B3_USE_DOUBLE_PRECISION
  239. const double kMaxError = 1e-12;
  240. #else
  241. const double kMaxError = 5e-5;
  242. #endif
  243. const int kNumLoops = 20;
  244. for (int level = 0; level < kNumLevels; level++) {
  245. const int nbodies = BT_ID_POW(2, level);
  246. CoilCreator coil(nbodies);
  247. double error;
  248. calculateJacobianError(coil, kNumLoops, &error);
  249. EXPECT_GT(kMaxError, error);
  250. DillCreator dill(level);
  251. calculateDotJacUError(dill, kNumLoops, &error);
  252. EXPECT_GT(kMaxError, error);
  253. }
  254. const int kRandomLoops = 20;
  255. const int kMaxRandomBodies = 16;
  256. for (int loop = 0; loop < kRandomLoops; loop++) {
  257. RandomTreeCreator random(kMaxRandomBodies);
  258. double error;
  259. calculateJacobianError(random, kNumLoops, &error);
  260. EXPECT_GT(kMaxError, error);
  261. }
  262. }
  263. // test for jacobian*u == velocity
  264. TEST(InvDynJacobians, VelocitiesFromJacobians) {
  265. const int kRandomLoops = 20;
  266. const int kMaxRandomBodies = 16;
  267. const int kNumLoops = 20;
  268. #ifdef B3_USE_DOUBLE_PRECISION
  269. const double kMaxError = 1e-12;
  270. #else
  271. const double kMaxError = 5e-5;
  272. #endif
  273. for (int loop = 0; loop < kRandomLoops; loop++) {
  274. RandomTreeCreator random(kMaxRandomBodies);
  275. double error;
  276. calculateVelocityJacobianError(random, kNumLoops, &error);
  277. EXPECT_GT(kMaxError, error);
  278. }
  279. }
  280. #endif
  281. int main(int argc, char** argv) {
  282. b3Srand(1234);
  283. ::testing::InitGoogleTest(&argc, argv);
  284. return RUN_ALL_TESTS();
  285. }