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 #include "Beowulf/Beowulf.H"
00040 #include "Beowulf/BeowulfOpts.H"
00041 #include "Component/ModelManager.H"
00042 #include "Devices/DeviceOpts.H"
00043 #include "Devices/FrameGrabberConfigurator.H"
00044 #include "Devices/KeyBoard.H"
00045 #include "Devices/RadioDecoder.H"
00046 #include "Devices/lcd.H"
00047 #include "Gist/FFN.H"
00048 #include "Image/FFTWWrapper.H"
00049 #include "Image/Image.H"
00050 #include "Image/Pixels.H"
00051 #include "Transport/FrameIstream.H"
00052 #include "Util/Timer.H"
00053 #include "Util/log.H"
00054
00055 #include <math.h>
00056 #include <pthread.h>
00057 #include <signal.h>
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060
00061 #define OUTPATH "/home/tmp/7/gistNav/"
00062
00063
00064 char dir[10];
00065
00066
00067 bool doWrite;
00068
00069
00070 bool grabOk;
00071
00072
00073 bool doQuit;
00074
00075
00076 bool quitGrab;
00077
00078
00079 int frame=0;
00080
00081
00082 static bool goforever = true;
00083
00084
00085
00086 void terminate(int s)
00087 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); }
00088
00089 #define HBLOCKS 4
00090 #define WBLOCKS 4
00091 #define DIMSINALL 640
00092 #define DIMSIN 40
00093 #define DIMSHID 90
00094 #define DIMS1HID 40
00095 #define DIMS2HID 40
00096 #define DIMSOUT 13
00097 #define IMAGEHEIGHT 120
00098 #define IMAGEWIDTH 160
00099 #define IMAGESIZE IMAGEHEIGHT*IMAGEWIDTH
00100 #define WINDOWHEIGHT 30
00101 #define WINDOWWIDTH 40
00102 #define WINDOWSIZE WINDOWHEIGHT*WINDOWWIDTH
00103 #define LEARNRATE 0.5
00104 #define MOMENTUM 0.0
00105
00106 double compsteer, compspeed;
00107 Image< PixRGB<byte> > ima(IMAGEWIDTH,IMAGEHEIGHT, NO_INIT);
00108
00109
00110 void gzWriteFrame(int frameNum, Image< PixRGB<byte> >& imag)
00111 {
00112 char temp[256];
00113 char head[256];
00114
00115 head[0] = '\0';
00116 temp[0] = '\0';
00117
00118
00119 FILE *fp;
00120
00121
00122 float taccel, tsteer;
00123 taccel = compspeed;
00124 tsteer = compsteer;
00125
00126
00127 sprintf(temp, "./frame%06d.ppm", frameNum);
00128 LDEBUG("Filename: %s", temp);
00129
00130
00131
00132 fp = fopen(temp, "wb");
00133
00134
00135 if( fp == NULL )
00136 LFATAL("There was an error opening the gzip file for writing");
00137
00138
00139 sprintf(temp, "P6\n");
00140
00141 strcpy(head, temp);
00142
00143 sprintf(temp, "#ACCEL:%-10f STEER:%-10f\n", taccel, tsteer);
00144 strcat(head, temp);
00145 sprintf(temp, "%d %d\n255\n", imag.getWidth(), imag.getHeight() );
00146 strcat(head, temp);
00147
00148
00149 if( !fwrite(head, 1, strlen(head), fp) )
00150 LFATAL("There was an error gzwriting %"ZU" bytes", strlen(head));
00151
00152 if( !fwrite(reinterpret_cast<void*>(imag.getArrayPtr()), 1,
00153 3*imag.getSize(), fp))
00154
00155
00156 LFATAL("There was an error gzwriting %d bytes", 3*imag.getSize());
00157
00158
00159 fclose(fp);
00160 }
00161
00162
00163 void* imageWriterThread(void *junk)
00164 {
00165
00166 if (doWrite)
00167 {
00168 Image< PixRGB<byte> > currFrame = ima;
00169
00170 Image< PixRGB<byte> >::iterator data = currFrame.beginw();
00171 data ++;
00172 grabOk = true;
00173
00174 gzWriteFrame(frame++, currFrame);
00175
00176 doWrite = false;
00177 }
00178 else
00179 usleep(500);
00180
00181
00182
00183
00184 return NULL;
00185 }
00186
00187
00188 void grabCycleInit( KeyBoard& myKB, nub::soft_ref<lcd> lcdDisp,
00189 nub::soft_ref<FrameIstream>& gb )
00190 {
00191 char tmp[100];
00192 int i = 0;
00193
00194
00195
00196
00197
00198 if( chdir( OUTPATH ) )
00199 {
00200 LINFO("There was an error changing to directory: %s", OUTPATH);
00201 lcdDisp->clear();
00202 lcdDisp->printf(0, 0, " Output directory" );
00203 lcdDisp->printf(0, 1, " Does not exist!" );
00204 lcdDisp->printf(0, 2, " Trying to create. ");
00205 sleep(1);
00206
00207 if( mkdir( OUTPATH, S_IRUSR | S_IWUSR ) != 0 )
00208 {
00209 LINFO("There was an error creating directory: %s", OUTPATH);
00210 lcdDisp->clear();
00211 lcdDisp->printf(0, 0, " mkdir() failed! ");
00212 lcdDisp->printf(0, 1, " bailing out... ");
00213 sleep(1);
00214 return;
00215 }
00216 if (chdir( OUTPATH )) LFATAL("chdir error");
00217 }
00218
00219 i = 0;
00220 do
00221 {
00222 sprintf( tmp, "%d", i++ );
00223 }
00224 while ( mkdir(tmp, S_IRUSR | S_IWUSR | S_IEXEC ) != 0 );
00225 sprintf( tmp, "%s%d", OUTPATH, i-1 );
00226 if (chdir( tmp )) LFATAL("chdir error");
00227
00228 char * ptr = getcwd( NULL, 0 );
00229 LDEBUG( "Working directory is %s", ptr);
00230 free( ptr );
00231
00232 frame = 0; doQuit = false;
00233 doWrite = false; grabOk = true; quitGrab = false;
00234
00235
00236
00237 }
00238
00239
00240 void grabCycle( KeyBoard& myKB, nub::soft_ref<lcd> lcdDisp,
00241 nub::soft_ref<FrameIstream>& gb )
00242 {
00243
00244 while (doWrite) usleep(500);
00245 grabOk = false;
00246 doWrite = true;
00247 imageWriterThread(NULL);
00248 while(grabOk == false) usleep(500);
00249 }
00250
00251
00252 int main(const int argc, const char **argv)
00253 {
00254 int i, j, h, l;
00255 bool updateweights = false;
00256 double steer, speed;
00257 double rcspeed, rcsteer;
00258
00259
00260
00261
00262 srand((unsigned)time(0));
00263
00264 int flag=0;
00265 long ctr=0;
00266 double total,mean,std;
00267
00268
00269
00270
00271 Image<double> trueout(1, DIMSOUT, ZEROS);
00272 double masks[DIMSIN][WINDOWSIZE];
00273 Image<double> meanarr(1, DIMSIN, ZEROS);
00274 Image<double> stdarr(1, DIMSIN,ZEROS);
00275
00276
00277
00278 double evecs[DIMSIN][DIMSINALL];
00279 Image<double> allfeatures(1, DIMSINALL, ZEROS);
00280 Image<double> features(1, DIMSIN, ZEROS);
00281
00282 double **magspec = (double **)malloc((WINDOWHEIGHT)*sizeof(double *));
00283 if(magspec == NULL)
00284 LINFO("Error allocating memory for magspec");
00285 for(i = 0; i < WINDOWHEIGHT; i++)
00286 {
00287 magspec[i] = (double *)malloc((WINDOWWIDTH/2+1)*sizeof(double));
00288 if(magspec[i] == NULL)
00289 LINFO("Error allocating memory for magspec[i]");
00290 }
00291
00292 double *bwimage = (double *)malloc(IMAGESIZE*sizeof(double));
00293 if(bwimage == NULL)
00294 LINFO("Error allocating memory for bwimage");
00295 else
00296 LINFO("okay");
00297
00298 double *bwimage_sm = (double *)malloc(WINDOWSIZE*sizeof(double));
00299
00300 FeedForwardNetwork *ffn_dm = new FeedForwardNetwork();
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 ffn_dm->init3L("/lab/bb/data/nnwh1dm.dat","/lab/bb/data/nnwh2dm.dat",
00330 "/lab/bb/data/nnwodm.dat",DIMSIN,DIMS1HID,DIMS2HID,DIMSOUT,LEARNRATE,0.0);
00331
00332 FFTWWrapper *fftw = new FFTWWrapper( WINDOWWIDTH,WINDOWHEIGHT);
00333 fftw->init(bwimage_sm);
00334
00335 FILE *fp=fopen("/lab/bb/data/meanarrall.mat","rb");
00336 if(fp==NULL) LFATAL("Mean file not found");
00337
00338 fseek(fp, 0, SEEK_END);
00339 long pos = ftell(fp);
00340 fseek(fp,pos-(DIMSIN*sizeof(double)), SEEK_SET);
00341
00342
00343 fclose(fp);
00344
00345
00346
00347 fp = fopen("/lab/bb/data/stdarrall.mat","rb");
00348 if(fp == NULL) LFATAL("STD file not found");
00349 fseek(fp, 0, SEEK_END);
00350 pos=ftell(fp);
00351 fseek(fp,pos-(DIMSIN*sizeof(double)), SEEK_SET);
00352
00353
00354 fclose(fp);
00355
00356
00357
00358
00359 fp = fopen("/lab/bb/data/G2small.mat","rb");
00360
00361
00362 if(fp == NULL) LFATAL("Mask file not found");
00363 fseek(fp, 0, SEEK_END);
00364 pos = ftell(fp);
00365 fseek(fp,pos-(DIMSIN*WINDOWSIZE*sizeof(double)), SEEK_SET);
00366 for(i = 0; i < DIMSIN; i++)
00367 if (fread(masks[i],sizeof(double),WINDOWSIZE,fp) != WINDOWSIZE) LFATAL("fread error");
00368 fclose(fp);
00369
00370 fp=fopen("/lab/bb/data/evec40all.mat","rb");
00371 if(fp==NULL) LFATAL("Eigenvector file not found");
00372 fseek(fp, 0, SEEK_END);
00373 pos=ftell(fp);
00374 fseek(fp,pos-(DIMSIN*DIMSINALL*sizeof(double)), SEEK_SET);
00375 for(i=0;i<DIMSIN;i++)
00376 if (fread(evecs[i],sizeof(double),DIMSINALL,fp) != DIMSINALL) LFATAL("fread error");
00377 fclose(fp);
00378
00379 double *iptr = NULL;
00380
00381 TCPmessage smsg;
00382
00383
00384
00385 KeyBoard myKB;
00386 LINFO("Press A to do initialization");
00387
00388 while (myKB.getKey( true ) != KBD_KEY1)
00389
00390
00391 LINFO("\a");
00392
00393
00394 ModelManager manager("Gist Navigator - Master");
00395
00396
00397 nub::soft_ref<FrameGrabberConfigurator>
00398 gbc(new FrameGrabberConfigurator(manager));
00399 manager.addSubComponent(gbc);
00400
00401
00402 nub::soft_ref<Beowulf>
00403 beo(new Beowulf(manager, "Beowulf Master", "BeowulfMaster", true));
00404 manager.addSubComponent(beo);
00405 manager.setOptionValString(&OPT_BeowulfSlaveNames, "bb1ag");
00406
00407 nub::soft_ref<RadioDecoder> rd(new RadioDecoder(manager));
00408 manager.addSubComponent(rd);
00409
00410 nub::soft_ref<lcd> lcdDisp(new lcd(manager));
00411 lcdDisp->setModelParamString("SerialPortDevName",
00412 "/dev/ttyS0", MC_RECURSE);
00413 manager.addSubComponent(lcdDisp);
00414
00415
00416
00417
00418 manager.setOptionValString(&OPT_FrameGrabberType, "1394");
00419 manager.setOptionValString(&OPT_FrameGrabberDims, "160x120");
00420 manager.setOptionValString(&OPT_FrameGrabberMode, "YUV444");
00421 manager.setOptionValString(&OPT_FrameGrabberNbuf, "20");
00422
00423
00424 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00425
00426 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00427 if (gb.isInvalid())
00428 LFATAL("No grabber. Why does god hate me?");
00429 LINFO("GB width = %d height = %d\n",gb->getWidth(),gb->getHeight());
00430
00431
00432 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00433 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00434 signal(SIGALRM, terminate);
00435
00436
00437 manager.start();
00438
00439
00440 lcdDisp->clear();
00441 lcdDisp->printf(0, 1, "Calibrate Controls");
00442 lcdDisp->printf(0, 2, "Rest Controls NOW!");
00443 rd->zeroCalibrate();
00444
00445
00446 lcdDisp->clear();
00447 lcdDisp->printf(0, 1, "Calibrate Controls");
00448 lcdDisp->printf(0, 2, "Full Swing NOW!");
00449 rd->rangeCalibrate();
00450
00451
00452 manager.saveConfig();
00453
00454 lcdDisp->clear();
00455 lcdDisp->printf(0, 2, "Press 2 (B) to Start");
00456 smsg.reset(ctr, 0);
00457 smsg.addInt32(0);
00458 beo->send(0, smsg);
00459 while (myKB.getKey( true ) != KBD_KEY2)
00460 printf("\a");
00461 lcdDisp->clear();
00462 printf("\a\a\a");
00463
00464 grabCycleInit( myKB, lcdDisp, gb );
00465
00466
00467 Timer tim(1000000);
00468 tim.reset();
00469 uint64 end, start=tim.get();
00470 uint64 endpause, startpause=0, totalpause=0;
00471 double elapsed;
00472 double lastspeed = 0.4;
00473 double totspeed = 0.0;
00474 unsigned long speedctr = 0;
00475 bool setspeed = false;
00476 int keypress = KBD_NONE;
00477 bool paused = false;
00478 while(!flag && goforever)
00479 {
00480
00481
00482 if( myKB.getKey( false ) == KBD_KEY3 || keypress == KBD_KEY3)
00483 {
00484 printf("pause\n");
00485 keypress=KBD_NONE;
00486 usleep(500);
00487 if(paused)
00488 {
00489 paused=false;
00490 endpause=tim.get();
00491 totalpause+=endpause-startpause;
00492 }
00493 else
00494 {
00495 smsg.reset(ctr, 0);
00496
00497 smsg.addInt32(-1);
00498 beo->send(0, smsg);
00499 paused=true;
00500 startpause=tim.get();
00501 }
00502 }
00503 if(paused) continue;
00504
00505
00506 ima = gb->readRGB();
00507 Image< PixRGB<byte> >::const_iterator data = ima.begin();
00508
00509
00510
00511
00512 total = 0.0;
00513 iptr = bwimage;
00514 while (data != ima.end())
00515 {
00516 double max = 0.0;
00517 if (data->red() > max) max = data->red();
00518 if (data->green() > max) max = data->green();
00519 if (data->blue() > max) max = data->blue();
00520 *iptr = max;
00521 data++;
00522 total += *iptr++;
00523 }
00524
00525
00526 mean = total / ((double)IMAGESIZE);
00527 std = 0.0;
00528 for(i = 0; i < IMAGESIZE; i++)
00529 std += pow(bwimage[i]-mean,2);
00530 std /= IMAGESIZE-1;
00531 std = sqrt(std);
00532 for(i = 0; i < IMAGESIZE; i++)
00533 {
00534 bwimage[i] -= mean;
00535 bwimage[i] /= std;
00536 }
00537
00538
00539
00540
00541 for(h = 0; h < HBLOCKS; h++)
00542 {
00543 for(l = 0; l < WBLOCKS; l++)
00544 {
00545
00546 iptr = bwimage_sm;
00547 for(i = 0; i < WINDOWHEIGHT; i++)
00548 for(j = 0; j < WINDOWWIDTH; j++)
00549 *iptr++ = bwimage[h*WINDOWHEIGHT*IMAGEWIDTH+
00550 l*WINDOWWIDTH + i*IMAGEWIDTH + j];
00551 fftw->compute(magspec);
00552
00553
00554
00555
00556
00557 for(i=0;i<DIMSIN;i++)
00558 {
00559 const int halfwidth=WINDOWWIDTH/2+1;
00560 const int halfheight=WINDOWHEIGHT/2;
00561
00562 int heighttop=halfheight;
00563 switch (i%8)
00564 {
00565 case 0:
00566 heighttop = halfheight;
00567 break;
00568 case 1:
00569 case 7:
00570 heighttop = (int)((double)halfheight*0.75);
00571 break;
00572 case 2:
00573 case 6:
00574 heighttop = (int)((double)halfheight*0.5);
00575 break;
00576 case 3:
00577 case 5:
00578 heighttop = (int)((double)halfheight*0.25);
00579 break;
00580 case 4:
00581 heighttop = 0;
00582 break;
00583 }
00584
00585
00586 for(int j = 0; j < heighttop; j++)
00587 {
00588 for(int k = 0; k < halfwidth; k++)
00589 {
00590 int index = h*WBLOCKS*DIMSIN+l*DIMSIN+i;
00591 allfeatures.setVal
00592 (0, index, allfeatures.getVal(0, index) +
00593 magspec[j][k]*masks[i][j*WINDOWWIDTH+k]);
00594 }
00595 }
00596 if(heighttop>0)
00597 for(int k = halfwidth; k < WINDOWWIDTH; k++)
00598 {
00599 int index = h*WBLOCKS*DIMSIN+l*DIMSIN+i;
00600 allfeatures.setVal
00601 (0, index, allfeatures.getVal(0,index) +
00602 magspec[0][WINDOWWIDTH-k]*masks[i][k]);
00603 }
00604
00605 for(int j = 1; j < heighttop; j++)
00606 {
00607 for(int k = halfwidth; k < WINDOWWIDTH; k++)
00608 {
00609 int index = h*WBLOCKS*DIMSIN+l*DIMSIN+i;
00610 allfeatures.setVal
00611 (0, index, allfeatures.getVal(0,index) +
00612 magspec[WINDOWHEIGHT-j][WINDOWWIDTH-k]*
00613 masks[i][j*WINDOWWIDTH+k] );
00614 }
00615 }
00616
00617
00618 for(int j = halfheight; j < WINDOWHEIGHT; j++)
00619 {
00620 for(int k = 0; k < halfwidth; k++)
00621 {
00622 int index = h*WBLOCKS*DIMSIN+l*DIMSIN+i;
00623 allfeatures.setVal
00624 (0, index, allfeatures.getVal(0,index) +
00625 magspec[j][k]*masks[i][j*WINDOWWIDTH+k]);
00626 }
00627 }
00628
00629 for(int j = halfheight; j < WINDOWHEIGHT; j++)
00630 {
00631 for(int k = halfwidth; k < WINDOWWIDTH; k++)
00632 {
00633 int index = h*WBLOCKS*DIMSIN+l*DIMSIN+i;
00634 allfeatures.setVal
00635 (0, index, allfeatures.getVal(0, index) +
00636 magspec[halfheight-(j-halfheight)][WINDOWWIDTH-k]*
00637 masks[i][j*WINDOWWIDTH+k]);
00638 }
00639 }
00640 }
00641 }
00642 }
00643
00644
00645
00646 for(i = 0; i < DIMSIN; i++)
00647 for(j = 0; j < DIMSINALL; j++)
00648 features.setVal
00649 (0, i, features.getVal(0,i) + evecs[i][j]*allfeatures.getVal(0,j));
00650
00651
00652
00653 features = (features - meanarr)/stdarr;
00654 ffn_dm->run3L(features);
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664 rcspeed = rd->getVal(0);
00665 if(fabs(rcspeed) > 0.1)
00666 {
00667 setspeed = true;
00668 lastspeed = rcspeed;
00669 totspeed += rcspeed;
00670 speedctr++;
00671 }
00672 else
00673 {
00674 if(setspeed)
00675 {
00676 setspeed = false;
00677 lastspeed = totspeed/(double)speedctr;
00678 totspeed = 0.0;
00679 speedctr = 0;
00680 }
00681 }
00682 speed = lastspeed;
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695 double target=(double)rand()/(double)RAND_MAX;
00696 double mysum=0.0;double tprob=0.0;
00697 for(i=0;i<DIMSOUT-1;i++)
00698 mysum+=ffn_dm->getOutput().getVal(0,i);
00699 for(i=0;i<DIMSOUT-1;i++)
00700 {
00701 tprob+=ffn_dm->getOutput().getVal(0,i)/mysum;
00702 if (tprob>target)
00703 break;
00704 }
00705 steer=((((double)i+1.0)-1.0)/((double)DIMSOUT-1.0)*2.0-1.0)*-1.0;
00706
00707
00708
00709
00710
00711 compspeed = speed;
00712 compsteer = steer;
00713 printf("computed speed=%f, steer=%f ctr=%ld\n",speed,steer,ctr+1);
00714
00715 updateweights=false;
00716 rcsteer=rd->getVal(1);
00717 if(fabs(rcspeed)>0.1 || fabs(rcsteer)>0.1){
00718 printf("big rc signal\n");
00719 updateweights=true;
00720
00721 steer=rcsteer;
00722 }
00723 printf("rc speed=%f, steer=%f ctr=%ld\n",rcspeed,rcsteer,ctr+1);
00724
00725
00726 smsg.reset(ctr, 0);
00727
00728 smsg.addDouble(steer);
00729
00730
00731 smsg.addDouble(speed-speed*fabs(speed)/1.6666666667);
00732 beo->send(0, smsg);
00733
00734
00735 if(updateweights==true){
00736 trueout.clear(0.0);
00737 int on;
00738 on=(int)round(((steer*-1.0+1.0)/2.0)*((double)DIMSOUT-1.0)+1.0);
00739 trueout.setVal(0, on-1, 1.0);
00740 if (on>1)
00741 trueout.setVal(0, on-2, 0.5);
00742
00743 if (on>2)
00744 trueout.setVal(0, on-3, 0.25);
00745
00746 if (on<DIMSOUT)
00747 trueout.setVal(0, on, 0.5);
00748 if (on<DIMSOUT-1)
00749 trueout.setVal(0, on+1, 0.25);
00750
00751 ffn_dm->backprop3L(trueout);
00752 }
00753
00754
00755 if(ctr%90==0 && ctr!=0)grabCycle( myKB, lcdDisp, gb );
00756
00757
00758 if(ctr%5000==0 && ctr!=0)
00759 ffn_dm->write3L("/lab/bb/data/nnwh1dm.dat",
00760 "/lab/bb/data/nnwh2dm.dat", "/lab/bb/data/nnwodm.dat");
00761
00762
00763 keypress=myKB.getKey( false );
00764 if( keypress == KBD_KEY4 ){
00765 flag=1;
00766 doQuit=true;
00767 LDEBUG("quit = true;");
00768 }
00769 ctr++;
00770 if(ctr==0)flag=1;
00771 lcdDisp->clear();
00772 lcdDisp->printf(0, 2, "%ld",ctr);
00773 smsg.reset(ctr, 0);
00774 smsg.addInt32(ctr);
00775 beo->send(0, smsg);
00776
00777 }
00778
00779
00780 end=tim.get();
00781 elapsed=((end-start)-totalpause)/(double)1000000;
00782 printf("time=%f secs; processed %ld pics\n",elapsed,ctr);
00783
00784
00785 ffn_dm->write3L("/lab/bb/data/nnwh1dm.dat", "/lab/bb/data/nnwh2dm.dat",
00786 "/lab/bb/data/nnwodm.dat");
00787 delete ffn_dm;
00788 manager.stop();
00789
00790
00791 return 0;
00792 }
00793
00794
00795
00796
00797
00798