00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Robot Group Leipzig * 00003 * martius (at) informatik.uni-leipzig.de * 00004 * fhesse (at) informatik.uni-leipzig.de * 00005 * der (at) 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.14 2008/05/14 15:11:57 martius 00024 * typo 00025 * 00026 * Revision 1.13 2008/05/07 16:45:52 martius 00027 * code cosmetics and documentation 00028 * 00029 * Revision 1.12 2008/05/07 11:03:48 martius 00030 * code cosmetics 00031 * 00032 * Revision 1.11 2008/04/23 07:17:16 martius 00033 * makefiles cleaned 00034 * new also true realtime factor displayed, 00035 * warning if out of sync 00036 * drawinterval in full speed is 10 frames, independent of the speed 00037 * 00038 * Revision 1.10 2007/11/07 13:21:16 martius 00039 * doInternal stuff changed signature 00040 * 00041 * Revision 1.9 2007/08/28 14:13:09 martius 00042 * typo 00043 * 00044 * Revision 1.8 2006/07/14 12:23:40 martius 00045 * selforg becomes HEAD 00046 * 00047 * Revision 1.7.4.17 2006/06/29 16:39:55 robot3 00048 * -you can now see bounding shapes if you type ./start -drawboundings 00049 * -includes cleared up 00050 * -abstractobstacle and abstractground have now .cpp-files 00051 * 00052 * Revision 1.7.4.16 2006/06/25 17:00:32 martius 00053 * Id 00054 * 00055 * Revision 1.7.4.15 2006/06/25 16:57:14 martius 00056 * abstractrobot is configureable 00057 * name and revision 00058 * 00059 * Revision 1.7.4.14 2006/04/04 17:03:21 fhesse 00060 * docu added 00061 * 00062 * Revision 1.7.4.13 2006/04/04 14:13:24 fhesse 00063 * documentation improved 00064 * 00065 * Revision 1.7.4.12 2006/03/31 11:11:38 fhesse 00066 * minor changes in docu 00067 * 00068 * Revision 1.7.4.11 2006/02/01 18:33:40 martius 00069 * use Axis type for Joint axis. very important, since otherwise Vec3 * pose is not the right direction vector anymore 00070 * 00071 * Revision 1.7.4.10 2005/12/29 16:47:40 martius 00072 * joint has getPosition 00073 * 00074 * Revision 1.7.4.9 2005/12/15 17:04:08 martius 00075 * Primitives are not longer inherited from OSGPrimitive, moreover 00076 * they aggregate them. 00077 * Joint have better getter and setter 00078 * 00079 * Revision 1.7.4.8 2005/12/14 15:37:09 martius 00080 * robots are working with osg 00081 * 00082 * Revision 1.7.4.7 2005/12/13 18:11:39 martius 00083 * still trying to port robots 00084 * 00085 * Revision 1.7.4.6 2005/12/13 12:32:09 martius 00086 * nonvisual joints 00087 * 00088 * Revision 1.7.4.5 2005/12/12 23:41:30 martius 00089 * added Joint wrapper 00090 * 00091 * Revision 1.7.4.4 2005/12/11 23:35:08 martius 00092 * *** empty log message *** 00093 * 00094 * Revision 1.7.4.3 2005/12/06 10:13:25 martius 00095 * openscenegraph integration started 00096 * 00097 * Revision 1.7.4.2 2005/11/15 12:29:26 martius 00098 * new selforg structure and OdeAgent, OdeRobot ... 00099 * 00100 * Revision 1.7.4.1 2005/11/14 17:37:17 martius 00101 * moved to selforg 00102 * 00103 * Revision 1.7 2005/11/09 13:24:42 martius 00104 * added GPL 00105 * 00106 ***************************************************************************/ 00107 #include <assert.h> 00108 #include <ode/ode.h> 00109 00110 // include primitives (box, spheres, cylinders ...) 00111 #include "primitive.h" 00112 #include "osgprimitive.h" 00113 00114 // include joints 00115 #include "joint.h" 00116 00117 // include header file 00118 #include "nimm4.h" 00119 00120 using namespace osg; 00121 00122 00123 namespace lpzrobots { 00124 00125 // constructor: 00126 // - give handle for ODE and OSG stuff 00127 // - size of robot, maximal used force and speed factor are adjustable 00128 // - sphereWheels switches between spheres or wheels as wheels 00129 // (wheels are only drawn, collision handling is always with spheres) 00130 Nimm4::Nimm4(const OdeHandle& odeHandle, const OsgHandle& osgHandle, 00131 const std::string& name, 00132 double size/*=1.0*/, double force /*=3*/, double speed/*=15*/, 00133 bool sphereWheels /*=true*/) 00134 : // calling OdeRobots construtor with name of the actual robot 00135 OdeRobot(odeHandle, osgHandle, name, "$Id: nimm4.cpp,v 1.14 2008/05/14 15:11:57 martius Exp $") 00136 { 00137 // robot is not created till now 00138 created=false; 00139 00140 // choose color (here the color of the "Nimm Zwei" candy is used, 00141 // where the name of the Nimm2 and Nimm4 robots comes from ;-) 00142 this->osgHandle.color = Color(2, 156/255.0, 0, 1.0f); 00143 00144 // maximal used force is calculated from the force factor and size given to the constructor 00145 max_force = force*size*size; 00146 00147 // speed and type of wheels are set 00148 this->speed = speed; 00149 this->sphereWheels = sphereWheels; 00150 00151 height=size; 00152 length=size/2.5; // length of body 00153 width=size/2; // diameter of body 00154 radius=size/6; // wheel radius 00155 wheelthickness=size/20; // thickness of the wheels (if wheels used, no spheres) 00156 cmass=8*size; // mass of the body 00157 wmass=size; // mass of the wheels 00158 sensorno=4; // number of sensors 00159 motorno=4; // number of motors 00160 segmentsno=5; // number of segments of the robot 00161 }; 00162 00163 00164 /** sets actual motorcommands 00165 @param motors motors scaled to [-1,1] 00166 @param motornumber length of the motor array 00167 */ 00168 void Nimm4::setMotors(const motor* motors, int motornumber){ 00169 assert(created); // robot must exist 00170 // the number of controlled motors is minimum of 00171 // "number of motorcommands" (motornumber) and 00172 // "number of motors inside the robot" (motorno) 00173 int len = (motornumber < motorno)? motornumber : motorno; 00174 00175 // for each motor the motorcommand (between -1 and 1) multiplied with speed 00176 // is set and the maximal force to realize this command are set 00177 for (int i=0; i<len; i++){ 00178 joint[i]->setParam(dParamVel2, motors[i]*speed); 00179 joint[i]->setParam(dParamFMax2, max_force); 00180 } 00181 }; 00182 00183 /** returns actual sensorvalues 00184 @param sensors sensors scaled to [-1,1] (more or less) 00185 @param sensornumber length of the sensor array 00186 @return number of actually written sensors 00187 */ 00188 int Nimm4::getSensors(sensor* sensors, int sensornumber){ 00189 assert(created); // robot must exist 00190 00191 // the number of sensors to read is the minimum of 00192 // "number of sensors requested" (sensornumber) and 00193 // "number of sensors inside the robot" (sensorno) 00194 int len = (sensornumber < sensorno)? sensornumber : sensorno; 00195 00196 // for each sensor the anglerate of the joint is red and scaled with 1/speed 00197 for (int i=0; i<len; i++){ 00198 sensors[i]=joint[i]->getPosition2Rate(); 00199 sensors[i]/=speed; //scaling 00200 } 00201 // the number of red sensors is returned 00202 return len; 00203 }; 00204 00205 00206 void Nimm4::place(const osg::Matrix& pose){ 00207 // the position of the robot is the center of the body (without wheels) 00208 // to set the vehicle on the ground when the z component of the position is 0 00209 // width*0.6 is added (without this the wheels and half of the robot will be in the ground) 00210 Matrix p2; 00211 p2 = pose * Matrix::translate(Vec3(0, 0, width*0.6)); 00212 create(p2); 00213 }; 00214 00215 00216 /** 00217 * updates the osg notes 00218 */ 00219 void Nimm4::update(){ 00220 assert(created); // robot must exist 00221 00222 for (int i=0; i<segmentsno; i++) { // update objects 00223 object[i]->update(); 00224 } 00225 for (int i=0; i < 4; i++) { // update joints 00226 joint[i]->update(); 00227 } 00228 00229 }; 00230 00231 /** this function is called in each timestep. It should perform robot-internal checks, 00232 like space-internal collision detection, sensor resets/update etc. 00233 @param global structure that contains global data from the simulation environment 00234 */ 00235 void Nimm4::doInternalStuff(GlobalData& global){} 00236 00237 /** creates vehicle at desired pose 00238 @param pose matrix with desired position and orientation 00239 */ 00240 void Nimm4::create( const osg::Matrix& pose ){ 00241 if (created) { // if robot exists destroy it 00242 destroy(); 00243 } 00244 // create car space 00245 odeHandle.createNewSimpleSpace(parentspace, true); 00246 00247 OdeHandle wheelHandle(odeHandle); 00248 // make the material of the wheels a hard rubber 00249 wheelHandle.substance.toRubber(60); 00250 // create cylinder for main body 00251 // initialize it with ode-, osghandle and mass 00252 // rotate and place body (here by 90° around the y-axis) 00253 // use texture 'wood' for capsule 00254 // put it into object[0] 00255 Capsule* cap = new Capsule(width/2, length); 00256 cap->init(odeHandle, cmass, osgHandle); 00257 cap->setPose(Matrix::rotate(M_PI/2, 0, 1, 0) * pose); 00258 cap->getOSGPrimitive()->setTexture("Images/wood.rgb"); 00259 object[0]=cap; 00260 00261 // create wheels 00262 for (int i=1; i<5; i++) { 00263 // create sphere with radius 00264 // and initialize it with odehandle, osghandle and mass 00265 // calculate position of wheels(must be at desired positions relative to the body) 00266 // rotate and place body (here by 90Deg around the x-axis) 00267 // set texture for wheels 00268 Sphere* sph = new Sphere(radius); 00269 sph->init(wheelHandle, wmass, osgHandle.changeColor(Color(0.8,0.8,0.8))); 00270 Vec3 wpos = Vec3( ((i-1)/2==0?-1:1)*length/2.0, 00271 ((i-1)%2==0?-1:1)*(width*0.5+wheelthickness), 00272 -width*0.6+radius ); 00273 sph->setPose(Matrix::rotate(M_PI/2, 0, 0, 1) * Matrix::translate(wpos) * pose); 00274 sph->getOSGPrimitive()->setTexture("Images/wood.rgb"); 00275 object[i]=sph; 00276 } 00277 00278 // generate 4 joints to connect the wheels to the body 00279 for (int i=0; i<4; i++) { 00280 Pos anchor(dBodyGetPosition (object[i+1]->getBody())); 00281 joint[i] = new Hinge2Joint(object[0], object[i+1], anchor, Axis(0,0,1)*pose, Axis(0,1,0)*pose); 00282 joint[i]->init(odeHandle, osgHandle, true, 2.01 * radius); 00283 } 00284 for (int i=0; i<4; i++) { 00285 // set stops to make sure wheels always stay in alignment 00286 joint[i]->setParam(dParamLoStop, 0); 00287 joint[i]->setParam(dParamHiStop, 0); 00288 } 00289 00290 created=true; // robot is created 00291 }; 00292 00293 00294 /** destroys vehicle and space 00295 */ 00296 void Nimm4::destroy(){ 00297 if (created){ 00298 for (int i=0; i<4; i++){ 00299 if(joint[i]) delete joint[i]; // destroy bodies and geoms 00300 } 00301 for (int i=0; i<segmentsno; i++){ 00302 if(object[i]) delete object[i]; // destroy bodies and geoms 00303 } 00304 odeHandle.deleteSpace(); // destroy space 00305 } 00306 created=false; // robot does not exist (anymore) 00307 } 00308 00309 }