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