00001 /*!@file BeoSub/test-BeoSubIMUGravity.C test the IMU */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002 // 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/test-BeoSubIMUGravity.C $ 00035 // $Id: test-BeoSubIMUGravity.C 7880 2007-02-09 02:34:07Z itti $ 00036 // 00037 00038 #include "BeoSub/BeoSubIMU.H" 00039 #include "Devices/HMR3300.H" 00040 #include "Component/ModelManager.H" 00041 #include <math.h> 00042 00043 //Global vars set by listeners 00044 float xAccel = 0, yAccel = 0, zAccel = 0; 00045 Angle xVel = 0, yVel = 0, zVel = 0, cHeading = 0, cPitch = 0, cRoll = 0; 00046 Angle hError = 0, pError = 0, rError = 0; 00047 Angle realPitch = 0, realRoll = 0; 00048 float realXAccel = 0, realZAccel = 0; 00049 00050 bool firstTime = true; 00051 00052 class TestHMR3300Listener : public HMR3300Listener { 00053 public: 00054 //! Destructor 00055 virtual ~TestHMR3300Listener() {}; 00056 00057 //! New data was received 00058 virtual void newData(const Angle heading, const Angle pitch, 00059 const Angle roll) 00060 { 00061 00062 //The following code is meant to be run upon initialization. It assumes that the sub is on a level surfac and then calculates the error due to mounting of the compass. 00063 if(firstTime){ 00064 hError = heading; pError = pitch; rError = roll; 00065 firstTime = false; 00066 } 00067 cHeading = heading; cPitch = pitch; cRoll = roll; 00068 00069 realPitch = (cPitch - pError); 00070 realRoll = (cRoll - rError); 00071 00072 //NOTE: The gravity constant may need to be more precise AND may need to be negative! 00073 realXAccel = (xAccel - (sin(realPitch.getVal())*-9.82) - (sin(realRoll.getVal())*-9.82)); 00074 realZAccel = (zAccel - (cos(realPitch.getVal())*-9.82) - (cos(realRoll.getVal())*-9.82)); 00075 00076 printf("X-Accel = %f, Y-Accel = %f, Z-Accel = %f, X-Vel = %f, Y-Vel = %f, Z-Vel = %f, Heading = %f Pitch = %f Roll = %f\n\n", 00077 xAccel, yAccel, zAccel, xVel.getVal(), yVel.getVal(), zVel.getVal(), 00078 cHeading.getVal(), realPitch.getVal(), realRoll.getVal()); 00079 00080 //Re-calculate accelerations based on compass reading and gravity and output again 00081 printf("REAL xAccel = %f, REAL zAccel = %f\n\n", realXAccel, realZAccel); 00082 //printf("xAccel = %f, zAccel = %f\n", xAccel, zAccel); 00083 00084 //printf("heading = %f, pitch = %f, roll = %f\n", cHeading.getVal(), realPitch.getVal(), realRoll.getVal()); 00085 } 00086 }; 00087 00088 00089 //! A hook which will be called when a new IMU reading is received 00090 class TestBeoSubIMUListener : public BeoSubIMUListener { 00091 public: 00092 //! Destructor 00093 virtual ~TestBeoSubIMUListener() {} 00094 00095 //! New data was received 00096 virtual void newData(const float xa, const float ya, const float za, 00097 const Angle xv, const Angle yv, const Angle zv) 00098 { 00099 xAccel = xa; yAccel = ya; zAccel = za; 00100 xVel = xv; yVel = yv; zVel = zv; 00101 //printf("XBOOGLY: %f\n", xAccel); 00102 } 00103 }; 00104 00105 int main(const int argc, const char **argv) 00106 { 00107 // get a manager going: 00108 ModelManager manager("IMU Manager"); 00109 00110 // instantiate our model components: 00111 nub::soft_ref<BeoSubIMU> imu(new BeoSubIMU(manager)); 00112 manager.addSubComponent(imu); 00113 00114 nub::soft_ref<HMR3300> hmr(new HMR3300(manager) ); 00115 manager.addSubComponent(hmr); 00116 00117 00118 // Parse command-line: 00119 if (manager.parseCommandLine(argc, argv, "<serdev>", 2, 2) == false) 00120 return(1); 00121 00122 // let's configure our serial devices: 00123 imu->setModelParamVal("IMUSerialPortDevName", 00124 manager.getExtraArg(0), MC_RECURSE); 00125 00126 hmr->setModelParamVal("HMR3300SerialPortDevName", 00127 manager.getExtraArg(1), MC_RECURSE); 00128 00129 // let's register our listeners: 00130 rutz::shared_ptr<TestBeoSubIMUListener> lis(new TestBeoSubIMUListener); 00131 rutz::shared_ptr<BeoSubIMUListener> lis2; lis2.dynCastFrom(lis); // cast down 00132 imu->setListener(lis2); 00133 00134 rutz::shared_ptr<TestHMR3300Listener> lisn(new TestHMR3300Listener); 00135 rutz::shared_ptr<HMR3300Listener> lisn2; lisn2.dynCastFrom(lisn); // cast down 00136 hmr->setListener(lisn2); 00137 00138 // get started: 00139 manager.start(); 00140 00141 // this is completely event driven, so here we just sleep. When data 00142 // is received, it will trigger our listener: 00143 while (1) sleep(1000); 00144 00145 // stop everything and exit: 00146 manager.stop(); 00147 return 0; 00148 } 00149 00150 // ###################################################################### 00151 /* So things look consistent in everyone's emacs... */ 00152 /* Local Variables: */ 00153 /* indent-tabs-mode: nil */ 00154 /* End: */