7. Joint

This is the 7th ODE (Open Dynamics Engine) tutorial.

In this tutorial, I explain a joint.

There are a lot of joints around us. For example, a hinge of a cellular phone,  a hinge of door, and so on. A joint connect two bodies.  In another word, a joint is a constraint which restricts a movement of two bodies.  ODE uses a joint in the same meaning of a constraint.


  • How to use a joint
  1. Create a joint
    • dJointCreate***()
  2. connect bodies with the joint
    • dJointAttach ()
  3. Set an anchor of the joint
    • dJointSet*** Anchor()
  4. Set an axis of the joint
    • dJointSet***

*** means a type of joint, such as a Hinge, Ball, Slider, Universal, and so non. Please read the User Manual of ODE.

  • How to set parameters of the joint
  1. Set range of motion
    dJointSetHingeParam (dJointID, dParamLoStop, the minimum range of motion);
    dJointSetHingeParam (dJointID, dParamHiStop, the maximum range of motion);
  2. Set angular velocity and maximum torque
    dJointSetHingeParam (dJointID, dParamVel, target angular velocity);
    dJointSetHingeParam (dJointID, dParamFMax, maximum torque);

You can set parameters of a joint. dParamLoStop is the minimum and dParamHiStop is the maximum range of motion. dParamVel is target speed and dParamFMax is maximum torque to apply the joint.

  • Sample program 7

Now,  a sample program of a hopping robot (above the figure)  is shown as follows.

C++:
  1. // sample7.cpp  by Kose Demura 2006-2008
  2. #ifdef  dDOUBLE
  3. #define dsDrawSphere dsDrawSphereD
  4. #define dsDrawCapsule  dsDrawCapsuleD
  5. #endif
  6.  
  7. typedef struct {
  8.   dBodyID body;   // a body
  9.   dGeomID geom; //  a geometry
  10.   dReal   radius, length, mass; // radius[m], leng[m], mass[kg]
  11. } myLink;
  12.  
  13. myLink ball, pole;
  14.  
  15. static void simLoop (int pause)
  16. {
  17.   const dReal *pos1,*R1,*pos2,*R2; 
  18.  
  19.   dSpaceCollide(space,0,&nearCallback);  // Collision detection
  20.   dWorldStep(world,0.01); // Step a world for 0.01 [sec]
  21.   dJointGroupEmpty(contactgroup)// Empty the joint group
  22.  
  23.   // Draw a ball
  24.   dsSetColor(1.0,0.0,0.0);        // Set red color
  25.   pos1 = dBodyGetPosition(ball.body); // Get a position
  26.   R1   = dBodyGetRotation(ball.body); // Get an orientation
  27.   dsDrawSphere(pos1,R1,ball.radius);  // Draw a sphere
  28.  
  29.   // Draw a capsule
  30.   pos2 = dBodyGetPosition(pole.body); // Get a position
  31.   R2   = dBodyGetRotation(pole.body); // Get an orientation
  32.   dsDrawCapsule(pos2,R2,pole.length,pole.radius); // Draw a capsule
  33. }
  34.  
  35. // Create an object
  36. void createBallPole() {
  37.   dMass m1;
  38.   dReal x0 = 0.0, y0 = 0.0, z0 = 2.5;
  39.  
  40.   // a ball
  41.   ball.radius = 0.2;  ball.mass   = 1.0;
  42.   ball.body = dBodyCreate(world);
  43.   dMassSetZero(&m1);
  44.   dMassSetSphereTotal(&m1,ball.mass,ball.radius);
  45.   dBodySetMass(ball.body,&m1);
  46.   dBodySetPosition(ball.body, x0, y0, z0);
  47.   ball.geom = dCreateSphere(space,ball.radius);
  48.   dGeomSetBody(ball.geom,ball.body);
  49.  
  50.   // a capsule
  51.   pole.radius = 0.025;  pole.length = 1.0;  pole.mass   = 1.0;
  52.   pole.body =  dBodyCreate(world);
  53.   dMassSetZero(&m1);
  54.   dMassSetCapsuleTotal(&m1,pole.mass,3,pole.radius,pole.length);
  55.   dBodySetMass(pole.body,&m1);
  56.   dBodySetPosition(pole.body, x0, y0, z0 - 0.5 * pole.length);
  57.   pole.geom =  dCreateCapsule(space,pole.radius,pole.length);
  58.   dGeomSetBody(pole.geom,pole.body);
  59.  
  60.   // hinge joint
  61.   joint = dJointCreateHinge(world, 0); // Create a hinge joint
  62.   dJointAttach(joint, ball.body,pole.body); // Attach joint to bodies
  63.   dJointSetHingeAnchor(joint, x0, y0, z0 - ball.radius); // Set a joint anchor
  64.   dJointSetHingeAxis(joint, 1, 0, 0); // Set a hinge axis(1,0,0)
  65. }
  66.  
  67. int main (int argc, char **argv)
  68. {
  69.   setDrawStuff();  // Set a draw function
  70.   dInitODE();       // Initialize ODE
  71.   world = dWorldCreate(); // Create a world
  72.   space = dHashSpaceCreate(0); // Create a collision space
  73.   contactgroup = dJointGroupCreate(0); // Create a joint group
  74.   dWorldSetGravity(world,0,0,-9.8)// Set gravity
  75.   ground = dCreatePlane(space,0,0,1,0); // Create a ground
  76.   createBallPole(); // Create ball and capsule
  77.   dsSimulationLoop (argc,argv,352,288,&fn)// Simulation loop
  78.   dWorldDestroy (world); // Destroy the world
  79.  dCloseODE()// Close ODE
  80.   return 0;
  81. }

In this example, a robot is composed of a hinge joint which connects a torso and a leg.

Let's read the source code. createBallandPole function is to create a ball for a torso , and a cylinder for a leg, and create a joint by  dJointCreateHinge(). connect bodies with the joint by  dJointAttach().

Then, dSetHingeAnchor() sets an anchor of the joint and dJointSetHingeAxis() sets an axis of the hinge joint. This example, the rotation axis vector is set to (1,0,0).  So,  the axis vector is the x axis.

I did not set parameters of the joint to simplify  the souce code. Usually, you can set many parameters about joints. Please see the ODE user manual in detail.

You can download the sample program, sample7.cpp, from here. Please download and enjoy it.

See you !

demu

APIs

  • void dJointAttach (dJointID joint, dBodyID body1, dBodyID dBody2)
    Attach the joint between body1 and body2
  • dJointID dJointCreateHinge (dWorldID world, dJointGroupID)
    Create a hinge Joint. dJointGroupID is set to zero.
  • void dJointSetHingeAnchor (dJointID joint, dReal x, dReal y, dReal z)
    Set an anchor point (x, y, z) ot the hinge joint.
  • void dJointSetHingeAxis (dJointID joint, dReal x, dReal y, dReal z)
    Set an axis vector (x, y, z) of the hinge joint.


Leave a Reply

カウンタ (since 2008-3-15)
Copyright © 1998-2008    Kosei Demura