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 "Robots/HeliBot/HeliPose.H"
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/DrawOps.H"
00042 #include "Image/ColorOps.H"
00043 #include "Image/FilterOps.H"
00044 #include "GUI/ImageDisplayStream.H"
00045 #include "GUI/XWinManaged.H"
00046 #include "Devices/Serial.H"
00047 #include "Devices/WiiMote.H"
00048 #include "Util/CpuTimer.H"
00049 #include "Util/Timer.H"
00050 #include "Image/MathOps.H"
00051 #include "Image/Transforms.H"
00052 #include "Image/Layout.H"
00053 #include "Media/FrameSeries.H"
00054 #include "Transport/FrameInfo.H"
00055 #include "Raster/GenericFrame.H"
00056 #include "Raster/Raster.H"
00057 #include "GUI/XWinManaged.H"
00058 #include "GUI/DebugWin.H"
00059 #include "Controllers/PID.H"
00060 #include "Devices/IMU_SFE_Atomic.H"
00061
00062 #include <pthread.h>
00063
00064 #define KEY_UP 98
00065 #define KEY_DOWN 104
00066 #define KEY_LEFT 100
00067 #define KEY_RIGHT 102
00068
00069 int getKey(nub::ref<OutputFrameSeries> &ofs)
00070 {
00071 const nub::soft_ref<ImageDisplayStream> ids =
00072 ofs->findFrameDestType<ImageDisplayStream>();
00073
00074 const rutz::shared_ptr<XWinManaged> uiwin =
00075 ids.is_valid()
00076 ? ids->getWindow("Output")
00077 : rutz::shared_ptr<XWinManaged>();
00078 return uiwin->getLastKeyPress();
00079 }
00080
00081 Point2D<int> getMouseClick(nub::ref<OutputFrameSeries> &ofs, const char* wname)
00082 {
00083 const nub::soft_ref<ImageDisplayStream> ids =
00084 ofs->findFrameDestType<ImageDisplayStream>();
00085
00086 const rutz::shared_ptr<XWinManaged> uiwin =
00087 ids.is_valid()
00088 ? ids->getWindow(wname)
00089 : rutz::shared_ptr<XWinManaged>();
00090
00091 if (uiwin.is_valid())
00092 return uiwin->getLastMouseClick();
00093 else
00094 return Point2D<int>(-1,-1);
00095 }
00096
00097 struct RadioStatus
00098 {
00099 int thr;
00100 int elevator;
00101 int aileron;
00102 int yaw;
00103
00104 bool pushButton;
00105 bool rightSwitch;
00106 bool leftSwitch;
00107 };
00108
00109
00110 void drawWorld(float x, float y, float z, float rot, nub::ref<OutputFrameSeries>& ofs)
00111 {
00112
00113 Image<PixRGB<byte> > world(255, 255, ZEROS);
00114 Point2D<int> loc(128+(int)(x/4.0), 128+(int)(y/4.0));
00115
00116 float heliLength = 30;
00117 int headX = int(cos(rot)*heliLength/2);
00118 int headY = int(sin(rot)*heliLength/2);
00119
00120 drawCircle(world, Point2D<int>(loc.i + headX, loc.j - headY), 6, PixRGB<byte>(0,255,0), 3);
00121 drawLine(world, loc, rot, heliLength, PixRGB<byte>(0,255,0), 3);
00122
00123 ofs->writeRGB(world, "world", FrameInfo("world", SRC_POS));
00124 }
00125
00126 RadioStatus getRadioStatus(nub::ref<Serial>& serial)
00127 {
00128 RadioStatus radioStatus;
00129
00130 std::vector<unsigned char> data = serial->readFrame(0, 255);
00131
00132 if(data.size() == 9)
00133 {
00134
00135 radioStatus.thr = (data[0]) | (data[1] << 8);
00136 radioStatus.elevator = (data[2]) | (data[3] << 8);
00137 radioStatus.aileron = (data[4]) | (data[5] << 8);
00138 radioStatus.yaw = (data[6]) | (data[7] << 8);
00139 unsigned char switches = data[8];
00140
00141 radioStatus.pushButton = switches & 0x04;
00142 radioStatus.rightSwitch = switches & 0x08;
00143 radioStatus.leftSwitch = switches & 0x10;
00144 } else {
00145 LERROR("BAD RADIO FRAME SIZE!");
00146 radioStatus.thr = -1;
00147 radioStatus.elevator = -1;
00148 radioStatus.aileron = -1;
00149 radioStatus.yaw = -1;
00150 }
00151
00152
00153 return radioStatus;
00154
00155 }
00156
00157 void sendRadioStatus(const RadioStatus& radioStatus, nub::ref<Serial>& serial)
00158 {
00159 unsigned char data[10];
00160
00161 data[0] = 255;
00162 data[1] = 255;
00163
00164 data[2] = (radioStatus.thr >> 8);
00165 data[3] = radioStatus.thr;
00166
00167 data[4] = (radioStatus.elevator >> 8);
00168 data[5] = radioStatus.elevator;
00169
00170 data[6] = (radioStatus.aileron >> 8);
00171 data[7] = radioStatus.aileron;
00172
00173 data[8] = (radioStatus.yaw >> 8);
00174 data[9] = radioStatus.yaw;
00175
00176 serial->write(data, 10);
00177 }
00178
00179 void tunePID(const RadioStatus& radioStatus, PID<float>& pid)
00180 {
00181
00182 if (!radioStatus.pushButton)
00183 {
00184 if (radioStatus.leftSwitch)
00185 pid.setPIDPgain(pid.getPIDPgain() + 0.0001);
00186 else
00187 {
00188 float gain = pid.getPIDPgain() - 0.0001;
00189 if (gain > 0)
00190 pid.setPIDPgain(gain);
00191 }
00192 LINFO("PID p gain %f", pid.getPIDPgain());
00193 }
00194 }
00195
00196 int main(int argc, const char **argv)
00197 {
00198
00199 ModelManager manager("Test wiimote");
00200
00201 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00202 manager.addSubComponent(ofs);
00203
00204 nub::ref<Serial> serial(new Serial(manager));
00205 manager.addSubComponent(serial);
00206
00207 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00208
00209
00210 nub::ref<HeliPose> heliPose(new HeliPose(manager, ifs, ofs));
00211 manager.addSubComponent(heliPose);
00212
00213 PID<float> yawPid(0.0010, 0.01, 0.0, -1, 1, 0, 0, 0, 1.0, -1.0);
00214 PID<float> posXPid(0.0010, 0.0, 0.0, -1, 1, 0, 0, 0, 1.0, -1.0);
00215 PID<float> posYPid(0.00270, 0.0, 0.0, -1, 1, 0, 0, 0, 1.0, -1.0);
00216
00217 serial->configure("/dev/ttyUSB1", 115200, "8N1", false, false, 0);
00218
00219
00220 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00221
00222 manager.start();
00223
00224 Timer timer;
00225 timer.reset();
00226
00227 LINFO("Press enter to calculate the mean/std of the IMU");
00228 getchar();
00229 heliPose->getIMUBias();
00230 LINFO("Done");
00231
00232 float frame = 0;
00233 float currentYaw = 0;
00234 while(1)
00235 {
00236
00237
00238 HeliPose::Pose pose = heliPose->getPose();
00239
00240 drawWorld(pose.translation.x,pose.translation.y,pose.translation.z,pose.rotation.z, ofs);
00241
00242 RadioStatus radioStatus;
00243
00244
00245 printf("%f %i %i %i %i %f %f %f %f %f %f\n",
00246 timer.getSecs(),
00247 radioStatus.thr, radioStatus.elevator, radioStatus.aileron, radioStatus.yaw,
00248 pose.accelX, pose.accelY, pose.accelZ,
00249 pose.roll, pose.pitch, pose.yaw);
00250
00251
00252
00253
00254 fflush(stdout);
00255
00256 tunePID(radioStatus, yawPid);
00257
00258
00259
00260 if (frame > 500)
00261 {
00262 frame = 0;
00263 currentYaw = pose.rotation.z*180/M_PI;
00264 } else {
00265 frame++;
00266 }
00267
00268 if (pose.valid)
00269 {
00270 float yawInput = -1*yawPid.update(currentYaw, pose.rotation.z*180/M_PI);
00271
00272
00273
00274 radioStatus.yaw += (int)(yawInput*(1523-510));
00275
00276
00277
00278 }
00279
00280
00281
00282
00283 sendRadioStatus(radioStatus, serial);
00284
00285 ofs->updateNext();
00286 frame++;
00287 }
00288
00289
00290 manager.stop();
00291
00292
00293 return 0;
00294 }
00295
00296
00297
00298
00299
00300