demo_feedback.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // Test for breaking joints, by Bram Stolk
  23. #include <ode/odeconfig.h>
  24. #include <assert.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <ode/ode.h>
  29. #include <drawstuff/drawstuff.h>
  30. #include "texturepath.h"
  31. #ifdef _MSC_VER
  32. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  33. #endif
  34. #ifdef dDOUBLE
  35. #define dsDrawBox dsDrawBoxD
  36. #define dsDrawCylinder dsDrawCylinderD
  37. #endif
  38. // dynamics and collision objects (chassis, 3 wheels, environment)
  39. static dWorldID world;
  40. static dSpaceID space;
  41. static const int STACKCNT=10; // nr of weights on bridge
  42. static const int SEGMCNT=16; // nr of segments in bridge
  43. static const float SEGMDIM[3] = { 0.9, 4, 0.1 };
  44. static dGeomID groundgeom;
  45. static dBodyID segbodies[SEGMCNT];
  46. static dGeomID seggeoms[SEGMCNT];
  47. static dBodyID stackbodies[STACKCNT];
  48. static dGeomID stackgeoms[STACKCNT];
  49. static dJointID hinges[SEGMCNT-1];
  50. static dJointID sliders[2];
  51. static dJointFeedback jfeedbacks[SEGMCNT-1];
  52. static dReal colours[SEGMCNT];
  53. static int stress[SEGMCNT-1];
  54. static dJointGroupID contactgroup;
  55. // this is called by dSpaceCollide when two objects in space are
  56. // potentially colliding.
  57. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  58. {
  59. assert(o1);
  60. assert(o2);
  61. if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
  62. {
  63. fprintf(stderr,"testing space %p %p\n", o1,o2);
  64. // colliding a space with something
  65. dSpaceCollide2(o1,o2,data,&nearCallback);
  66. // Note we do not want to test intersections within a space,
  67. // only between spaces.
  68. return;
  69. }
  70. const int N = 32;
  71. dContact contact[N];
  72. int n = dCollide (o1,o2,N,&(contact[0].geom),sizeof(dContact));
  73. if (n > 0)
  74. {
  75. for (int i=0; i<n; i++)
  76. {
  77. contact[i].surface.mode = dContactSoftERP | dContactSoftCFM | dContactApprox1;
  78. contact[i].surface.mu = 100.0;
  79. contact[i].surface.soft_erp = 0.96;
  80. contact[i].surface.soft_cfm = 0.02;
  81. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  82. dJointAttach (c,
  83. dGeomGetBody(contact[i].geom.g1),
  84. dGeomGetBody(contact[i].geom.g2));
  85. }
  86. }
  87. }
  88. // start simulation - set viewpoint
  89. static void start()
  90. {
  91. dAllocateODEDataForThread(dAllocateMaskAll);
  92. static float xyz[3] = { -6, 8, 6};
  93. static float hpr[3] = { -65.0f, -27.0f, 0.0f};
  94. dsSetViewpoint (xyz,hpr);
  95. }
  96. // called when a key pressed
  97. static void command (int cmd)
  98. {
  99. }
  100. void drawGeom (dGeomID g)
  101. {
  102. const dReal *pos = dGeomGetPosition(g);
  103. const dReal *R = dGeomGetRotation(g);
  104. int type = dGeomGetClass (g);
  105. if (type == dBoxClass)
  106. {
  107. dVector3 sides;
  108. dGeomBoxGetLengths (g, sides);
  109. dsDrawBox (pos,R,sides);
  110. }
  111. if (type == dCylinderClass)
  112. {
  113. dReal r,l;
  114. dGeomCylinderGetParams(g, &r, &l);
  115. dsDrawCylinder (pos, R, l, r);
  116. }
  117. }
  118. static void inspectJoints(void)
  119. {
  120. const dReal forcelimit = 2000.0;
  121. int i;
  122. for (i=0; i<SEGMCNT-1; i++)
  123. {
  124. if (dJointGetBody(hinges[i], 0))
  125. {
  126. // This joint has not snapped already... inspect it.
  127. dReal l0 = dCalcVectorLength3(jfeedbacks[i].f1);
  128. dReal l1 = dCalcVectorLength3(jfeedbacks[i].f2);
  129. colours[i+0] = 0.95*colours[i+0] + 0.05 * l0/forcelimit;
  130. colours[i+1] = 0.95*colours[i+1] + 0.05 * l1/forcelimit;
  131. if (l0 > forcelimit || l1 > forcelimit)
  132. stress[i]++;
  133. else
  134. stress[i]=0;
  135. if (stress[i]>4)
  136. {
  137. // Low-pass filter the noisy feedback data.
  138. // Only after 4 consecutive timesteps with excessive load, snap.
  139. fprintf(stderr,"SNAP! (that was the sound of joint %d breaking)\n", i);
  140. dJointAttach (hinges[i], 0, 0);
  141. }
  142. }
  143. }
  144. }
  145. // simulation loop
  146. static void simLoop (int pause)
  147. {
  148. int i;
  149. double simstep = 0.002; // 2ms simulation steps
  150. double dt = dsElapsedTime();
  151. int nrofsteps = (int) ceilf(dt/simstep);
  152. for (i=0; i<nrofsteps && !pause; i++)
  153. {
  154. dSpaceCollide (space,0,&nearCallback);
  155. dWorldQuickStep (world, simstep);
  156. dJointGroupEmpty (contactgroup);
  157. inspectJoints();
  158. }
  159. for (i=0; i<SEGMCNT; i++)
  160. {
  161. float r=0,g=0,b=0.2;
  162. float v = colours[i];
  163. if (v>1.0) v=1.0;
  164. if (v<0.5)
  165. {
  166. r=2*v;
  167. g=1.0;
  168. }
  169. else
  170. {
  171. r=1.0;
  172. g=2*(1.0-v);
  173. }
  174. dsSetColor (r,g,b);
  175. drawGeom(seggeoms[i]);
  176. }
  177. dsSetColor (1,1,1);
  178. for (i=0; i<STACKCNT; i++)
  179. drawGeom(stackgeoms[i]);
  180. }
  181. int main (int argc, char **argv)
  182. {
  183. dMass m;
  184. // setup pointers to drawstuff callback functions
  185. dsFunctions fn;
  186. fn.version = DS_VERSION;
  187. fn.start = &start;
  188. fn.step = &simLoop;
  189. fn.command = &command;
  190. fn.stop = 0;
  191. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  192. // create world
  193. dInitODE2(0);
  194. world = dWorldCreate();
  195. space = dHashSpaceCreate (0);
  196. contactgroup = dJointGroupCreate (0);
  197. dWorldSetGravity (world,0,0,-9.8);
  198. dWorldSetQuickStepNumIterations (world, 20);
  199. int i;
  200. for (i=0; i<SEGMCNT; i++)
  201. {
  202. segbodies[i] = dBodyCreate (world);
  203. dBodySetPosition(segbodies[i], i - SEGMCNT/2.0, 0, 5);
  204. dMassSetBox (&m, 1, SEGMDIM[0], SEGMDIM[1], SEGMDIM[2]);
  205. dBodySetMass (segbodies[i], &m);
  206. seggeoms[i] = dCreateBox (0, SEGMDIM[0], SEGMDIM[1], SEGMDIM[2]);
  207. dGeomSetBody (seggeoms[i], segbodies[i]);
  208. dSpaceAdd (space, seggeoms[i]);
  209. }
  210. for (i=0; i<SEGMCNT-1; i++)
  211. {
  212. hinges[i] = dJointCreateHinge (world,0);
  213. dJointAttach (hinges[i], segbodies[i],segbodies[i+1]);
  214. dJointSetHingeAnchor (hinges[i], i + 0.5 - SEGMCNT/2.0, 0, 5);
  215. dJointSetHingeAxis (hinges[i], 0,1,0);
  216. dJointSetHingeParam (hinges[i],dParamFMax, 8000.0);
  217. // NOTE:
  218. // Here we tell ODE where to put the feedback on the forces for this hinge
  219. dJointSetFeedback (hinges[i], jfeedbacks+i);
  220. stress[i]=0;
  221. }
  222. for (i=0; i<STACKCNT; i++)
  223. {
  224. stackbodies[i] = dBodyCreate(world);
  225. dMassSetBox (&m, 2.0, 2, 2, 0.6);
  226. dBodySetMass(stackbodies[i],&m);
  227. stackgeoms[i] = dCreateBox(0, 2, 2, 0.6);
  228. dGeomSetBody(stackgeoms[i], stackbodies[i]);
  229. dBodySetPosition(stackbodies[i], 0,0,8+2*i);
  230. dSpaceAdd(space, stackgeoms[i]);
  231. }
  232. sliders[0] = dJointCreateSlider (world,0);
  233. dJointAttach(sliders[0], segbodies[0], 0);
  234. dJointSetSliderAxis (sliders[0], 1,0,0);
  235. dJointSetSliderParam (sliders[0],dParamFMax, 4000.0);
  236. dJointSetSliderParam (sliders[0],dParamLoStop, 0.0);
  237. dJointSetSliderParam (sliders[0],dParamHiStop, 0.2);
  238. sliders[1] = dJointCreateSlider (world,0);
  239. dJointAttach(sliders[1], segbodies[SEGMCNT-1], 0);
  240. dJointSetSliderAxis (sliders[1], 1,0,0);
  241. dJointSetSliderParam (sliders[1],dParamFMax, 4000.0);
  242. dJointSetSliderParam (sliders[1],dParamLoStop, 0.0);
  243. dJointSetSliderParam (sliders[1],dParamHiStop, -0.2);
  244. groundgeom = dCreatePlane(space, 0,0,1,0);
  245. for (i=0; i<SEGMCNT; i++)
  246. colours[i]=0.0;
  247. // run simulation
  248. dsSimulationLoop (argc,argv,352,288,&fn);
  249. dJointGroupEmpty (contactgroup);
  250. dJointGroupDestroy (contactgroup);
  251. // First destroy seggeoms, then space, then the world.
  252. for (i=0; i<SEGMCNT; i++)
  253. dGeomDestroy (seggeoms[i]);
  254. for (i=0; i<STACKCNT; i++)
  255. dGeomDestroy (stackgeoms[i]);
  256. dSpaceDestroy(space);
  257. dWorldDestroy (world);
  258. dCloseODE();
  259. return 0;
  260. }