test-BeoSubPIDRot.C

Go to the documentation of this file.
00001 /*!@file BeoSub/test-BeoSubPIDRot.C test submarine ballasts */
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/BeoSub/test-BeoSubPIDRot.C $
00035 // $Id: test-BeoSubPIDRot.C 6990 2006-08-11 18:13:51Z rjpeters $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/BeoChip.H"
00040 #include "BeoSub/BeoSubIMU.H"
00041 #include "BeoSub/BeoSubBallast.H"
00042 #include "Controllers/PID.H"
00043 
00044 #include <cstdlib>
00045 #include <iostream>
00046 #include <pthread.h>
00047 
00048 nub::soft_ref<BeoChip> bc_ptr;
00049 PID<float> *pid;
00050 
00051 float cvel;
00052 float cdelta;
00053 bool start = true;
00054 
00055 /* IMU is z-down */
00056 void thrust(const float vel, const float delta) {
00057   /* thrust should be z-down as well */
00058   float left = vel+delta;
00059   if(left > 1) left = 1;
00060   else if(left < -1) left = -1;
00061   float right = vel-delta;
00062   if(right > 1) right = 1;
00063   else if(right < -1) right = -1;
00064   bc_ptr->setServo(3, left);
00065   bc_ptr->setServo(2, right);
00066   //bc_ptr->setServo(3, -1);
00067   //bc_ptr->setServo(2, 1);
00068 }
00069 
00070 // here a listener for the compass:
00071 class IMUListener : public BeoSubIMUListener {
00072 public:
00073   //! Destructor
00074   virtual ~IMUListener() {};
00075 
00076   //! New data was received
00077   virtual void newData(const float xa, const float ya, const float za,
00078                        const Angle xv, const Angle yv, const Angle zv)
00079   {
00080           float current = zv.getVal();
00081           // feed the PID controller:
00082           cdelta  = pid->update(0, current);
00083           LINFO("%f %f", current, cdelta);
00084 
00085           if(start) {
00086                   //thrust(0, 1);
00087                   thrust(cvel, cdelta);
00088           }
00089   }
00090 };
00091 
00092 //! This program provides basic testing of a BeoSubBallast
00093 int main(const int argc, const char* argv[])
00094 {
00095   MYLOGVERB = LOG_INFO;
00096 
00097   // instantiate a model manager:
00098   ModelManager manager("BeoSubPIDRot test program");
00099 
00100   // Instantiate our various ModelComponents:
00101   nub::soft_ref<BeoChip> bc(new BeoChip(manager));
00102   manager.addSubComponent(bc);
00103   bc_ptr = bc;
00104 
00105   // instantiate our model components:
00106   nub::soft_ref<BeoSubIMU> imu(new BeoSubIMU(manager));
00107   manager.addSubComponent(imu);
00108 
00109   //pid = new PID<float>(0.1, 0.001, 0, -0.5, 0.5);
00110   pid = new PID<float>(0.5, 0.001, 0, -500, 500);
00111   //pid = new PID<float>(0.5, 0.1, 0, -0.5, 0.5);
00112 
00113   if (manager.parseCommandLine(argc, argv, "<BeoChip> <IMU>", 2, 2) == false)
00114     return(1);
00115 
00116   // let's configure our serial device:
00117   bc->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0));
00118   bc->setModelParamVal("BeoChipUseRTSCTS", false);
00119   imu->setModelParamVal("IMUSerialPortDevName", manager.getExtraArg(1), MC_RECURSE);
00120 
00121   rutz::shared_ptr<IMUListener> lis(new IMUListener);
00122   rutz::shared_ptr<BeoSubIMUListener> lis2; lis2.dynCastFrom(lis); // cast down
00123   imu->setListener(lis2);
00124 
00125   // let's get all our ModelComponent instances started:
00126   manager.start();
00127 
00128   LINFO("Reseting the BeoChip...");
00129   bc->reset(MC_RECURSE);
00130   sleep(1);
00131 
00132   LINFO("Waiting for a bit. Turn BeoChip on if not already on."); sleep(1);
00133   LINFO("Echo request (should bring an echo reply back)...");
00134   bc->echoRequest(); sleep(1);
00135 
00136   cvel = 0.8;
00137   char c;
00138   std::cin >> c;
00139   start = false;
00140 
00141 
00142   manager.stop();
00143   thrust(0, 0);
00144   return 0;
00145 }
00146 
00147 // ######################################################################
00148 /* So things look consistent in everyone's emacs... */
00149 /* Local Variables: */
00150 /* indent-tabs-mode: nil */
00151 /* End: */
Generated on Sun May 8 08:40:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3