BeoSubSim.C

Go to the documentation of this file.
00001 /*!@file BeoSub/BeoSubSim.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00005 // by the 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/BeoSubSim.C $
00035 // $Id: BeoSubSim.C 7880 2007-02-09 02:34:07Z itti $
00036 //
00037 
00038 #include "BeoSub/BeoSubSim.H"
00039 #include "Devices/HMR3300.H"
00040 #include "BeoSub/BeoSubBallast.H"
00041 #include "BeoSub/BeoSubIMU.H"
00042 #include "Util/MathFunctions.H"
00043 #include "Devices/IEEE1394grabber.H"
00044 
00045 // ######################################################################
00046 //! Class definition for BeoSubListener
00047 /*! This is the listener class that is attached to each BeoChip in the
00048   ballast tube of the submarine. This class is just a
00049   pass-through to the function dispatchBeoChipEvent() of class
00050   BeoSubOneBal. */
00051 /*class BeoSubListener : public BeoChipListener
00052 {
00053 public:
00054   //! Constructor
00055   BeoSubListener(BeoSubSim *sub) : itsBeoSub(sub)  { }
00056 
00057   //! destructor
00058   virtual ~BeoSubListener()  { }
00059 
00060   //! get an event
00061   virtual void event(const BeoChipEventType t, const int valint,
00062                      const float valfloat)
00063   {
00064     //LDEBUG("Event: %d val = %d, fval = %f", int(t), valint, valfloat);
00065     itsBeoSub->dispatchBeoChipEvent(t, valint, valfloat);
00066   }
00067 
00068 private:
00069   BeoSubSim *itsBeoSub;        //!< pointer to our master
00070 };*/
00071 
00072 // ######################################################################
00073 // here a listener for the compass:
00074 /*class HMRlistener : public HMR3300Listener {
00075 public:
00076   //! Constructor
00077   HMRlistener(BeoSubOneBal *sub) : itsBeoSub(sub) { }
00078 
00079   //! Destructor
00080   virtual ~HMRlistener() {};
00081 
00082   //! New data was received
00083   virtual void newData(const Angle heading, const Angle pitch,
00084                        const Angle roll)
00085   {
00086     //LDEBUG("<Heading=%f Pitch=%f Roll=%f>", heading.getVal(),
00087     //       pitch.getVal(), roll.getVal());
00088     itsBeoSub->updateCompass(heading, pitch, roll);
00089   }
00090 
00091 private:
00092   BeoSubSim *itsBeoSub;
00093   };*/
00094 
00095 
00096 // ######################################################################
00097 BeoSubSim::BeoSubSim(OptionManager& mgr) :
00098   BeoSub(mgr),
00099   itsLeftThrusterServoNum("BeoSubLeftThrusterServoNum", this, 3),
00100   itsRightThrusterServoNum("BeoSubRightThrusterServoNum", this, 2),
00101   itsThrustLeft(0),
00102   itsThrustRight(0),
00103   relDistance(0),
00104   isStrafe(false),
00105   imageCounter(0),
00106   itsCurrentZ(0),
00107   itsCurrentX(0),
00108   upImageFlag(false),
00109   frontImageFlag(false),
00110   downImageFlag(false),
00111   itsDepthSensor(10),
00112   itsHeadingSensor(10),
00113   itsPitchSensor(10),
00114   itsDepthPID(0.1F, 0.001F, 0.1F, 1.5F, 1.5F),
00115   itsHeadingPID(0.1F, 0.001F, 0.1F, Angle(170.0), Angle(170.0)),
00116   itsPitchPID(0.1F, 0.001F, 0.1F, Angle(170.0), Angle(170.0)),
00117   itsDepthPIDon(false),
00118   itsHeadingPIDon(false),
00119   itsPitchPIDon(false)
00120 
00121 {
00122 
00123 }
00124 
00125 // ######################################################################
00126 BeoSubSim::~BeoSubSim()
00127 {  }
00128 
00129 // ######################################################################
00130 void BeoSubSim::start1()
00131 {
00132   // select firewire subchannels for our cameras:
00133   /*itsCameraDown->setModelParamVal("FrameGrabberSubChan", int(BEOSUBCAMDOWN));
00134   itsCameraFront->setModelParamVal("FrameGrabberSubChan", int(BEOSUBCAMFRONT));
00135   itsCameraUp->setModelParamVal("FrameGrabberSubChan", int(BEOSUBCAMUP));*/
00136   BeoSub::start1();
00137 }
00138 
00139 // ######################################################################
00140 void BeoSubSim::start2()
00141 {
00142   // turn on the analog port capture for the pressure sensor, do not
00143   // capture pulses:
00144   /*itsBeo->captureAnalog(0, true);
00145   itsBeo->captureAnalog(1, false);
00146   itsBeo->capturePulse(0, false);
00147   itsBeo->capturePulse(1, false);
00148 
00149   // set all servos to neutral:
00150   for (int i = 0; i < 8; i ++) itsBeo->setServo(i, 0.0F);
00151 
00152   // turn on KBD capture for our ballasts:
00153   itsBeo->captureKeyboard(true);
00154   itsBeo->debounceKeyboard(false);
00155   */
00156   // initialize all ballasts at once:
00157   CLINFO("filling ballasts...");
00158   setBallasts(1.0F, 1.0F, false); sleep(4);
00159   CLINFO("emptying ballasts...");
00160   setBallasts(0.0F, 0.0F, true);
00161 
00162   // turn the PIDs on:
00163   itsHeadingPIDon = true;
00164   itsPitchPIDon = true;
00165 }
00166 
00167 // ######################################################################
00168 void BeoSubSim::updateCompass(const Angle heading, const Angle pitch,
00169                                  const Angle roll)
00170 {
00171   // note the time at which the data was received:
00172   double t = itsMasterClock.getSecs();
00173 
00174   // inject these values into our current attitude:
00175   pthread_mutex_lock(&itsLock);
00176   itsCurrentAttitude.heading = heading;
00177   itsCurrentAttitude.pitch = pitch;
00178   //itsCurrentAttitude.roll = roll;
00179   itsCurrentAttitude.compassTime = t;
00180 
00181   // update our heading and pitch sensors:
00182   //itsHeadingSensor.newMeasurement(heading);
00183   //itsPitchSensor.newMeasurement(pitch);
00184 
00185   pthread_mutex_unlock(&itsLock);
00186 }
00187 
00188 
00189 // ######################################################################
00190 void BeoSubSim::updateDepth(const float depth)
00191 {
00192   // note the time at which the data was received:
00193   double t = itsMasterClock.getSecs();
00194 
00195   // inject these values into our current attitude:
00196   pthread_mutex_lock(&itsLock);
00197   itsCurrentAttitude.depth = depth;
00198   itsCurrentAttitude.pressureTime = t;
00199 
00200   // update our depth sensor:
00201   itsDepthSensor.newMeasurement(depth);
00202 
00203   pthread_mutex_unlock(&itsLock);
00204 }
00205 
00206 void BeoSubSim::updatePosition(const float z, const float x) {
00207   itsCurrentZ = z;
00208   itsCurrentX = x;
00209 }
00210 // ######################################################################
00211 void BeoSubSim::thrust(const float leftval, const float rightval)
00212 {
00213   itsThrustLeft = leftval;
00214   itsThrustRight = rightval;
00215 }
00216 
00217 // ######################################################################
00218 void BeoSubSim::getThrusters(float& leftval, float& rightval) const
00219 { leftval = itsThrustLeft; rightval = itsThrustRight; }
00220 
00221 // ######################################################################
00222 void BeoSubSim::setFrontBallast(const float val, const bool blocking)
00223 {  }
00224 
00225 // ######################################################################
00226 void BeoSubSim::setRearBallast(const float val, const bool blocking)
00227 {  }
00228 
00229 // ######################################################################
00230 void BeoSubSim::setBallasts(const float f, const float r,
00231                                const bool blocking)
00232 {  }
00233 
00234 // ######################################################################
00235 float BeoSubSim::getFrontBallast() const
00236 { return 0;}
00237 
00238 // ######################################################################
00239 float BeoSubSim::getRearBallast() const
00240 { return 0; }
00241 
00242 // ######################################################################
00243 void BeoSubSim::getBallasts(float& f, float& r) const
00244 {  }
00245 
00246 // ######################################################################
00247 void BeoSubSim::dropMarker(const bool blocking)
00248 {
00249   LFATAL("unimplemented!");
00250 }
00251 
00252 // ######################################################################
00253 void BeoSubSim::dispatchBeoChipEvent(const BeoChipEventType t,
00254                                   const int valint,
00255                                   const float valfloat)
00256 {
00257 
00258 }
00259 // problems
00260 // ######################################################################
00261 Image< PixRGB<byte> > BeoSubSim::grabImage(const enum BeoSubCamera cam) const
00262 {
00263   if(cam == BEOSUBCAMFRONT) {
00264     const_cast<BeoSubSim *>(this)-> upImageFlag = true;
00265      const_cast<BeoSubSim *>(this)-> imageCounter++;
00266     sleep(1);
00267     return Raster::ReadRGB(sformat("upimage%d.ppm", imageCounter));
00268   }
00269   if(cam == BEOSUBCAMDOWN) {
00270     const_cast<BeoSubSim *>(this)-> downImageFlag = true;
00271      const_cast<BeoSubSim *>(this)-> imageCounter++;
00272     sleep(1);
00273     return Raster::ReadRGB(sformat("downimage%d.ppm", imageCounter));
00274   }
00275   if(cam == BEOSUBCAMUP) {
00276     const_cast<BeoSubSim *>(this)-> frontImageFlag = true;
00277     const_cast<BeoSubSim *>(this)-> imageCounter++;
00278     sleep(5);
00279 
00280     return Raster::ReadRGB(sformat("upimage%d.ppm", imageCounter));
00281   }
00282   //do a wait
00283   //do a read
00284   LERROR("Wrong camera %d -- RETURNING EMPTY IMAGE", int(cam));
00285   return Image< PixRGB<byte> >();
00286   //return grab(cam);
00287 }
00288 
00289 //######################################################################
00290 void BeoSubSim::advanceRel(const float relDist, const bool stop) {
00291   float value = relDistance = relDist;
00292   if(relDist > 1.0F) value = 1.0F;
00293   if(relDist < -1.0F) value = -1.0F;
00294   thrust(value, value);
00295   usleep((int) (100000));
00296 }
00297 
00298 void BeoSubSim::strafeRel(const float relDist) {
00299   relDistance = relDist;
00300   isStrafe = true;
00301   usleep((int) (100000));
00302 }
00303 
00304 void BeoSubSim::turnAbs(const Angle finalHeading, const bool blocking) {
00305   itsTargetAttitude.heading = finalHeading;
00306 }
00307 
00308 void BeoSubSim::turnRel(const Angle relHeading, const bool blocking) {
00309 
00310   itsTargetAttitude.heading = itsCurrentAttitude.heading + relHeading;
00311   sleep(1);
00312 }
00313 
00314 void BeoSubSim::turnOpen(const Angle relHeading, const bool blocking) {
00315 
00316   itsTargetAttitude.heading = itsCurrentAttitude.heading + relHeading;
00317   sleep(1);
00318 }
00319 
00320 void BeoSubSim::diveAbs(const float finalDepth, const bool blocking) {
00321   itsTargetAttitude.depth = finalDepth;
00322 }
00323 
00324 void BeoSubSim::diveRel(const float relDepth, const bool blocking) {
00325     itsTargetAttitude.depth = itsCurrentAttitude.depth + relDepth;
00326 }
00327 
00328 // ######################################################################
00329 void BeoSubSim::useDepthPID(const bool useit)
00330 { itsDepthPIDon = useit; }
00331 
00332 // ######################################################################
00333 void BeoSubSim::useHeadingPID(const bool useit)
00334 { itsHeadingPIDon = useit; }
00335 
00336 // ######################################################################
00337 void BeoSubSim::usePitchPID(const bool useit)
00338 { itsPitchPIDon = useit; }
00339 
00340 
00341 // ######################################################################
00342 /* So things look consistent in everyone's emacs... */
00343 /* Local Variables: */
00344 /* indent-tabs-mode: nil */
00345 /* End: */
Generated on Sun May 8 08:40:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3