test-BeoHawkSim.C
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
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/BeoHawk/BeoHawkSim.H"
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052
00053 void handle_keys(nub::soft_ref<OutputFrameSeries> ofs, nub::soft_ref<BeoHawkSim> harrierSim)
00054 {
00055
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;
00074 case 52: upThruster = 3.0; break;
00075 case 33: panThruster = 1.0; break;
00076 case 32: panThruster = -1.0; break;
00077 case 40: forwardThruster = 1.0; break;
00078 case 54: forwardThruster = -1.0; break;
00079 case 39: tiltThruster = 1.0; break;
00080 case 53: tiltThruster = -1.0; break;
00081 case 65:
00082 panThruster = 0;
00083 tiltThruster = 0;
00084 forwardThruster = 0;
00085 upThruster = 0;
00086 break;
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);
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
00145 ModelManager manager("Sub Simulator");
00146
00147
00148
00149
00150 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00151 manager.addSubComponent(ofs);
00152
00153
00154 nub::soft_ref<BeoHawkSim> harrierSim(new BeoHawkSim(manager));
00155 manager.addSubComponent(harrierSim);
00156
00157 nub::soft_ref<Serial> serial(new Serial(manager));
00158 manager.addSubComponent(serial);
00159
00160
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
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
00178
00179
00180
00181
00182
00183 handle_keys(ofs, harrierSim);
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 }
00196 return 0;
00197
00198 }