00001 /*!@file src/SeaBee/test-SeaBeeJoyStick.C test Joystick interface to SeaBee */ 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: Michael Montalbo <montalbo@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/test-SeaBeeJoyStick.C $ 00035 // $Id: 00036 // 00037 00038 #include "Beowulf/Beowulf.H" 00039 #include "Component/ModelManager.H" 00040 #include "Devices/JoyStick.H" 00041 #include "Util/Types.H" 00042 #include "Util/log.H" 00043 #include "Raster/Raster.H" 00044 00045 #include <unistd.h> 00046 #include <signal.h> 00047 00048 #ifdef HAVE_LINUX_JOYSTICK_H 00049 00050 #define COM_B_NODE 0 00051 #define INIT_COMM 10000 00052 #define JS_AXIS_UPDATE 20000 00053 #define ABORT 90000 00054 00055 #define X_MIN -32767 00056 #define X_MAX 32767 00057 #define Y_MIN -32767 00058 #define Y_MAX 32767 00059 #define D_MIN -32767 00060 #define D_MAX 32767 00061 00062 00063 nub::soft_ref<Beowulf> beo; 00064 volatile bool keepGoing = false; 00065 00066 //! A simple joystick listener 00067 class TestJoyStickListener : public JoyStickListener 00068 { 00069 public: 00070 virtual ~TestJoyStickListener() { } 00071 00072 virtual void axis(const uint num, const int16 val) 00073 { 00074 if(keepGoing) 00075 { 00076 00077 int32 rnode = 0; 00078 TCPmessage smsg; // buffer to send messages 00079 00080 int32 sframe = 0; 00081 int32 saction = JS_AXIS_UPDATE; 00082 00083 smsg.reset(sframe, saction); 00084 smsg.addInt32(int32(num)); 00085 00086 //p is the percentage of thrust being applied [-100.0...100.0] for the axis 00087 float p = 0.0; 00088 float v = 0.0; 00089 v = (float)val; 00090 00091 switch(num) 00092 { 00093 case 0: 00094 p = (float)(v/X_MAX*100.0); 00095 LINFO("X-Axis = %f", p); 00096 break; 00097 case 1: 00098 p = v/Y_MAX*100.0; 00099 LINFO("Y-Axis = %f", p); 00100 break; 00101 case 2: 00102 p = v/D_MAX*100.0; 00103 LINFO("Depth = %f", p); 00104 break; 00105 default: 00106 LERROR("Unknown axis event recieved"); 00107 } 00108 00109 smsg.addFloat(p); 00110 00111 // send the message to COM_B 00112 beo->send(rnode, smsg); 00113 00114 } 00115 } 00116 00117 virtual void button(const uint num, const bool state) 00118 { 00119 LINFO("Button[%d] = %s", num, state?"true":"false"); 00120 } 00121 }; 00122 00123 #endif 00124 00125 // ###################################################################### 00126 //! Signal handler (e.g., for control-C) 00127 void terminate(int s) 00128 { LERROR("*** INTERRUPT ***"); keepGoing = false; exit(1); } 00129 00130 00131 // ###################################################################### 00132 //! Test JoyStick code 00133 /*! Test Joystick code. */ 00134 int main(const int argc, const char **argv) 00135 { 00136 #ifndef HAVE_LINUX_JOYSTICK_H 00137 00138 LFATAL("<linux/joystick.h> must be installed to use this program"); 00139 00140 #else 00141 00142 // get a manager going: 00143 ModelManager manager("JoyStick Manager"); 00144 00145 // instantiate our model components: 00146 nub::soft_ref<JoyStick> js(new JoyStick(manager) ); 00147 manager.addSubComponent(js); 00148 00149 beo.reset(new Beowulf(manager, "Beowulf Master", "BeowulfMaster", true)); 00150 manager.addSubComponent(beo); 00151 00152 manager.exportOptions(MC_RECURSE); 00153 00154 // Parse command-line: 00155 if (manager.parseCommandLine(argc, argv, "[device]", 0, 1) == false) 00156 return(1); 00157 00158 // let's configure our device: 00159 if (manager.numExtraArgs() > 0) 00160 js->setModelParamVal("JoyStickDevName", 00161 manager.getExtraArg(0), MC_RECURSE); 00162 00163 // register a listener: 00164 rutz::shared_ptr<TestJoyStickListener> lis(new TestJoyStickListener); 00165 rutz::shared_ptr<JoyStickListener> lis2; lis2.dynCastFrom(lis); // cast down 00166 js->setListener(lis2); 00167 00168 TCPmessage rmsg; // buffer to receive messages 00169 TCPmessage smsg; // buffer to send messages 00170 int32 rframe = 0, raction = 0, rnode = 0; 00171 00172 00173 // catch signals and redirect them to terminate for clean exit: 00174 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00175 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00176 signal(SIGALRM, terminate); 00177 00178 // get started: 00179 manager.start(); 00180 00181 // send params to dorsal and ventral node to initialize contact: 00182 smsg.reset(0, INIT_COMM); 00183 smsg.addInt32(int32(235)); 00184 00185 // send the same initial values message to CPU_B 00186 beo->send(COM_B_NODE, smsg); 00187 00188 // SYNCHRONIZATION: wait until the other board is ready 00189 LINFO("waiting until COM_B is ready to go"); 00190 rnode = COM_B_NODE; 00191 while(!beo->receive(rnode, rmsg, rframe, raction, 5)); 00192 rmsg.reset(rframe, raction); 00193 LINFO("%d is ready", rnode); 00194 Raster::waitForKey(); 00195 00196 keepGoing = true; 00197 // Everything is event-driven so in our main loop here we just sleep: 00198 while(keepGoing) sleep(100); 00199 00200 // send abort to COM_B 00201 smsg.reset(0, ABORT); 00202 beo->send(COM_B_NODE, smsg); 00203 00204 // stop everything and exit: 00205 manager.stop(); 00206 return 0; 00207 00208 #endif // HAVE_LINUX_JOYSTICK_H 00209 00210 } 00211 00212 // ###################################################################### 00213 /* So things look consistent in everyone's emacs... */ 00214 /* Local Variables: */ 00215 /* indent-tabs-mode: nil */ 00216 /* End: */