demo_joints.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. /*
  23. perform tests on all the joint types.
  24. this should be done using the double precision version of the library.
  25. usage:
  26. test_joints [-nXXX] [-g] [-i] [-e] [path_to_textures]
  27. if a test number is given then that specific test is performed, otherwise
  28. all the tests are performed. the tests are numbered `xxyy', where xx
  29. corresponds to the joint type and yy is the sub-test number. not every
  30. number maps to an actual test.
  31. flags:
  32. i: the test is interactive.
  33. g: turn off graphical display (can't use this with `i').
  34. e: turn on occasional error perturbations
  35. n: performe test XXX
  36. some tests compute and display error values. these values are scaled so
  37. <1 is good and >1 is bad. other tests just show graphical results which
  38. you must verify visually.
  39. */
  40. #include <ctype.h>
  41. #include <ode/ode.h>
  42. #include <drawstuff/drawstuff.h>
  43. #include "texturepath.h"
  44. #ifdef _MSC_VER
  45. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  46. #endif
  47. // select correct drawing functions
  48. #ifdef dDOUBLE
  49. #define dsDrawBox dsDrawBoxD
  50. #endif
  51. // some constants
  52. #define NUM_JOINTS 10 // number of joints to test (the `xx' value)
  53. #define SIDE (0.5f) // side length of a box - don't change this
  54. #define MASS (1.0) // mass of a box
  55. #define STEPSIZE 0.05
  56. // dynamics objects
  57. static dWorldID world;
  58. static dBodyID body[2];
  59. static dJointID joint;
  60. // data from the command line arguments
  61. static int cmd_test_num = -1;
  62. static int cmd_interactive = 0;
  63. static int cmd_graphics = 1;
  64. static char *cmd_path_to_textures = NULL;
  65. static int cmd_occasional_error = 0; // perturb occasionally
  66. // info about the current test
  67. struct TestInfo;
  68. static int test_num = 0; // number of the current test
  69. static int iteration = 0;
  70. static int max_iterations = 0;
  71. static dReal max_error = 0;
  72. //****************************************************************************
  73. // utility stuff
  74. static dReal length (dVector3 a)
  75. {
  76. return dSqrt (a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
  77. }
  78. // get the max difference between a 3x3 matrix and the identity
  79. dReal cmpIdentity (const dMatrix3 A)
  80. {
  81. dMatrix3 I;
  82. dSetZero (I,12);
  83. I[0] = 1;
  84. I[5] = 1;
  85. I[10] = 1;
  86. return dMaxDifference (A,I,3,3);
  87. }
  88. //****************************************************************************
  89. // test world construction and utilities
  90. void constructWorldForTest (dReal gravity, int bodycount,
  91. /* body 1 pos */ dReal pos1x, dReal pos1y, dReal pos1z,
  92. /* body 2 pos */ dReal pos2x, dReal pos2y, dReal pos2z,
  93. /* body 1 rotation axis */ dReal ax1x, dReal ax1y, dReal ax1z,
  94. /* body 1 rotation axis */ dReal ax2x, dReal ax2y, dReal ax2z,
  95. /* rotation angles */ dReal a1, dReal a2)
  96. {
  97. // create world
  98. world = dWorldCreate();
  99. dWorldSetERP (world,0.2);
  100. dWorldSetCFM (world,1e-6);
  101. dWorldSetGravity (world,0,0,gravity);
  102. dMass m;
  103. dMassSetBox (&m,1,SIDE,SIDE,SIDE);
  104. dMassAdjust (&m,MASS);
  105. body[0] = dBodyCreate (world);
  106. dBodySetMass (body[0],&m);
  107. dBodySetPosition (body[0], pos1x, pos1y, pos1z);
  108. dQuaternion q;
  109. dQFromAxisAndAngle (q,ax1x,ax1y,ax1z,a1);
  110. dBodySetQuaternion (body[0],q);
  111. if (bodycount==2) {
  112. body[1] = dBodyCreate (world);
  113. dBodySetMass (body[1],&m);
  114. dBodySetPosition (body[1], pos2x, pos2y, pos2z);
  115. dQFromAxisAndAngle (q,ax2x,ax2y,ax2z,a2);
  116. dBodySetQuaternion (body[1],q);
  117. }
  118. else body[1] = 0;
  119. }
  120. // add an oscillating torque to body 0
  121. void addOscillatingTorque (dReal tscale)
  122. {
  123. static dReal a=0;
  124. dBodyAddTorque (body[0],tscale*cos(2*a),tscale*cos(2.7183*a),
  125. tscale*cos(1.5708*a));
  126. a += 0.01;
  127. }
  128. void addOscillatingTorqueAbout(dReal tscale, dReal x, dReal y, dReal z)
  129. {
  130. static dReal a=0;
  131. dBodyAddTorque (body[0], tscale*cos(a) * x, tscale*cos(a) * y,
  132. tscale * cos(a) * z);
  133. a += 0.02;
  134. }
  135. // damp the rotational motion of body 0 a bit
  136. void dampRotationalMotion (dReal kd)
  137. {
  138. const dReal *w = dBodyGetAngularVel (body[0]);
  139. dBodyAddTorque (body[0],-kd*w[0],-kd*w[1],-kd*w[2]);
  140. }
  141. // add a spring force to keep the bodies together, otherwise they may fly
  142. // apart with some joints.
  143. void addSpringForce (dReal ks)
  144. {
  145. const dReal *p1 = dBodyGetPosition (body[0]);
  146. const dReal *p2 = dBodyGetPosition (body[1]);
  147. dBodyAddForce (body[0],ks*(p2[0]-p1[0]),ks*(p2[1]-p1[1]),ks*(p2[2]-p1[2]));
  148. dBodyAddForce (body[1],ks*(p1[0]-p2[0]),ks*(p1[1]-p2[1]),ks*(p1[2]-p2[2]));
  149. }
  150. // add an oscillating Force to body 0
  151. void addOscillatingForce (dReal fscale)
  152. {
  153. static dReal a=0;
  154. dBodyAddForce (body[0],fscale*cos(2*a),fscale*cos(2.7183*a),
  155. fscale*cos(1.5708*a));
  156. a += 0.01;
  157. }
  158. //****************************************************************************
  159. // stuff specific to the tests
  160. //
  161. // 0xx : fixed
  162. // 1xx : ball and socket
  163. // 2xx : hinge
  164. // 3xx : slider
  165. // 4xx : hinge 2
  166. // 5xx : contact
  167. // 6xx : amotor
  168. // 7xx : universal joint
  169. // 8xx : PR joint (Prismatic and Rotoide)
  170. // setup for the given test. return 0 if there is no such test
  171. int setupTest (int n)
  172. {
  173. switch (n) {
  174. // ********** fixed joint
  175. case 0: { // 2 body
  176. constructWorldForTest (0,2,
  177. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  178. 1,1,0, 1,1,0,
  179. 0.25*M_PI,0.25*M_PI);
  180. joint = dJointCreateFixed (world,0);
  181. dJointAttach (joint,body[0],body[1]);
  182. dJointSetFixed (joint);
  183. return 1;
  184. }
  185. case 1: { // 1 body to static env
  186. constructWorldForTest (0,1,
  187. 0.5*SIDE,0.5*SIDE,1, 0,0,0,
  188. 1,0,0, 1,0,0,
  189. 0,0);
  190. joint = dJointCreateFixed (world,0);
  191. dJointAttach (joint,body[0],0);
  192. dJointSetFixed (joint);
  193. return 1;
  194. }
  195. case 2: { // 2 body with relative rotation
  196. constructWorldForTest (0,2,
  197. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  198. 1,1,0, 1,1,0,
  199. 0.25*M_PI,-0.25*M_PI);
  200. joint = dJointCreateFixed (world,0);
  201. dJointAttach (joint,body[0],body[1]);
  202. dJointSetFixed (joint);
  203. return 1;
  204. }
  205. case 3: { // 1 body to static env with relative rotation
  206. constructWorldForTest (0,1,
  207. 0.5*SIDE,0.5*SIDE,1, 0,0,0,
  208. 1,0,0, 1,0,0,
  209. 0.25*M_PI,0);
  210. joint = dJointCreateFixed (world,0);
  211. dJointAttach (joint,body[0],0);
  212. dJointSetFixed (joint);
  213. return 1;
  214. }
  215. // ********** hinge joint
  216. case 200: // 2 body
  217. constructWorldForTest (0,2,
  218. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  219. 1,1,0, 1,1,0, 0.25*M_PI,0.25*M_PI);
  220. joint = dJointCreateHinge (world,0);
  221. dJointAttach (joint,body[0],body[1]);
  222. dJointSetHingeAnchor (joint,0,0,1);
  223. dJointSetHingeAxis (joint,1,-1,1.41421356);
  224. return 1;
  225. case 220: // hinge angle polarity test
  226. case 221: // hinge angle rate test
  227. constructWorldForTest (0,2,
  228. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  229. 1,0,0, 1,0,0, 0,0);
  230. joint = dJointCreateHinge (world,0);
  231. dJointAttach (joint,body[0],body[1]);
  232. dJointSetHingeAnchor (joint,0,0,1);
  233. dJointSetHingeAxis (joint,0,0,1);
  234. max_iterations = 50;
  235. return 1;
  236. case 230: // hinge motor rate (and polarity) test
  237. case 231: // ...with stops
  238. constructWorldForTest (0,2,
  239. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  240. 1,0,0, 1,0,0, 0,0);
  241. joint = dJointCreateHinge (world,0);
  242. dJointAttach (joint,body[0],body[1]);
  243. dJointSetHingeAnchor (joint,0,0,1);
  244. dJointSetHingeAxis (joint,0,0,1);
  245. dJointSetHingeParam (joint,dParamFMax,1);
  246. if (n==231) {
  247. dJointSetHingeParam (joint,dParamLoStop,-0.5);
  248. dJointSetHingeParam (joint,dParamHiStop,0.5);
  249. }
  250. return 1;
  251. case 250: // limit bounce test (gravity down)
  252. case 251: { // ...gravity up
  253. constructWorldForTest ((n==251) ? 0.1 : -0.1, 2,
  254. 0.5*SIDE,0,1+0.5*SIDE, -0.5*SIDE,0,1-0.5*SIDE,
  255. 1,0,0, 1,0,0, 0,0);
  256. joint = dJointCreateHinge (world,0);
  257. dJointAttach (joint,body[0],body[1]);
  258. dJointSetHingeAnchor (joint,0,0,1);
  259. dJointSetHingeAxis (joint,0,1,0);
  260. dJointSetHingeParam (joint,dParamLoStop,-0.9);
  261. dJointSetHingeParam (joint,dParamHiStop,0.7854);
  262. dJointSetHingeParam (joint,dParamBounce,0.5);
  263. // anchor 2nd body with a fixed joint
  264. dJointID j = dJointCreateFixed (world,0);
  265. dJointAttach (j,body[1],0);
  266. dJointSetFixed (j);
  267. return 1;
  268. }
  269. // ********** slider
  270. case 300: // 2 body
  271. constructWorldForTest (0,2,
  272. 0,0,1, 0.2,0.2,1.2,
  273. 0,0,1, -1,1,0, 0,0.25*M_PI);
  274. joint = dJointCreateSlider (world,0);
  275. dJointAttach (joint,body[0],body[1]);
  276. dJointSetSliderAxis (joint,1,1,1);
  277. return 1;
  278. case 320: // slider angle polarity test
  279. case 321: // slider angle rate test
  280. constructWorldForTest (0,2,
  281. 0,0,1, 0,0,1.2,
  282. 1,0,0, 1,0,0, 0,0);
  283. joint = dJointCreateSlider (world,0);
  284. dJointAttach (joint,body[0],body[1]);
  285. dJointSetSliderAxis (joint,0,0,1);
  286. max_iterations = 50;
  287. return 1;
  288. case 330: // slider motor rate (and polarity) test
  289. case 331: // ...with stops
  290. constructWorldForTest (0, 2,
  291. 0,0,1, 0,0,1.2,
  292. 1,0,0, 1,0,0, 0,0);
  293. joint = dJointCreateSlider (world,0);
  294. dJointAttach (joint,body[0],body[1]);
  295. dJointSetSliderAxis (joint,0,0,1);
  296. dJointSetSliderParam (joint,dParamFMax,100);
  297. if (n==331) {
  298. dJointSetSliderParam (joint,dParamLoStop,-0.4);
  299. dJointSetSliderParam (joint,dParamHiStop,0.4);
  300. }
  301. return 1;
  302. case 350: // limit bounce tests
  303. case 351: {
  304. constructWorldForTest ((n==351) ? 0.1 : -0.1, 2,
  305. 0,0,1, 0,0,1.2,
  306. 1,0,0, 1,0,0, 0,0);
  307. joint = dJointCreateSlider (world,0);
  308. dJointAttach (joint,body[0],body[1]);
  309. dJointSetSliderAxis (joint,0,0,1);
  310. dJointSetSliderParam (joint,dParamLoStop,-0.5);
  311. dJointSetSliderParam (joint,dParamHiStop,0.5);
  312. dJointSetSliderParam (joint,dParamBounce,0.5);
  313. // anchor 2nd body with a fixed joint
  314. dJointID j = dJointCreateFixed (world,0);
  315. dJointAttach (j,body[1],0);
  316. dJointSetFixed (j);
  317. return 1;
  318. }
  319. // ********** hinge-2 joint
  320. case 420: // hinge-2 steering angle polarity test
  321. case 421: // hinge-2 steering angle rate test
  322. constructWorldForTest (0,2,
  323. 0.5*SIDE,0,1, -0.5*SIDE,0,1,
  324. 1,0,0, 1,0,0, 0,0);
  325. joint = dJointCreateHinge2 (world,0);
  326. dJointAttach (joint,body[0],body[1]);
  327. dJointSetHinge2Anchor (joint,-0.5*SIDE,0,1);
  328. dJointSetHinge2Axis1 (joint,0,0,1);
  329. dJointSetHinge2Axis2 (joint,1,0,0);
  330. max_iterations = 50;
  331. return 1;
  332. case 430: // hinge 2 steering motor rate (+polarity) test
  333. case 431: // ...with stops
  334. case 432: // hinge 2 wheel motor rate (+polarity) test
  335. constructWorldForTest (0,2,
  336. 0.5*SIDE,0,1, -0.5*SIDE,0,1,
  337. 1,0,0, 1,0,0, 0,0);
  338. joint = dJointCreateHinge2 (world,0);
  339. dJointAttach (joint,body[0],body[1]);
  340. dJointSetHinge2Anchor (joint,-0.5*SIDE,0,1);
  341. dJointSetHinge2Axis1 (joint,0,0,1);
  342. dJointSetHinge2Axis2 (joint,1,0,0);
  343. dJointSetHinge2Param (joint,dParamFMax,1);
  344. dJointSetHinge2Param (joint,dParamFMax2,1);
  345. if (n==431) {
  346. dJointSetHinge2Param (joint,dParamLoStop,-0.5);
  347. dJointSetHinge2Param (joint,dParamHiStop,0.5);
  348. }
  349. return 1;
  350. // ********** angular motor joint
  351. case 600: // test euler angle calculations
  352. constructWorldForTest (0,2,
  353. -SIDE*0.5,0,1, SIDE*0.5,0,1,
  354. 0,0,1, 0,0,1, 0,0);
  355. joint = dJointCreateAMotor (world,0);
  356. dJointAttach (joint,body[0],body[1]);
  357. dJointSetAMotorNumAxes (joint,3);
  358. dJointSetAMotorAxis (joint,0,1, 0,0,1);
  359. dJointSetAMotorAxis (joint,2,2, 1,0,0);
  360. dJointSetAMotorMode (joint,dAMotorEuler);
  361. max_iterations = 200;
  362. return 1;
  363. // ********** universal joint
  364. case 700: // 2 body
  365. case 701:
  366. case 702:
  367. constructWorldForTest (0,2,
  368. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  369. 1,1,0, 1,1,0, 0.25*M_PI,0.25*M_PI);
  370. joint = dJointCreateUniversal (world,0);
  371. dJointAttach (joint,body[0],body[1]);
  372. dJointSetUniversalAnchor (joint,0,0,1);
  373. dJointSetUniversalAxis1 (joint, 1, -1, 1.41421356);
  374. dJointSetUniversalAxis2 (joint, 1, -1, -1.41421356);
  375. return 1;
  376. case 720: // universal transmit torque test
  377. case 721:
  378. case 722:
  379. case 730: // universal torque about axis 1
  380. case 731:
  381. case 732:
  382. case 740: // universal torque about axis 2
  383. case 741:
  384. case 742:
  385. constructWorldForTest (0,2,
  386. 0.5*SIDE,0.5*SIDE,1, -0.5*SIDE,-0.5*SIDE,1,
  387. 1,0,0, 1,0,0, 0,0);
  388. joint = dJointCreateUniversal (world,0);
  389. dJointAttach (joint,body[0],body[1]);
  390. dJointSetUniversalAnchor (joint,0,0,1);
  391. dJointSetUniversalAxis1 (joint,0,0,1);
  392. dJointSetUniversalAxis2 (joint, 1, -1,0);
  393. max_iterations = 100;
  394. return 1;
  395. // Joint PR (Prismatic and Rotoide)
  396. case 800: // 2 body
  397. case 801: // 2 bodies with spring force and prismatic fixed
  398. case 802: // 2 bodies with torque on body1 and prismatic fixed
  399. constructWorldForTest (0, 2,
  400. -1.0, 0.0, 1.0,
  401. 1.0, 0.0, 1.0,
  402. 1,0,0, 1,0,0,
  403. 0, 0);
  404. joint = dJointCreatePR (world, 0);
  405. dJointAttach (joint, body[0], body[1]);
  406. dJointSetPRAnchor (joint,-0.5, 0.0, 1.0);
  407. dJointSetPRAxis1 (joint, 0, 1, 0);
  408. dJointSetPRAxis2 (joint, 1, 0, 0);
  409. dJointSetPRParam (joint,dParamLoStop,-0.5);
  410. dJointSetPRParam (joint,dParamHiStop,0.5);
  411. dJointSetPRParam (joint,dParamLoStop2,0);
  412. dJointSetPRParam (joint,dParamHiStop2,0);
  413. return 1;
  414. case 803: // 2 bodies with spring force and prismatic NOT fixed
  415. case 804: // 2 bodies with torque force and prismatic NOT fixed
  416. case 805: // 2 bodies with force only on first body
  417. constructWorldForTest (0, 2,
  418. -1.0, 0.0, 1.0,
  419. 1.0, 0.0, 1.0,
  420. 1,0,0, 1,0,0,
  421. 0, 0);
  422. joint = dJointCreatePR (world, 0);
  423. dJointAttach (joint, body[0], body[1]);
  424. dJointSetPRAnchor (joint,-0.5, 0.0, 1.0);
  425. dJointSetPRAxis1 (joint, 0, 1, 0);
  426. dJointSetPRAxis2 (joint, 1, 0, 0);
  427. dJointSetPRParam (joint,dParamLoStop,-0.5);
  428. dJointSetPRParam (joint,dParamHiStop,0.5);
  429. dJointSetPRParam (joint,dParamLoStop2,-0.5);
  430. dJointSetPRParam (joint,dParamHiStop2,0.5);
  431. return 1;
  432. }
  433. return 0;
  434. }
  435. // do stuff specific to this test each iteration. you can check some
  436. // invariants for the test -- the return value is some scaled error measurement
  437. // that must be less than 1.
  438. // return a dInfinity if error is not measured for this n.
  439. dReal doStuffAndGetError (int n)
  440. {
  441. switch (n) {
  442. // ********** fixed joint
  443. case 0: { // 2 body
  444. addOscillatingTorque (0.1);
  445. dampRotationalMotion (0.1);
  446. // check the orientations are the same
  447. const dReal *R1 = dBodyGetRotation (body[0]);
  448. const dReal *R2 = dBodyGetRotation (body[1]);
  449. dReal err1 = dMaxDifference (R1,R2,3,3);
  450. // check the body offset is correct
  451. dVector3 p,pp;
  452. const dReal *p1 = dBodyGetPosition (body[0]);
  453. const dReal *p2 = dBodyGetPosition (body[1]);
  454. for (int i=0; i<3; i++) p[i] = p2[i] - p1[i];
  455. dMultiply1_331 (pp,R1,p);
  456. pp[0] += 0.5;
  457. pp[1] += 0.5;
  458. return (err1 + length (pp)) * 300;
  459. }
  460. case 1: { // 1 body to static env
  461. addOscillatingTorque (0.1);
  462. // check the orientation is the identity
  463. dReal err1 = cmpIdentity (dBodyGetRotation (body[0]));
  464. // check the body offset is correct
  465. dVector3 p;
  466. const dReal *p1 = dBodyGetPosition (body[0]);
  467. for (int i=0; i<3; i++) p[i] = p1[i];
  468. p[0] -= 0.25;
  469. p[1] -= 0.25;
  470. p[2] -= 1;
  471. return (err1 + length (p)) * 1e6;
  472. }
  473. case 2: { // 2 body
  474. addOscillatingTorque (0.1);
  475. dampRotationalMotion (0.1);
  476. // check the body offset is correct
  477. // Should really check body rotation too. Oh well.
  478. const dReal *R1 = dBodyGetRotation (body[0]);
  479. dVector3 p,pp;
  480. const dReal *p1 = dBodyGetPosition (body[0]);
  481. const dReal *p2 = dBodyGetPosition (body[1]);
  482. for (int i=0; i<3; i++) p[i] = p2[i] - p1[i];
  483. dMultiply1_331 (pp,R1,p);
  484. pp[0] += 0.5;
  485. pp[1] += 0.5;
  486. return length(pp) * 300;
  487. }
  488. case 3: { // 1 body to static env with relative rotation
  489. addOscillatingTorque (0.1);
  490. // check the body offset is correct
  491. dVector3 p;
  492. const dReal *p1 = dBodyGetPosition (body[0]);
  493. for (int i=0; i<3; i++) p[i] = p1[i];
  494. p[0] -= 0.25;
  495. p[1] -= 0.25;
  496. p[2] -= 1;
  497. return length (p) * 1e6;
  498. }
  499. // ********** hinge joint
  500. case 200: // 2 body
  501. addOscillatingTorque (0.1);
  502. dampRotationalMotion (0.1);
  503. return dInfinity;
  504. case 220: // hinge angle polarity test
  505. dBodyAddTorque (body[0],0,0,0.01);
  506. dBodyAddTorque (body[1],0,0,-0.01);
  507. if (iteration == 40) {
  508. dReal a = dJointGetHingeAngle (joint);
  509. if (a > 0.5 && a < 1) return 0; else return 10;
  510. }
  511. return 0;
  512. case 221: { // hinge angle rate test
  513. static dReal last_angle = 0;
  514. dBodyAddTorque (body[0],0,0,0.01);
  515. dBodyAddTorque (body[1],0,0,-0.01);
  516. dReal a = dJointGetHingeAngle (joint);
  517. dReal r = dJointGetHingeAngleRate (joint);
  518. dReal er = (a-last_angle)/STEPSIZE; // estimated rate
  519. last_angle = a;
  520. return fabs(r-er) * 4e4;
  521. }
  522. case 230: // hinge motor rate (and polarity) test
  523. case 231: { // ...with stops
  524. static dReal a = 0;
  525. dReal r = dJointGetHingeAngleRate (joint);
  526. dReal err = fabs (cos(a) - r);
  527. if (a==0) err = 0;
  528. a += 0.03;
  529. dJointSetHingeParam (joint,dParamVel,cos(a));
  530. if (n==231) return dInfinity;
  531. return err * 1e6;
  532. }
  533. // ********** slider joint
  534. case 300: // 2 body
  535. addOscillatingTorque (0.05);
  536. dampRotationalMotion (0.1);
  537. addSpringForce (0.5);
  538. return dInfinity;
  539. case 320: // slider angle polarity test
  540. dBodyAddForce (body[0],0,0,0.1);
  541. dBodyAddForce (body[1],0,0,-0.1);
  542. if (iteration == 40) {
  543. dReal a = dJointGetSliderPosition (joint);
  544. if (a > 0.2 && a < 0.5)
  545. return 0;
  546. else
  547. return 10; // Failed
  548. }
  549. return 0;
  550. case 321: { // slider angle rate test
  551. static dReal last_pos = 0;
  552. dBodyAddForce (body[0],0,0,0.1);
  553. dBodyAddForce (body[1],0,0,-0.1);
  554. dReal p = dJointGetSliderPosition (joint);
  555. dReal r = dJointGetSliderPositionRate (joint);
  556. dReal er = (p-last_pos)/STEPSIZE; // estimated rate (almost exact)
  557. last_pos = p;
  558. return fabs(r-er) * 1e9;
  559. }
  560. case 330: // slider motor rate (and polarity) test
  561. case 331: { // ...with stops
  562. static dReal a = 0;
  563. dReal r = dJointGetSliderPositionRate (joint);
  564. dReal err = fabs (0.7*cos(a) - r);
  565. if (a < 0.04) err = 0;
  566. a += 0.03;
  567. dJointSetSliderParam (joint,dParamVel,0.7*cos(a));
  568. if (n==331) return dInfinity;
  569. return err * 1e6;
  570. }
  571. // ********** hinge-2 joint
  572. case 420: // hinge-2 steering angle polarity test
  573. dBodyAddTorque (body[0],0,0,0.01);
  574. dBodyAddTorque (body[1],0,0,-0.01);
  575. if (iteration == 40) {
  576. dReal a = dJointGetHinge2Angle1 (joint);
  577. if (a > 0.5 && a < 0.6) return 0; else return 10;
  578. }
  579. return 0;
  580. case 421: { // hinge-2 steering angle rate test
  581. static dReal last_angle = 0;
  582. dBodyAddTorque (body[0],0,0,0.01);
  583. dBodyAddTorque (body[1],0,0,-0.01);
  584. dReal a = dJointGetHinge2Angle1 (joint);
  585. dReal r = dJointGetHinge2Angle1Rate (joint);
  586. dReal er = (a-last_angle)/STEPSIZE; // estimated rate
  587. last_angle = a;
  588. return fabs(r-er)*2e4;
  589. }
  590. case 430: // hinge 2 steering motor rate (+polarity) test
  591. case 431: { // ...with stops
  592. static dReal a = 0;
  593. dReal r = dJointGetHinge2Angle1Rate (joint);
  594. dReal err = fabs (cos(a) - r);
  595. if (a==0) err = 0;
  596. a += 0.03;
  597. dJointSetHinge2Param (joint,dParamVel,cos(a));
  598. if (n==431) return dInfinity;
  599. return err * 1e6;
  600. }
  601. case 432: { // hinge 2 wheel motor rate (+polarity) test
  602. static dReal a = 0;
  603. dReal r = dJointGetHinge2Angle2Rate (joint);
  604. dReal err = fabs (cos(a) - r);
  605. if (a==0) err = 0;
  606. a += 0.03;
  607. dJointSetHinge2Param (joint,dParamVel2,cos(a));
  608. return err * 1e6;
  609. }
  610. // ********** angular motor joint
  611. case 600: { // test euler angle calculations
  612. // desired euler angles from last iteration
  613. static dReal a1,a2,a3;
  614. // find actual euler angles
  615. dReal aa1 = dJointGetAMotorAngle (joint,0);
  616. dReal aa2 = dJointGetAMotorAngle (joint,1);
  617. dReal aa3 = dJointGetAMotorAngle (joint,2);
  618. // printf ("actual = %.4f %.4f %.4f\n\n",aa1,aa2,aa3);
  619. dReal err = dInfinity;
  620. if (iteration > 0) {
  621. err = dFabs(aa1-a1) + dFabs(aa2-a2) + dFabs(aa3-a3);
  622. err *= 1e10;
  623. }
  624. // get random base rotation for both bodies
  625. dMatrix3 Rbase;
  626. dRFromAxisAndAngle (Rbase, 3*(dRandReal()-0.5), 3*(dRandReal()-0.5),
  627. 3*(dRandReal()-0.5), 3*(dRandReal()-0.5));
  628. dBodySetRotation (body[0],Rbase);
  629. // rotate body 2 by random euler angles w.r.t. body 1
  630. a1 = 3.14 * 2 * (dRandReal()-0.5);
  631. a2 = 1.57 * 2 * (dRandReal()-0.5);
  632. a3 = 3.14 * 2 * (dRandReal()-0.5);
  633. dMatrix3 R1,R2,R3,Rtmp1,Rtmp2;
  634. dRFromAxisAndAngle (R1,0,0,1,-a1);
  635. dRFromAxisAndAngle (R2,0,1,0,a2);
  636. dRFromAxisAndAngle (R3,1,0,0,-a3);
  637. dMultiply0 (Rtmp1,R2,R3,3,3,3);
  638. dMultiply0 (Rtmp2,R1,Rtmp1,3,3,3);
  639. dMultiply0 (Rtmp1,Rbase,Rtmp2,3,3,3);
  640. dBodySetRotation (body[1],Rtmp1);
  641. // printf ("desired = %.4f %.4f %.4f\n",a1,a2,a3);
  642. return err;
  643. }
  644. // ********** universal joint
  645. case 700: { // 2 body: joint constraint
  646. dVector3 ax1, ax2;
  647. addOscillatingTorque (0.1);
  648. dampRotationalMotion (0.1);
  649. dJointGetUniversalAxis1(joint, ax1);
  650. dJointGetUniversalAxis2(joint, ax2);
  651. return fabs(10*dCalcVectorDot3(ax1, ax2));
  652. }
  653. case 701: { // 2 body: angle 1 rate
  654. static dReal last_angle = 0;
  655. addOscillatingTorque (0.1);
  656. dampRotationalMotion (0.1);
  657. dReal a = dJointGetUniversalAngle1(joint);
  658. dReal r = dJointGetUniversalAngle1Rate(joint);
  659. dReal diff = a - last_angle;
  660. if (diff > M_PI) diff -= 2*M_PI;
  661. if (diff < -M_PI) diff += 2*M_PI;
  662. dReal er = diff / STEPSIZE; // estimated rate
  663. last_angle = a;
  664. // I'm not sure why the error is so large here.
  665. return fabs(r - er) * 1e1;
  666. }
  667. case 702: { // 2 body: angle 2 rate
  668. static dReal last_angle = 0;
  669. addOscillatingTorque (0.1);
  670. dampRotationalMotion (0.1);
  671. dReal a = dJointGetUniversalAngle2(joint);
  672. dReal r = dJointGetUniversalAngle2Rate(joint);
  673. dReal diff = a - last_angle;
  674. if (diff > M_PI) diff -= 2*M_PI;
  675. if (diff < -M_PI) diff += 2*M_PI;
  676. dReal er = diff / STEPSIZE; // estimated rate
  677. last_angle = a;
  678. // I'm not sure why the error is so large here.
  679. return fabs(r - er) * 1e1;
  680. }
  681. case 720: { // universal transmit torque test: constraint error
  682. dVector3 ax1, ax2;
  683. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  684. dampRotationalMotion (0.1);
  685. dJointGetUniversalAxis1(joint, ax1);
  686. dJointGetUniversalAxis2(joint, ax2);
  687. return fabs(10*dCalcVectorDot3(ax1, ax2));
  688. }
  689. case 721: { // universal transmit torque test: angle1 rate
  690. static dReal last_angle = 0;
  691. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  692. dampRotationalMotion (0.1);
  693. dReal a = dJointGetUniversalAngle1(joint);
  694. dReal r = dJointGetUniversalAngle1Rate(joint);
  695. dReal diff = a - last_angle;
  696. if (diff > M_PI) diff -= 2*M_PI;
  697. if (diff < -M_PI) diff += 2*M_PI;
  698. dReal er = diff / STEPSIZE; // estimated rate
  699. last_angle = a;
  700. return fabs(r - er) * 1e10;
  701. }
  702. case 722: { // universal transmit torque test: angle2 rate
  703. static dReal last_angle = 0;
  704. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  705. dampRotationalMotion (0.1);
  706. dReal a = dJointGetUniversalAngle2(joint);
  707. dReal r = dJointGetUniversalAngle2Rate(joint);
  708. dReal diff = a - last_angle;
  709. if (diff > M_PI) diff -= 2*M_PI;
  710. if (diff < -M_PI) diff += 2*M_PI;
  711. dReal er = diff / STEPSIZE; // estimated rate
  712. last_angle = a;
  713. return fabs(r - er) * 1e10;
  714. }
  715. case 730:{
  716. dVector3 ax1, ax2;
  717. dJointGetUniversalAxis1(joint, ax1);
  718. dJointGetUniversalAxis2(joint, ax2);
  719. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  720. dampRotationalMotion (0.1);
  721. return fabs(10*dCalcVectorDot3(ax1, ax2));
  722. }
  723. case 731:{
  724. dVector3 ax1;
  725. static dReal last_angle = 0;
  726. dJointGetUniversalAxis1(joint, ax1);
  727. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  728. dampRotationalMotion (0.1);
  729. dReal a = dJointGetUniversalAngle1(joint);
  730. dReal r = dJointGetUniversalAngle1Rate(joint);
  731. dReal diff = a - last_angle;
  732. if (diff > M_PI) diff -= 2*M_PI;
  733. if (diff < -M_PI) diff += 2*M_PI;
  734. dReal er = diff / STEPSIZE; // estimated rate
  735. last_angle = a;
  736. return fabs(r - er) * 2e3;
  737. }
  738. case 732:{
  739. dVector3 ax1;
  740. static dReal last_angle = 0;
  741. dJointGetUniversalAxis1(joint, ax1);
  742. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  743. dampRotationalMotion (0.1);
  744. dReal a = dJointGetUniversalAngle2(joint);
  745. dReal r = dJointGetUniversalAngle2Rate(joint);
  746. dReal diff = a - last_angle;
  747. if (diff > M_PI) diff -= 2*M_PI;
  748. if (diff < -M_PI) diff += 2*M_PI;
  749. dReal er = diff / STEPSIZE; // estimated rate
  750. last_angle = a;
  751. return fabs(r - er) * 1e10;
  752. }
  753. case 740:{
  754. dVector3 ax1, ax2;
  755. dJointGetUniversalAxis1(joint, ax1);
  756. dJointGetUniversalAxis2(joint, ax2);
  757. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  758. dampRotationalMotion (0.1);
  759. return fabs(10*dCalcVectorDot3(ax1, ax2));
  760. }
  761. case 741:{
  762. dVector3 ax2;
  763. static dReal last_angle = 0;
  764. dJointGetUniversalAxis2(joint, ax2);
  765. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  766. dampRotationalMotion (0.1);
  767. dReal a = dJointGetUniversalAngle1(joint);
  768. dReal r = dJointGetUniversalAngle1Rate(joint);
  769. dReal diff = a - last_angle;
  770. if (diff > M_PI) diff -= 2*M_PI;
  771. if (diff < -M_PI) diff += 2*M_PI;
  772. dReal er = diff / STEPSIZE; // estimated rate
  773. last_angle = a;
  774. return fabs(r - er) * 1e10;
  775. }
  776. case 742:{
  777. dVector3 ax2;
  778. static dReal last_angle = 0;
  779. dJointGetUniversalAxis2(joint, ax2);
  780. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  781. dampRotationalMotion (0.1);
  782. dReal a = dJointGetUniversalAngle2(joint);
  783. dReal r = dJointGetUniversalAngle2Rate(joint);
  784. dReal diff = a - last_angle;
  785. if (diff > M_PI) diff -= 2*M_PI;
  786. if (diff < -M_PI) diff += 2*M_PI;
  787. dReal er = diff / STEPSIZE; // estimated rate
  788. last_angle = a;
  789. return fabs(r - er) * 1e4;
  790. }
  791. // ********** slider joint
  792. case 801:
  793. case 803:
  794. addSpringForce (0.25);
  795. return dInfinity;
  796. case 802:
  797. case 804: {
  798. static dReal a = 0;
  799. dBodyAddTorque (body[0], 0, 0.01*cos(1.5708*a), 0);
  800. a += 0.01;
  801. return dInfinity;
  802. }
  803. case 805:
  804. addOscillatingForce (0.1);
  805. return dInfinity;
  806. }
  807. return dInfinity;
  808. }
  809. //****************************************************************************
  810. // simulation stuff common to all the tests
  811. // start simulation - set viewpoint
  812. static void start()
  813. {
  814. dAllocateODEDataForThread(dAllocateMaskAll);
  815. static float xyz[3] = {1.0382f,-1.0811f,1.4700f};
  816. static float hpr[3] = {135.0000f,-19.5000f,0.0000f};
  817. dsSetViewpoint (xyz,hpr);
  818. }
  819. // simulation loop
  820. static void simLoop (int pause)
  821. {
  822. // stop after a given number of iterations, as long as we are not in
  823. // interactive mode
  824. if (cmd_graphics && !cmd_interactive &&
  825. (iteration >= max_iterations)) {
  826. dsStop();
  827. return;
  828. }
  829. iteration++;
  830. if (!pause) {
  831. // do stuff for this test and check to see if the joint is behaving well
  832. dReal error = doStuffAndGetError (test_num);
  833. if (error > max_error) max_error = error;
  834. if (cmd_interactive && error < dInfinity) {
  835. printf ("scaled error = %.4e\n",error);
  836. }
  837. // take a step
  838. dWorldStep (world,STEPSIZE);
  839. // occasionally re-orient the first body to create a deliberate error.
  840. if (cmd_occasional_error) {
  841. static int count = 0;
  842. if ((count % 20)==0) {
  843. // randomly adjust orientation of body[0]
  844. const dReal *R1;
  845. dMatrix3 R2,R3;
  846. R1 = dBodyGetRotation (body[0]);
  847. dRFromAxisAndAngle (R2,dRandReal()-0.5,dRandReal()-0.5,
  848. dRandReal()-0.5,dRandReal()-0.5);
  849. dMultiply0 (R3,R1,R2,3,3,3);
  850. dBodySetRotation (body[0],R3);
  851. // randomly adjust position of body[0]
  852. const dReal *pos = dBodyGetPosition (body[0]);
  853. dBodySetPosition (body[0],
  854. pos[0]+0.2*(dRandReal()-0.5),
  855. pos[1]+0.2*(dRandReal()-0.5),
  856. pos[2]+0.2*(dRandReal()-0.5));
  857. }
  858. count++;
  859. }
  860. }
  861. if (cmd_graphics) {
  862. dReal sides1[3] = {SIDE,SIDE,SIDE};
  863. dReal sides2[3] = {SIDE*0.99f,SIDE*0.99f,SIDE*0.99f};
  864. dsSetTexture (DS_WOOD);
  865. dsSetColor (1,1,0);
  866. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1);
  867. if (body[1]) {
  868. dsSetColor (0,1,1);
  869. dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2);
  870. }
  871. }
  872. }
  873. //****************************************************************************
  874. // conduct a specific test, and report the results
  875. void doTest (int argc, char **argv, int n, int fatal_if_bad_n)
  876. {
  877. test_num = n;
  878. iteration = 0;
  879. max_iterations = 300;
  880. max_error = 0;
  881. if (! setupTest (n)) {
  882. if (fatal_if_bad_n) dError (0,"bad test number");
  883. return;
  884. }
  885. // setup pointers to drawstuff callback functions
  886. dsFunctions fn;
  887. fn.version = DS_VERSION;
  888. fn.start = &start;
  889. fn.step = &simLoop;
  890. fn.command = 0;
  891. fn.stop = 0;
  892. if (cmd_path_to_textures)
  893. fn.path_to_textures = cmd_path_to_textures;
  894. else
  895. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  896. // run simulation
  897. if (cmd_graphics) {
  898. dsSimulationLoop (argc,argv,352,288,&fn);
  899. }
  900. else {
  901. for (int i=0; i < max_iterations; i++) simLoop (0);
  902. }
  903. dWorldDestroy (world);
  904. body[0] = 0;
  905. body[1] = 0;
  906. joint = 0;
  907. // print results
  908. printf ("test %d: ",n);
  909. if (max_error == dInfinity) printf ("error not computed\n");
  910. else {
  911. printf ("max scaled error = %.4e",max_error);
  912. if (max_error < 1) printf (" - passed\n");
  913. else printf (" - FAILED\n");
  914. }
  915. }
  916. //****************************************************************************
  917. // main
  918. int main (int argc, char **argv)
  919. {
  920. int i;
  921. dInitODE2(0);
  922. // process the command line args. anything that starts with `-' is assumed
  923. // to be a drawstuff argument.
  924. for (i=1; i<argc; i++) {
  925. if ( argv[i][0]=='-' && argv[i][1]=='i' && argv[i][2]==0) cmd_interactive = 1;
  926. else if ( argv[i][0]=='-' && argv[i][1]=='g' && argv[i][2]==0) cmd_graphics = 0;
  927. else if ( argv[i][0]=='-' && argv[i][1]=='e' && argv[i][2]==0) cmd_graphics = 0;
  928. else if ( argv[i][0]=='-' && argv[i][1]=='n' && isdigit(argv[i][2]) ) {
  929. char *endptr;
  930. long int n = strtol (&(argv[i][2]),&endptr,10);
  931. if (*endptr == 0) cmd_test_num = n;
  932. }
  933. else
  934. cmd_path_to_textures = argv[i];
  935. }
  936. // do the tests
  937. if (cmd_test_num == -1) {
  938. for (i=0; i<NUM_JOINTS*100; i++) doTest (argc,argv,i,0);
  939. }
  940. else {
  941. doTest (argc,argv,cmd_test_num,1);
  942. }
  943. dCloseODE();
  944. return 0;
  945. }