Robotsystem of the Robot Group Leipzig: nimm4.cpp

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Examples

- Search for |

# nimm4.cpp

Simple example for the creation of a simulated robot (lpzrobots/ode_robots/robots/nimm4.cpp).

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
    00003  *    martius@informatik.uni-leipzig.de                                    *
    00004  *    fhesse@informatik.uni-leipzig.de                                     *
    00005  *    der@informatik.uni-leipzig.de                                        *
    00006  *                                                                         *
    00007  *   This program is free software; you can redistribute it and/or modify  *
    00008  *   it under the terms of the GNU General Public License as published by  *
    00009  *   the Free Software Foundation; either version 2 of the License, or     *
    00010  *   (at your option) any later version.                                   *
    00011  *                                                                         *
    00012  *   This program is distributed in the hope that it will be useful,       *
    00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    00015  *   GNU General Public License for more details.                          *
    00016  *                                                                         *
    00017  *   You should have received a copy of the GNU General Public License     *
    00018  *   along with this program; if not, write to the                         *
    00019  *   Free Software Foundation, Inc.,                                       *
    00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    00021  *                                                                         *
    00022  *   $Log: nimm4.cpp,v $
    00023  *   Revision 1.8  2006/07/14 12:23:40  martius
    00024  *   selforg becomes HEAD
    00025  *
    00026  *   Revision 1.7.4.17  2006/06/29 16:39:55  robot3
    00027  *   -you can now see bounding shapes if you type ./start -drawboundings
    00028  *   -includes cleared up
    00029  *   -abstractobstacle and abstractground have now .cpp-files
    00030  *
    00031  *   Revision 1.7.4.16  2006/06/25 17:00:32  martius
    00032  *   Id
    00033  *
    00034  *   Revision 1.7.4.15  2006/06/25 16:57:14  martius
    00035  *   abstractrobot is configureable
    00036  *   name and revision
    00037  *
    00038  *   Revision 1.7.4.14  2006/04/04 17:03:21  fhesse
    00039  *   docu added
    00040  *
    00041  *   Revision 1.7.4.13  2006/04/04 14:13:24  fhesse
    00042  *   documentation improved
    00043  *
    00044  *   Revision 1.7.4.12  2006/03/31 11:11:38  fhesse
    00045  *   minor changes in docu
    00046  *
    00047  *   Revision 1.7.4.11  2006/02/01 18:33:40  martius
    00048  *   use Axis type for Joint axis. very important, since otherwise Vec3 * pose is not the right direction vector anymore
    00049  *
    00050  *   Revision 1.7.4.10  2005/12/29 16:47:40  martius
    00051  *   joint has getPosition
    00052  *
    00053  *   Revision 1.7.4.9  2005/12/15 17:04:08  martius
    00054  *   Primitives are not longer inherited from OSGPrimitive, moreover
    00055  *   they aggregate them.
    00056  *   Joint have better getter and setter
    00057  *
    00058  *   Revision 1.7.4.8  2005/12/14 15:37:09  martius
    00059  *   robots are working with osg
    00060  *
    00061  *   Revision 1.7.4.7  2005/12/13 18:11:39  martius
    00062  *   still trying to port robots
    00063  *
    00064  *   Revision 1.7.4.6  2005/12/13 12:32:09  martius
    00065  *   nonvisual joints
    00066  *
    00067  *   Revision 1.7.4.5  2005/12/12 23:41:30  martius
    00068  *   added Joint wrapper
    00069  *
    00070  *   Revision 1.7.4.4  2005/12/11 23:35:08  martius
    00071  *   *** empty log message ***
    00072  *
    00073  *   Revision 1.7.4.3  2005/12/06 10:13:25  martius
    00074  *   openscenegraph integration started
    00075  *
    00076  *   Revision 1.7.4.2  2005/11/15 12:29:26  martius
    00077  *   new selforg structure and OdeAgent, OdeRobot ...
    00078  *
    00079  *   Revision 1.7.4.1  2005/11/14 17:37:17  martius
    00080  *   moved to selforg
    00081  *
    00082  *   Revision 1.7  2005/11/09 13:24:42  martius
    00083  *   added GPL
    00084  *
    00085  ***************************************************************************/
    00086 #include <assert.h>
    00087 #include <ode/ode.h>
    00088
    00089 // include primitives (box, spheres, cylinders ...)
    00090 #include "primitive.h"
    00091 #include "osgprimitive.h"
    00092
    00093 // include joints
    00094 #include "joint.h"
    00095
    00096 // include header file
    00097 #include "nimm4.h"
    00098
    00099 using namespace osg;
    00100
    00101
    00102 namespace lpzrobots {
    00103
    00104   // constructor:
    00105   // - give handle for ODE and OSG stuff
    00106   // - size of robot, maximal used force and speed factor are adjustable
    00107   // - sphereWheels switches between spheres or wheels as wheels
    00108   //   (wheels are only drawn, collision handling is always with spheres)
    00109   Nimm4::Nimm4(const OdeHandle& odeHandle, const OsgHandle& osgHandle,
    00110                const std::string& name,
    00111                double size/*=1.0*/, double force /*=3*/, double speed/*=15*/,
    00112                bool sphereWheels /*=true*/)
    00113     : // calling OdeRobots construtor with name of the actual robot
    00114       OdeRobot(odeHandle, osgHandle, name, "$Id: nimm4.cpp,v 1.8 2006/07/14 12:23:40 martius Exp $")
    00115   {
    00116
    00117     // robot is not created till now
    00118     created=false;
    00119
    00120     // choose color (here the color of the "Nimm Zwei" candy is used,
    00121     // where the name of the Nimm2 and Nimm4 robots comes from ;-)
    00122     this->osgHandle.color = Color(2, 156/255.0, 0, 1.0f);
    00123
    00124     // maximal used force is calculated from the force factor and size given to the constructor
    00125     max_force   = force*size*size;
    00126
    00127     // speed and type of wheels are set
    00128     this->speed = speed;
    00129     this->sphereWheels = sphereWheels;
    00130
    00131
    00132     height=size;
    00133
    00134     length=size/2.5; // length of body
    00135     width=size/2;  // radius of body
    00136     radius=size/6; // wheel radius
    00137     wheelthickness=size/20; // thickness of the wheels (if wheels used, no spheres)
    00138     cmass=8*size;  // mass of the body
    00139     wmass=size;    // mass of the wheels
    00140     sensorno=4;    // number of sensors
    00141     motorno=4;     // number of motors
    00142     segmentsno=5;  // number of segments of the robot
    00143   };
    00144
    00145
    00146   /** sets actual motorcommands
    00147       @param motors motors scaled to [-1,1]
    00148       @param motornumber length of the motor array
    00149   */
    00150   void Nimm4::setMotors(const motor* motors, int motornumber){
    00151     assert(created); // robot must exist
    00152     // the number of controlled motors is minimum of
    00153     // "number of motorcommands" (motornumber) and
    00154     // "number of motors inside the robot" (motorno)
    00155     int len = (motornumber < motorno)? motornumber : motorno;
    00156
    00157     // for each motor the motorcommand (between -1 and 1) multiplied with speed
    00158     // is set and the maximal force to realize this command are set
    00159     for (int i=0; i<len; i++){
    00160       joint[i]->setParam(dParamVel2, motors[i]*speed);
    00161       joint[i]->setParam(dParamFMax2, max_force);
    00162     }
    00163
    00164     // another possibility is to set half of the difference between last set speed
    00165     // and the actual desired speed as new speed; max_force is also set
    00166     /*
    00167       double tmp;
    00168       int len = (motornumber < motorno)? motornumber : motorno;
    00169       for (int i=0; i<len; i++){
    00170       tmp=dJointGetHinge2Param(joint[i],dParamVel2);
    00171       dJointSetHinge2Param(joint[i],dParamVel2,tmp + 0.5*(motors[i]*speed-tmp) );
    00172       dJointSetHinge2Param (joint[i],dParamFMax2,max_force);
    00173       }
    00174     */
    00175   };
    00176
    00177   /** returns actual sensorvalues
    00178       @param sensors sensors scaled to [-1,1] (more or less)
    00179       @param sensornumber length of the sensor array
    00180       @return number of actually written sensors
    00181   */
    00182   int Nimm4::getSensors(sensor* sensors, int sensornumber){
    00183     assert(created); // robot must exist
    00184
    00185     // the number of sensors to read is the minimum of
    00186     // "number of sensors requested" (sensornumber) and
    00187     // "number of sensors inside the robot" (sensorno)
    00188     int len = (sensornumber < sensorno)? sensornumber : sensorno;
    00189
    00190     // for each sensor the anglerate of the joint is red and scaled with 1/speed
    00191     for (int i=0; i<len; i++){
    00192       sensors[i]=joint[i]->getPosition2Rate();
    00193       sensors[i]/=speed;  //scaling
    00194     }
    00195     // the number of red sensors is returned
    00196     return len;
    00197   };
    00198
    00199
    00200   void Nimm4::place(const Matrix& pose){
    00201     // the position of the robot is the center of the body (without wheels)
    00202     // to set the vehicle on the ground when the z component of the position is 0
    00203     // width*0.6 is added (without this the wheels and half of the robot will be in the ground)
    00204     Matrix p2;
    00205     p2 = pose * Matrix::translate(Vec3(0, 0, width*0.6));
    00206     create(p2);
    00207   };
    00208
    00209
    00210   /**
    00211    * updates the osg notes
    00212    */
    00213   void Nimm4::update(){
    00214     assert(created); // robot must exist
    00215
    00216     for (int i=0; i<segmentsno; i++) { // update objects
    00217       object[i]->update();
    00218     }
    00219     for (int i=0; i < 4; i++) { // update joints
    00220       joint[i]->update();
    00221     }
    00222
    00223   };
    00224
    00225   /** things for collision handling inside the space of the robot can be done here
    00226    */
    00227   void Nimm4::mycallback(void *data, dGeomID o1, dGeomID o2){
    00228     // do collisions handling for collisions between parts inside the space of the robot here
    00229     // this has no meaning for this robot, because collsions between wheels and body are ignored
    00230     // but if parts of the robot can move against each other this is important
    00231
    00232     // the follwing (not active) code part can be used to check if objects which had collisions
    00233     // are inside the list of objects of the robot
    00234     /*  Nimm4* me = (Nimm4*)data;
    00235         if(isGeomInObjectList(me->object, me->segmentsno, o1)
    00236         && isGeomInObjectList(me->object, me->segmentsno, o2)){
    00237         return;
    00238         }
    00239     */
    00240   }
    00241
    00242   /** this function is called in each timestep. It should perform robot-internal checks,
    00243       like space-internal collision detection, sensor resets/update etc.
    00244       @param GlobalData structure that contains global data from the simulation environment
    00245   */
    00246   void Nimm4::doInternalStuff(const GlobalData& global){}
    00247
    00248   /** checks for internal collisions and treats them.
    00249    *  In case of a treatment return true (collision will be ignored by other objects
    00250    *  and the default routine)  else false (collision is passed to other objects and
    00251    *  (if not treated) to the default routine).
    00252    */
    00253   bool Nimm4::collisionCallback(void *data, dGeomID o1, dGeomID o2){
    00254     assert(created); // robot must exist
    00255
    00256     // checks if one of the collision objects is part of thee space the robot is in
    00257     // and therefore part of the robot
    00258     if( o1 == (dGeomID)odeHandle.space || o2 == (dGeomID)odeHandle.space){
    00259       // if the space is involved check for collisions between parts inside the space
    00260       // this has no meaning here, because collsions between wheels and body are ignored
    00261       // but if parts of the robot can move against each other this is important
    00262       dSpaceCollide(odeHandle.space, this, mycallback);
    00263
    00264       bool colwithme;   // for collision with some part of the vehicle
    00265       bool colwithbody; // for collision with the (main) body
    00266       int i,n;
    00267       const int N = 10;
    00268       dContact contact[N];
    00269       // extract collision points between the two objects that intersect
    00270       n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
    00271       // for each collision point
    00272       for (i=0; i<n; i++){
    00273         // collisions set to false
    00274         colwithbody = false;
    00275         colwithme = false;
    00276         // if there is a collision with the body both bools have to be set to true
    00277         if( contact[i].geom.g1 == object[0]->getGeom() || contact[i].geom.g2 == object[0]->getGeom()){
    00278           colwithbody = true;
    00279           colwithme = true;
    00280         }
    00281         // if there is a collision with one of the wheels only colwithme has to be set true
    00282         if( isGeomInPrimitiveList(object+1, segmentsno-1, contact[i].geom.g1) ||
    00283             isGeomInPrimitiveList(object+1, segmentsno-1, contact[i].geom.g2)){
    00284           colwithme = true;
    00285         }
    00286         if( colwithme){ // if collision set the contact parameters
    00287           contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
    00288             dContactSoftERP | dContactSoftCFM | dContactApprox1;
    00289           contact[i].surface.slip1 = 0.005;
    00290           contact[i].surface.slip2 = 0.005;
    00291           if(colwithbody){ // if collision with body set small friction
    00292             contact[i].surface.mu = 0.1; // small friction of smooth body
    00293             contact[i].surface.soft_erp = 0.5;
    00294             contact[i].surface.soft_cfm = 0.001;
    00295           }else{  // if collision with the wheels set large friction to give wheels grip on the ground
    00296             contact[i].surface.mu = 1.1; //large friction
    00297             contact[i].surface.soft_erp = 0.9;
    00298             contact[i].surface.soft_cfm = 0.001;
    00299           }
    00300           // create a joint in the world with the properties set above
    00301           // (the joint must be put in group "contactgroup", which is deleted
    00302           // after each simulation step, see ode documentation)
    00303           dJointID c = dJointCreateContact( odeHandle.world, odeHandle.jointGroup, &contact[i]);
    00304           // attach the intersecting objects to the joint
    00305           dJointAttach ( c , dGeomGetBody(contact[i].geom.g1) , dGeomGetBody(contact[i].geom.g2)) ;
    00306         }
    00307       }
    00308       //if collision handled return true
    00309       return true;
    00310     }
    00311     //if collision not handled return false
    00312     return false;
    00313   }
    00314
    00315
    00316   /** creates vehicle at desired position
    00317       @param pos struct Position with desired position
    00318   */
    00319   void Nimm4::create( const Matrix& pose ){
    00320     if (created) {  // if robot exists destroy it
    00321       destroy();
    00322     }
    00323     // create car space and add it to the top level space
    00324     odeHandle.space = dSimpleSpaceCreate (parentspace);
    00325
    00326     // create cylinder for main body
    00327     // initialize it with ode-, osghandle and mass
    00328     // rotate and place body (here by 90° around the y-axis)
    00329     // use texture 'wood' for capsule
    00330     // put it into object[0]
    00331     Capsule* cap = new Capsule(width/2, length);
    00332     cap->init(odeHandle, cmass, osgHandle);
    00333     cap->setPose(Matrix::rotate(M_PI/2, 0, 1, 0) * pose);
    00334     cap->getOSGPrimitive()->setTexture("Images/wood.rgb");
    00335     object[0]=cap;
    00336
    00337     // create wheel bodies
    00338     osgHandle.color= Color(0.8,0.8,0.8);
    00339     for (int i=1; i<5; i++) {
    00340       // create sphere with radius
    00341       // and initializ it with odehandle, osghandle and mass
    00342       // calculate position of wheels(must be at desired positions relative to the body)
    00343       // rotate and place body (here by 90° around the x-axis)
    00344       // set texture for wheels
    00345       Sphere* sph = new Sphere(radius);
    00346       sph->init(odeHandle, wmass, osgHandle);
    00347       Vec3 wpos = Vec3( ((i-1)/2==0?-1:1)*length/2.0,
    00348                         ((i-1)%2==0?-1:1)*(width*0.5+wheelthickness),
    00349                         -width*0.6+radius );
    00350       sph->setPose(Matrix::rotate(M_PI/2, 0, 0, 1) * Matrix::translate(wpos) * pose);
    00351       sph->getOSGPrimitive()->setTexture("Images/wood.rgb");
    00352       object[i]=sph;
    00353     }
    00354
    00355     // generate 4 joints to connect the wheels to the body
    00356     for (int i=0; i<4; i++) {
    00357       Pos anchor(dBodyGetPosition (object[i+1]->getBody()));
    00358       joint[i] = new Hinge2Joint(object[0], object[i+1], anchor, Axis(0,0,1)*pose, Axis(0,1,0)*pose);
    00359       joint[i]->init(odeHandle, osgHandle, true, 2.01 * radius);
    00360     }
    00361     for (int i=0; i<4; i++) {
    00362       // set stops to make sure wheels always stay in alignment
    00363       joint[i]->setParam(dParamLoStop, 0);
    00364       joint[i]->setParam(dParamHiStop, 0);
    00365     }
    00366
    00367     created=true; // robot is created
    00368   };
    00369
    00370
    00371   /** destroys vehicle and space
    00372    */
    00373   void Nimm4::destroy(){
    00374     if (created){
    00375       for (int i=0; i<segmentsno; i++){
    00376         if(object[i]) delete object[i]; // destroy bodies and geoms
    00377       }
    00378       for (int i=0; i<4; i++){
    00379         if(joint[i]) delete joint[i]; // destroy bodies and geoms
    00380       }
    00381       dSpaceDestroy(odeHandle.space); // destroy space
    00382     }
    00383     created=false; // robot does not exist (anymore)
    00384   }
    00385
    00386 }