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