test-CBdirection.C

00001 /*!@file AppDevices/test-BeoChip.C test suite for Rand Voorhies' BeeSTEM */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/test-CBdirection.C $
00035 // $Id: test-CBdirection.C 8623 2007-07-25 17:57:51Z rjpeters $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/BeeSTEM.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "Controllers/PID.H"
00042 #include "Util/MathFunctions.H"
00043 
00044 #include <cstdlib>
00045 #include <iostream>
00046 
00047 PID<float> *pidHeading = NULL;
00048 PID<float> *pidPitch = NULL;
00049 PID<float> *pidRoll = NULL;
00050 PID<float> *pidDepth = NULL;
00051 
00052 int sideMotorLeft = 0;
00053 int sideMotorRight = 0;
00054 
00055 //! Our own little BeeSTEMListener
00056 class MyBeeSTEMListener : public BeeSTEMListener
00057 {
00058 public:
00059   MyBeeSTEMListener(nub::soft_ref<BeeSTEM> &b) :
00060                 itsBeeStem(b)
00061   {  }
00062 
00063   virtual ~MyBeeSTEMListener()
00064   { }
00065 
00066   virtual void event(const BeeSTEMEventType t, const unsigned char dat1,
00067                      const unsigned char dat2)
00068   {
00069     LDEBUG("Event: %d dat1 = %d, dat2 = %d", int(t), dat1, dat2);
00070 
00071     switch(t) {
00072 
00073     case COMPASS_HEADING_EVENT:
00074                         {
00075                                 float desiredHeading = 0;
00076                                 int heading = (unsigned int)dat1*2;
00077                                 if (pidHeading != NULL)
00078                                 {
00079                                         float rot = 100*pidHeading->update(desiredHeading, (float)heading);
00080 
00081                                         LINFO("%f %i %i %i", rot, heading, sideMotorLeft+(int)rot, sideMotorRight-(int)rot);
00082                                 }
00083 
00084                         }
00085                          break;  //Careful here! The heading is sent /2 because of byte limits
00086     case COMPASS_PITCH_EVENT:
00087                         {
00088                         //        float desiredPitch = -14;
00089                         //        int pitch = (signed char)dat1;
00090                         //        if (pidPitch != NULL)
00091                         //        {
00092                         //                //float speed = 100*pidPitch->update(desiredPitch, (float)pitch);
00093                         //        //        itsBeeStem->setMotor(2, (int)speed);
00094                         //        //                        LINFO("%f %i", speed, pitch);
00095                         //        }
00096                         }
00097                          break;
00098     case COMPASS_ROLL_EVENT:
00099                         {
00100                                 int roll = (signed char)dat1;
00101                                 roll++;
00102                         }
00103                          break;
00104     case ACCEL_X_EVENT:           break;
00105     case ACCEL_Y_EVENT:           break;
00106     case INT_PRESS_EVENT:         break;
00107     case EXT_PRESS_EVENT:
00108                         //        LINFO("%i", (unsigned char)dat1);
00109                                 break;
00110     case TEMP1_EVENT:             break;
00111     case TEMP2_EVENT:             break;
00112     case TEMP3_EVENT:             break;
00113     case DIG_IN_EVENT:            break;
00114     case ADC_IN_EVENT:            break;
00115     case MOTOR_A_CURR_EVENT:      break;
00116     case MOTOR_B_CURR_EVENT:      break;
00117     case MOTOR_C_CURR_EVENT:      break;
00118     case MOTOR_D_CURR_EVENT:      break;
00119     case MOTOR_E_CURR_EVENT:      break;
00120     case ECHO_REPLY_EVENT:        LINFO("BeeSTEM Echo Reply Recieved."); break;
00121     case RESET_EVENT:             LERROR("BeeSTEM RESET occurred!"); break;
00122     case SW_OVERFLOW_EVENT:       LERROR("BeeSTEM Software Overflow!"); break;
00123     case FRAMING_ERR_EVENT:       LERROR("BeeSTEM Framing Error!"); break;
00124     case OVR_ERR_EVENT:           LERROR("BeeSTEM Hardware Overflow!"); break;
00125     case HMR3300_LOST_EVENT:      break;
00126     case ACCEL_LOST_EVENT:        break;
00127     case TEMP1_LOST_EVENT:        break;
00128     case TEMP2_LOST_EVENT:        break;
00129     case TEMP3_LOST_EVENT:        break;
00130     case ESTOP_EVENT:             break;
00131     case UNRECOGNIZED_EVENT:      break;
00132     case BAD_OUT_CMD_SEQ_EVENT:   LERROR("BeeSTEM Reports a Bad Command Sequence!"); break;
00133     case BAD_IN_CMD_SEQ_EVENT:    break;
00134     case RESET_ACK_EVENT:         LINFO("BeeSTEM Acknowledges Reset Request"); break;
00135     case  NO_EVENT:               break;
00136     default:                      LERROR("Unknown event %d received!", int(t)); break;
00137 
00138     }
00139 
00140 
00141 
00142   }
00143 private:
00144         nub::soft_ref<BeeSTEM> itsBeeStem;
00145 };
00146 
00147 //! This program provides basic interfacing to the BeeSTEM
00148 /*! See the BeeSTEM class for details.*/
00149 int main(const int argc, const char* argv[])
00150 {
00151   char c = 0;
00152   int num = 0;
00153   int motor = 0;
00154   int speed = 0;
00155 
00156   MYLOGVERB = LOG_INFO;
00157 
00158   // instantiate a model manager:
00159   ModelManager manager("BeeSTEM test program");
00160 
00161   // Instantiate our various ModelComponents:
00162         nub::soft_ref<BeeSTEM> b = nub::soft_ref<BeeSTEM>(new BeeSTEM(manager,"BeeSTEM", "BeeSTEM", "/dev/ttyS1"));
00163   manager.addSubComponent(b);
00164 
00165   if (manager.parseCommandLine(argc, argv, "No Options Yet", 0,0) == false)
00166                                    return(1);
00167 
00168   rutz::shared_ptr<MyBeeSTEMListener> lis(new MyBeeSTEMListener(b));
00169   rutz::shared_ptr<BeeSTEMListener> lis2; lis2.dynCastFrom(lis); // cast down
00170   b->setListener(lis2);
00171 
00172   // let's get all our ModelComponent instances started:
00173   manager.start();
00174 
00175   pidHeading = new PID<float>(0.1, 0, 0, -100.0, 100.0);
00176   pidPitch = new PID<float>(0.1, 0, 0, -100.0, 100.0);
00177   pidRoll = new PID<float>(0.1, 0, 0, -100.0, 100.0);
00178 
00179   LINFO("Echo request (should bring an echo reply back)...");
00180   b->echoRequest(); sleep(2);
00181   while(c != 'q') {
00182     std::cin >> c;
00183     switch(c) {
00184     case 'h':
00185       std::cout << "Commands:" << std::endl;
00186       std::cout << "E: Echo Request" << std::endl;
00187       std::cout << "R: Reset the chip" << std::endl;
00188       std::cout << "F: Flood the chip with echo requests" << std::endl;
00189       std::cout << "C=on c=off: Toggle Compass Reporting" << std::endl;
00190       std::cout << "M: Set a thruster speed" << std::endl;
00191       std::cout << "D=on d=off:  Toggle Debugging Mode" << std::endl;
00192       break;
00193     case 'e':
00194       LINFO("Echo request (should bring an echo reply back)...");
00195       b->echoRequest();
00196       break;
00197     case 'r':
00198       LINFO("Reseting the BeeSTEM...");
00199       b->resetChip();
00200       break;
00201     case 'f':
00202       LINFO("Enter the number of echo requests to flood");
00203       std::cin >> num;
00204       LINFO("Flooding the BeeSTEM with %d echo requests...", num);
00205       for(int i=0; i<num;i++)
00206         b->echoRequest();
00207       break;
00208     case 'C':
00209       b->setReporting(HMR3300, true);
00210       break;
00211     case 'c':
00212       b->setReporting(HMR3300, false);
00213       break;
00214     case 'P':
00215       b->setReporting(HMR3300, true);
00216       b->setReporting(EXT_PRESS, true);
00217       break;
00218     case 'p':
00219       b->setReporting(HMR3300, false);
00220       b->setReporting(EXT_PRESS, false);
00221       break;
00222 
00223     case 'D':
00224       b->debugMode(true);
00225     case 'd':
00226       b->debugMode(false);
00227     case 'm':
00228     case 'M':
00229       LINFO("Motor:");
00230       std::cin >> motor;
00231       LINFO("Speed:");
00232       std::cin >> speed;
00233       b->setMotor(motor, speed);
00234       break;
00235 
00236     }
00237   }
00238 
00239 
00240   manager.stop();
00241   return 0;
00242 }
00243 
00244 // ######################################################################
00245 /* So things look consistent in everyone's emacs... */
00246 /* Local Variables: */
00247 /* indent-tabs-mode: nil */
00248 /* End: */
Generated on Sun May 8 08:42:15 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3