demo_joints.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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) return 0; else return 10;
  545. return a;
  546. }
  547. return 0;
  548. case 321: { // slider angle rate test
  549. static dReal last_pos = 0;
  550. dBodyAddForce (body[0],0,0,0.1);
  551. dBodyAddForce (body[1],0,0,-0.1);
  552. dReal p = dJointGetSliderPosition (joint);
  553. dReal r = dJointGetSliderPositionRate (joint);
  554. dReal er = (p-last_pos)/STEPSIZE; // estimated rate (almost exact)
  555. last_pos = p;
  556. return fabs(r-er) * 1e9;
  557. }
  558. case 330: // slider motor rate (and polarity) test
  559. case 331: { // ...with stops
  560. static dReal a = 0;
  561. dReal r = dJointGetSliderPositionRate (joint);
  562. dReal err = fabs (0.7*cos(a) - r);
  563. if (a < 0.04) err = 0;
  564. a += 0.03;
  565. dJointSetSliderParam (joint,dParamVel,0.7*cos(a));
  566. if (n==331) return dInfinity;
  567. return err * 1e6;
  568. }
  569. // ********** hinge-2 joint
  570. case 420: // hinge-2 steering angle polarity test
  571. dBodyAddTorque (body[0],0,0,0.01);
  572. dBodyAddTorque (body[1],0,0,-0.01);
  573. if (iteration == 40) {
  574. dReal a = dJointGetHinge2Angle1 (joint);
  575. if (a > 0.5 && a < 0.6) return 0; else return 10;
  576. }
  577. return 0;
  578. case 421: { // hinge-2 steering angle rate test
  579. static dReal last_angle = 0;
  580. dBodyAddTorque (body[0],0,0,0.01);
  581. dBodyAddTorque (body[1],0,0,-0.01);
  582. dReal a = dJointGetHinge2Angle1 (joint);
  583. dReal r = dJointGetHinge2Angle1Rate (joint);
  584. dReal er = (a-last_angle)/STEPSIZE; // estimated rate
  585. last_angle = a;
  586. return fabs(r-er)*2e4;
  587. }
  588. case 430: // hinge 2 steering motor rate (+polarity) test
  589. case 431: { // ...with stops
  590. static dReal a = 0;
  591. dReal r = dJointGetHinge2Angle1Rate (joint);
  592. dReal err = fabs (cos(a) - r);
  593. if (a==0) err = 0;
  594. a += 0.03;
  595. dJointSetHinge2Param (joint,dParamVel,cos(a));
  596. if (n==431) return dInfinity;
  597. return err * 1e6;
  598. }
  599. case 432: { // hinge 2 wheel motor rate (+polarity) test
  600. static dReal a = 0;
  601. dReal r = dJointGetHinge2Angle2Rate (joint);
  602. dReal err = fabs (cos(a) - r);
  603. if (a==0) err = 0;
  604. a += 0.03;
  605. dJointSetHinge2Param (joint,dParamVel2,cos(a));
  606. return err * 1e6;
  607. }
  608. // ********** angular motor joint
  609. case 600: { // test euler angle calculations
  610. // desired euler angles from last iteration
  611. static dReal a1,a2,a3;
  612. // find actual euler angles
  613. dReal aa1 = dJointGetAMotorAngle (joint,0);
  614. dReal aa2 = dJointGetAMotorAngle (joint,1);
  615. dReal aa3 = dJointGetAMotorAngle (joint,2);
  616. // printf ("actual = %.4f %.4f %.4f\n\n",aa1,aa2,aa3);
  617. dReal err = dInfinity;
  618. if (iteration > 0) {
  619. err = dFabs(aa1-a1) + dFabs(aa2-a2) + dFabs(aa3-a3);
  620. err *= 1e10;
  621. }
  622. // get random base rotation for both bodies
  623. dMatrix3 Rbase;
  624. dRFromAxisAndAngle (Rbase, 3*(dRandReal()-0.5), 3*(dRandReal()-0.5),
  625. 3*(dRandReal()-0.5), 3*(dRandReal()-0.5));
  626. dBodySetRotation (body[0],Rbase);
  627. // rotate body 2 by random euler angles w.r.t. body 1
  628. a1 = 3.14 * 2 * (dRandReal()-0.5);
  629. a2 = 1.57 * 2 * (dRandReal()-0.5);
  630. a3 = 3.14 * 2 * (dRandReal()-0.5);
  631. dMatrix3 R1,R2,R3,Rtmp1,Rtmp2;
  632. dRFromAxisAndAngle (R1,0,0,1,-a1);
  633. dRFromAxisAndAngle (R2,0,1,0,a2);
  634. dRFromAxisAndAngle (R3,1,0,0,-a3);
  635. dMultiply0 (Rtmp1,R2,R3,3,3,3);
  636. dMultiply0 (Rtmp2,R1,Rtmp1,3,3,3);
  637. dMultiply0 (Rtmp1,Rbase,Rtmp2,3,3,3);
  638. dBodySetRotation (body[1],Rtmp1);
  639. // printf ("desired = %.4f %.4f %.4f\n",a1,a2,a3);
  640. return err;
  641. }
  642. // ********** universal joint
  643. case 700: { // 2 body: joint constraint
  644. dVector3 ax1, ax2;
  645. addOscillatingTorque (0.1);
  646. dampRotationalMotion (0.1);
  647. dJointGetUniversalAxis1(joint, ax1);
  648. dJointGetUniversalAxis2(joint, ax2);
  649. return fabs(10*dDOT(ax1, ax2));
  650. }
  651. case 701: { // 2 body: angle 1 rate
  652. static dReal last_angle = 0;
  653. addOscillatingTorque (0.1);
  654. dampRotationalMotion (0.1);
  655. dReal a = dJointGetUniversalAngle1(joint);
  656. dReal r = dJointGetUniversalAngle1Rate(joint);
  657. dReal diff = a - last_angle;
  658. if (diff > M_PI) diff -= 2*M_PI;
  659. if (diff < -M_PI) diff += 2*M_PI;
  660. dReal er = diff / STEPSIZE; // estimated rate
  661. last_angle = a;
  662. // I'm not sure why the error is so large here.
  663. return fabs(r - er) * 1e1;
  664. }
  665. case 702: { // 2 body: angle 2 rate
  666. static dReal last_angle = 0;
  667. addOscillatingTorque (0.1);
  668. dampRotationalMotion (0.1);
  669. dReal a = dJointGetUniversalAngle2(joint);
  670. dReal r = dJointGetUniversalAngle2Rate(joint);
  671. dReal diff = a - last_angle;
  672. if (diff > M_PI) diff -= 2*M_PI;
  673. if (diff < -M_PI) diff += 2*M_PI;
  674. dReal er = diff / STEPSIZE; // estimated rate
  675. last_angle = a;
  676. // I'm not sure why the error is so large here.
  677. return fabs(r - er) * 1e1;
  678. }
  679. case 720: { // universal transmit torque test: constraint error
  680. dVector3 ax1, ax2;
  681. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  682. dampRotationalMotion (0.1);
  683. dJointGetUniversalAxis1(joint, ax1);
  684. dJointGetUniversalAxis2(joint, ax2);
  685. return fabs(10*dDOT(ax1, ax2));
  686. }
  687. case 721: { // universal transmit torque test: angle1 rate
  688. static dReal last_angle = 0;
  689. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  690. dampRotationalMotion (0.1);
  691. dReal a = dJointGetUniversalAngle1(joint);
  692. dReal r = dJointGetUniversalAngle1Rate(joint);
  693. dReal diff = a - last_angle;
  694. if (diff > M_PI) diff -= 2*M_PI;
  695. if (diff < -M_PI) diff += 2*M_PI;
  696. dReal er = diff / STEPSIZE; // estimated rate
  697. last_angle = a;
  698. return fabs(r - er) * 1e10;
  699. }
  700. case 722: { // universal transmit torque test: angle2 rate
  701. static dReal last_angle = 0;
  702. addOscillatingTorqueAbout (0.1, 1, 1, 0);
  703. dampRotationalMotion (0.1);
  704. dReal a = dJointGetUniversalAngle2(joint);
  705. dReal r = dJointGetUniversalAngle2Rate(joint);
  706. dReal diff = a - last_angle;
  707. if (diff > M_PI) diff -= 2*M_PI;
  708. if (diff < -M_PI) diff += 2*M_PI;
  709. dReal er = diff / STEPSIZE; // estimated rate
  710. last_angle = a;
  711. return fabs(r - er) * 1e10;
  712. }
  713. case 730:{
  714. dVector3 ax1, ax2;
  715. dJointGetUniversalAxis1(joint, ax1);
  716. dJointGetUniversalAxis2(joint, ax2);
  717. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  718. dampRotationalMotion (0.1);
  719. return fabs(10*dDOT(ax1, ax2));
  720. }
  721. case 731:{
  722. dVector3 ax1;
  723. static dReal last_angle = 0;
  724. dJointGetUniversalAxis1(joint, ax1);
  725. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  726. dampRotationalMotion (0.1);
  727. dReal a = dJointGetUniversalAngle1(joint);
  728. dReal r = dJointGetUniversalAngle1Rate(joint);
  729. dReal diff = a - last_angle;
  730. if (diff > M_PI) diff -= 2*M_PI;
  731. if (diff < -M_PI) diff += 2*M_PI;
  732. dReal er = diff / STEPSIZE; // estimated rate
  733. last_angle = a;
  734. return fabs(r - er) * 2e3;
  735. }
  736. case 732:{
  737. dVector3 ax1;
  738. static dReal last_angle = 0;
  739. dJointGetUniversalAxis1(joint, ax1);
  740. addOscillatingTorqueAbout (0.1, ax1[0], ax1[1], ax1[2]);
  741. dampRotationalMotion (0.1);
  742. dReal a = dJointGetUniversalAngle2(joint);
  743. dReal r = dJointGetUniversalAngle2Rate(joint);
  744. dReal diff = a - last_angle;
  745. if (diff > M_PI) diff -= 2*M_PI;
  746. if (diff < -M_PI) diff += 2*M_PI;
  747. dReal er = diff / STEPSIZE; // estimated rate
  748. last_angle = a;
  749. return fabs(r - er) * 1e10;
  750. }
  751. case 740:{
  752. dVector3 ax1, ax2;
  753. dJointGetUniversalAxis1(joint, ax1);
  754. dJointGetUniversalAxis2(joint, ax2);
  755. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  756. dampRotationalMotion (0.1);
  757. return fabs(10*dDOT(ax1, ax2));
  758. }
  759. case 741:{
  760. dVector3 ax2;
  761. static dReal last_angle = 0;
  762. dJointGetUniversalAxis2(joint, ax2);
  763. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  764. dampRotationalMotion (0.1);
  765. dReal a = dJointGetUniversalAngle1(joint);
  766. dReal r = dJointGetUniversalAngle1Rate(joint);
  767. dReal diff = a - last_angle;
  768. if (diff > M_PI) diff -= 2*M_PI;
  769. if (diff < -M_PI) diff += 2*M_PI;
  770. dReal er = diff / STEPSIZE; // estimated rate
  771. last_angle = a;
  772. return fabs(r - er) * 1e10;
  773. }
  774. case 742:{
  775. dVector3 ax2;
  776. static dReal last_angle = 0;
  777. dJointGetUniversalAxis2(joint, ax2);
  778. addOscillatingTorqueAbout (0.1, ax2[0], ax2[1], ax2[2]);
  779. dampRotationalMotion (0.1);
  780. dReal a = dJointGetUniversalAngle2(joint);
  781. dReal r = dJointGetUniversalAngle2Rate(joint);
  782. dReal diff = a - last_angle;
  783. if (diff > M_PI) diff -= 2*M_PI;
  784. if (diff < -M_PI) diff += 2*M_PI;
  785. dReal er = diff / STEPSIZE; // estimated rate
  786. last_angle = a;
  787. return fabs(r - er) * 1e4;
  788. }
  789. // ********** slider joint
  790. case 801:
  791. case 803:
  792. addSpringForce (0.25);
  793. return dInfinity;
  794. case 802:
  795. case 804: {
  796. static dReal a = 0;
  797. dBodyAddTorque (body[0], 0, 0.01*cos(1.5708*a), 0);
  798. a += 0.01;
  799. return dInfinity;
  800. }
  801. case 805:
  802. addOscillatingForce (0.1);
  803. return dInfinity;
  804. }
  805. return dInfinity;
  806. }
  807. //****************************************************************************
  808. // simulation stuff common to all the tests
  809. // start simulation - set viewpoint
  810. static void start()
  811. {
  812. dAllocateODEDataForThread(dAllocateMaskAll);
  813. static float xyz[3] = {1.0382f,-1.0811f,1.4700f};
  814. static float hpr[3] = {135.0000f,-19.5000f,0.0000f};
  815. dsSetViewpoint (xyz,hpr);
  816. }
  817. // simulation loop
  818. static void simLoop (int pause)
  819. {
  820. // stop after a given number of iterations, as long as we are not in
  821. // interactive mode
  822. if (cmd_graphics && !cmd_interactive &&
  823. (iteration >= max_iterations)) {
  824. dsStop();
  825. return;
  826. }
  827. iteration++;
  828. if (!pause) {
  829. // do stuff for this test and check to see if the joint is behaving well
  830. dReal error = doStuffAndGetError (test_num);
  831. if (error > max_error) max_error = error;
  832. if (cmd_interactive && error < dInfinity) {
  833. printf ("scaled error = %.4e\n",error);
  834. }
  835. // take a step
  836. dWorldStep (world,STEPSIZE);
  837. // occasionally re-orient the first body to create a deliberate error.
  838. if (cmd_occasional_error) {
  839. static int count = 0;
  840. if ((count % 20)==0) {
  841. // randomly adjust orientation of body[0]
  842. const dReal *R1;
  843. dMatrix3 R2,R3;
  844. R1 = dBodyGetRotation (body[0]);
  845. dRFromAxisAndAngle (R2,dRandReal()-0.5,dRandReal()-0.5,
  846. dRandReal()-0.5,dRandReal()-0.5);
  847. dMultiply0 (R3,R1,R2,3,3,3);
  848. dBodySetRotation (body[0],R3);
  849. // randomly adjust position of body[0]
  850. const dReal *pos = dBodyGetPosition (body[0]);
  851. dBodySetPosition (body[0],
  852. pos[0]+0.2*(dRandReal()-0.5),
  853. pos[1]+0.2*(dRandReal()-0.5),
  854. pos[2]+0.2*(dRandReal()-0.5));
  855. }
  856. count++;
  857. }
  858. }
  859. if (cmd_graphics) {
  860. dReal sides1[3] = {SIDE,SIDE,SIDE};
  861. dReal sides2[3] = {SIDE*0.99f,SIDE*0.99f,SIDE*0.99f};
  862. dsSetTexture (DS_WOOD);
  863. dsSetColor (1,1,0);
  864. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1);
  865. if (body[1]) {
  866. dsSetColor (0,1,1);
  867. dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2);
  868. }
  869. }
  870. }
  871. //****************************************************************************
  872. // conduct a specific test, and report the results
  873. void doTest (int argc, char **argv, int n, int fatal_if_bad_n)
  874. {
  875. test_num = n;
  876. iteration = 0;
  877. max_iterations = 300;
  878. max_error = 0;
  879. if (! setupTest (n)) {
  880. if (fatal_if_bad_n) dError (0,"bad test number");
  881. return;
  882. }
  883. // setup pointers to drawstuff callback functions
  884. dsFunctions fn;
  885. fn.version = DS_VERSION;
  886. fn.start = &start;
  887. fn.step = &simLoop;
  888. fn.command = 0;
  889. fn.stop = 0;
  890. if (cmd_path_to_textures)
  891. fn.path_to_textures = cmd_path_to_textures;
  892. else
  893. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  894. // run simulation
  895. if (cmd_graphics) {
  896. dsSimulationLoop (argc,argv,352,288,&fn);
  897. }
  898. else {
  899. for (int i=0; i < max_iterations; i++) simLoop (0);
  900. }
  901. dWorldDestroy (world);
  902. body[0] = 0;
  903. body[1] = 0;
  904. joint = 0;
  905. // print results
  906. printf ("test %d: ",n);
  907. if (max_error == dInfinity) printf ("error not computed\n");
  908. else {
  909. printf ("max scaled error = %.4e",max_error);
  910. if (max_error < 1) printf (" - passed\n");
  911. else printf (" - FAILED\n");
  912. }
  913. }
  914. //****************************************************************************
  915. // main
  916. int main (int argc, char **argv)
  917. {
  918. int i;
  919. dInitODE2(0);
  920. // process the command line args. anything that starts with `-' is assumed
  921. // to be a drawstuff argument.
  922. for (i=1; i<argc; i++) {
  923. if ( argv[i][0]=='-' && argv[i][1]=='i' && argv[i][2]==0) cmd_interactive = 1;
  924. else if ( argv[i][0]=='-' && argv[i][1]=='g' && argv[i][2]==0) cmd_graphics = 0;
  925. else if ( argv[i][0]=='-' && argv[i][1]=='e' && argv[i][2]==0) cmd_graphics = 0;
  926. else if ( argv[i][0]=='-' && argv[i][1]=='n' && isdigit(argv[i][2]) ) {
  927. char *endptr;
  928. long int n = strtol (&(argv[i][2]),&endptr,10);
  929. if (*endptr == 0) cmd_test_num = n;
  930. }
  931. else
  932. cmd_path_to_textures = argv[i];
  933. }
  934. // do the tests
  935. if (cmd_test_num == -1) {
  936. for (i=0; i<NUM_JOINTS*100; i++) doTest (argc,argv,i,0);
  937. }
  938. else {
  939. doTest (argc,argv,cmd_test_num,1);
  940. }
  941. dCloseODE();
  942. return 0;
  943. }