seaBeeJoyStick-slave.C

00001 /*!@file SeaBee/seaBeeJoyStick-slave */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Michael Montalbo <montalbo@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/seaBeeJoyStick-slave.C $
00034 // $Id: seaBeeJoyStick-slave.C 10794 2009-02-08 06:21:09Z itti $
00035 //
00036 //////////////////////////////////////////////////////////////////////////
00037 
00038 
00039 #include "Beowulf/Beowulf.H"
00040 #include "Component/ModelManager.H"
00041 #include "Devices/JoyStick.H"
00042 #include "Util/Types.H"
00043 #include "Util/log.H"
00044 #include "Util/MathFunctions.H"
00045 #include "Raster/Raster.H"
00046 #include "SeaBee/SubController.H"
00047 #include "Media/FrameSeries.H"
00048 #include "Transport/FrameInfo.H"
00049 #include "Raster/GenericFrame.H"
00050 #include "Media/MediaOpts.H"
00051 #include "Image/CutPaste.H"
00052 #include "Image/DrawOps.H"
00053 #include "Image/ShapeOps.H"
00054 
00055 #include <unistd.h>
00056 #include <signal.h>
00057 #include <math.h>
00058 
00059 #define COM_B_NODE 0
00060 #define INIT_DONE 10001
00061 #define JS_AXIS_UPDATE   20000
00062 #define SUB_GUI_UPDATE   20001
00063 #define ABORT 90000
00064 
00065 #define THRUSTER_UP_LEFT 0
00066 #define THRUSTER_UP_RIGHT 4
00067 #define THRUSTER_UP_BACK 2
00068 #define THRUSTER_FWD_RIGHT 3
00069 #define THRUSTER_FWD_LEFT 1
00070 
00071 float currentXAxis = 0.0;
00072 float currentYAxis = 0.0;
00073 
00074 float currentR = 0.0;
00075 float currentTheta = 0.0;
00076 
00077 
00078 volatile bool keepGoing = false;
00079 std::list<TCPmessage> inMessages;
00080 std::list<TCPmessage> outMessages;
00081 
00082 // gets the messages from COM_B and relay it to the agent manager
00083 void checkInMessages
00084 ( nub::soft_ref<Beowulf> beo );
00085 
00086 void packageGUIMessage
00087 ( nub::soft_ref<SubController> subController,
00088   Image<PixRGB<byte> > frontImg);
00089 
00090 // ######################################################################
00091 //! Signal handler (e.g., for control-C)
00092 void terminate(int s) { LERROR("*** INTERRUPT ***"); keepGoing = false; }
00093 
00094 // ######################################################################
00095 // ######################################################################
00096 int main( int argc, const char* argv[] )
00097 {
00098   MYLOGVERB = LOG_INFO;
00099 
00100   // instantiate a model manager:
00101   ModelManager manager("JoyStick Manager");
00102 
00103 
00104   // Instantiate our various ModelComponents:
00105   nub::soft_ref<Beowulf>
00106     beo(new Beowulf(manager, "Beowulf Slave", "BeowulfSlave", false));
00107   manager.addSubComponent(beo);
00108 
00109   nub::soft_ref<SubController> subController(new SubController(manager));
00110   manager.addSubComponent(subController);
00111 
00112 
00113   nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00114   manager.addSubComponent(ifs);
00115 
00116   manager.exportOptions(MC_RECURSE);
00117 
00118   // Parse command-line:
00119   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00120 
00121   TCPmessage rmsg;     // buffer to receive messages
00122   TCPmessage smsg;     // buffer to send messages
00123   int32 rframe = 0, raction = 0, rnode = -1;
00124 
00125   // catch signals and redirect them to terminate for clean exit:
00126   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00127   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00128   signal(SIGALRM, terminate);
00129 
00130   // let's get all our ModelComponent instances started:
00131   manager.start();
00132 
00133 
00134   while(!beo->receive(rnode, rmsg, rframe, raction, 5));
00135   int initval  = uint(rmsg.getElementInt32());
00136   LINFO("got INIT: %d", initval);
00137 
00138   // send a message of ready to go
00139   smsg.reset(rframe, INIT_DONE);
00140   beo->send(rnode, smsg);
00141 
00142   keepGoing = true;
00143   subController->setMotorsOn(true);
00144 
00145   while(keepGoing)
00146     {
00147       // check messages recieved from COM_A
00148       checkInMessages(beo);
00149 
00150       subController->setThruster(THRUSTER_FWD_RIGHT, (int)(clampValue(-currentYAxis + currentXAxis,-100.0,100.0)));
00151       subController->setThruster(THRUSTER_FWD_LEFT, (int)(clampValue(-currentYAxis - currentXAxis,-100.0,100.0)));
00152 
00153       const FrameState is = ifs->updateNext();
00154       if (is == FRAME_COMPLETE)
00155         break;
00156 
00157       //grab the images
00158       GenericFrame input = ifs->readFrame();
00159       if (!input.initialized())
00160         break;
00161       Image<PixRGB<byte> > frontImg = rescale(input.asRgb(), 320, 280);
00162 
00163       packageGUIMessage(subController, frontImg);
00164 
00165       //send GUI information back to master
00166       if(outMessages.size() > 0)
00167         {
00168           beo->send(-1, outMessages.front());
00169           outMessages.pop_front();
00170         }
00171 
00172     }
00173 
00174   subController->setMotorsOn(false);
00175   // we are done
00176   manager.stop();
00177   return 0;
00178 }
00179 
00180 // ######################################################################
00181 void checkInMessages
00182 ( nub::soft_ref<Beowulf> beo)
00183 {
00184   int32 rframe = 0, raction = 0, rnode = -1;
00185   TCPmessage rmsg;     // buffer to receive messages
00186 
00187   // get all the messages sent in
00188   while(beo->receive(rnode, rmsg, rframe, raction, 5))
00189     {
00190       LINFO("COM_B: recieving a message: %d", raction);
00191 
00192       // check the purpose of the message
00193       switch(raction)
00194         {
00195         case ABORT:
00196           {
00197             keepGoing = false; LINFO("Stop SeaBee COM-B");
00198             break;
00199           }
00200 
00201         case JS_AXIS_UPDATE:
00202           {
00203             int32 num =  rmsg.getElementInt32();
00204             float p = rmsg.getElementFloat();
00205             switch(num)
00206               {
00207               case 0:
00208                 LINFO("X-Axis = %f", p);
00209                 currentXAxis = p;
00210                 break;
00211               case 1:
00212                 LINFO("Y-Axis = %f", p);
00213                 currentYAxis = p;
00214                 break;
00215               case 2:
00216                 LINFO("Depth = %f", p);
00217                 break;
00218               default:
00219                 LERROR("Unknown axis event recieved");
00220               }
00221 
00222             break;
00223           }
00224         default:
00225           LFATAL("Unknown purpose");
00226         }
00227     }
00228 }
00229 
00230 
00231 void packageGUIMessage
00232 ( nub::soft_ref<SubController> subController,
00233   Image<PixRGB<byte> > frontImg)
00234 {
00235   TCPmessage smsg;     // buffer to send messages
00236 
00237   int32 sframe  = 0;
00238   int32 saction = SUB_GUI_UPDATE;
00239 
00240   smsg.reset(sframe, saction);
00241 
00242   smsg.addInt32(int32(*(subController->getIntPressurePtr())));
00243   smsg.addInt32(int32(*(subController->getHeadingPtr())));
00244   smsg.addInt32(int32(*(subController->getPitchPtr())));
00245   smsg.addInt32(int32(*(subController->getRollPtr())));
00246   smsg.addInt32(int32(*(subController->getDepthPtr())));
00247   smsg.addInt32(int32(*(subController->getThruster_Up_Left_Ptr())));
00248   smsg.addInt32(int32(*(subController->getThruster_Up_Right_Ptr())));
00249   smsg.addInt32(int32(*(subController->getThruster_Up_Back_Ptr())));
00250   smsg.addInt32(int32(*(subController->getThruster_Fwd_Left_Ptr())));
00251   smsg.addInt32(int32(*(subController->getThruster_Fwd_Right_Ptr())));
00252 
00253   smsg.addImage(frontImg);
00254 
00255   outMessages.push_back(smsg);
00256 
00257 }
00258 
00259 // ######################################################################
00260 /* So things look consistent in everyone's emacs... */
00261 /* Local Variables: */
00262 /* indent-tabs-mode: nil */
00263 /* End: */
Generated on Sun May 8 08:42:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3