test-harrierSim.C

00001 /*!@file AppDevices/test-armSim.C Test the sub simulator */
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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/HarrierBot/test-harrierSim.C $
00035 // $Id: test-harrierSim.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 
00039 
00040 #include "Component/ModelManager.H"
00041 #include "Raster/GenericFrame.H"
00042 #include "Image/Layout.H"
00043 #include "Media/FrameSeries.H"
00044 #include "Transport/FrameInfo.H"
00045 #include "Image/MatrixOps.H"
00046 #include "GUI/ImageDisplayStream.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Devices/Serial.H"
00049 #include "Robots/HarrierBot/HarrierSim.H"
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 
00053 void handle_keys(nub::soft_ref<OutputFrameSeries> ofs, nub::soft_ref<HarrierSim> harrierSim)
00054 {
00055   //handle keyboard input
00056   const nub::soft_ref<ImageDisplayStream> ids =
00057     ofs->findFrameDestType<ImageDisplayStream>();
00058 
00059   const rutz::shared_ptr<XWinManaged> uiwin =
00060     ids.is_valid()
00061     ? ids->getWindow("harrierSim")
00062     : rutz::shared_ptr<XWinManaged>();
00063 
00064   int key = uiwin->getLastKeyPress();
00065   if (key != -1)
00066   {
00067     float panThruster = 0;
00068     float tiltThruster = 0;
00069     float forwardThruster = 0;
00070     float upThruster = 0;
00071     switch(key)
00072     {
00073       case 38: upThruster = -3.0; break; //a
00074       case 52: upThruster = 3.0; break; //z
00075       case 33: panThruster = 1.0; break; //p
00076       case 32: panThruster = -1.0; break; //o
00077       case 40: forwardThruster = 1.0; break; //d
00078       case 54: forwardThruster = -1.0; break; //c
00079       case 39: tiltThruster = 1.0; break; //s
00080       case 53: tiltThruster = -1.0; break; //x
00081       case 65: //stop
00082                panThruster = 0;
00083                tiltThruster = 0;
00084                forwardThruster = 0;
00085                upThruster = 0;
00086                break; // space
00087 
00088     }
00089     harrierSim->setThrusters(panThruster, tiltThruster, forwardThruster, upThruster);
00090 
00091     LINFO("Key is %i\n", key);
00092   }
00093 }
00094 
00095 struct RadioStatus
00096 {
00097   int thr;
00098   int elevator;
00099   int aileron;
00100   int yaw;
00101 
00102   int ch1;
00103   int ch2;
00104   int ch3;
00105   int ch4;
00106 
00107 };
00108 
00109 RadioStatus getRadioStatus(nub::ref<Serial> serial)
00110 {
00111   RadioStatus radioStatus;
00112 
00113   std::vector<unsigned char> data = serial->readFrame(252, 85, 16); //start frame 0 end frame 255
00114 
00115   if(data.size() == 16)
00116   {
00117 
00118     radioStatus.aileron    = (data[0] <<8) | (data[1] );
00119     radioStatus.elevator   = (data[2] <<8) | (data[3] );
00120     radioStatus.thr         = (data[4] <<8) | (data[5] );
00121     radioStatus.yaw        = (data[6] <<8) | (data[7] );
00122 
00123     radioStatus.ch1   = (data[8]  << 8) | (data[9] );
00124     radioStatus.ch2   = (data[10] << 8) | (data[11] );
00125     radioStatus.ch3   = (data[12] << 8) | (data[13] );
00126     radioStatus.ch4   = (data[14] << 8) | (data[15] );
00127 
00128   } else {
00129     LERROR("BAD RADIO FRAME SIZE!");
00130     radioStatus.thr = -1;
00131     radioStatus.elevator = -1;
00132     radioStatus.aileron = -1;
00133     radioStatus.yaw = -1;
00134   }
00135 
00136 
00137   return radioStatus;
00138 
00139 }
00140 
00141 
00142 int main(int argc, char *argv[])
00143 {
00144   // Instantiate a ModelManager:
00145   ModelManager manager("Sub Simulator");
00146 
00147   //nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00148   //manager.addSubComponent(ifs);
00149 
00150   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00151   manager.addSubComponent(ofs);
00152 
00153   // Instantiate our various ModelComponents:
00154   nub::soft_ref<HarrierSim> harrierSim(new HarrierSim(manager));
00155   manager.addSubComponent(harrierSim);
00156 
00157   nub::soft_ref<Serial> serial(new Serial(manager));
00158   manager.addSubComponent(serial);
00159 
00160   // Parse command-line:
00161   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00162 
00163   serial->configure("/dev/ttyUSB0", 115200, "8N1", false, false, 0);
00164 
00165 
00166   // let's get all our ModelComponent instances started:
00167   manager.start();
00168 
00169 
00170   while(1){
00171     Layout<PixRGB<byte> > outDisp;
00172 
00173     harrierSim->simLoop();
00174     Image<PixRGB<byte> > downwardCam = flipVertic(harrierSim->getFrame(-1));
00175     ofs->writeRGB(downwardCam, "harrierSim", FrameInfo("harrierSim", SRC_POS));
00176 
00177     //RadioStatus radioStatus = getRadioStatus(serial);
00178     ////printf("%i\t\t%i\t\t%i\t\t%i\t\t%i\t\t%i\t\t%i\t\t %i\n",
00179     ////    radioStatus.thr, radioStatus.elevator, radioStatus.aileron, radioStatus.yaw,
00180     ////    radioStatus.ch1, radioStatus.ch2, radioStatus.ch3, radioStatus.ch4 );
00181     //
00182 
00183 //  //  handle_keys(ofs, harrierSim);
00184     //float upThruster = -25*((float)radioStatus.thr* (1.0/(1890.0-1130.0)) - 1.498684);
00185     //float panThruster = -4.0*((float)radioStatus.yaw* (1.0/(1845.0-1070.0)) - 1.917419);
00186     //float pitchThruster = -4.0*((float)radioStatus.elevator* (1.0/(1890.0-1060.0)) - 1.709639);
00187     //float rollThruster = 4.0*((float)radioStatus.aileron* (1.0/(1860.0-1050.0)) - 1.919753);
00188     ////LINFO("%f %f %f %f",
00189     ////    upThruster,
00190     ////    panThruster,
00191     ////    pitchThruster,
00192     ////    rollThruster);
00193     //harrierSim->setThrusters(panThruster, pitchThruster, rollThruster, upThruster);
00194 
00195   }
00196   return 0;
00197 
00198 }
Generated on Sun May 8 08:05:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3