forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSITL_State_common.cpp
More file actions
478 lines (441 loc) · 14.9 KB
/
SITL_State_common.cpp
File metadata and controls
478 lines (441 loc) · 14.9 KB
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
#include <AP_HAL/AP_HAL.h>
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
#include "AP_HAL_SITL.h"
#include "AP_HAL_SITL_Namespace.h"
#include "HAL_SITL_Class.h"
#include "UARTDriver.h"
#include "Scheduler.h"
#include "CANSocketIface.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/select.h>
#include <AP_Param/AP_Param.h>
#include <SITL/SIM_JSBSim.h>
#include <AP_HAL/utility/Socket_native.h>
#include "SITL_State_common.h"
#include <SITL/SIM_RF_Ainstein_LR_D1.h>
#include <SITL/SIM_RF_Ainstein_LR_D1_v19.h>
#include <SITL/SIM_RF_Benewake_TF02.h>
#include <SITL/SIM_RF_Benewake_TF03.h>
#include <SITL/SIM_RF_Benewake_TFmini.h>
#include <SITL/SIM_RF_BLping.h>
#include <SITL/SIM_RF_DTS6012M.h>
#include <SITL/SIM_RF_GYUS42v2.h>
#include <SITL/SIM_RF_JRE.h>
#include <SITL/SIM_RF_Lanbao.h>
#include <SITL/SIM_RF_LeddarOne.h>
#include <SITL/SIM_RF_LightWareSerialBinary.h>
#include <SITL/SIM_RF_LightWareSerial.h>
#include <SITL/SIM_RF_MAVLink.h>
#include <SITL/SIM_RF_MaxsonarSerialLV.h>
#include <SITL/SIM_RF_NMEA.h>
#include <SITL/SIM_RF_NoopLoop.h>
#include <SITL/SIM_RF_RDS02UF.h>
#include <SITL/SIM_RF_TeraRanger_Serial.h>
#include <SITL/SIM_RF_USD1_v0.h>
#include <SITL/SIM_RF_USD1_v1.h>
#include <SITL/SIM_RF_Wasp.h>
#include <SITL/SIM_RF_LightWare_GRF.h>
using namespace HALSITL;
static const struct {
const char *name;
SITL::SerialRangeFinder *(*createfn)();
} serial_rangefinder_definitions[] {
{ "ainsteinlrd1", SITL::RF_Ainstein_LR_D1::create },
{ "ainsteinlrd1_v19", SITL::RF_Ainstein_LR_D1_v19::create },
{ "benewake_tf02", SITL::RF_Benewake_TF02::create },
{ "benewake_tf03", SITL::RF_Benewake_TF03::create },
{ "benewake_tfmini", SITL::RF_Benewake_TFmini::create },
{ "blping", SITL::RF_BLping::create },
{ "dts6012m", SITL::RF_DTS6012M::create },
{ "gyus42v2", SITL::RF_GYUS42v2::create },
{ "jre", SITL::RF_JRE::create },
{ "lanbao", SITL::RF_Lanbao::create },
{ "leddarone", SITL::RF_LeddarOne::create },
{ "leddarone", SITL::RF_LeddarOne::create },
{ "lightwareserial-binary", SITL::RF_LightWareSerialBinary::create },
{ "lightwareserial", SITL::RF_LightWareSerial::create },
{ "lightware_grf", SITL::RF_LightWareGRF::create },
{ "maxsonarseriallv", SITL::RF_MaxsonarSerialLV::create },
{ "nmea", SITL::RF_NMEA::create },
{ "nmea", SITL::RF_NMEA::create },
{ "nooploop_tofsense", SITL::RF_Nooploop::create },
{ "rds02uf", SITL::RF_RDS02UF::create },
#if !defined(HAL_BUILD_AP_PERIPH)
{ "rf_mavlink", SITL::RF_MAVLink::create },
#endif
{ "teraranger_serial", SITL::RF_TeraRanger_Serial::create },
{ "USD1_v0", SITL::RF_USD1_v0::create },
{ "USD1_v1", SITL::RF_USD1_v1::create },
{ "wasp", SITL::RF_Wasp::create },
};
#define streq(a, b) (!strcmp(a, b))
SITL::SerialDevice *SITL_State_Common::create_serial_sim(const char *name, const char *arg, const uint8_t portNumber)
{
for (const auto &definition : serial_rangefinder_definitions) {
if (!streq(definition.name, name)) {
continue;
}
if (num_serial_rangefinders >= ARRAY_SIZE(serial_rangefinders)) {
AP_HAL::panic("Too many simulated serial rangefinders");
}
serial_rangefinders[num_serial_rangefinders] = definition.createfn();
return serial_rangefinders[num_serial_rangefinders++];
}
if (false) {
// this is an empty clause to ease else-if syntax
#if !defined(HAL_BUILD_AP_PERIPH)
} else if (streq(name, "vicon")) {
if (vicon != nullptr) {
AP_HAL::panic("Only one vicon system at a time");
}
vicon = NEW_NOTHROW SITL::Vicon();
return vicon;
#endif
#if AP_SIM_ADSB_ENABLED
} else if (streq(name, "adsb")) {
// ADSB is a stand-out as it is the only serial device which
// will cope with begin() being called multiple times on a
// serial port
if (adsb == nullptr) {
adsb = NEW_NOTHROW SITL::ADSB();
}
sitl_model->set_adsb(adsb);
return adsb;
#endif // AP_SIM_ADSB_ENABLED
} else if (streq(name, "frsky-d")) {
if (frsky_d != nullptr) {
AP_HAL::panic("Only one frsky_d at a time");
}
frsky_d = NEW_NOTHROW SITL::Frsky_D();
return frsky_d;
// } else if (streq(name, "frsky-SPort")) {
// if (frsky_sport != nullptr) {
// AP_HAL::panic("Only one frsky_sport at a time");
// }
// frsky_sport = NEW_NOTHROW SITL::Frsky_SPort();
// return frsky_sport;
// } else if (streq(name, "frsky-SPortPassthrough")) {
// if (frsky_sport_passthrough != nullptr) {
// AP_HAL::panic("Only one frsky_sport passthrough at a time");
// }
// frsky_sport = NEW_NOTHROW SITL::Frsky_SPortPassthrough();
// return frsky_sportpassthrough;
#if AP_SIM_CRSF_ENABLED
} else if (streq(name, "crsf")) {
if (crsf != nullptr) {
AP_HAL::panic("Only one crsf at a time");
}
crsf = NEW_NOTHROW SITL::CRSF();
return crsf;
#endif
#if AP_SIM_PS_LD06_ENABLED
} else if (streq(name, "ld06")) {
if (ld06 != nullptr) {
AP_HAL::panic("Only one ld06 at a time");
}
ld06 = NEW_NOTHROW SITL::PS_LD06();
return ld06;
#endif // AP_SIM_PS_LD06_ENABLED
#if AP_SIM_PS_RPLIDARA2_ENABLED
} else if (streq(name, "rplidara2")) {
if (rplidara2 != nullptr) {
AP_HAL::panic("Only one rplidara2 at a time");
}
rplidara2 = NEW_NOTHROW SITL::PS_RPLidarA2();
return rplidara2;
#endif
#if AP_SIM_PS_RPLIDARA1_ENABLED
} else if (streq(name, "rplidara1")) {
if (rplidara1 != nullptr) {
AP_HAL::panic("Only one rplidara1 at a time");
}
rplidara1 = NEW_NOTHROW SITL::PS_RPLidarA1();
return rplidara1;
#endif
#if AP_SIM_PS_RPLIDARS2_ENABLED
} else if (streq(name, "rplidars2")) {
if (rplidars2 != nullptr) {
AP_HAL::panic("Only one rplidars2 at a time");
}
rplidars2 = NEW_NOTHROW SITL::PS_RPLidarS2();
return rplidars2;
#endif
#if AP_SIM_PS_TERARANGERTOWER_ENABLED
} else if (streq(name, "terarangertower")) {
if (terarangertower != nullptr) {
AP_HAL::panic("Only one terarangertower at a time");
}
terarangertower = NEW_NOTHROW SITL::PS_TeraRangerTower();
return terarangertower;
#endif
#if AP_SIM_PS_LIGHTWARE_SF45B_ENABLED
} else if (streq(name, "sf45b")) {
if (sf45b != nullptr) {
AP_HAL::panic("Only one sf45b at a time");
}
sf45b = NEW_NOTHROW SITL::PS_LightWare_SF45B();
return sf45b;
#endif
#if AP_SIM_ADSB_SAGETECH_MXS_ENABLED
} else if (streq(name, "sagetech_mxs")) {
if (sagetech_mxs != nullptr) {
AP_HAL::panic("Only one sagetech_mxs at a time");
}
sagetech_mxs = NEW_NOTHROW SITL::ADSB_Sagetech_MXS();
if (adsb == nullptr) {
adsb = NEW_NOTHROW SITL::ADSB();
}
sitl_model->set_adsb(adsb);
return sagetech_mxs;
#endif
#if AP_SIM_LOWEHEISER_ENABLED
} else if (streq(name, "loweheiser")) {
sitl_model->set_loweheiser(&_sitl->loweheiser_sim);
return &_sitl->loweheiser_sim;
#endif
#if !defined(HAL_BUILD_AP_PERIPH)
} else if (streq(name, "richenpower")) {
sitl_model->set_richenpower(&_sitl->richenpower_sim);
return &_sitl->richenpower_sim;
} else if (streq(name, "fetteconewireesc")) {
sitl_model->set_fetteconewireesc(&_sitl->fetteconewireesc_sim);
return &_sitl->fetteconewireesc_sim;
} else if (streq(name, "ie24")) {
sitl_model->set_ie24(&_sitl->ie24_sim);
return &_sitl->ie24_sim;
#endif // HAL_BUILD_AP_PERIPH
#if AP_SIM_VOLZ_ENABLED
} else if (streq(name, "volz")) {
sitl_model->set_volz(&_sitl->volz_sim);
return &_sitl->volz_sim;
#endif // AP_SIM_VOLZ_ENABLED
} else if (streq(name, "megasquirt")) {
if (efi_ms != nullptr) {
AP_HAL::panic("Only one megasquirt at a time");
}
efi_ms = NEW_NOTHROW SITL::EFI_MegaSquirt();
return efi_ms;
} else if (streq(name, "hirth")) {
if (efi_hirth != nullptr) {
AP_HAL::panic("Only one hirth at a time");
}
efi_hirth = NEW_NOTHROW SITL::EFI_Hirth();
return efi_hirth;
} else if (streq(name, "VectorNav")) {
if (vectornav != nullptr) {
AP_HAL::panic("Only one VectorNav at a time");
}
vectornav = NEW_NOTHROW SITL::VectorNav(SITL::VectorNav::VNModel::VN300);
return vectornav;
} else if (streq(name, "VectorNav_VN100")) {
if (vectornav != nullptr) {
AP_HAL::panic("Only one VectorNav at a time");
}
vectornav = NEW_NOTHROW SITL::VectorNav(SITL::VectorNav::VNModel::VN100);
return vectornav;
} else if (streq(name, "MicroStrain5")) {
if (microstrain5 != nullptr) {
AP_HAL::panic("Only one MicroStrain5 at a time");
}
microstrain5 = NEW_NOTHROW SITL::MicroStrain5();
return microstrain5;
} else if (streq(name, "MicroStrain7")) {
if (microstrain7 != nullptr) {
AP_HAL::panic("Only one MicroStrain7 at a time");
}
microstrain7 = NEW_NOTHROW SITL::MicroStrain7();
return microstrain7;
} else if (streq(name, "ILabs")) {
if (inertiallabs != nullptr) {
AP_HAL::panic("Only one InertialLabs INS at a time");
}
inertiallabs = NEW_NOTHROW SITL::InertialLabs();
return inertiallabs;
#if AP_SIM_AIS_ENABLED
} else if (streq(name, "AIS")) {
if ((ais != nullptr) || (ais_replay != nullptr)) {
AP_HAL::panic("Only one AIS at a time");
}
ais = NEW_NOTHROW SITL::AIS();
return ais;
} else if (streq(name, "AISReplay")) {
if ((ais != nullptr) || (ais_replay != nullptr)) {
AP_HAL::panic("Only one AIS at a time");
}
ais_replay = NEW_NOTHROW SITL::AIS_Replay();
return ais_replay;
#endif
} else if (strncmp(name, "gps", 3) == 0) {
uint8_t x = atoi(arg);
if (x <= 0 || x > ARRAY_SIZE(gps)) {
AP_HAL::panic("Bad GPS number %u (%s)", x, arg);
}
gps[x-1] = NEW_NOTHROW SITL::GPS(x-1);
return gps[x-1];
} else if (streq(name, "ELRS")) {
// Only allocate if not done already
// MAVLink serial ports have begin called several times
if (elrs == nullptr) {
elrs = NEW_NOTHROW SITL::ELRS(portNumber, this);
_sitl->set_stop_MAVLink_sim_state();
}
return elrs;
}
AP_HAL::panic("unknown simulated device: %s", name);
}
/*
update simulators
*/
void SITL_State_Common::sim_update(void)
{
#if AP_SIM_SOLOGIMBAL_ENABLED
if (gimbal != nullptr) {
gimbal->update(*sitl_model);
}
#endif
#if AP_SIM_ADSB_ENABLED
if (adsb != nullptr) {
adsb->update(*sitl_model);
}
#endif // AP_SIM_ADSB_ENABLED
#if AP_SIM_VICON_ENABLED
if (vicon != nullptr) {
Quaternion attitude;
sitl_model->get_attitude(attitude);
vicon->update(sitl_model->get_location(),
sitl_model->get_position_relhome(),
sitl_model->get_velocity_ef(),
attitude);
}
#endif // AP_SIM_VICON_ENABLED
for (uint8_t i=0; i<num_serial_rangefinders; i++) {
serial_rangefinders[i]->update(sitl_model->rangefinder_range());
}
if (efi_ms != nullptr) {
efi_ms->update();
}
if (efi_hirth != nullptr) {
efi_hirth->update();
}
if (frsky_d != nullptr) {
frsky_d->update();
}
// if (frsky_sport != nullptr) {
// frsky_sport->update();
// }
// if (frsky_sportpassthrough != nullptr) {
// frsky_sportpassthrough->update();
// }
#if AP_SIM_CRSF_ENABLED
if (crsf != nullptr) {
crsf->update();
}
#endif
#if AP_SIM_PS_LD06_ENABLED
if (ld06 != nullptr) {
ld06->update(sitl_model->get_location());
}
#endif // AP_SIM_PS_LD06_ENABLED
#if AP_SIM_PS_RPLIDARA2_ENABLED
if (rplidara2 != nullptr) {
rplidara2->update(sitl_model->get_location());
}
#endif
#if AP_SIM_PS_RPLIDARA1_ENABLED
if (rplidara1 != nullptr) {
rplidara1->update(sitl_model->get_location());
}
#endif
#if AP_SIM_PS_RPLIDARS2_ENABLED
if (rplidars2 != nullptr) {
rplidars2->update(sitl_model->get_location());
}
#endif
#if AP_SIM_PS_TERARANGERTOWER_ENABLED
if (terarangertower != nullptr) {
terarangertower->update(sitl_model->get_location());
}
#endif
#if AP_SIM_PS_LIGHTWARE_SF45B_ENABLED
if (sf45b != nullptr) {
sf45b->update(sitl_model->get_location());
}
#endif
#if AP_SIM_ADSB_SAGETECH_MXS_ENABLED
if (sagetech_mxs != nullptr) {
sagetech_mxs->update(sitl_model);
}
#endif
if (vectornav != nullptr) {
vectornav->update();
}
if (microstrain5 != nullptr) {
microstrain5->update();
}
if (microstrain7 != nullptr) {
microstrain7->update();
}
if (inertiallabs != nullptr) {
inertiallabs->update();
}
#if AP_SIM_AIS_ENABLED
if (ais != nullptr) {
ais->update(*sitl_model);
}
if (ais_replay != nullptr) {
ais_replay->update();
}
#endif
for (uint8_t i=0; i<ARRAY_SIZE(gps); i++) {
if (gps[i] != nullptr) {
gps[i]->update();
}
}
if (elrs != nullptr) {
elrs->update();
}
}
/*
update voltage and current pins
*/
void SITL_State_Common::update_voltage_current(struct sitl_input &input, float throttle)
{
float voltage = 0;
float current = 0;
if (_sitl != nullptr) {
if (_sitl->state.battery_voltage <= 0) {
if (_vehicle == ArduSub) {
voltage = _sitl->batt_voltage;
for (uint8_t i=0; i<6; i++) {
float pwm = input.servos[i];
//printf("i: %d, pwm: %.2f\n", i, pwm);
float fraction = fabsf((pwm - 1500) / 500.0f);
voltage -= fraction * 0.5f;
float draw = fraction * 15;
current += draw;
}
} else {
// simulate simple battery setup
// lose 0.7V at full throttle
voltage = _sitl->batt_voltage - 0.7f * throttle;
// assume 50A at full throttle
current = 50.0f * throttle;
}
} else {
// FDM provides voltage and current
voltage = _sitl->state.battery_voltage;
current = _sitl->state.battery_current;
}
}
// assume 3DR power brick
voltage_pin_voltage = (voltage / 10.1f);
current_pin_voltage = current/17.0f;
// fake battery2 as just a 25% gain on the first one
voltage2_pin_voltage = voltage_pin_voltage * 0.25f;
current2_pin_voltage = current_pin_voltage * 0.25f;
}
#endif // HAL_BOARD_SITL