-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsim.cc
489 lines (408 loc) · 7.82 KB
/
sim.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <iostream>
#include <string>
#include <math.h>
#include "sim.hh"
#include "./comm/udp_client_server.hh"
#include "./XPlane/XPdata.hh"
#include "./environment/gravity.hh"
Sim::Sim():
_ip("0.0.0.0"),
_simulator(NONE),
_server(udp_server("0.0.0.0", -1)),
_client(udp_client("0.0.0.0", -1)),
_aoa(-999), _aos(-999),
_atm_pressure(-999),
_roll(-999), _pitch(-999), _yaw(-999),
_gear(-999), _brakew(-999), _brakel(-999), _braker(-999),
_latitude(-999), _longitude(-999), _altitude_msl(-999),
_gx(-999), _gy(-999), _gz(-999),
_ax(-999), _ay(-999), _az(-999),
_wx(-999), _wy(-999), _wz(-999),
_vkias(-999),
_t1c(-999), _t2c(-999), _t3c(-999), _t4c(-999),
_t5c(-999), _t6c(-999), _t7c(-999), _t8c(-999),
_t1a(-999), _t2a(-999), _t3a(-999), _t4a(-999),
_t5a(-999), _t6a(-999), _t7a(-999), _t8a(-999),
_x(-999), _y(-999), _z(-999),
_ve(-999), _vn(-999), _vu(-999),
_elevc(-999), _ailc(-999), _rudc(-999)
{
}
Sim::Sim(std::string ip, int udp_port_in,
int udp_port_out, Simulator simulator):
_ip(ip),
_simulator(simulator),
_server(udp_server(_ip.c_str(), udp_port_out)),
_client(udp_client(_ip.c_str(), udp_port_in)),
_aoa(-999), _aos(-999),
_atm_pressure(-999),
_roll(-999), _pitch(-999), _yaw(-999),
_gear(-999), _brakew(-999), _brakel(-999), _braker(-999),
_latitude(-999), _longitude(-999), _altitude_msl(-999),
_gx(-999), _gy(-999), _gz(-999),
_ax(-999), _ay(-999), _az(-999),
_wx(-999), _wy(-999), _wz(-999),
_vkias(-999),
_t1c(-999), _t2c(-999), _t3c(-999), _t4c(-999),
_t5c(-999), _t6c(-999), _t7c(-999), _t8c(-999),
_t1a(-999), _t2a(-999), _t3a(-999), _t4a(-999),
_t5a(-999), _t6a(-999), _t7a(-999), _t8a(-999),
_x(-999), _y(-999), _z(-999),
_ve(-999), _vn(-999), _vu(-999),
_elevc(-999), _ailc(-999), _rudc(-999)
{
}
Sim::~Sim()
{
}
void Sim::connect(){
_server.connect();
_client.connect();
}
int Sim::_readDatagram(){
int readBytes = 0;
_datagram.resize(1000, 0);
readBytes = _server.recv(&*_datagram.begin(), _datagram.size(), 0);
//std::cout << "Read bytes: " << readBytes << std::endl;
if (readBytes < 0)
return readBytes;
_datagram.resize(readBytes);
return readBytes;
}
void Sim::readFromSim(){
if(_simulator == XPLANE){
XPdataVector v;
if(_readDatagram() > 0)
{
int isData = 1;
const char *data= "DATA";
std::vector<char>::iterator i(_datagram.begin());
std::vector<char>::iterator ii(_datagram.end());
for(int iii = 0; iii < 4; ++iii)
if (*(i+iii) != data[iii])
isData = 0;
if(isData){
std::tr1::shared_ptr<XPdata> _xpd;
i+=5;
while(i != (ii)){
_xpd.reset(XPdata::create(i));
if(_xpd)
v.push_back(_xpd);
}
}
for(auto v_i(v.begin()); v_i != v.end(); ++v_i)
(*v_i)->accept(this);
}
}
}
Simulator Sim::get_simulator()
{
return _simulator;
}
void Sim::clearvout()
{
vout.clear();
}
float Sim::get_aoa()
{
return _aoa;
}
float Sim::get_aos()
{
return _aos;
}
float Sim::get_atm_pressure()
{
return _atm_pressure;
}
float Sim::get_roll()
{
return _roll;
}
float Sim::get_pitch()
{
return _pitch;
}
float Sim::get_yaw()
{
return _yaw;
}
float Sim::get_gear()
{
return _gear;
}
float Sim::get_brakew()
{
return _brakew;
}
float Sim::get_brakel()
{
return _brakel;
}
float Sim::get_braker()
{
return _braker;
}
float Sim::get_latitude()
{
return _latitude;
}
float Sim::get_longitude()
{
return _longitude;
}
float Sim::get_altitude_msl()
{
return _altitude_msl;
}
float Sim::get_gx()
{
return _gx;
}
float Sim::get_gy()
{
return _gy;
}
float Sim::get_gz()
{
return _gz;
}
float Sim::get_ax()
{
return _ax;
}
float Sim::get_ay()
{
return _ay;
}
float Sim::get_az()
{
return _az;
}
float Sim::get_wx()
{
return _wx;
}
float Sim::get_wy()
{
return _wy;
}
float Sim::get_wz()
{
return _wz;
}
float Sim::get_vkias()
{
return _vkias;
}
float Sim::get_t1c()
{
return _t1c;
}
float Sim::get_t2c()
{
return _t2c;
}
float Sim::get_t3c()
{
return _t3c;
}
float Sim::get_t4c()
{
return _t4c;
}
float Sim::get_t5c()
{
return _t5c;
}
float Sim::get_t6c()
{
return _t6c;
}
float Sim::get_t7c()
{
return _t7c;
}
float Sim::get_t8c()
{
return _t8c;
}
float Sim::get_t1a()
{
return _t1a;
}
float Sim::get_t2a()
{
return _t2a;
}
float Sim::get_t3a()
{
return _t3a;
}
float Sim::get_t4a()
{
return _t4a;
}
float Sim::get_t5a()
{
return _t5a;
}
float Sim::get_t6a()
{
return _t6a;
}
float Sim::get_t7a()
{
return _t7a;
}
float Sim::get_t8a()
{
return _t8a;
}
float Sim::get_x()
{
return _x;
}
float Sim::get_y()
{
return _y;
}
float Sim::get_z()
{
return _z;
}
float Sim::get_ve()
{
return _ve;
}
float Sim::get_vn()
{
return _vn;
}
float Sim::get_vu()
{
return _vu;
}
float Sim::get_elevc()
{
return _elevc;
}
float Sim::get_ailc()
{
return _ailc;
}
float Sim::get_rudc()
{
return _rudc;
}
void Sim::set_tc(float t1, float t2, float t3, float t4,
float t5, float t6, float t7, float t8)
{
if(_simulator == XPLANE)
{
vout.push_back(std::tr1::shared_ptr<XPdata>
(new XPthrottelc(t1, t2, t3, t4, t5, t6, t7, t8)));
}
}
void Sim::sendToSim()
{
if(_simulator == XPLANE)
{
std::vector<char> _dtg;
XPdata::prologue_to_dtg(_dtg);
for (XPdataVector::const_iterator i(vout.begin()),
i_end(vout.end()); i != i_end; ++i){
(*i)->to_dtg(_dtg);
}
_client.send(&*_dtg.begin(), _dtg.size());
clearvout();
}
}
void Sim::visit(XPaerangles * xp)
{
_aoa = xp->get_aoa()*M_PI/180;
_aos = xp->get_aos()*M_PI/180;
}
void Sim::visit(XPatmosphere * xp)
{
_atm_pressure = xp->get_AMpressure();
}
void Sim::visit(XPattitude * xp)
{
_roll = xp->get_roll()*M_PI/180;
_pitch = xp->get_pitch()*M_PI/180;
_yaw = xp->get_yaw()*M_PI/180;
}
void Sim::visit(XPgearbrakes * xp)
{
_brakew = xp->get_bw();
_braker = xp->get_br();
_brakel = xp->get_bl();
_gear = xp->get_gear();
}
void Sim::visit(XPgps * xp)
{
_latitude = xp->get_latitude()*M_PI/180;
_longitude = xp->get_longitude()*M_PI/180;
_altitude_msl = xp->get_altitudeFmsl()*0.3048; // Feet to meters
}
void Sim::visit(XPloads * xp)
{
// WGS84 gravity model
float g = _gravity.g(_latitude, _altitude_msl);
// NED frame of coordinates
_gx = xp->get_gAxial();
_gy = xp->get_gSide();
_gz = xp->get_gNormal();
_ax = _gx * g;
_ay = _gy * g;
_az = _gz * g;
}
void Sim::visit(XPpqr * xp)
{
_wx = xp->get_p();
_wy = xp->get_q();
_wz = xp->get_r();
}
void Sim::visit(XPspeeds * xp)
{
_vkias = xp->get_vindKias();
}
void Sim::visit(XPthrottela * xp)
{
_t1a = xp->get_t1a();
_t2a = xp->get_t2a();
_t3a = xp->get_t3a();
_t4a = xp->get_t4a();
_t5a = xp->get_t5a();
_t6a = xp->get_t6a();
_t7a = xp->get_t7a();
_t8a = xp->get_t8a();
}
void Sim::visit(XPthrottelc * xp)
{
_t1c = xp->get_t1c();
_t2c = xp->get_t2c();
_t3c = xp->get_t3c();
_t4c = xp->get_t4c();
_t5c = xp->get_t5c();
_t6c = xp->get_t6c();
_t7c = xp->get_t7c();
_t8c = xp->get_t8c();
}
void Sim::visit(XPxyz * xp)
{
// In XPlane, X points at the EAST, Y at UP and Z at SOUTH
_x = -xp->get_z();
_y = xp->get_x();
_z = -xp->get_y();
_ve = xp->get_vx();
_vn = -xp->get_vz();
_vu = xp->get_vy();
}
void Sim::visit(XPyoke * xp)
{
_ailc = xp->get_ail();
_elevc = xp->get_elev();
_rudc = xp->get_rud();
}