[ODE] Instantaneous reversal of velocity: possible with motor?
nlin@nlin.net
nlin at nlin.net
Wed Mar 20 20:56:01 2002
I'm trying to use a motor to simulate an extremely quickly and powerfully
rotating mechanical part which should be able to reverse its angular
velocity within one timestep.
I have set global and joint ERP to (almost) 1.0 and CFM to (almost) 0.0,
but the body still takes a few moments to switch from rotating in one
direction to rotating in the other direction. I have allowed maximum
force of up to 1.e20 and still the body takes several frames to reverse
its velocity when I change the motor angular velocity.
I thought motors were supposed to be able to set the velocity within exactly
one timestep. Am I missing something?
Here's my source code, a hacked-up version of test_boxstack. When you run
it one spinning box will appear; press any key (e.g. l) to cause the box
to reverse direction.
Thanks,
-Norman
#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
#ifdef MSVC
#pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
#endif
// select correct drawing functions
#ifdef dDOUBLE
#define dsDrawBox dsDrawBoxD
#define dsDrawSphere dsDrawSphereD
#define dsDrawCylinder dsDrawCylinderD
#define dsDrawCappedCylinder dsDrawCappedCylinderD
#endif
// some constants
#define NUM 200 // max number of objects
#define DENSITY (5.0) // density of all objects
#define GPB 3 // maximum number of geometries per body
// dJointID hinge;
dJointID motor;
// dynamics and collision objects
struct MyObject {
dBodyID body; // the body
dGeomID geom[GPB]; // geometries representing this body
};
static int num=0; // number of objects in simulation
static int nextobj=0; // next object to recycle if num==NUM
static dWorldID world;
static dSpaceID space;
static MyObject obj[NUM];
static dJointGroupID contactgroup;
static int selected = -1; // selected object
// this is called by dSpaceCollide when two objects in space are
// potentially colliding.
static void nearCallback (void *data, dGeomID o1, dGeomID o2)
{
int i;
// if (o1->body && o2->body) return;
// exit without doing anything if the two bodies are connected by a joint
dBodyID b1 = dGeomGetBody(o1);
dBodyID b2 = dGeomGetBody(o2);
if (b1 && b2 && dAreConnected (b1,b2)) return;
dContact contact[3]; // up to 3 contacts per box
for (i=0; i<3; i++) {
contact[i].surface.mode = dContactBounce; //dContactMu2;
contact[i].surface.mode = 0;
contact[i].surface.mu = dInfinity;
contact[i].surface.mu2 = 0;
contact[i].surface.bounce = 5.5;
contact[i].surface.bounce_vel = 9.1;
contact[i].surface.bounce = 0.9;
contact[i].surface.bounce_vel = 0.9;
}
if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
// dMatrix3 RI;
// dRSetIdentity (RI);
// const dReal ss[3] = {0.02,0.02,0.02};
for (i=0; i<numc; i++) {
dJointID c = dJointCreateContact (world,contactgroup,contact+i);
dJointAttach (c,b1,b2);
// dsDrawBox (contact[i].geom.pos,RI,ss);
}
}
}
// start simulation - set viewpoint
static void start()
{
static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
dsSetViewpoint (xyz,hpr);
printf ("To drop another object, press:\n");
printf (" b for box.\n");
printf (" s for sphere.\n");
printf (" c for cylinder.\n");
printf (" x for a composite object.\n");
printf ("To select an object, press space.\n");
printf ("To disable the selected object, press d.\n");
printf ("To enable the selected object, press e.\n");
}
char locase (char c)
{
if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
else return c;
}
// called when a key pressed
static void command (int cmd)
{
int i,j,k;
dReal sides[3];
dMass m;
static int vel=-2;
vel = -vel;
dJointSetAMotorParam(motor, dParamVel3, vel);
cmd = locase (cmd);
if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x') {
if (num < NUM) {
i = num;
num++;
}
else {
i = nextobj;
nextobj++;
if (nextobj >= num) nextobj = 0;
// destroy the body and geoms for slot i
dBodyDestroy (obj[i].body);
for (k=0; k < GPB; k++) {
if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
}
memset (&obj[i],0,sizeof(obj[i]));
}
obj[i].body = dBodyCreate (world);
for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
for (k=0; k<3; k++) sides[k] = 1;
dBodySetPosition (obj[i].body,
dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
dBodySetPosition (obj[i].body,
0,0,5);
dMatrix3 R;
dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
dRFromAxisAndAngle (R,1,0,0,90);
dBodySetRotation (obj[i].body,R);
dBodySetData (obj[i].body,(void*) i);
if (cmd == 'b') {
dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
}
else if (cmd == 'c') {
sides[0] *= 0.5;
dMassSetCappedCylinder (&m,DENSITY,3,sides[0],sides[1]);
obj[i].geom[0] = dCreateCCylinder (space,sides[0],sides[1]);
}
else if (cmd == 's') {
sides[0] *= 0.5;
dMassSetSphere (&m,DENSITY,sides[0]);
obj[i].geom[0] = dCreateSphere (space,sides[0]);
}
else if (cmd == 'x') {
dGeomID g2[GPB]; // encapsulated geometries
dReal dpos[GPB][3]; // delta-positions for encapsulated geometries
// start accumulating masses for the encapsulated geometries
dMass m2;
dMassSetZero (&m);
// set random delta positions
for (j=0; j<GPB; j++) {
for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
}
for (k=0; k<3; k++) {
obj[i].geom[k] = dCreateGeomTransform (space);
dGeomTransformSetCleanup (obj[i].geom[k],1);
if (k==0) {
dReal radius = dRandReal()*0.25+0.05;
g2[k] = dCreateSphere (0,radius);
dMassSetSphere (&m2,DENSITY,radius);
}
else if (k==1) {
g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
}
else {
dReal radius = dRandReal()*0.1+0.05;
dReal length = dRandReal()*1.0+0.1;
g2[k] = dCreateCCylinder (0,radius,length);
dMassSetCappedCylinder (&m2,DENSITY,3,radius,length);
}
dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
// set the transformation (adjust the mass too)
dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
dMatrix3 Rtx;
dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
dGeomSetRotation (g2[k],Rtx);
dMassRotate (&m2,Rtx);
// add to the total mass
dMassAdd (&m,&m2);
}
// move all encapsulated objects so that the center of mass is (0,0,0)
for (k=0; k<2; k++) {
dGeomSetPosition (g2[k],
dpos[k][0]-m.c[0],
dpos[k][1]-m.c[1],
dpos[k][2]-m.c[2]);
}
dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
}
for (k=0; k < GPB; k++) {
if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
}
dBodySetMass (obj[i].body,&m);
}
if (cmd == ' ') {
selected++;
if (selected >= num) selected = 0;
if (selected < 0) selected = 0;
}
else if (cmd == 'd' && selected >= 0 && selected < num) {
dBodyDisable (obj[selected].body);
}
else if (cmd == 'e' && selected >= 0 && selected < num) {
dBodyEnable (obj[selected].body);
}
}
// draw a geom
void drawGeom (dGeomID g, const dReal *pos, const dReal *R)
{
if (!g) return;
if (!pos) pos = dGeomGetPosition (g);
if (!R) R = dGeomGetRotation (g);
int type = dGeomGetClass (g);
if (type == dBoxClass) {
dVector3 sides;
dGeomBoxGetLengths (g,sides);
dsDrawBox (pos,R,sides);
}
else if (type == dSphereClass) {
dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
}
else if (type == dCCylinderClass) {
dReal radius,length;
dGeomCCylinderGetParams (g,&radius,&length);
dsDrawCappedCylinder (pos,R,length,radius);
}
else if (type == dGeomTransformClass) {
dGeomID g2 = dGeomTransformGetGeom (g);
const dReal *pos2 = dGeomGetPosition (g2);
const dReal *R2 = dGeomGetRotation (g2);
dVector3 actual_pos;
dMatrix3 actual_R;
dMULTIPLY0_331 (actual_pos,R,pos2);
actual_pos[0] += pos[0];
actual_pos[1] += pos[1];
actual_pos[2] += pos[2];
dMULTIPLY0_333 (actual_R,R,R2);
drawGeom (g2,actual_pos,actual_R);
}
}
// simulation loop
static void simLoop (int pause)
{
dsSetColor (0,0,2);
dSpaceCollide (space,0,&nearCallback);
if (!pause) dWorldStep (world,0.05);
// remove all contact joints
dJointGroupEmpty (contactgroup);
dsSetColor (1,1,0);
dsSetTexture (DS_WOOD);
for (int i=0; i<num; i++) {
int color_changed = 0;
if (i==selected) {
dsSetColor (0,0.7,1);
color_changed = 1;
}
else if (! dBodyIsEnabled (obj[i].body)) {
dsSetColor (1,0,0);
color_changed = 1;
}
for (int j=0; j < GPB; j++) drawGeom (obj[i].geom[j],0,0);
if (color_changed) dsSetColor (1,1,0);
}
}
int main (int argc, char **argv)
{
// setup pointers to drawstuff callback functions
dsFunctions fn;
fn.version = DS_VERSION;
fn.start = &start;
fn.step = &simLoop;
fn.command = &command;
fn.stop = 0;
fn.path_to_textures = "../../drawstuff/textures";
// create world
world = dWorldCreate();
space = dHashSpaceCreate();
contactgroup = dJointGroupCreate (0);
dWorldSetGravity (world,0.0,0,0.0);
dWorldSetCFM (world,1e-5);
dWorldSetERP (world,1.0);
// dWorldSetCFM (world,0);
dCreatePlane (space,0,0,1,0);
memset (obj,0,sizeof(obj));
dReal sides[3];
dMass m;
int i,k;
if (num < NUM) {
i = num;
num++;
}
else {
i = nextobj;
nextobj++;
if (nextobj >= num) nextobj = 0;
// destroy the body and geoms for slot i
dBodyDestroy (obj[i].body);
for (k=0; k < GPB; k++) {
if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
}
memset (&obj[i],0,sizeof(obj[i]));
}
obj[i].body = dBodyCreate (world);
for (k=0; k<3; k++) sides[k] = 1;
dBodySetPosition (obj[i].body,
0,0,2);
dMatrix3 R;
dRFromAxisAndAngle (R,1,0,0,0);
dBodySetRotation (obj[i].body,R);
dBodySetData (obj[i].body,(void*) i);
dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
for (k=0; k < GPB; k++) {
if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
}
dBodySetMass (obj[i].body,&m);
dBodyID b2;
b2 = dBodyCreate(world);
dRFromAxisAndAngle (R,1,0,0,0);
dBodySetPosition (b2,
0,0,4);
dBodySetRotation (b2,R);
dBodySetData (b2,(void*) i);
dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
dBodySetMass (b2,&m);
dJointID j2;
j2 = dJointCreateFixed(world,0);
dJointAttach(j2,b2,0);
motor = dJointCreateAMotor (world,0);
dJointAttach (motor,obj[i].body,b2);
dJointSetAMotorNumAxes (motor,3);
dJointSetAMotorAxis (motor,0,1, 0,0,1);
dJointSetAMotorAxis (motor,2,2, 1,0,0);
dJointSetAMotorMode (motor,dAMotorEuler);
const float stop = 0.0;
// const float stop_cfm = 0.01;
// const float stop_erp = 0.8;
const float stop_cfm = 0.0;
const float stop_erp = 1.0;
dJointSetAMotorMode(motor,dAMotorEuler);
dJointSetAMotorAxis(motor,0,1,0,0,1);
dJointSetAMotorAxis(motor,2,2,0,1,0);
// dJointSetAMotorParam(motor,dParamLoStop,-stop); // twist
dJointSetAMotorParam(motor,dParamHiStop,stop);
dJointSetAMotorParam(motor,dParamVel,0);
dJointSetAMotorParam(motor,dParamFMax,0);
// dJointSetAMotorParam(motor,dParamLoStop2,-stop); // back
// dJointSetAMotorParam(motor,dParamHiStop2,stop); // front
dJointSetAMotorParam(motor,dParamVel2,0);
dJointSetAMotorParam(motor,dParamFMax2,0);
// dJointSetAMotorParam(motor,dParamLoStop3,-stop); // back
// dJointSetAMotorParam(motor,dParamHiStop3,stop); // front
dJointSetAMotorParam(motor,dParamVel3,2);
dJointSetAMotorParam(motor,dParamFMax3,1.e10);
dJointSetAMotorParam(motor,dParamStopERP,stop_erp);
dJointSetAMotorParam(motor,dParamStopCFM,stop_cfm);
dJointSetAMotorParam(motor,dParamStopERP2,stop_erp);
dJointSetAMotorParam(motor,dParamStopCFM2,stop_cfm);
dJointSetAMotorParam(motor,dParamStopERP3,stop_erp);
dJointSetAMotorParam(motor,dParamStopCFM3,stop_cfm);
dJointSetAMotorParam(motor,dParamBounce,0);
dJointSetAMotorParam(motor,dParamBounce2,0);
dJointSetAMotorParam(motor,dParamBounce3,0);
// run simulation
dsSimulationLoop (argc,argv,352,288,&fn);
dJointGroupDestroy (contactgroup);
dSpaceDestroy (space);
dWorldDestroy (world);
return 0;
}