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 #include "Component/ModelManager.H"
00041 #include "Devices/BeoChip.H"
00042 #include "Devices/DeviceOpts.H"
00043 #include "Beobot/BeobotConfig.H"
00044 #include "Util/MathFunctions.H"
00045 #include "Raster/Raster.H"
00046
00047 #include <cstdlib>
00048
00049 #include "Beowulf/Beowulf.H"
00050 #include "Util/Types.H"
00051 #include "Util/log.H"
00052
00053 #include <cstring>
00054 #include <signal.h>
00055 #include <time.h>
00056 #include <unistd.h>
00057
00058 static bool goforever = true;
00059
00060 void terminate(int s) { LERROR("*** INTERRUPT ***"); goforever = false; exit(1);}
00061
00062
00063
00064 class MyBeoChipListener : public BeoChipListener
00065 {
00066 public:
00067 MyBeoChipListener(nub::soft_ref<BeoChip> bc) :
00068 itsBeoChip(bc), minp0(9999), maxp0(0), minp1(9999), maxp1(0),
00069 counter0(0), counter1(0), kbd(0x1f) { }
00070
00071 virtual ~MyBeoChipListener() { }
00072
00073 virtual void event(const BeoChipEventType t, const int valint,
00074 const float valfloat)
00075 {
00076 LDEBUG("Event: %d val = %d, fval = %f", int(t), valint, valfloat);
00077 switch(t)
00078 {
00079 case PWM0:
00080 if (valint < minp0) minp0 = valint;
00081 else if (valint > maxp0) maxp0 = valint;
00082 itsBeoChip->setServo(0, valfloat);
00083 if (++counter0 >= 10)
00084 {
00085 itsBeoChip->lcdPrintf(5, 2, "%04d %04d-%04d",
00086 valint, minp0, maxp0);
00087 itsBeoChip->lcdPrintf(6, 1, "%03d",
00088 itsBeoChip->getServoRaw(0));
00089 counter0 = 0;
00090 }
00091 break;
00092 case PWM1:
00093 if (valint < minp1) minp1 = valint;
00094 else if (valint > maxp1) maxp1 = valint;
00095 itsBeoChip->setServo(1, valfloat);
00096 if (++counter1 >= 10)
00097 {
00098 itsBeoChip->lcdPrintf(5, 3, "%04d %04d-%04d",
00099 valint, minp1, maxp1);
00100 itsBeoChip->lcdPrintf(17, 1, "%03d",
00101 itsBeoChip->getServoRaw(1));
00102 counter1 = 0;
00103 }
00104 break;
00105 case KBD: kbd = valint; break;
00106 case RESET: LERROR("BeoChip RESET occurred!"); break;
00107 case ECHOREP: LINFO("BeoChip Echo reply received."); break;
00108 case INOVERFLOW: LERROR("BeoChip input overflow!"); break;
00109 case SERIALERROR: LERROR("BeoChip serial error!"); break;
00110 case OUTOVERFLOW: LERROR("BeoChip output overflow!"); break;
00111 default: LERROR("Unknown event %d received!", int(t)); break;
00112 }
00113 }
00114
00115 nub::soft_ref<BeoChip> itsBeoChip;
00116 int minp0, maxp0, minp1, maxp1;
00117 int counter0, counter1;
00118 int kbd;
00119 };
00120
00121
00122
00123
00124
00125
00126
00127
00128 int main(const int argc, const char* argv[])
00129 {
00130 MYLOGVERB = LOG_INFO;
00131
00132
00133 ModelManager manager("Beobot: collect frames");
00134
00135
00136 BeobotConfig bbc;
00137 nub::soft_ref<BeoChip> b(new BeoChip(manager));
00138 manager.addSubComponent(b);
00139
00140
00141
00142
00143
00144 if (manager.parseCommandLine(argc, argv, "<serdev>", 1, 1) == false)
00145 return(1);
00146
00147
00148 b->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0));
00149
00150
00151 rutz::shared_ptr<MyBeoChipListener> lis(new MyBeoChipListener(b));
00152 rutz::shared_ptr<BeoChipListener> lis2; lis2.dynCastFrom(lis);
00153 b->setListener(lis2);
00154
00155
00156 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00157 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00158
00159 TCPmessage rmsg;
00160 TCPmessage smsg;
00161
00162
00163 manager.start();
00164
00165
00166 LINFO("Resetting BeoChip...");
00167 b->resetChip(); sleep(1);
00168
00169
00170 b->setServoRaw(bbc.gearServoNum, bbc.gearMinVal);
00171
00172
00173 b->debounceKeyboard(true);
00174 b->captureKeyboard(true);
00175
00176
00177 b->calibratePulse(0,
00178 bbc.pwm0NeutralVal,
00179 bbc.pwm0MinVal,
00180 bbc.pwm0MaxVal);
00181 b->calibratePulse(1,
00182 bbc.pwm1NeutralVal,
00183 bbc.pwm1MinVal,
00184 bbc.pwm1MaxVal);
00185 b->capturePulse(0, true);
00186 b->capturePulse(1, true);
00187
00188
00189 b->lcdClear();
00190 b->lcdPrintf(0, 0, "collectFrames: 00000");
00191 b->lcdPrintf(0, 1, "STEER=XXX SPEED=XXX");
00192 b->lcdPrintf(0, 2, "PWM0=0000 0000-0000");
00193 b->lcdPrintf(0, 3, "PWM1=0000 0000-0000");
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 while(goforever) {
00204
00205
00206 char kb[6]; kb[5] = '\0';
00207 for (int i = 0; i < 5; i ++) kb[i] = (lis->kbd>>(4-i))&1 ? '1':'0';
00208
00209
00210 if (kb[0] == '0' && kb[4] == '0') {
00211 b->lcdPrintf(15, 0, "QUIT ");
00212 goforever = false; break;
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 manager.stop();
00250 return 0;
00251 }
00252
00253
00254
00255
00256
00257