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 #include "Beowulf/Beowulf.H"
00068 #include "Component/ModelManager.H"
00069 #include "Media/FrameSeries.H"
00070 #include "Transport/FrameIstream.H"
00071 #include "Neuro/SaccadeController.H"
00072 #include "Neuro/SaccadeControllers.H"
00073 #include "Neuro/SaccadeControllerConfigurator.H"
00074 #include "Neuro/NeuroOpts.H"
00075 #include "Image/Image.H"
00076 #include "Image/Pixels.H"
00077 #include "Raster/Raster.H"
00078 #include "GUI/XWinManaged.H"
00079 #include "Media/MediaOpts.H"
00080 #include "Util/Timer.H"
00081 #include "Simulation/SimEventQueue.H"
00082 #include "Simulation/SimEventQueueConfigurator.H"
00083 #include "Devices/DeviceOpts.H"
00084
00085 #include "Beobot/BeobotBrainMT.H"
00086 #include "Beobot/BeobotConfig.H"
00087 #include "Beobot/BeobotControl.H"
00088 #include "Beobot/BeobotBeoChipListener.H"
00089
00090 #include "RCBot/Motion/MotionEnergy.H"
00091 #include "Controllers/PID.H"
00092
00093 #include "Image/MathOps.H"
00094 #include "Image/CutPaste.H"
00095 #include "Image/DrawOps.H"
00096 #include "Image/FilterOps.H"
00097 #include "Image/PyramidOps.H"
00098 #include "Image/ShapeOps.H"
00099 #include "Image/Transforms.H"
00100
00101 #include "Beobot/beobot-GSnav-def.H"
00102 #include "Beobot/GSnavResult.H"
00103
00104 #include <signal.h>
00105
00106 #include <errno.h>
00107
00108 #define ERR_INTERVAL 5000
00109
00110 static bool goforever = true;
00111
00112
00113
00114
00115 std::string getInputPrefix(nub::soft_ref<InputFrameSeries> ifs,
00116 int &inputType);
00117
00118
00119 int getOpMode(std::string opmodestr);
00120
00121
00122 void setupBeoChip(nub::soft_ref<BeoChip> b, BeobotConfig bbc);
00123
00124
00125 int beoChipProc
00126 (rutz::shared_ptr<BeobotBeoChipListener> lis, nub::soft_ref<BeoChip> b);
00127
00128
00129 void getBbmtResults
00130 ( nub::ref<BeobotBrainMT> bbmt,
00131 Image<PixRGB<byte> > &currIma, Image<double> &cgist,
00132 Image<float> &currSalMap, ImageSet<float> &cmap,
00133 std::vector<Point2D<int> > &salpt, std::vector<std::vector<double> > &feat,
00134 std::vector<Rectangle> &objRect);
00135
00136
00137 void dispResults
00138 ( Image< PixRGB<byte> > disp, rutz::shared_ptr<XWinManaged> win,
00139 Image< PixRGB<byte> > ima, Image< PixRGB<byte> > prevIma,
00140 std::vector<Point2D<int> > clmpt, std::vector<Point2D<int> > nlmpt,
00141 Image<float> currSalMap,
00142 std::vector<Point2D<int> > salpt, std::vector<Rectangle> objRect);
00143
00144
00145 bool checkNode
00146 ( int opMode, nub::soft_ref<Beowulf> beo,
00147 int32 &rnode, TCPmessage &rmsg, int32 &raction, int32 &rframe);
00148
00149
00150 bool checkInput(int opMode, bool resetNextLandmark,
00151 uint64 inputFrameRate, uint64 inputTime);
00152
00153
00154
00155 void setupVentralPacket
00156 ( TCPmessage &smsg, int rframe,
00157 bool resetVentralSearch, Image< PixRGB<byte> > currIma,
00158 Image<double> cgist, std::vector<Point2D<int> > salpt,
00159 std::vector<std::vector<double> > feat, std::vector<Rectangle> objRect,
00160 float dx, float dy, uint snumGT, float ltravGT);
00161
00162
00163 void setupDorsalPacket
00164 ( TCPmessage &smsg, int rframe, ImageSet<float> cmap,
00165 bool resetCurrLandmark, std::vector<Point2D<int> > clmpt,
00166 bool resetNextLandmark, std::vector<Point2D<int> > nlmpt);
00167
00168
00169 void processDorsalResult
00170 ( TCPmessage &rmsg, int32 raction, int32 rframe,
00171 std::vector<Point2D<int> > &clmpt, std::vector<Point2D<int> > &nlmpt,
00172 bool &resetNextLandmark, float &sp, float &st);
00173
00174
00175 void processVentralResult
00176 ( TCPmessage &rmsg, int32 raction, int32 rframe,
00177 std::vector<Point2D<int> > &clmpt, std::vector<Point2D<int> > &nlmpt,
00178 bool &resetCurrLandmark, bool &resetNextLandmark);
00179
00180
00181 void getOdometry(float &dx, float &dy);
00182
00183
00184 void executeMotorCommand(int opMode, float st, float sp);
00185
00186
00187 Image<double>
00188 getGroundTruth(uint fNum, uint &snumGT, float <ravGT, float &dx, float &dy);
00189
00190
00191 void reportResults(std::string resultPrefix, uint nsegment);
00192
00193
00194
00195 void terminate(int s)
00196 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); }
00197
00198
00199 int main(const int argc, const char **argv)
00200 {
00201 MYLOGVERB = LOG_INFO;
00202
00203
00204 ModelManager manager("beobot Navigation using Gist and Saliency - Master");
00205
00206
00207
00208
00209
00210
00211 nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00212 manager.addSubComponent(ifs);
00213
00214 nub::ref<BeobotBrainMT> bbmt(new BeobotBrainMT(manager));
00215 manager.addSubComponent(bbmt);
00216
00217 nub::soft_ref<Beowulf>
00218 beo(new Beowulf(manager, "Beowulf Master", "BeowulfMaster", true));
00219 manager.addSubComponent(beo);
00220
00221 manager.exportOptions(MC_RECURSE);
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 if (manager.parseCommandLine(argc, argv,
00232 "<operation mode> <environment file> "
00233 "[TR: number of segments | TS: delay] "
00234 "[current Segment Number | TS: fstart] "
00235 "[test run file prefix] ",
00236 2, 5)
00237 == false) return(1);
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 int nSegment = 0, currSegNum = -1;
00252
00253
00254
00255 std::string envFName = manager.getExtraArg(1);
00256 std::string saveFilePath = "";
00257 std::string::size_type lspos = envFName.find_last_of('/');
00258 int ldpos = envFName.find_last_of('.');
00259 std::string envPrefix;
00260 if(lspos != std::string::npos)
00261 {
00262 saveFilePath = envFName.substr(0, lspos+1);
00263 envPrefix = envFName.substr(lspos+1, ldpos - lspos - 1);
00264 }
00265 else
00266 envPrefix = envFName.substr(0, ldpos - 1);
00267
00268
00269 int inputType;
00270 std::string testRunFPrefix =
00271 std::string("results_") + getInputPrefix(ifs, inputType);
00272 if(manager.numExtraArgs() > 4)
00273 testRunFPrefix = manager.getExtraArgAs<std::string>(4);
00274
00275 std::string testRunFolder =
00276 saveFilePath + testRunFPrefix + std::string("/");
00277 std::string resultPrefix = testRunFolder + envPrefix;
00278
00279
00280 int opMode = getOpMode(manager.getExtraArg(0));
00281
00282
00283 uint64 inputFrameRate = 0;
00284 uint fstart = 0;
00285
00286
00287 LINFO("opmode: %d", opMode);
00288 if(opMode == TRAIN_MODE)
00289 {
00290 if(manager.numExtraArgs() < 4)
00291 LFATAL("Training needs at least 4 params");
00292 nSegment = manager.getExtraArgAs<int>(2);
00293 currSegNum = manager.getExtraArgAs<int>(3);
00294 LINFO("number of segments: %d", nSegment);
00295 LINFO("current segment: %d", currSegNum);
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 else if(opMode == TEST_MODE)
00314 {
00315 if(manager.numExtraArgs() > 2)
00316 inputFrameRate = manager.getExtraArgAs<uint>(2);
00317
00318 if(manager.numExtraArgs() > 3)
00319 fstart = manager.getExtraArgAs<uint>(3);
00320 }
00321 else LFATAL("invalid opmode");
00322
00323 LINFO("Environment file: %s", envFName.c_str());
00324 LINFO("Save file to this folder: %s", saveFilePath.c_str());
00325 LINFO("envPrefix: %s", envPrefix.c_str());
00326 LINFO("test run prefix: %s", testRunFPrefix.c_str());
00327 LINFO("test run folder: %s", testRunFolder.c_str());
00328 LINFO("result prefix: %s", resultPrefix.c_str());
00329 LINFO("frame frequency: %f ms/frame", inputFrameRate/1000.0F);
00330
00331
00332 if (mkdir(testRunFolder.c_str(), 0777) == -1 && errno != EEXIST)
00333 {
00334 LFATAL("Cannot create log folder: %s", testRunFolder.c_str());
00335 }
00336
00337
00338
00339
00340 int w = ifs->getWidth(), h = ifs->getHeight();
00341 std::string dims = convertToString(Dims(w, h));
00342 LINFO("image size: [%dx%d]", w, h);
00343 manager.setOptionValString(&OPT_InputFrameDims, dims);
00344
00345 manager.setModelParamVal("InputFrameDims", Dims(w, h),
00346 MC_RECURSE | MC_IGNORE_MISSING);
00347
00348 TCPmessage rmsg;
00349 TCPmessage smsg;
00350 int32 rframe = 0, raction = 0, rnode = 0;
00351
00352
00353 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00354 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00355 signal(SIGALRM, terminate);
00356
00357
00358 manager.start();
00359
00360
00361 smsg.reset(0, INIT_COMM);
00362 smsg.addInt32(int32(fstart));
00363 smsg.addInt32(int32(w));
00364 smsg.addInt32(int32(h));
00365 smsg.addInt32(int32(opMode));
00366 smsg.addInt32(int32(nSegment));
00367 smsg.addInt32(int32(currSegNum));
00368 smsg.addString(envFName.c_str());
00369 smsg.addString(testRunFPrefix.c_str());
00370 smsg.addString(saveFilePath.c_str());
00371 smsg.addString(resultPrefix.c_str());
00372
00373
00374 beo->send(DORSAL_NODE, smsg);
00375 beo->send(VENTRAL_NODE, smsg);
00376
00377
00378 Image<float> currSalMap(w >> sml, h >> sml, ZEROS);
00379
00380
00381 Image<PixRGB<byte> > disp(w * 5, h, ZEROS);
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 ImageSet<float> cmap(NUM_CHANNELS);
00393 Point2D<int> lastwin(-1,-1); Point2D<int> lastwinsc(-1,-1);
00394
00395
00396 LINFO("waiting until Ventral and Dorsal is ready to go");
00397 rnode = VENTRAL_NODE;
00398 while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00399 LINFO("%d is ready", rnode);
00400 rnode = DORSAL_NODE;
00401 while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00402 LINFO("%d is ready", rnode);
00403 rmsg.reset(rframe, raction);
00404 LINFO("Ventral and Dorsal are ready to go");
00405 Raster::waitForKey();
00406
00407
00408 std::string resFName = resultPrefix + sformat("_M_results.txt");
00409
00410
00411 bool resetCurrLandmark = false;
00412 std::vector<Point2D<int> > clmpt;
00413 bool resetNextLandmark = true;
00414 std::vector<Point2D<int> > nlmpt;
00415
00416 float sp = 0.0f; float st = 0.0f;
00417
00418 Timer inputTimer(1000000); uint64 t[NAVG]; float frate = 0.0f;
00419 Timer ventralTimer(1000000); Timer dorsalTimer(1000000);
00420 float vTime = -1.0F; float dTime = -1.0F;
00421
00422
00423 bool dorsalReplied = false; std::vector<float> procTime;
00424 bool resetVentralSearch = false;
00425 bool stopSent = false;
00426 Image<PixRGB<byte> > ima; int32 fNum = fstart;
00427 Image<PixRGB<byte> > prevIma; inputTimer.reset();
00428 ifs->updateNext(); ima = ifs->readRGB();
00429 if(!ima.initialized()) { goforever = false; }
00430 else { bbmt->input(ima);}
00431 while(goforever || !dorsalReplied)
00432 {
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 rnode = DORSAL_NODE; int32 rDframe;
00448 if(checkNode(opMode, beo, rnode, rmsg, raction, rDframe))
00449 {
00450 LINFO("IN DORSAL: %d", rDframe);
00451 processDorsalResult(rmsg, raction, rframe, clmpt, nlmpt,
00452 resetNextLandmark, sp, st);
00453 dTime = dorsalTimer.get()/1000.0F;
00454 LDEBUG("Dorsal time[%d]: %f", rDframe, dTime);
00455 dorsalReplied = true;
00456 }
00457
00458
00459
00460 rnode = VENTRAL_NODE; int32 rVframe;
00461 if(dorsalReplied &&
00462 checkNode(opMode, beo, rnode, rmsg, raction, rVframe))
00463 {
00464 processVentralResult(rmsg, raction, rframe, clmpt, nlmpt,
00465 resetCurrLandmark, resetNextLandmark);
00466 LINFO("IN VENTRAL: %d ::: %d", rVframe, resetNextLandmark);
00467
00468 vTime = ventralTimer.get()/1000.0F;
00469 LDEBUG("Ventral time[%d]: %f", rVframe, vTime);
00470 stopSent = false;
00471 }
00472
00473
00474 float dx, dy; getOdometry(dx, dy);
00475
00476
00477 executeMotorCommand(opMode, sp, st);
00478
00479
00480
00481 uint64 inputTime = inputTimer.get();
00482 if(checkInput(opMode, resetNextLandmark, inputFrameRate, inputTime)
00483 && bbmt->outputReady())
00484 {
00485 t[fNum % NAVG] = inputTime; inputTimer.reset();
00486 Timer t1(1000000); t1.reset();
00487 float bbmtTime = bbmt->getProcessTime();
00488 rframe = fNum;
00489
00490
00491 Image<PixRGB<byte> > currIma; Image<float> currSalMap;
00492 Image<double> cgist;
00493 std::vector<Point2D<int> > salpt;
00494 std::vector<std::vector<double> >feat;
00495 std::vector<Rectangle> objRect;
00496 getBbmtResults(bbmt, currIma, cgist, currSalMap,
00497 cmap, salpt, feat, objRect);
00498
00499
00500 FrameState fState = ifs->updateNext(); ima = ifs->readRGB();
00501 if(!ima.initialized() || fState == FRAME_COMPLETE)
00502 { goforever = false; }
00503 else bbmt->input(ima);
00504 uint snumGT = 0; float ltravGT = -1.0F;
00505
00506
00507 if(opMode == TEST_MODE)
00508 {
00509 getGroundTruth(fNum, snumGT, ltravGT, dx, dy);
00510
00511 LINFO("GT: (%d, %f) d (%f, %f)", snumGT, ltravGT, dx, dy);
00512 }
00513
00514
00515
00516 if(resetNextLandmark) ventralTimer.reset();
00517 if(inputFrameRate != 0 &&
00518 ventralTimer.get() > (inputFrameRate * SEARCH_TIME_LIMIT) &&
00519 !stopSent && sp != 0.0)
00520 {
00521 resetVentralSearch = true;
00522 LINFO("ventral time: %f ms. STOP", ventralTimer.get()/1000.0f);
00523 stopSent = true;
00524 }
00525
00526
00527 setupVentralPacket
00528 (smsg, rframe, resetVentralSearch, currIma,
00529 cgist, salpt, feat, objRect, dx, dy, snumGT, ltravGT);
00530 beo->send(VENTRAL_NODE, smsg);
00531 resetVentralSearch = false;
00532 LINFO("send ventral done");
00533
00534
00535 setupDorsalPacket(smsg, rframe, cmap,
00536 resetCurrLandmark, clmpt,
00537 resetNextLandmark, salpt);
00538 beo->send(DORSAL_NODE, smsg);
00539 dorsalTimer.reset();
00540 dorsalReplied = false;
00541
00542
00543 bool rCL = resetCurrLandmark;
00544 bool rNL = resetNextLandmark;
00545 if(resetCurrLandmark) resetCurrLandmark = false;
00546 if(resetNextLandmark) resetNextLandmark = false;
00547
00548
00549
00550
00551
00552 prevIma = currIma;
00553
00554
00555
00556 float iTime = t[fNum % NAVG]/1000.0F;
00557 FILE *rFile = fopen(resFName.c_str(), "at");
00558 if (rFile != NULL)
00559 {
00560
00561 float dispVTime = vTime;
00562 if(!rNL) dispVTime = ventralTimer.get()/1000.0F;
00563
00564 std::string line =
00565 sformat("%5d %11.5f %d %d %11.5f %11.5f %11.5f",
00566 fNum, bbmtTime, rCL, rNL, dTime, dispVTime, iTime);
00567 LINFO("%s", line.c_str());
00568 line += std::string("\n");
00569 fputs(line.c_str(), rFile);
00570 fclose (rFile);
00571 }
00572 else LFATAL("can't create result file: %s", resFName.c_str());
00573
00574 fNum++;
00575 if (fNum % NAVG == 0 && fNum > 0)
00576 {
00577 uint64 sum = 0ULL; for(int i = 0; i < NAVG; i ++) sum += t[i];
00578 frate = 1000000.0F / float(sum) * float(NAVG);
00579 LINFO("Fr: %6.3f fps -> %8.3f ms/f\n", frate, 1000.0/frate);
00580 }
00581 uint64 ptime = t1.get(); float ptime2 = ptime/1000.0;
00582 procTime.push_back(ptime2);
00583 LDEBUG("total proc time: %f", ptime2);
00584 t1.reset();
00585 }
00586
00587 }
00588
00589
00590 uint64 inputTime = inputTimer.get();
00591 while(!checkInput(opMode, resetNextLandmark, inputFrameRate, inputTime))
00592 {
00593 rnode = VENTRAL_NODE; int32 rVframe;
00594 if(checkNode(opMode, beo, rnode, rmsg, raction, rVframe))
00595 {
00596 processVentralResult(rmsg, raction, rframe, clmpt, nlmpt,
00597 resetCurrLandmark, resetNextLandmark);
00598 vTime = ventralTimer.get()/1000.0F;
00599 LINFO("Ventral time[%d]: %f", rVframe, vTime);
00600 }
00601
00602 usleep(1000);
00603 inputTime = inputTimer.get();
00604 }
00605
00606
00607 FILE *rFile = fopen(resFName.c_str(), "at");
00608 if (rFile != NULL)
00609 {
00610 float dispVTime = vTime;
00611 if(!resetNextLandmark) dispVTime = ventralTimer.get()/1000.0F;
00612
00613 std::string line =
00614 sformat("%5d %11.5f %d %d %11.5f %11.5f %11.5f",
00615 fNum, -1.0F, resetCurrLandmark, resetNextLandmark,
00616 dTime, dispVTime, inputTime/1000.0);
00617 LINFO("%s", line.c_str());
00618 line += std::string("\n");
00619 fputs(line.c_str(), rFile);
00620 fclose (rFile);
00621 }
00622 else LFATAL("can't create result file: %s", resFName.c_str());
00623
00624
00625 LINFO("sending ABORT to Dorsal");
00626 rframe = fNum;
00627 smsg.reset(rframe, ABORT);
00628 beo->send(DORSAL_NODE, smsg);
00629
00630
00631
00632
00633
00634 LINFO("sending ABORT to ventral");
00635 beo->send(VENTRAL_NODE, smsg);
00636 rnode = VENTRAL_NODE;
00637 while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00638 LINFO("rec Ventral");
00639 rmsg.reset(rframe,raction);
00640
00641
00642
00643 switch(opMode)
00644 {
00645 case TRAIN_MODE: break;
00646
00647 case TEST_MODE:
00648 {
00649
00650 reportResults(resultPrefix, 9);
00651
00652 float min = 0.0f, max = 0.0f;
00653 if(procTime.size() > 0){ min = procTime[0]; max = procTime[0]; }
00654 for(uint i = 1; i < procTime.size(); i++)
00655 {
00656 if (min > procTime[i]) min = procTime[i];
00657 if (max < procTime[i]) max = procTime[i];
00658 }
00659 LINFO("proc Time: %f - %f", min, max);
00660 }
00661 break;
00662
00663 default: LERROR("Unknown operation mode");
00664 }
00665
00666 LINFO("we can now exit");
00667 Raster::waitForKey();
00668
00669
00670 manager.stop();
00671 return 0;
00672 }
00673
00674
00675 std::string getInputPrefix(nub::soft_ref<InputFrameSeries> ifs,
00676 int &inputType)
00677 {
00678 std::string input =
00679 ifs->getModelParamVal<std::string>("InputFrameSource");
00680 std::string prefix;
00681
00682
00683 std::string camInput("ieee1394");
00684 if(!input.compare(camInput))
00685 {
00686 inputType = CAMERA_INPUT;
00687
00688 time_t rawtime; struct tm * timeinfo;
00689 time ( &rawtime );
00690 timeinfo = localtime ( &rawtime );
00691 std::string cTime(asctime (timeinfo));
00692 cTime = cTime.substr(4);
00693
00694 for(uint i = 0; i < cTime.length(); i++)
00695 if(cTime[i] == ' ' || cTime[i] == ':') cTime[i] = '_';
00696 prefix = std::string("cam_") + cTime;
00697 LINFO("Camera input, prefix: %s", prefix.c_str());
00698 }
00699
00700 else if(input.find_last_of('#') == (input.length() - 5))
00701 {
00702 inputType = FILE_INPUT;
00703
00704 prefix = input.substr(0, input.length() - 5);
00705 std::string::size_type spos = prefix.find_last_of('/');
00706 if(spos != std::string::npos)
00707 prefix = prefix.substr(spos+1);
00708
00709
00710 if(prefix.find_last_of('_') == prefix.length() - 1)
00711 {
00712
00713 prefix = prefix.substr(0, prefix.length() - 1);
00714 }
00715
00716 LINFO("Frame series input, prefix: %s", prefix.c_str());
00717 }
00718
00719 else
00720 {
00721 inputType = FILE_INPUT;
00722
00723 prefix = input.substr(0, input.length() - 4);
00724 std::string::size_type spos = prefix.find_last_of('/');
00725 if(spos != std::string::npos)
00726 prefix = prefix.substr(spos+1);
00727
00728 LINFO("mpeg Input, prefix: %s", prefix.c_str());
00729 }
00730
00731 return prefix;
00732 }
00733
00734
00735 int getOpMode(std::string opmodestr)
00736 {
00737
00738 std::string trainMode("train"); int op;
00739
00740 if(!opmodestr.compare(trainMode)) op = TRAIN_MODE;
00741
00742 else op = TEST_MODE;
00743 return op;
00744 }
00745
00746
00747 void setupBeoChip(nub::soft_ref<BeoChip> b, BeobotConfig bbc)
00748 {
00749
00750 LINFO("Resetting BeoChip...");
00751 b->resetChip(); sleep(1);
00752
00753
00754 b->calibrateServo(bbc.steerServoNum, bbc.steerNeutralVal,
00755 bbc.steerMinVal, bbc.steerMaxVal);
00756 b->calibrateServo(bbc.speedServoNum, bbc.speedNeutralVal,
00757 bbc.speedMinVal, bbc.speedMaxVal);
00758 b->calibrateServo(bbc.gearServoNum, bbc.gearNeutralVal,
00759 bbc.gearMinVal, bbc.gearMaxVal);
00760
00761
00762 b->setServoRaw(bbc.gearServoNum, bbc.gearMinVal);
00763
00764
00765
00766
00767
00768 b->debounceKeyboard(true);
00769 b->captureKeyboard(true);
00770
00771
00772 b->calibratePulse(0,
00773 bbc.pwm0NeutralVal,
00774 bbc.pwm0MinVal,
00775 bbc.pwm0MaxVal);
00776 b->calibratePulse(1,
00777 bbc.pwm1NeutralVal,
00778 bbc.pwm1MinVal,
00779 bbc.pwm1MaxVal);
00780 b->capturePulse(0, true);
00781 b->capturePulse(1, true);
00782
00783
00784 b->lcdClear();
00785 b->lcdPrintf(0, 0, "collectFrames: 00000");
00786 b->lcdPrintf(0, 1, "STEER=XXX SPEED=XXX");
00787 b->lcdPrintf(0, 2, "PWM0=0000 0000-0000");
00788 b->lcdPrintf(0, 3, "PWM1=0000 0000-0000");
00789 }
00790
00791
00792 void getOdometry(float &dx, float &dy)
00793 {
00794 dx = 0.0F;
00795 dy = 0.0F;
00796 }
00797
00798
00799 void executeMotorCommand(int opMode, float st, float sp)
00800 {
00801 switch(opMode)
00802 {
00803 case TRAIN_MODE:
00804 case TEST_MODE:
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 break;
00815 default:
00816 LERROR("Unknown operation mode");
00817 }
00818 }
00819
00820
00821 void dispResults
00822 ( Image< PixRGB<byte> > disp, rutz::shared_ptr<XWinManaged> win,
00823 Image< PixRGB<byte> > ima, Image< PixRGB<byte> > prevIma,
00824 std::vector<Point2D<int> > clmpt, std::vector<Point2D<int> > nlmpt,
00825 Image<float> currSalMap,
00826 std::vector<Point2D<int> > salpt, std::vector<Rectangle> objRect)
00827 {
00828 int w = ima.getWidth();
00829 int h = ima.getHeight();
00830 const int foa_size = std::min(w, h) / 12;
00831
00832
00833 inplacePaste(disp, ima, Point2D<int>(0, 0));
00834
00835
00836 Image<float> dispsm = quickInterpolate(currSalMap * SMFAC, 1 << sml);
00837 inplaceNormalize(dispsm, 0.0f, 255.0f);
00838 Image<PixRGB<byte> > sacImg = Image<PixRGB<byte> >(toRGB(dispsm));
00839 for(uint i = 0; i < objRect.size(); i++)
00840 drawRect(sacImg,objRect[i],PixRGB<byte>(255,0,0));
00841 inplacePaste(disp, sacImg, Point2D<int>(w, 0));
00842
00843
00844 for(uint i = 0; i < salpt.size(); i++)
00845 {
00846 Point2D<int> salpt2(salpt[i].i + w, salpt[i].j);
00847 drawDisk(disp, salpt[i], foa_size/6+2, PixRGB<byte>(20, 50, 255));
00848 drawDisk(disp, salpt[i], foa_size/6, PixRGB<byte>(255, 255, 20));
00849 drawDisk(disp, salpt2, foa_size/6+2, PixRGB<byte>(20, 50, 255));
00850 drawDisk(disp, salpt2, foa_size/6, PixRGB<byte>(255, 255, 20));
00851 }
00852
00853
00854 Image< PixRGB<byte> > roiImg = ima;
00855 for(uint i = 0; i < objRect.size(); i++)
00856 {
00857 drawRect(roiImg, objRect[i], PixRGB<byte>(255,255,0));
00858 drawDisk(roiImg, salpt[i], 3, PixRGB<byte>(255,0,0));
00859 std::string ntext(sformat("%d", i));
00860 writeText(roiImg, salpt[i], ntext.c_str());
00861 }
00862 inplacePaste(disp, roiImg, Point2D<int>(w*2, 0));
00863
00864
00865 if(prevIma.initialized())
00866 {
00867
00868 Image< PixRGB<byte> > clmDisp = prevIma;
00869 for(uint i = 0; i < clmpt.size(); i++)
00870 {
00871 if(clmpt[i].isValid())
00872 {
00873 drawDisk(clmDisp, clmpt[i], 3, PixRGB<byte>(0,255,0));
00874 std::string ntext(sformat("%d", i));
00875 writeText(clmDisp, clmpt[i], ntext.c_str());
00876 }
00877 }
00878 inplacePaste(disp, clmDisp, Point2D<int>(w*3, 0));
00879
00880
00881 Image< PixRGB<byte> > nlmDisp = prevIma;
00882 for(uint i = 0; i < nlmpt.size(); i++)
00883 {
00884 if(nlmpt[i].isValid())
00885 {
00886 drawDisk(nlmDisp, nlmpt[i], 3, PixRGB<byte>(0,255,255));
00887 std::string ntext(sformat("%d", i));
00888 writeText(nlmDisp, nlmpt[i], ntext.c_str());
00889 }
00890 }
00891 inplacePaste(disp, nlmDisp, Point2D<int>(w*4, 0));
00892 }
00893
00894
00895 win->drawImage(disp,0,0);
00896
00897 }
00898
00899
00900 int beoChipProc(rutz::shared_ptr<BeobotBeoChipListener> lis,
00901 nub::soft_ref<BeoChip> b)
00902 {
00903
00904 char kb[6]; kb[5] = '\0';
00905 for (int i = 0; i < 5; i ++) kb[i] = (lis->kbd>>(4-i))&1 ? '1':'0';
00906
00907
00908 if (kb[0] == '0' && kb[4] == '0') {
00909 b->lcdPrintf(15, 0, "QUIT ");
00910
00911
00912 return BC_QUIT_SIGNAL;
00913 }
00914
00915 return BC_NO_SIGNAL;
00916 }
00917
00918
00919 void getBbmtResults
00920 ( nub::ref<BeobotBrainMT> bbmt,
00921 Image<PixRGB<byte> > &currIma, Image<double> &cgist,
00922 Image<float> &currSalMap, ImageSet<float> &cmap,
00923 std::vector<Point2D<int> > &salpt, std::vector<std::vector<double> > &feat,
00924 std::vector<Rectangle> &objRect)
00925 {
00926
00927 currIma = bbmt->getCurrImage();
00928 cgist = bbmt->getGist();
00929 currSalMap = bbmt->getSalMap();
00930
00931
00932 for(uint i = 0; i < NUM_CHANNELS; i++) cmap[i] = bbmt->getCurrCMap(i);
00933
00934 salpt.clear(); objRect.clear();
00935 uint numpt = bbmt->getNumSalPoint();
00936 for(uint i = 0; i < numpt; i++)
00937 {
00938 salpt.push_back(bbmt->getSalPoint(i));
00939 objRect.push_back(bbmt->getObjRect(i));
00940
00941 std::vector<double> features; bbmt->getSalientFeatures(i, features);
00942 feat.push_back(features);
00943
00944 }
00945 }
00946
00947
00948 bool checkNode
00949 ( int opMode, nub::soft_ref<Beowulf> beo,
00950 int32 &rnode, TCPmessage &rmsg, int32 &raction, int32 &rframe)
00951 {
00952
00953 if(beo->receive(rnode, rmsg, rframe, raction)) return true;
00954
00955 return false;
00956 }
00957
00958
00959 bool checkInput(int opMode, bool resetNextLandmark, uint64 inputFrameRate,
00960 uint64 inputTime)
00961 {
00962
00963 if(opMode == TRAIN_MODE && resetNextLandmark) return true;
00964
00965
00966 if(opMode == TEST_MODE && inputFrameRate == 0 &&
00967 resetNextLandmark) return true;
00968
00969
00970 if(opMode == TEST_MODE && inputFrameRate != 0 &&
00971 (inputFrameRate - ERR_INTERVAL) < inputTime) return true;
00972
00973 return false;
00974 }
00975
00976
00977 void setupVentralPacket
00978 ( TCPmessage &smsg, int rframe,
00979 bool resetVentralSearch, Image< PixRGB<byte> > currIma,
00980 Image<double> cgist, std::vector<Point2D<int> > salpt,
00981 std::vector<std::vector<double> > feat, std::vector<Rectangle> objRect,
00982 float dx, float dy, uint snumGT, float ltravGT)
00983 {
00984
00985
00986
00987
00988
00989
00990 smsg.reset(rframe, SEARCH_LM);
00991 smsg.addInt32(int32(resetVentralSearch));
00992
00993
00994 smsg.addImage(currIma);
00995
00996
00997 smsg.addInt32(int32(cgist.getSize()));
00998 Image<double>::iterator aptr = cgist.beginw();
00999 for(int i = 0; i < cgist.getSize(); i++) smsg.addDouble(*aptr++);
01000
01001
01002 smsg.addInt32(int32(salpt.size()));
01003 for(uint i = 0; i < salpt.size(); i++)
01004 {
01005
01006 smsg.addInt32(int32(salpt[i].i));
01007 smsg.addInt32(int32(salpt[i].j));
01008
01009
01010 smsg.addInt32(int32(objRect[i].top()));
01011 smsg.addInt32(int32(objRect[i].left()));
01012 smsg.addInt32(int32(objRect[i].bottomO()));
01013 smsg.addInt32(int32(objRect[i].rightO()));
01014 smsg.addInt32(int32(feat[i].size()));
01015 LDEBUG("[%u] salpt:(%s) r:[%s]", i,
01016 convertToString(salpt[i]).c_str(),
01017 convertToString(objRect[i]).c_str());
01018 for(uint j = 0; j < feat[i].size(); j++)
01019 smsg.addDouble(feat[i][j]);
01020 }
01021
01022
01023 smsg.addFloat(dx);
01024 smsg.addFloat(dy);
01025
01026
01027 smsg.addInt32(int32(snumGT));
01028 smsg.addFloat(ltravGT);
01029
01030 LINFO("setup VENTRAL %d", rframe );
01031 }
01032
01033
01034 void setupDorsalPacket
01035 ( TCPmessage &smsg, int rframe, ImageSet<float> cmap,
01036 bool resetCurrLandmark, std::vector<Point2D<int> > clmpt,
01037 bool resetNextLandmark, std::vector<Point2D<int> > nlmpt)
01038 {
01039
01040 smsg.reset(rframe, TRACK_LM);
01041 smsg.addImageSet(cmap);
01042 smsg.addInt32(int32(resetCurrLandmark));
01043 smsg.addInt32(int32(resetNextLandmark));
01044 smsg.addInt32(int32(clmpt.size()));
01045 LDEBUG("[%4d] reset currLM? %d nextLM? %d",
01046 rframe, resetCurrLandmark, resetNextLandmark);
01047
01048 for(uint i = 0; i < clmpt.size(); i++)
01049 {
01050 smsg.addInt32(int32(clmpt[i].i));
01051 smsg.addInt32(int32(clmpt[i].j));
01052 LDEBUG("curr[%d]: (%3d, %3d)", i, clmpt[i].i, clmpt[i].j);
01053 }
01054
01055 smsg.addInt32(int32(nlmpt.size()));
01056 for(uint i = 0; i < nlmpt.size(); i++)
01057 {
01058 smsg.addInt32(int32(nlmpt[i].i));
01059 smsg.addInt32(int32(nlmpt[i].j));
01060 LDEBUG("next[%d]: (%3d, %3d)", i, nlmpt[i].i, nlmpt[i].j);
01061 }
01062 }
01063
01064
01065 void processVentralResult
01066 ( TCPmessage &rmsg, int32 raction, int32 rframe,
01067 std::vector<Point2D<int> > &clmpt, std::vector<Point2D<int> > &nlmpt,
01068 bool &resetCurrLandmark, bool &resetNextLandmark)
01069 {
01070 if(raction == SEARCH_LM_RES)
01071 {
01072 LDEBUG("Packet size: %d", rmsg.getSize());
01073 bool isLocalized = true;
01074 int searchFrame = int(rmsg.getElementInt32());
01075 int sentAt = int(rmsg.getElementInt32());
01076 rmsg.reset(rframe, raction);
01077 LINFO("Ventral processing[%d]: SEARCH_LM_RES: %d sent at %d\n",
01078 rframe, searchFrame, sentAt);
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102 if(isLocalized)
01103 {
01104
01105 clmpt.clear();
01106 for(uint i = 0; i < nlmpt.size(); i++)
01107 clmpt.push_back(nlmpt[i]);
01108 resetCurrLandmark = true;
01109
01110 }
01111 else
01112 {
01113
01114
01115 }
01116 resetNextLandmark = true;
01117 }
01118 }
01119
01120
01121 void processDorsalResult
01122 (TCPmessage &rmsg, int32 raction, int32 rframe,
01123 std::vector<Point2D<int> > &clmpt, std::vector<Point2D<int> > &nlmpt,
01124 bool &resetNextLandmark, float &sp, float &st)
01125 {
01126 if(raction == TRACK_LM_RES)
01127 {
01128 LDEBUG("Dorsal TRACK_LM_RES[%4d]", rframe);
01129 uint nClmpt = (rmsg.getElementInt32());
01130 clmpt.clear();
01131 for(uint i = 0; i < nClmpt; i++)
01132 {
01133 uint cI = int(rmsg.getElementInt32());
01134 uint cJ = int(rmsg.getElementInt32());
01135 clmpt.push_back(Point2D<int>(cI,cJ));
01136 if(clmpt[i].isValid())
01137 LDEBUG("track currLM[%d]: (%3d, %3d)",
01138 i, clmpt[i].i, clmpt[i].j);
01139 else
01140 LINFO("track currLM[%d]: (%3d, %3d) LOST",
01141 i, clmpt[i].i, clmpt[i].j);
01142 }
01143
01144 uint nNlmpt = (rmsg.getElementInt32());
01145 nlmpt.clear();
01146 for(uint i = 0; i < nNlmpt; i++)
01147 {
01148 uint nI = int(rmsg.getElementInt32());
01149 uint nJ = int(rmsg.getElementInt32());
01150 nlmpt.push_back(Point2D<int>(nI,nJ));
01151 if(nlmpt[i].isValid())
01152 LDEBUG("track nextLM[%d]: (%3d, %3d)",
01153 i, nlmpt[i].i, nlmpt[i].j);
01154 else
01155 LINFO("track nextLM[%d]: (%3d, %3d) LOST",
01156 i, nlmpt[i].i, nlmpt[i].j);
01157 }
01158 rmsg.reset(rframe, raction);
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197 }
01198 }
01199
01200
01201 Image<double>
01202 getGroundTruth(uint fNum, uint &snumGT, float <ravGT, float &dx, float &dy)
01203 {
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294 std::string stem("../data/HNB_T/gist/");
01295 uint sfnum [4] = { 655, 653, 521, 353 };
01296 uint ssfnum[4] = { 0, 655, 1308, 1829 };
01297
01298
01299
01300 int afnum = 0;
01301
01302 for(uint i = 0; i < 4; i++)
01303 {
01304 if((fNum >= ssfnum[i]) && (fNum < ssfnum[i]+sfnum[i]))
01305 {
01306 stem = stem + sformat("_T_%dE", i+1);
01307 afnum = fNum - ssfnum[i];
01308 dx = 1.0/float(sfnum[i]); dy = 0.0F;
01309 snumGT = i; ltravGT = float(afnum)/float(sfnum[i]);
01310 break;
01311 }
01312 }
01313
01314
01315
01316
01317
01318 Image<double> cgist(1, 714, NO_INIT);
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344 dy =+ 0.0F;
01345
01346 return cgist;
01347 }
01348
01349
01350 void reportResults(std::string resultPrefix, uint nsegment)
01351 {
01352
01353
01354
01355
01356
01357
01358
01359 GSnav_M_Result r2; r2.read(resultPrefix);
01360
01361
01362
01363
01364
01365 LINFO("bbmtTime: %f (%f - %f) std: %f",
01366 r2.meanBbmtTime, r2.minBbmtTime, r2.maxBbmtTime,
01367 r2.stdevBbmtTime);
01368
01369 LINFO("Dorsal Time: %f (%f - %f) std: %f",
01370 r2.meanDorsalTime, r2.minDorsalTime, r2.maxDorsalTime,
01371 r2.stdevDorsalTime);
01372
01373 LINFO("Ventral Time: %f (%f - %f) std: %f",
01374 r2.meanVentralSearchTime, r2.minVentralSearchTime,
01375 r2.maxVentralSearchTime, r2.stdevVentralSearchTime);
01376
01377 LINFO("Input Time: %f (%f - %f) std: %f",
01378 r2.meanInputTime, r2.minInputTime, r2.maxInputTime,
01379 r2.stdevInputTime);
01380
01381
01382 }
01383
01384
01385
01386
01387
01388