00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #include <ode/ode.h>
00088 #include <assert.h>
00089 #include <osg/Matrix>
00090
00091 #include "nimm2.h"
00092 #include "irsensor.h"
00093
00094 using namespace osg;
00095
00096 namespace lpzrobots {
00097
00098
00099
00100 Nimm2::Nimm2(const OdeHandle& odehandle, const OsgHandle& osgHandle,
00101 const Nimm2Conf& conf = getDefaultConf()):
00102
00103 OdeRobot(odehandle, osgHandle, "Nimm2"), conf(conf) {
00104
00105
00106 created=false;
00107
00108
00109
00110
00111
00112
00113 max_force = conf.force*conf.size*conf.size;
00114
00115 height=conf.size;
00116
00117 width=conf.size/2;
00118 radius=conf.size/4;
00119 wheelthickness=conf.size/20;
00120 cmass=4*conf.size;
00121 wmass=conf.size/5.0;
00122 if(conf.singleMotor){
00123 sensorno=1;
00124 motorno=1;
00125 } else {
00126 sensorno=2;
00127 motorno=2;
00128 }
00129
00130 if (conf.cigarMode){
00131 length=conf.size*2.0;
00132 wheeloffset= -length/4;
00133 number_bumpers=2;
00134 cmass=4*conf.size;
00135 max_force = 2*conf.force*conf.size*conf.size;
00136 }
00137 else{
00138 length=conf.size/2;
00139 wheeloffset=0.0;
00140 number_bumpers=2;
00141 }
00142
00143
00144 sensorno+= conf.irFront * 2;
00145 };
00146
00147
00148
00149
00150
00151
00152 void Nimm2::setMotors(const motor* motors, int motornumber){
00153 assert(created);
00154 assert(motornumber == motorno);
00155 if(conf.singleMotor){
00156 joint[0]->setParam(dParamVel2, motors[0]*conf.speed);
00157 joint[0]->setParam(dParamFMax2,max_force);
00158 joint[1]->setParam(dParamVel2, motors[0]*conf.speed);
00159 joint[1]->setParam(dParamFMax2,max_force);
00160 } else {
00161 for (int i=0; i<2; i++){
00162 joint[i]->setParam(dParamVel2, motors[i]*conf.speed);
00163 joint[i]->setParam(dParamFMax2,max_force);
00164 }
00165 }
00166 };
00167
00168
00169
00170
00171
00172
00173 int Nimm2::getSensors(sensor* sensors, int sensornumber){
00174 assert(created);
00175
00176
00177
00178
00179 int len = conf.singleMotor ? 1 : 2;
00180 for (int i=0; i<len; i++){
00181 sensors[i]=joint[i]->getPosition2Rate();
00182 sensors[i]/=conf.speed;
00183 }
00184
00185
00186 len += irSensorBank.get(sensors+len, sensornumber-len);
00187 return len;
00188 };
00189
00190
00191 void Nimm2::place(const Matrix& pose){
00192
00193
00194
00195 Matrix p2;
00196 p2 = pose * Matrix::translate(Vec3(0, 0, width*0.6));
00197 create(p2);
00198
00199 };
00200
00201
00202
00203
00204
00205 int Nimm2::getSegmentsPosition(vector<Position> &poslist){
00206 assert(created);
00207 for (int i=0; i<3; i++){
00208 poslist.push_back(Position(dBodyGetPosition(object[i]->getBody())));
00209 }
00210 return 3;
00211 };
00212
00213
00214
00215
00216 void Nimm2::update(){
00217 assert(created);
00218
00219 for (int i=0; i<3; i++) {
00220 object[i]->update();
00221 }
00222 for (int i=0; i < 2; i++) {
00223 joint[i]->update();
00224 }
00225 if (conf.bumper){
00226 for (int i=0; i<number_bumpers; i++){
00227 bumper[i].trans->update();
00228 }
00229 }
00230
00231
00232 irSensorBank.update();
00233 }
00234
00235
00236 void Nimm2::mycallback(void *data, dGeomID o1, dGeomID o2){
00237
00238
00239
00240
00241 }
00242
00243 bool Nimm2::collisionCallback(void *data, dGeomID o1, dGeomID o2){
00244
00245 assert(created);
00246 bool colwithme = false;
00247 if( o1 == (dGeomID)odeHandle.space || o2 == (dGeomID)odeHandle.space ){
00248 if(o1 == (dGeomID)odeHandle.space) irSensorBank.sense(o2);
00249 if(o2 == (dGeomID)odeHandle.space) irSensorBank.sense(o1);
00250
00251 bool colwithbody;
00252 int i,n;
00253 const int N = 10;
00254 dContact contact[N];
00255
00256 n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
00257 for (i=0; i<n; i++){
00258 colwithme = true;
00259 colwithbody = false;
00260 if( contact[i].geom.g1 == object[0]->getGeom() || contact[i].geom.g2 == object[0]->getGeom() ||
00261 ( bumper[0].trans && bumper[1].trans) && (
00262 contact[i].geom.g1 == bumper[0].trans->getGeom() ||
00263 contact[i].geom.g2 == bumper[0].trans->getGeom() ||
00264 contact[i].geom.g1 == bumper[1].trans->getGeom() ||
00265 contact[i].geom.g2 == bumper[1].trans->getGeom()) ){
00266
00267 colwithbody = true;
00268 }
00269 contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
00270 dContactSoftERP | dContactSoftCFM | dContactApprox1;
00271
00272
00273 contact[i].surface.slip1 = 0.005;
00274 contact[i].surface.slip2 = 0.005;
00275 if(colwithbody){
00276 contact[i].surface.mu = 0.1;
00277 contact[i].surface.soft_erp = 0.5;
00278 contact[i].surface.soft_cfm = 0.005;
00279 }else{
00280 contact[i].surface.mu = 5.0;
00281 contact[i].surface.soft_erp = 0.5;
00282 contact[i].surface.soft_cfm = 0.001;
00283 }
00284 dJointID c = dJointCreateContact( odeHandle.world, odeHandle.jointGroup, &contact[i]);
00285 dJointAttach ( c , dGeomGetBody(contact[i].geom.g1) , dGeomGetBody(contact[i].geom.g2));
00286 }
00287 }
00288 return colwithme;
00289 }
00290
00291 void Nimm2::doInternalStuff(const GlobalData& globalData){
00292
00293 irSensorBank.reset();
00294 }
00295
00296
00297
00298
00299 void Nimm2::create(const Matrix& pose){
00300 if (created) {
00301 destroy();
00302 }
00303
00304
00305
00306 odeHandle.space = dSimpleSpaceCreate (parentspace);
00307
00308
00309
00310
00311
00312
00313
00314 Capsule* cap = new Capsule(width/2, length);
00315 cap->init(odeHandle, cmass, osgHandle);
00316 cap->setPose(Matrix::rotate(M_PI/2, 0, 1, 0) * pose);
00317 cap->getOSGPrimitive()->setTexture("Images/wood.rgb");
00318 object[0]=cap;
00319
00320
00321
00322
00323
00324
00325 if (conf.bumper){
00326 for (int i=0; i<number_bumpers; i++){
00327 bumper[i].bump = new Capsule(width/4, 2*radius+width/2);
00328 bumper[i].trans = new Transform(object[0], bumper[i].bump,
00329 Matrix::rotate(M_PI/2.0, Vec3(1, 0, 0)) *
00330 Matrix::translate(0, 0, i==0 ? -(length/2) : (length/2)));
00331 bumper[i].trans->init(odeHandle, 0, osgHandle);
00332 }
00333 }
00334
00335
00336 OsgHandle osgHandleWheels(osgHandle);
00337 osgHandleWheels.color = Color(1.0,1.0,1.0);
00338 for (int i=1; i<3; i++) {
00339 if(conf.sphereWheels) {
00340 Sphere* wheel = new Sphere(radius);
00341 wheel->init(odeHandle, wmass, osgHandleWheels);
00342
00343 wheel->setPose(Matrix::rotate(M_PI/2.0, 1, 0, 0) *
00344 Matrix::translate(wheeloffset, (i==2 ? -1 : 1) * (width*0.5+wheelthickness), 0) *
00345 pose);
00346 wheel->getOSGPrimitive()->setTexture("Images/tire.rgb");
00347 object[i] = wheel;
00348 }else{
00349
00350
00351
00352
00353
00354
00355
00356 }
00357 }
00358
00359
00360
00361
00362
00363 for (int i=0; i<2; i++) {
00364 joint[i] = new Hinge2Joint(object[0], object[i+1], object[i+1]->getPosition(),
00365 Axis(0, 0, 1)*pose, Axis(0, -1, 0)*pose);
00366 joint[i]->init(odeHandle, osgHandleWheels, true, 2.01 * radius);
00367
00368 joint[i]->setParam(dParamLoStop,0);
00369 joint[i]->setParam(dParamHiStop,0);
00370 }
00371
00372
00373 irSensorBank.init(odeHandle, osgHandle);
00374
00375 if (conf.irFront){
00376 for(int i=-1; i<2; i+=2){
00377 IRSensor* sensor = new IRSensor();
00378 irSensorBank.registerSensor(sensor, object[0],
00379 Matrix::rotate(i*M_PI/10, Vec3(0,0,1)) *
00380 Matrix::translate(0,i*width/10,length/2 + width/2 - width/60 ),
00381 conf.irRange, RaySensor::drawAll);
00382 }
00383 }
00384
00385
00386 created=true;
00387 };
00388
00389
00390
00391
00392 void Nimm2::destroy(){
00393 if (created){
00394 for (int i=0; i<3; i++){
00395 if(object[i]) delete object[i];
00396 }
00397 for (int i=0; i<2; i++){
00398 if(joint[i]) delete joint[i];
00399 }
00400 for (int i=0; i<2; i++){
00401 if(bumper[i].bump) delete bumper[i].bump;
00402 if(bumper[i].trans) delete bumper[i].trans;
00403 }
00404 irSensorBank.clear();
00405 dSpaceDestroy(odeHandle.space);
00406 }
00407 created=false;
00408 }
00409
00410 }
00411