00001 /*!@file Beobot/beobot-remote.C Drive Beobot with a remote control */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/beobot-remote.C $ 00034 // $Id: beobot-remote.C 7277 2006-10-18 22:55:24Z beobot $ 00035 // 00036 00037 #include "Component/ModelManager.H" 00038 #include "Devices/BeoChip.H" 00039 #include "Devices/DeviceOpts.H" 00040 #include "Util/MathFunctions.H" 00041 00042 #include <cstdlib> 00043 00044 //! Our own little BeoChipListener 00045 class MyBeoChipListener : public BeoChipListener 00046 { 00047 public: 00048 MyBeoChipListener(nub::soft_ref<BeoChip> bc) : 00049 itsBeoChip(bc), minp0(9999), maxp0(0), minp1(9999), maxp1(0), 00050 counter0(0), counter1(0) { } 00051 00052 virtual ~MyBeoChipListener() { } 00053 00054 virtual void event(const BeoChipEventType t, const int valint, 00055 const float valfloat) 00056 { 00057 //LINFO("Event: %d val = %d, fval = %f", int(t), valint, valfloat); 00058 switch(t) 00059 { 00060 case PWM0: 00061 if (valint < minp0) minp0 = valint; 00062 else if (valint > maxp0) maxp0 = valint; 00063 itsBeoChip->setServo(0, valfloat); 00064 if (++counter0 >= 10) 00065 { 00066 itsBeoChip->lcdPrintf(5, 2, "%04d %04d-%04d", 00067 valint, minp0, maxp0); 00068 itsBeoChip->lcdPrintf(6, 1, "%03d", 00069 itsBeoChip->getServoRaw(0)); 00070 counter0 = 0; 00071 } 00072 break; 00073 case PWM1: 00074 if (valint < minp1) minp1 = valint; 00075 else if (valint > maxp1) maxp1 = valint; 00076 itsBeoChip->setServo(1, valfloat); 00077 if (++counter1 >= 10) 00078 { 00079 itsBeoChip->lcdPrintf(5, 3, "%04d %04d-%04d", 00080 valint, minp1, maxp1); 00081 itsBeoChip->lcdPrintf(17, 1, "%03d", 00082 itsBeoChip->getServoRaw(1)); 00083 counter1 = 0; 00084 } 00085 break; 00086 case RESET: LERROR("BeoChip RESET occurred!"); break; 00087 case ECHOREP: LINFO("BeoChip Echo reply received."); break; 00088 case INOVERFLOW: LERROR("BeoChip input overflow!"); break; 00089 case SERIALERROR: LERROR("BeoChip serial error!"); break; 00090 case OUTOVERFLOW: LERROR("BeoChip output overflow!"); break; 00091 default: LERROR("Unknown event %d received!", int(t)); break; 00092 } 00093 } 00094 00095 nub::soft_ref<BeoChip> itsBeoChip; 00096 int minp0, maxp0, minp1, maxp1; 00097 int counter0, counter1; 00098 }; 00099 00100 //! Drive the Beobot under remote control 00101 /*! This simple test program demonstrate how to capture PWM signals 00102 from the Beochip, which on the Beobot correspond to steering and 00103 speed inputs from the remote control. The captured signals are then 00104 directly fed into the Beobot's steering and speed servos controlled 00105 by the BeoChip. */ 00106 int main(const int argc, const char* argv[]) 00107 { 00108 MYLOGVERB = LOG_INFO; 00109 00110 // instantiate a model manager: 00111 ModelManager manager("Beobot remote"); 00112 00113 // Instantiate our various ModelComponents: 00114 nub::soft_ref<BeoChip> b(new BeoChip(manager)); 00115 manager.addSubComponent(b); 00116 00117 // Parse command-line: 00118 if (manager.parseCommandLine(argc, argv, "<serdev>", 1, 1) == false) 00119 return(1); 00120 00121 // let's configure our serial device: 00122 b->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0)); 00123 00124 // let's register our listener: 00125 rutz::shared_ptr<BeoChipListener> lis(new MyBeoChipListener(b)); 00126 b->setListener(lis); 00127 00128 // let's get all our ModelComponent instances started: 00129 manager.start(); 00130 00131 // reset the beochip: 00132 LINFO("Resetting BeoChip..."); 00133 b->resetChip(); sleep(1); 00134 00135 // keep the gear at the lowest speed/highest torque 00136 b->setServoRaw(2, 0); 00137 00138 // calibrate the PWMs: 00139 b->calibratePulse(0, 934, 665, 1280); 00140 b->calibratePulse(1, 865, 590, 1320); 00141 b->capturePulse(0, true); 00142 b->capturePulse(1, true); 00143 00144 // let's play with the LCD: 00145 b->lcdClear(); // 01234567890123456789 00146 b->lcdPrintf(0, 0, "remote-calib -- 1.00"); 00147 b->lcdPrintf(0, 1, "STEER=XXX SPEED=XXX"); 00148 b->lcdPrintf(0, 2, "PWM0=0000 0000-0000"); 00149 b->lcdPrintf(0, 3, "PWM1=0000 0000-0000"); 00150 00151 while(true) sleep(1); 00152 00153 manager.stop(); 00154 return 0; 00155 } 00156 00157 // ###################################################################### 00158 /* So things look consistent in everyone's emacs... */ 00159 /* Local Variables: */ 00160 /* indent-tabs-mode: nil */ 00161 /* End: */