demo_feedback.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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", (void*)o1, (void*)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)
  98. {}
  99. void drawGeom (dGeomID g)
  100. {
  101. const dReal *pos = dGeomGetPosition(g);
  102. const dReal *R = dGeomGetRotation(g);
  103. int type = dGeomGetClass (g);
  104. if (type == dBoxClass)
  105. {
  106. dVector3 sides;
  107. dGeomBoxGetLengths (g, sides);
  108. dsDrawBox (pos,R,sides);
  109. }
  110. if (type == dCylinderClass)
  111. {
  112. dReal r,l;
  113. dGeomCylinderGetParams(g, &r, &l);
  114. dsDrawCylinder (pos, R, l, r);
  115. }
  116. }
  117. static void inspectJoints(void)
  118. {
  119. const dReal forcelimit = 2000.0;
  120. int i;
  121. for (i=0; i<SEGMCNT-1; i++)
  122. {
  123. if (dJointGetBody(hinges[i], 0))
  124. {
  125. // This joint has not snapped already... inspect it.
  126. dReal l0 = dCalcVectorLength3(jfeedbacks[i].f1);
  127. dReal l1 = dCalcVectorLength3(jfeedbacks[i].f2);
  128. colours[i+0] = 0.95*colours[i+0] + 0.05 * l0/forcelimit;
  129. colours[i+1] = 0.95*colours[i+1] + 0.05 * l1/forcelimit;
  130. if (l0 > forcelimit || l1 > forcelimit)
  131. stress[i]++;
  132. else
  133. stress[i]=0;
  134. if (stress[i]>4)
  135. {
  136. // Low-pass filter the noisy feedback data.
  137. // Only after 4 consecutive timesteps with excessive load, snap.
  138. fprintf(stderr,"SNAP! (that was the sound of joint %d breaking)\n", i);
  139. dJointAttach (hinges[i], 0, 0);
  140. }
  141. }
  142. }
  143. }
  144. // simulation loop
  145. static void simLoop (int pause)
  146. {
  147. int i;
  148. double simstep = 0.002; // 2ms simulation steps
  149. double dt = dsElapsedTime();
  150. int nrofsteps = (int) ceilf(dt/simstep);
  151. for (i=0; i<nrofsteps && !pause; i++)
  152. {
  153. dSpaceCollide (space,0,&nearCallback);
  154. dWorldQuickStep (world, simstep);
  155. dJointGroupEmpty (contactgroup);
  156. inspectJoints();
  157. }
  158. for (i=0; i<SEGMCNT; i++)
  159. {
  160. float r=0,g=0,b=0.2;
  161. float v = colours[i];
  162. if (v>1.0) v=1.0;
  163. if (v<0.5)
  164. {
  165. r=2*v;
  166. g=1.0;
  167. }
  168. else
  169. {
  170. r=1.0;
  171. g=2*(1.0-v);
  172. }
  173. dsSetColor (r,g,b);
  174. drawGeom(seggeoms[i]);
  175. }
  176. dsSetColor (1,1,1);
  177. for (i=0; i<STACKCNT; i++)
  178. drawGeom(stackgeoms[i]);
  179. }
  180. int main (int argc, char **argv)
  181. {
  182. dMass m;
  183. // setup pointers to drawstuff callback functions
  184. dsFunctions fn;
  185. fn.version = DS_VERSION;
  186. fn.start = &start;
  187. fn.step = &simLoop;
  188. fn.command = &command;
  189. fn.stop = 0;
  190. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  191. // create world
  192. dInitODE2(0);
  193. world = dWorldCreate();
  194. space = dHashSpaceCreate (0);
  195. contactgroup = dJointGroupCreate (0);
  196. dWorldSetGravity (world,0,0,-9.8);
  197. dWorldSetQuickStepNumIterations (world, 20);
  198. int i;
  199. for (i=0; i<SEGMCNT; i++)
  200. {
  201. segbodies[i] = dBodyCreate (world);
  202. dBodySetPosition(segbodies[i], i - SEGMCNT/2.0, 0, 5);
  203. dMassSetBox (&m, 1, SEGMDIM[0], SEGMDIM[1], SEGMDIM[2]);
  204. dBodySetMass (segbodies[i], &m);
  205. seggeoms[i] = dCreateBox (0, SEGMDIM[0], SEGMDIM[1], SEGMDIM[2]);
  206. dGeomSetBody (seggeoms[i], segbodies[i]);
  207. dSpaceAdd (space, seggeoms[i]);
  208. }
  209. for (i=0; i<SEGMCNT-1; i++)
  210. {
  211. hinges[i] = dJointCreateHinge (world,0);
  212. dJointAttach (hinges[i], segbodies[i],segbodies[i+1]);
  213. dJointSetHingeAnchor (hinges[i], i + 0.5 - SEGMCNT/2.0, 0, 5);
  214. dJointSetHingeAxis (hinges[i], 0,1,0);
  215. dJointSetHingeParam (hinges[i],dParamFMax, 8000.0);
  216. // NOTE:
  217. // Here we tell ODE where to put the feedback on the forces for this hinge
  218. dJointSetFeedback (hinges[i], jfeedbacks+i);
  219. stress[i]=0;
  220. }
  221. for (i=0; i<STACKCNT; i++)
  222. {
  223. stackbodies[i] = dBodyCreate(world);
  224. dMassSetBox (&m, 2.0, 2, 2, 0.6);
  225. dBodySetMass(stackbodies[i],&m);
  226. stackgeoms[i] = dCreateBox(0, 2, 2, 0.6);
  227. dGeomSetBody(stackgeoms[i], stackbodies[i]);
  228. dBodySetPosition(stackbodies[i], 0,0,8+2*i);
  229. dSpaceAdd(space, stackgeoms[i]);
  230. }
  231. sliders[0] = dJointCreateSlider (world,0);
  232. dJointAttach(sliders[0], segbodies[0], 0);
  233. dJointSetSliderAxis (sliders[0], 1,0,0);
  234. dJointSetSliderParam (sliders[0],dParamFMax, 4000.0);
  235. dJointSetSliderParam (sliders[0],dParamLoStop, 0.0);
  236. dJointSetSliderParam (sliders[0],dParamHiStop, 0.2);
  237. sliders[1] = dJointCreateSlider (world,0);
  238. dJointAttach(sliders[1], segbodies[SEGMCNT-1], 0);
  239. dJointSetSliderAxis (sliders[1], 1,0,0);
  240. dJointSetSliderParam (sliders[1],dParamFMax, 4000.0);
  241. dJointSetSliderParam (sliders[1],dParamLoStop, 0.0);
  242. dJointSetSliderParam (sliders[1],dParamHiStop, -0.2);
  243. groundgeom = dCreatePlane(space, 0,0,1,0);
  244. for (i=0; i<SEGMCNT; i++)
  245. colours[i]=0.0;
  246. // run simulation
  247. dsSimulationLoop (argc,argv,352,288,&fn);
  248. dJointGroupEmpty (contactgroup);
  249. dJointGroupDestroy (contactgroup);
  250. // First destroy seggeoms, then space, then the world.
  251. for (i=0; i<SEGMCNT; i++)
  252. dGeomDestroy (seggeoms[i]);
  253. for (i=0; i<STACKCNT; i++)
  254. dGeomDestroy (stackgeoms[i]);
  255. dSpaceDestroy(space);
  256. dWorldDestroy (world);
  257. dCloseODE();
  258. return 0;
  259. }