friction.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. //234567890123456789012345678901234567890123456789012345678901234567890123456789
  23. // 1 2 3 4 5 6 7
  24. ////////////////////////////////////////////////////////////////////////////////
  25. // This file create unit test for some of the functions found in:
  26. // ode/src/joint.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <algorithm>
  31. #include <UnitTest++.h>
  32. #include <ode/ode.h>
  33. #include "../ode/src/config.h"
  34. #include "../ode/src/joints/joints.h"
  35. /*
  36. * Tests for contact friction
  37. */
  38. SUITE(JointContact)
  39. {
  40. struct ContactSetup
  41. {
  42. dWorldID world;
  43. dBodyID body1;
  44. dBodyID body2;
  45. dJointID joint;
  46. ContactSetup()
  47. {
  48. world = dWorldCreate();
  49. body1 = dBodyCreate(world);
  50. body2 = dBodyCreate(world);
  51. dBodySetPosition(body1, -1, 0, 0);
  52. dBodySetPosition(body2, 1, 0, 0);
  53. }
  54. ~ContactSetup()
  55. {
  56. dBodyDestroy(body1);
  57. dBodyDestroy(body2);
  58. dWorldDestroy(world);
  59. }
  60. };
  61. TEST_FIXTURE(ContactSetup,
  62. test_ZeroMu)
  63. {
  64. dxJoint::Info1 info1;
  65. dxJoint::Info2Descr info2;
  66. dReal dummy_J[3][12] = {{0}};
  67. dReal dummy_c[3];
  68. dReal dummy_cfm[3];
  69. dReal dummy_lo[3];
  70. dReal dummy_hi[3];
  71. int dummy_findex[3];
  72. dReal info2_fps = 100;
  73. dReal info2_erp = 0;
  74. info2.J1l = dummy_J[0];
  75. info2.J1a = dummy_J[0] + 3;
  76. info2.J2l = dummy_J[0] + 6;
  77. info2.J2a = dummy_J[0] + 9;
  78. info2.rowskip = 12;
  79. info2.c = dummy_c;
  80. info2.cfm = dummy_cfm;
  81. info2.lo = dummy_lo;
  82. info2.hi = dummy_hi;
  83. info2.findex = dummy_findex;
  84. #define ZERO_ALL do { \
  85. memset(dummy_J, 0, sizeof dummy_J); \
  86. memset(dummy_c, 0, sizeof dummy_c); \
  87. memset(dummy_cfm, 0, sizeof dummy_cfm); \
  88. memset(dummy_lo, 0, sizeof dummy_lo); \
  89. memset(dummy_hi, 0, sizeof dummy_hi); \
  90. std::fill(dummy_findex, dummy_findex+3, -1);; \
  91. } \
  92. while (0)
  93. dContact contact;
  94. contact.surface.mode = dContactMu2 | dContactFDir1 | dContactApprox1;
  95. contact.geom.pos[0] = 0;
  96. contact.geom.pos[1] = 0;
  97. contact.geom.pos[2] = 0;
  98. // normal points into body1
  99. contact.geom.normal[0] = -1;
  100. contact.geom.normal[1] = 0;
  101. contact.geom.normal[2] = 0;
  102. contact.geom.depth = 0;
  103. contact.geom.g1 = 0;
  104. contact.geom.g2 = 0;
  105. // we ask for fdir1 = +Y, so fdir2 = normal x fdir1 = -Z
  106. contact.fdir1[0] = 0;
  107. contact.fdir1[1] = 1;
  108. contact.fdir1[2] = 0;
  109. /*
  110. * First, test with mu = 0, mu2 = 1
  111. * Because there is no friction on the first direction (+Y) the body
  112. * is allowed to translate in the Y axis and rotate around the Z axis.
  113. *
  114. * That is, the only constraint will be for the second dir (-Z):
  115. * so J[1] = [ 0 0 -1 0 1 0 0 0 1 0 1 0 ]
  116. */
  117. contact.surface.mu = 0;
  118. contact.surface.mu2 = 1;
  119. joint = dJointCreateContact(world, 0, &contact);
  120. dJointAttach(joint, body1, body2);
  121. joint->getInfo1(&info1);
  122. CHECK_EQUAL(2, (int)info1.m);
  123. ZERO_ALL;
  124. joint->getInfo2(info2_fps, info2_erp, &info2);
  125. CHECK_CLOSE(0, dummy_J[1][0], 1e-6);
  126. CHECK_CLOSE(0, dummy_J[1][1], 1e-6);
  127. CHECK_CLOSE(-1, dummy_J[1][2], 1e-6);
  128. CHECK_CLOSE(0, dummy_J[1][3], 1e-6);
  129. CHECK_CLOSE(1, dummy_J[1][4], 1e-6);
  130. CHECK_CLOSE(0, dummy_J[1][5], 1e-6);
  131. CHECK_CLOSE(0, dummy_J[1][6], 1e-6);
  132. CHECK_CLOSE(0, dummy_J[1][7], 1e-6);
  133. CHECK_CLOSE(1, dummy_J[1][8], 1e-6);
  134. CHECK_CLOSE(0, dummy_J[1][9], 1e-6);
  135. CHECK_CLOSE(1, dummy_J[1][10], 1e-6);
  136. CHECK_CLOSE(0, dummy_J[1][11], 1e-6);
  137. CHECK_EQUAL(0, dummy_findex[1]); // because of dContactApprox1
  138. dJointDestroy(joint);
  139. /*
  140. * Now try with no frictino in the second direction. The Jacobian should look like:
  141. * J[1] = [ 0 1 0 0 0 1 0 -1 0 0 0 1 ]
  142. */
  143. // try again, with zero mu2
  144. contact.surface.mu = 1;
  145. contact.surface.mu2 = 0;
  146. joint = dJointCreateContact(world, 0, &contact);
  147. dJointAttach(joint, body1, body2);
  148. joint->getInfo1(&info1);
  149. CHECK_EQUAL(2, (int)info1.m);
  150. ZERO_ALL;
  151. joint->getInfo2(info2_fps, info2_erp, &info2);
  152. CHECK_CLOSE(0, dummy_J[1][0], 1e-6);
  153. CHECK_CLOSE(1, dummy_J[1][1], 1e-6);
  154. CHECK_CLOSE(0, dummy_J[1][2], 1e-6);
  155. CHECK_CLOSE(0, dummy_J[1][3], 1e-6);
  156. CHECK_CLOSE(0, dummy_J[1][4], 1e-6);
  157. CHECK_CLOSE(1, dummy_J[1][5], 1e-6);
  158. CHECK_CLOSE(0, dummy_J[1][6], 1e-6);
  159. CHECK_CLOSE(-1, dummy_J[1][7], 1e-6);
  160. CHECK_CLOSE(0, dummy_J[1][8], 1e-6);
  161. CHECK_CLOSE(0, dummy_J[1][9], 1e-6);
  162. CHECK_CLOSE(0, dummy_J[1][10], 1e-6);
  163. CHECK_CLOSE(1, dummy_J[1][11], 1e-6);
  164. CHECK_EQUAL(0, dummy_findex[1]); // because of dContactApprox1
  165. dJointDestroy(joint);
  166. }
  167. }