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