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 #include "Beowulf/Beowulf.H"
00039 #include "Component/ModelManager.H"
00040 #include "Component/OptionManager.H"
00041 #include "Controllers/PID.H"
00042 #include "Devices/FrameGrabberConfigurator.H"
00043 #include "Devices/DeviceOpts.H"
00044 #include "GUI/XWindow.H"
00045 #include "Image/ColorOps.H"
00046 #include "Image/CutPaste.H"
00047 #include "Image/DrawOps.H"
00048 #include "Image/Image.H"
00049 #include "Image/MathOps.H"
00050 #include "Image/Pixels.H"
00051 #include "Image/ShapeOps.H"
00052 #include "RCBot/Motion/MotionEnergy.H"
00053 #include "Raster/Raster.H"
00054 #include "Media/FrameSeries.H"
00055 #include "Transport/FrameIstream.H"
00056 #include "Util/Timer.H"
00057 #include "Util/Types.H"
00058 #include "Util/log.H"
00059 #include <math.h>
00060 #include <signal.h>
00061
00062 #include "Devices/BeoChip.H"
00063 #include "Beobot/BeobotConfig.H"
00064 #include "Controllers/PID.H"
00065 #include "Beobot/BeobotBeoChipListener.H"
00066
00067
00068 #define NAVG 20
00069
00070 static bool goforever = true;
00071 BeobotConfig bbc;
00072
00073
00074 void resetBeoChip(nub::soft_ref<BeoChip> b);
00075
00076
00077 void terminate(int s)
00078 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); }
00079
00080
00081 int main(const int argc, const char **argv)
00082 {
00083 MYLOGVERB = LOG_INFO;
00084
00085
00086 ModelManager manager("Beobot - Drive Straight");
00087
00088
00089 nub::soft_ref<BeoChip> b(new BeoChip(manager));
00090 manager.addSubComponent(b);
00091
00092 nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00093 manager.addSubComponent(ifs);
00094
00095 manager.exportOptions(MC_RECURSE);
00096
00097
00098 if (manager.parseCommandLine(argc, argv, "steer P I D speed P I D", 6, 6) == false)
00099 return(1);
00100
00101
00102
00103
00104 b->setModelParamVal("BeoChipDeviceName", std::string("/dev/ttyS0"));
00105
00106
00107 rutz::shared_ptr<BeobotBeoChipListener> lis(new BeobotBeoChipListener(b));
00108 rutz::shared_ptr<BeoChipListener> lis2; lis2.dynCastFrom(lis);
00109 b->setListener(lis2);
00110
00111 int w = ifs->getWidth(), h = ifs->getHeight();
00112 XWindow winoH(Dims(w, 4*h), 2*w+20, 0, "test-output H");
00113 XWindow winoV(Dims(w, 4*h), 3*w+30, 0, "test-output V");
00114 XWindow wini (Dims(w, 4*h), w+10 , 0, "test-input windowL");
00115 XWindow wini2(Dims(w, 4*h), 0 , 0, "test-input windowC");
00116
00117
00118 manager.start();
00119
00120
00121 resetBeoChip(b);
00122
00123
00124 MotionEnergyPyrBuilder<byte> motionPyr(Gaussian5);
00125
00126
00127 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00128 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00129 signal(SIGALRM, terminate);
00130
00131 PID<float> steer_pid(4.0, 0.0, 0.0, -1.0, 1.0);
00132 PID<float> speed_pid(1.6, 0.3, 0.3, -0.6, 0.6);
00133 steer_pid.setPIDPgain(manager.getExtraArgAs<float>(0));
00134 steer_pid.setPIDIgain(manager.getExtraArgAs<float>(1));
00135 steer_pid.setPIDDgain(manager.getExtraArgAs<float>(2));
00136 speed_pid.setPIDPgain(manager.getExtraArgAs<float>(3));
00137 speed_pid.setPIDIgain(manager.getExtraArgAs<float>(4));
00138 speed_pid.setPIDDgain(manager.getExtraArgAs<float>(5));
00139
00140
00141 Timer tim(1000000); uint64 t[NAVG]; int frame = 0;
00142
00143 while(goforever)
00144 {
00145 tim.reset();
00146
00147
00148 ifs->updateNext();
00149 Image< PixRGB<byte> > ima = ifs->readRGB();
00150 if(!ima.initialized()) {goforever = false; break;}
00151 wini2.drawImage(ima,0,0);
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 Image<byte> lum = luminance(ima);
00163 wini.drawImage(lum,0,0);
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 motionPyr.updateMotion(lum, 1);
00175 ImageSet<float> hpyr = motionPyr.buildHorizontalMotion();
00176 ImageSet<float> vpyr = motionPyr.buildVerticalMotion();
00177
00178
00179 int sc = 1; float mn,mx, mn2, mx2;
00180 for(uint i = 0; i < hpyr.size(); i++)
00181 {
00182 Image<float> tmp = hpyr[i]; getMinMax(tmp,mn,mx);
00183 for(uint j = 0; j < i; j++) tmp = intXY(tmp, true);
00184 winoH.drawImage(tmp, 0, h*i);
00185
00186 tmp = vpyr[i]; getMinMax(tmp,mn2,mx2);
00187 for(uint j = 0; j < i; j++) tmp = intXY(tmp,true);
00188 winoV.drawImage(tmp, 0, h*i);
00189 sc *=2;
00190
00191
00192
00193
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 double speed = fabs(mean(vpyr[0]));
00258 double dir = -mean(hpyr[0]);
00259 if(dir > 0.0) LINFO("GO RIGHT"); else LINFO("GO LEFT");
00260
00261
00262
00263
00264
00265
00266
00267
00268 double st = steer_pid.update(0.0 , dir);
00269 double sp = speed_pid.update(0.175, speed);
00270 if(st > 1.0) st = 1.0; else if (st < -1.0) st = -1.0;
00271 if(sp > 0.6) sp = 0.6; else if (sp < 0.0) sp = 0.0;
00272 LINFO("%4d STEER: %11.7f -> %11.7f SPEED: %11.7f -> %11.7f", frame,
00273 dir , st, speed, sp);
00274 sp = 0.4f;
00275
00276
00277 b->setServo(bbc.steerServoNum, st);
00278 b->setServo(bbc.speedServoNum, sp);
00279
00280
00281
00282
00283
00284 t[frame % NAVG] = tim.get();
00285 if (frame % NAVG == 0 && frame > 0)
00286 {
00287 uint64 avg = 0; for (int i = 0; i < NAVG; i ++) avg += t[i];
00288 float avg2 = 1000000.0 / (float)avg * NAVG;
00289 LINFO("Framerate: %5.2f fps", avg2);
00290 }
00291 frame++;
00292 }
00293
00294
00295 manager.stop();
00296 return 0;
00297 }
00298
00299
00300 void resetBeoChip(nub::soft_ref<BeoChip> b)
00301 {
00302
00303 LINFO("Resetting BeoChip...");
00304 b->resetChip(); sleep(1);
00305
00306
00307 b->calibrateServo(bbc.steerServoNum, bbc.steerNeutralVal,
00308 bbc.steerMinVal, bbc.steerMaxVal);
00309 b->calibrateServo(bbc.speedServoNum, bbc.speedNeutralVal,
00310 bbc.speedMinVal, bbc.speedMaxVal);
00311 b->calibrateServo(bbc.gearServoNum, bbc.gearNeutralVal,
00312 bbc.gearMinVal, bbc.gearMaxVal);
00313
00314
00315 b->setServoRaw(bbc.gearServoNum, bbc.gearMinVal);
00316
00317
00318 b->debounceKeyboard(true);
00319 b->captureKeyboard(true);
00320
00321
00322 b->calibratePulse(0,
00323 bbc.pwm0NeutralVal,
00324 bbc.pwm0MinVal,
00325 bbc.pwm0MaxVal);
00326 b->calibratePulse(1,
00327 bbc.pwm1NeutralVal,
00328 bbc.pwm1MinVal,
00329 bbc.pwm1MaxVal);
00330 b->capturePulse(0, true);
00331 b->capturePulse(1, true);
00332
00333
00334 b->lcdClear();
00335 b->lcdPrintf(0, 0, "collectFrames: 00000");
00336 b->lcdPrintf(0, 1, "STEER=XXX SPEED=XXX");
00337 b->lcdPrintf(0, 2, "PWM0=0000 0000-0000");
00338 b->lcdPrintf(0, 3, "PWM1=0000 0000-0000");
00339 }
00340
00341
00342
00343
00344
00345