PreFrontalCortex.C
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 #include "BeoSub/BeeBrain/PreFrontalCortex.H"
00038
00039
00040 PreFrontalCortexAgent::PreFrontalCortexAgent(std::string name) : Agent(name)
00041 {
00042 itsRunTimer.reset(new Timer(1000000));
00043 itsCurrentMissionTimer.reset(new Timer(1000000));
00044 }
00045
00046
00047 PreFrontalCortexAgent::PreFrontalCortexAgent
00048 (std::string name, rutz::shared_ptr<AgentManagerA> ama) :
00049 Agent(name), itsAgentManager(ama)
00050 {
00051 itsRunTimer.reset(new Timer(1000000));
00052 itsCurrentMissionTimer.reset(new Timer(1000000));
00053 }
00054
00055
00056 PreFrontalCortexAgent::~PreFrontalCortexAgent()
00057 { }
00058
00059
00060 void PreFrontalCortexAgent::start()
00061 {
00062 populateMissions();
00063 currentMission = itsMissions[0];
00064
00065
00066
00067 }
00068
00069
00070
00071 void PreFrontalCortexAgent::populateMissions()
00072 {
00073 LINFO("Populating Missions...");
00074
00075
00076 rutz::shared_ptr<Mission> gate(new Mission());
00077 gate->timeForMission = 90;
00078 gate->missionName = GATE;
00079 gate->missionState = NOT_STARTED;
00080 rutz::shared_ptr<OceanObject> cross(new OceanObject(OceanObject::CROSS));
00081 gate->missionObjects.push_back(cross);
00082 itsMissions.push_back(gate);
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 stateChanged();
00131 }
00132
00133
00134 void PreFrontalCortexAgent::lookForObjects
00135 (std::vector<rutz::shared_ptr<OceanObject> > oceanObjects,
00136 bool startLooking)
00137 {
00138 CommandType cmdType = SEARCH_OCEAN_OBJECT_CMD;
00139
00140 if(startLooking == false)
00141 {
00142 cmdType = STOP_SEARCH_OCEAN_OBJECT_CMD;
00143 }
00144
00145 for(uint i = 0; i < oceanObjects.size(); i++)
00146 {
00147 rutz::shared_ptr<OceanObject> o = oceanObjects[i];
00148 switch(o->getType())
00149 {
00150 case OceanObject::CROSS:
00151 itsAgentManager->pushCommand
00152 (cmdType,
00153 POSITION,
00154 o);
00155 itsAgentManager->pushCommand
00156 (cmdType,
00157 ORIENTATION,
00158 o);
00159 itsAgentManager->pushCommand
00160 (cmdType,
00161 MASS,
00162 o);
00163 break;
00164 case OceanObject::BIN:
00165 itsAgentManager->pushCommand
00166 (cmdType,
00167 POSITION,
00168 o);
00169 break;
00170 case OceanObject::BUOY:
00171 itsAgentManager->pushCommand
00172 (cmdType,
00173 POSITION,
00174 o);
00175 itsAgentManager->pushCommand
00176 (cmdType,
00177 FREQUENCY,
00178 o);
00179 break;
00180 case OceanObject::PIPE:
00181 itsAgentManager->pushCommand
00182 (cmdType,
00183 POSITION,
00184 o);
00185 itsAgentManager->pushCommand
00186 (cmdType,
00187 ORIENTATION,
00188 o);
00189 break;
00190 case OceanObject::PINGER:
00191 itsAgentManager->pushCommand
00192 (cmdType,
00193 ORIENTATION,
00194 o);
00195 itsAgentManager->pushCommand
00196 (cmdType,
00197 DISTANCE,
00198 o);
00199 break;
00200 default:
00201 LERROR("unknown ocean object type");
00202 break;
00203 }
00204 }
00205 }
00206
00207
00208 void PreFrontalCortexAgent::msgOceanObjectUpdate()
00209 {
00210 LINFO("Recieved msgOceanObjectUpdate");
00211 stateChanged();
00212 }
00213
00214
00215 void PreFrontalCortexAgent::msgMovementComplete()
00216 {
00217 LINFO("Recieved msgMovementComplete");
00218 stateChanged();
00219 }
00220
00221
00222 bool PreFrontalCortexAgent::pickAndExecuteAnAction()
00223 {
00224
00225 if(itsRunTimer->get() > AUVSI07_COMPETITION_TIME)
00226 {
00227
00228
00229
00230 LINFO("FIX ME - Almost out of time");
00231 }
00232
00233
00234 currentMission.reset();
00235 for(uint i = 0; i < itsMissions.size(); i++)
00236 if(itsMissions[i]->missionState != COMPLETED)
00237 {
00238 currentMission = itsMissions[i]; break;
00239 }
00240
00241 if(currentMission.is_invalid())
00242 {
00243
00244 return false;
00245 }
00246
00247 if(currentMission->missionName == GATE)
00248 return executeGateMission();
00249 if(currentMission->missionName == HIT_START_BUOY)
00250 return executeBuoyMission();
00251 if(currentMission->missionName == FIRST_BIN)
00252 return executeFirstBinMission();
00253 if(currentMission->missionName == SECOND_BIN)
00254 return executeSecondBinMission();
00255 if(currentMission->missionName == GET_TREASURE)
00256 return executeCrossMission();
00257
00258 return false;
00259 }
00260
00261
00262 bool PreFrontalCortexAgent::executeGateMission()
00263 {
00264
00265 if(currentMission->missionState != NOT_STARTED &&
00266 itsCurrentMissionTimer->get() > currentMission->timeForMission)
00267 {
00268
00269 currentMission->missionState = FAILED;
00270 return true;
00271 }
00272
00273
00274 if(currentMission->missionState == NOT_STARTED)
00275 {
00276
00277 itsRunTimer->reset();
00278 itsCurrentMissionTimer->reset();
00279
00280 LINFO("Starting Mission");
00281
00282 lookForObjects(currentMission->missionObjects);
00283
00284 rutz::shared_ptr<ComplexMovement>
00285 goThroughGate(new ComplexMovement());
00286 goThroughGate->addMove(&PreMotorComplex::forward, ONCE, 5.0);
00287 itsPreMotorComplex->run(goThroughGate);
00288 currentMission->missionState = SEARCHING;
00289 return true;
00290 }
00291
00292 else if(currentMission->missionState == SEARCHING)
00293 {
00294 LINFO("Searching for cross");
00295
00296 rutz::shared_ptr<OceanObject> theCross;
00297
00298 for(uint i = 0; i < currentMission->missionObjects.size(); i++)
00299 {
00300 if(currentMission->missionObjects[i]->getType()
00301 == OceanObject::CROSS)
00302 {
00303 theCross = currentMission->missionObjects[i];
00304 break;
00305 }
00306 }
00307
00308
00309 if(theCross.is_valid()
00310 && theCross->getPosition().isValidY()
00311 && theCross->getPosition().isValidX())
00312 {
00313 currentMission->missionState = CENTERING;
00314 }
00315
00316 return true;
00317 }
00318
00319 else if(currentMission->missionState == CENTERING)
00320 {
00321 LINFO("Centering on cross");
00322 rutz::shared_ptr<OceanObject> theCross;
00323 for(uint i = 0; i < currentMission->missionObjects.size(); i++)
00324 {
00325 if(currentMission->missionObjects[i]->getType()
00326 == OceanObject::CROSS)
00327 {
00328 theCross = currentMission->missionObjects[i];
00329 break;
00330 }
00331 }
00332
00333 if(theCross.is_valid()
00334 && abs(theCross->getPosition().x - 160) < 20
00335 && abs(theCross->getPosition().y - 120) < 20)
00336 {
00337 itsPreMotorComplex->msgHalt();
00338
00339 currentMission->missionState = COMPLETED;
00340 }
00341 else if(theCross.is_valid())
00342 {
00343 rutz::shared_ptr<ComplexMovement>
00344 centerOnCross(new ComplexMovement());
00345 centerOnCross->addMove(&PreMotorComplex::vis_center, REPEAT, theCross->getPositionPtr());
00346 itsPreMotorComplex->run(centerOnCross);
00347 }
00348
00349 return true;
00350 }
00351
00352 else if(currentMission->missionState == COMPLETED)
00353 {
00354 LINFO("Move through gate complete");
00355 lookForObjects(currentMission->missionObjects, false);
00356
00357
00358
00359
00360
00361 return false;
00362
00363 }
00364
00365 return false;
00366 }
00367
00368
00369 bool PreFrontalCortexAgent::executeBuoyMission()
00370 {
00371
00372 if(currentMission->missionState != NOT_STARTED &&
00373 itsCurrentMissionTimer->get() > currentMission->timeForMission)
00374 {
00375
00376 currentMission->missionState = FAILED;
00377 return true;
00378 }
00379
00380
00381 if(currentMission->missionState == NOT_STARTED)
00382 {
00383 LINFO("Starting Buoy Mission");
00384
00385
00386
00387
00388
00389
00390
00391
00392 currentMission->missionState = SEARCHING;
00393 return true;
00394 }
00395 else if(currentMission->missionState == SEARCHING)
00396 {
00397 return true;
00398 }
00399 else if(currentMission->missionState == CENTERING) {
00400 return true;
00401 }
00402
00403 else if(currentMission->missionState == COMPLETED)
00404 {
00405 return true;
00406 }
00407 return false;
00408 }
00409
00410
00411 bool PreFrontalCortexAgent::executeFirstBinMission()
00412 {
00413
00414 if(currentMission->missionState != NOT_STARTED &&
00415 itsCurrentMissionTimer->get() > currentMission->timeForMission)
00416 {
00417
00418 currentMission->missionState = FAILED;
00419 return true;
00420 }
00421
00422
00423 if(currentMission->missionState == NOT_STARTED)
00424 {
00425 LINFO("Starting First Bin Mission");
00426 itsCurrentMissionTimer->reset();
00427
00428 currentMission->missionState = SEARCHING;
00429 return true;
00430 }
00431 else if(currentMission->missionState == SEARCHING)
00432 {
00433 return false;
00434 }
00435 else if(currentMission->missionState == CENTERING)
00436 {
00437 return true;
00438 }
00439
00440 else if(currentMission->missionState == COMPLETED)
00441 {
00442 return true;
00443 }
00444 return false;
00445 }
00446
00447
00448 bool PreFrontalCortexAgent::executeSecondBinMission()
00449 {
00450
00451 if(currentMission->missionState != NOT_STARTED &&
00452 itsCurrentMissionTimer->get() > currentMission->timeForMission)
00453 {
00454
00455 currentMission->missionState = FAILED;
00456 return true;
00457 }
00458
00459
00460 if(currentMission->missionState == NOT_STARTED)
00461 {
00462 LINFO("Starting Second Bin Mission");
00463 itsCurrentMissionTimer->reset();
00464
00465 currentMission->missionState = SEARCHING;
00466 return true;
00467 }
00468 else if(currentMission->missionState == SEARCHING)
00469 {
00470 return false;
00471 }
00472 else if(currentMission->missionState == CENTERING)
00473 {
00474 return true;
00475 }
00476
00477 else if(currentMission->missionState == COMPLETED)
00478 {
00479 return true;
00480 }
00481 return false;
00482 }
00483
00484
00485 bool PreFrontalCortexAgent::executeCrossMission()
00486 {
00487
00488 if(currentMission->missionState != NOT_STARTED &&
00489 itsCurrentMissionTimer->get() > currentMission->timeForMission)
00490 {
00491
00492 currentMission->missionState = FAILED;
00493 return true;
00494 }
00495
00496
00497 if(currentMission->missionState == NOT_STARTED)
00498 {
00499 LINFO("Starting Cross Mission");
00500 itsCurrentMissionTimer->reset();
00501
00502 currentMission->missionState = SEARCHING;
00503 return true;
00504 }
00505 else if(currentMission->missionState == SEARCHING)
00506 {
00507 return false;
00508 }
00509 else if(currentMission->missionState == CENTERING)
00510 {
00511 return true;
00512 }
00513
00514 else if(currentMission->missionState == COMPLETED)
00515 {
00516 return true;
00517 }
00518 return false;
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532