00001 /*!@file Beobot/beobot-collectFrames.C 00002 Run beobot-collectFrames-master at A to collect frames 00003 Run beobot-collectFrames at B to remote control motion */ 00004 00005 // //////////////////////////////////////////////////////////////////// // 00006 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00007 // University of Southern California (USC) and the iLab at USC. // 00008 // See http://iLab.usc.edu for information about this project. // 00009 // //////////////////////////////////////////////////////////////////// // 00010 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00011 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00012 // in Visual Environments, and Applications'' by Christof Koch and // 00013 // Laurent Itti, California Institute of Technology, 2001 (patent // 00014 // pending; application number 09/912,225 filed July 23, 2001; see // 00015 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00016 // //////////////////////////////////////////////////////////////////// // 00017 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00018 // // 00019 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00020 // redistribute it and/or modify it under the terms of the GNU General // 00021 // Public License as published by the Free Software Foundation; either // 00022 // version 2 of the License, or (at your option) any later version. // 00023 // // 00024 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00025 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00026 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00027 // PURPOSE. See the GNU General Public License for more details. // 00028 // // 00029 // You should have received a copy of the GNU General Public License // 00030 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00031 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00032 // Boston, MA 02111-1307 USA. // 00033 // //////////////////////////////////////////////////////////////////// // 00034 // 00035 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu> 00036 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/beobot-collectFrames.C $ 00037 // $Id: beobot-collectFrames.C 7353 2006-10-30 15:27:31Z rjpeters $ 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 //! Our own little BeoChipListener 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 //! Drive the Beobot under remote control 00123 /*! This simple test program demonstrate how to capture PWM signals 00124 from the Beochip, which on the Beobot correspond to steering and 00125 speed inputs from the remote control. The captured signals are then 00126 directly fed into the Beobot's steering and speed servos controlled 00127 by the BeoChip. */ 00128 int main(const int argc, const char* argv[]) 00129 { 00130 MYLOGVERB = LOG_INFO; 00131 00132 // instantiate a model manager: 00133 ModelManager manager("Beobot: collect frames"); 00134 00135 // Instantiate our various ModelComponents: 00136 BeobotConfig bbc; 00137 nub::soft_ref<BeoChip> b(new BeoChip(manager)); 00138 manager.addSubComponent(b); 00139 // nub::soft_ref<Beowulf> 00140 // beo(new Beowulf(manager, "Beowulf Slave", "BeowulfSlave", false)); 00141 // manager.addSubComponent(beo); 00142 00143 // Parse command-line: 00144 if (manager.parseCommandLine(argc, argv, "<serdev>", 1, 1) == false) 00145 return(1); 00146 00147 // let's configure our serial device: 00148 b->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0)); 00149 00150 // let's register our listener: 00151 rutz::shared_ptr<MyBeoChipListener> lis(new MyBeoChipListener(b)); 00152 rutz::shared_ptr<BeoChipListener> lis2; lis2.dynCastFrom(lis); // cast down 00153 b->setListener(lis2); 00154 00155 // setup signal handling: 00156 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00157 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00158 00159 TCPmessage rmsg; // message being received and to process 00160 TCPmessage smsg; // message being sent 00161 00162 // let's get all our ModelComponent instances started: 00163 manager.start(); 00164 00165 // reset the beochip: 00166 LINFO("Resetting BeoChip..."); 00167 b->resetChip(); sleep(1); 00168 00169 // keep the gear at the lowest speed/highest torque 00170 b->setServoRaw(bbc.gearServoNum, bbc.gearMinVal); 00171 00172 // turn on the keyboard 00173 b->debounceKeyboard(true); 00174 b->captureKeyboard(true); 00175 00176 // calibrate the PWMs: 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 // let's play with the LCD: 00189 b->lcdClear(); // 01234567890123456789 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 // int32 rframe, raction, rnode = -1; // receive from any node 00196 // while(beo->receive(rnode, rmsg, rframe, raction, 5)); 00197 00198 // const float val = rmsg.getElementFloat(); 00199 // LINFO("rec %f", val); 00200 // rmsg.reset(rframe, raction); 00201 00202 // wait for keyboard and process it: 00203 while(goforever) { 00204 00205 // print keyboard values: 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 // quit if both extreme keys pressed simultaneously: 00210 if (kb[0] == '0' && kb[4] == '0') { 00211 b->lcdPrintf(15, 0, "QUIT "); 00212 goforever = false; break; 00213 } 00214 00215 // b->setServoRaw(0,120); 00216 // b->setServoRaw(1,127); 00217 // Raster::waitForKey(); 00218 // for(int i = 0; i < 256; i++) 00219 // { 00220 // b->setServoRaw(1,i); 00221 // usleep(50000); 00222 // LINFO("val: %d", i); 00223 // if(i % 5 == 0) 00224 // Raster::waitForKey(); 00225 // } 00226 00227 // if (kb[1] == '0' || kb[3] == '0') { 00228 00229 // smsg.reset(0, 1); 00230 // if (kb[1] == '0') 00231 // { 00232 // // start capturing frames 00233 // smsg.addInt32(1); 00234 // b->lcdPrintf(15, 0, "CAPTR"); 00235 // LINFO("start capturing"); 00236 // } 00237 // else 00238 // { 00239 // // stop capturing frames 00240 // smsg.addInt32(0); 00241 // b->lcdPrintf(15, 0, "STOP "); 00242 // LINFO("stop capturing"); 00243 // } 00244 // beo->send(-1, smsg); 00245 // } 00246 // usleep(50000); 00247 } 00248 00249 manager.stop(); 00250 return 0; 00251 } 00252 00253 // ###################################################################### 00254 /* So things look consistent in everyone's emacs... */ 00255 /* Local Variables: */ 00256 /* indent-tabs-mode: nil */ 00257 /* End: */