test-SeaBeeJoyStick.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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;
00079
00080 int32 sframe = 0;
00081 int32 saction = JS_AXIS_UPDATE;
00082
00083 smsg.reset(sframe, saction);
00084 smsg.addInt32(int32(num));
00085
00086
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
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
00127 void terminate(int s)
00128 { LERROR("*** INTERRUPT ***"); keepGoing = false; exit(1); }
00129
00130
00131
00132
00133
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
00143 ModelManager manager("JoyStick Manager");
00144
00145
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
00155 if (manager.parseCommandLine(argc, argv, "[device]", 0, 1) == false)
00156 return(1);
00157
00158
00159 if (manager.numExtraArgs() > 0)
00160 js->setModelParamVal("JoyStickDevName",
00161 manager.getExtraArg(0), MC_RECURSE);
00162
00163
00164 rutz::shared_ptr<TestJoyStickListener> lis(new TestJoyStickListener);
00165 rutz::shared_ptr<JoyStickListener> lis2; lis2.dynCastFrom(lis);
00166 js->setListener(lis2);
00167
00168 TCPmessage rmsg;
00169 TCPmessage smsg;
00170 int32 rframe = 0, raction = 0, rnode = 0;
00171
00172
00173
00174 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00175 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00176 signal(SIGALRM, terminate);
00177
00178
00179 manager.start();
00180
00181
00182 smsg.reset(0, INIT_COMM);
00183 smsg.addInt32(int32(235));
00184
00185
00186 beo->send(COM_B_NODE, smsg);
00187
00188
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
00198 while(keepGoing) sleep(100);
00199
00200
00201 smsg.reset(0, ABORT);
00202 beo->send(COM_B_NODE, smsg);
00203
00204
00205 manager.stop();
00206 return 0;
00207
00208 #endif // HAVE_LINUX_JOYSTICK_H
00209
00210 }
00211
00212
00213
00214
00215
00216