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 #include "Beobot/Beobot.H"
00046 #include "Beobot/beobot-defs.H"
00047 #include "Component/ModelManager.H"
00048 #include "Devices/DeviceOpts.H"
00049 #include "Devices/FrameGrabberConfigurator.H"
00050 #include "Devices/RadioDecoder.H"
00051 #include "GUI/XWindow.H"
00052 #include "Image/Image.H"
00053 #include "Image/Pixels.H"
00054 #include "Image/fancynorm.H"
00055 #include "Transport/FrameIstream.H"
00056 #include "Util/Types.H"
00057
00058 #include <cmath>
00059 #include <netdb.h>
00060 #include <signal.h>
00061 #include <unistd.h>
00062
00063
00064 #define SLAVES "bb1ag:9790 bb1bg:9789 bb1bg:9790"
00065
00066
00067 #define NBAVG 10
00068
00069 #define NBSTATS 6
00070
00071
00072 #define USE_SINGLE_CPU 0
00073
00074 static bool goforever = true;
00075
00076
00077 void terminate(int s)
00078 { LERROR("*** INTERRUPT ***"); goforever = false; }
00079
00080
00081 int main(const int argc, const char **argv)
00082 {
00083 MYLOGVERB = LOG_INFO;
00084
00085
00086 ModelManager manager("Beobot Main");
00087
00088
00089 nub::soft_ref<FrameGrabberConfigurator>
00090 gbc(new FrameGrabberConfigurator(manager));
00091 manager.addSubComponent(gbc);
00092
00093 nub::soft_ref<RadioDecoder> radio(new RadioDecoder(manager));
00094 manager.addSubComponent(radio);
00095
00096
00097
00098
00099 manager.setOptionValString(&OPT_FrameGrabberType, "1394");
00100 manager.setOptionValString(&OPT_FrameGrabberDims, "160x120");
00101
00102
00103 if (manager.parseCommandLine(argc, argv, "<hostname> <port>",
00104 2, 2) == false) return(1);
00105
00106
00107 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00108 if (gb.isInvalid())
00109 LFATAL("You need to select a frame grabber type via the "
00110 "--fg-type=XX command-line option for this program "
00111 "to be useful");
00112 int imgwidth = gb->getWidth(), imgheight = gb->getHeight();
00113
00114 struct hostent *he = gethostbyname(manager.getExtraArg(0).c_str());
00115 in_addr_t myIP = 0;
00116 if (he == NULL)
00117 LFATAL("Cannot determine IP address of %s",manager.getExtraArg(0).c_str());
00118 else myIP = ntohl( ((in_addr *)(he->h_addr_list[0]))->s_addr );
00119 short int port = manager.getExtraArgAs<int>(1);
00120 LINFO("Start on %s[%lx]:%d",
00121 manager.getExtraArg(0).c_str(), (unsigned long) myIP, port);
00122
00123
00124 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00125 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00126 signal(SIGALRM, terminate);
00127
00128
00129
00130
00131
00132
00133 Image< PixRGB<byte> > *ima;
00134 int frame = 0;
00135
00136
00137 #if USE_SINGLE_CPU != 0
00138 Beobot beobot(NULL, imgwidth, imgheight, LEVEL_MIN, LEVEL_MAX,
00139 DELTA_MIN, DELTA_MAX, SMLEVEL, NBORIENTS, VCXNORM_MAXNORM,
00140 JETLEVEL, JETDEPTH, NBNEIGH, myIP, port);
00141 #else
00142 Beobot beobot(SLAVES, imgwidth, imgheight, LEVEL_MIN, LEVEL_MAX,
00143 DELTA_MIN, DELTA_MAX, SMLEVEL, NBORIENTS, VCXNORM_MAXNORM,
00144 JETLEVEL, JETDEPTH, NBNEIGH, myIP, port);
00145 #endif
00146
00147
00148 ima = beobot.getRetinaPtr();
00149
00150
00151 int stats[NBSTATS]; Timer tim; int navg = 0;
00152 for (int i = 0; i < NBSTATS; i ++) stats[i] = 0;
00153
00154 #ifdef DEBUG_MODE
00155
00156 XWindow xw(gb->peekDims()), xw2(gb->peekDims());
00157 #endif
00158
00159 LINFO("All initializations complete. READY.");
00160
00161
00162 manager.start();
00163
00164
00165 while(goforever)
00166 {
00167 tim.reset();
00168
00169
00170 *ima = gb->readRGB();
00171 stats[0] += tim.getReset();
00172
00173
00174 if (frame == 0 || USE_SINGLE_CPU) beobot.lowLevel(frame);
00175 else beobot.lowLevelStart(frame);
00176 stats[1] += tim.getReset();
00177
00178
00179
00180
00181
00182
00183
00184 stats[2] += tim.getReset();
00185
00186
00187
00188 stats[3] += tim.getReset();
00189
00190 #ifdef DEBUG_MODE
00191
00192 Image< PixRGB<byte> >tmp;
00193
00194 #endif
00195
00196
00197
00198 stats[4] += tim.getReset();
00199
00200
00201
00202 BeobotAction a; Point2D<int> win;
00203
00204 float speed = a.getSpeed(), turn = a.getTurn();
00205
00206
00207
00208
00209
00210
00211 if (fabs(speed) > 0.2) speed = 0.2;
00212
00213 LINFO("frame %d: speed = %.1f, turn = %.1f, winner = [%d, %d]",
00214 frame, speed, turn, win.i, win.j);
00215
00216
00217 a.setSpeed(speed); a.setTurn(turn); a.setGear(1);
00218
00219
00220
00221
00222
00223
00224 if (frame > 0 && USE_SINGLE_CPU == 0) beobot.lowLevelEnd(frame);
00225 stats[5] += tim.getReset();
00226
00227
00228 navg ++;
00229 if (navg == NBAVG) {
00230 LINFO("GB:%.1f LL:%.1f IL:%.1f HL:%.1f DC:%.1f LL:%.1f",
00231 ((float)(stats[0])) / (float)NBAVG,
00232 ((float)(stats[1])) / (float)NBAVG,
00233 ((float)(stats[2])) / (float)NBAVG,
00234 ((float)(stats[3])) / (float)NBAVG,
00235 ((float)(stats[4])) / (float)NBAVG,
00236 ((float)(stats[5])) / (float)NBAVG);
00237 navg = 0; for (int i = 0; i < NBSTATS; i ++) stats[i] = 0;
00238 }
00239
00240 #ifdef DEBUG_MODE
00241
00242 PixRGB<byte> yellow(255, 255, 0);
00243 ima->drawCircle(win, imgwidth/12, yellow, 2);
00244
00245
00246 xw.drawImage(*ima);
00247 #endif
00248
00249
00250 frame ++;
00251 }
00252
00253
00254 manager.stop();
00255 exit(0);
00256 }
00257
00258
00259
00260
00261
00262