-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
telemetry.cpp
539 lines (451 loc) · 13.4 KB
/
telemetry.cpp
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "multi.h"
#include "pulses/afhds3.h"
#include "pulses/flysky.h"
#include "mixer_scheduler.h"
#include "io/multi_protolist.h"
#include "hal/module_port.h"
#if defined(LIBOPENUI)
#include "libopenui.h"
#endif
#if !defined(SIMU)
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
#endif
#include "spektrum.h"
#if defined(CROSSFIRE)
#include "crossfire.h"
#endif
#if defined(GHOST)
#include "ghost.h"
#endif
#if defined(MULTIMODULE)
#include "hitec.h"
#include "hott.h"
#include "multi.h"
#endif
#if defined(MULTIMODULE) || defined(PPM)
#include "mlink.h"
#endif
#if defined(MULTIMODULE) || defined(AFHDS2) || defined(AFHDS3)
#include "flysky_ibus.h"
#endif
uint8_t telemetryStreaming = 0;
uint8_t telemetryRxBuffer[TELEMETRY_RX_PACKET_SIZE];
uint8_t telemetryRxBufferCount = 0;
uint8_t telemetryState = TELEMETRY_INIT;
TelemetryData telemetryData;
#if defined(INTERNAL_MODULE_SERIAL_TELEMETRY)
static uint8_t intTelemetryRxBuffer[TELEMETRY_RX_PACKET_SIZE];
static uint8_t intTelemetryRxBufferCount;
#endif
static rxStatStruct rxStat;
uint8_t * getTelemetryRxBuffer(uint8_t moduleIdx)
{
#if defined(INTERNAL_MODULE_SERIAL_TELEMETRY)
if (moduleIdx == INTERNAL_MODULE)
return intTelemetryRxBuffer;
#endif
return telemetryRxBuffer;
}
uint8_t &getTelemetryRxBufferCount(uint8_t moduleIdx)
{
#if defined(INTERNAL_MODULE_SERIAL_TELEMETRY)
if (moduleIdx == INTERNAL_MODULE)
return intTelemetryRxBufferCount;
#endif
return telemetryRxBufferCount;
}
rxStatStruct *getRxStatLabels() {
// default to RSSI/db notation
rxStat.label = STR_RXSTAT_LABEL_RSSI;
rxStat.unit = STR_RXSTAT_UNIT_DBM;
// Currently we can only display a single rx stat in settings/telemetry.
// If both modules are used we choose the internal one
// TODO: have to rx stat sections in settings/telemetry
uint8_t moduleToUse = INTERNAL_MODULE;
if(g_model.moduleData[INTERNAL_MODULE].type == MODULE_TYPE_NONE &&
g_model.moduleData[EXTERNAL_MODULE].type != MODULE_TYPE_NONE) {
moduleToUse = EXTERNAL_MODULE;
}
uint8_t moduleType = g_model.moduleData[moduleToUse].type;
switch (moduleType) {
#if defined(MULTIMODULE)
case MODULE_TYPE_MULTIMODULE: {
uint8_t multiProtocol = g_model.moduleData[moduleToUse].multi.rfProtocol;
if (multiProtocol == MODULE_SUBTYPE_MULTI_FS_AFHDS2A ||
multiProtocol == MODULE_SUBTYPE_MULTI_HOTT ||
multiProtocol == MODULE_SUBTYPE_MULTI_MLINK) {
rxStat.label = STR_RXSTAT_LABEL_RQLY;
rxStat.unit = STR_RXSTAT_UNIT_PERCENT;
}
} break;
#endif
case MODULE_TYPE_PPM:
if (g_model.moduleData[moduleToUse].subType == PPM_PROTO_TLM_MLINK) {
rxStat.label = STR_RXSTAT_LABEL_RQLY;
rxStat.unit = STR_RXSTAT_UNIT_PERCENT;
}
break;
case MODULE_TYPE_CROSSFIRE:
case MODULE_TYPE_GHOST:
rxStat.label = STR_RXSTAT_LABEL_RQLY;
rxStat.unit = STR_RXSTAT_UNIT_PERCENT;
break;
#if defined(PCBNV14) && defined(AFHDS2)
case MODULE_TYPE_FLYSKY_AFHDS2A:
extern uint32_t NV14internalModuleFwVersion;
if (moduleToUse == INTERNAL_MODULE) {
if (NV14internalModuleFwVersion >= 0x1000E) {
rxStat.label = STR_RXSTAT_LABEL_SIGNAL;
rxStat.unit = STR_RXSTAT_UNIT_NOUNIT;
}
}
break;
#endif
}
return &rxStat;
}
// TODO: move to module port driver
//
// static int (*_telemetryGetByte)(void*, uint8_t*) = nullptr;
// static void* _telemetryGetByteCtx = nullptr;
// void telemetrySetGetByte(void* ctx, int (*fct)(void*, uint8_t*))
// {
// _telemetryGetByte = nullptr;
// _telemetryGetByteCtx = ctx;
// _telemetryGetByte = fct;
// }
// static bool telemetryGetByte(uint8_t* data)
// {
// auto _getByte = _telemetryGetByte;
// auto _ctx = _telemetryGetByteCtx;
// if (_getByte) {
// return _getByte(_ctx, data);
// }
// // return sportGetByte(data);
// return false;
// }
static void (*telemetryMirrorSendByte)(void*, uint8_t) = nullptr;
static void* telemetryMirrorSendByteCtx = nullptr;
void telemetrySetMirrorCb(void* ctx, void (*fct)(void*, uint8_t))
{
telemetryMirrorSendByte = nullptr;
telemetryMirrorSendByteCtx = ctx;
telemetryMirrorSendByte = fct;
}
void telemetryMirrorSend(uint8_t data)
{
auto _sendByte = telemetryMirrorSendByte;
auto _ctx = telemetryMirrorSendByteCtx;
if (_sendByte) {
_sendByte(_ctx, data);
}
}
#if !defined(SIMU)
static TimerHandle_t telemetryTimer = nullptr;
static StaticTimer_t telemetryTimerBuffer;
static void telemetryTimerCb(TimerHandle_t xTimer)
{
(void)xTimer;
DEBUG_TIMER_START(debugTimerTelemetryWakeup);
telemetryWakeup();
DEBUG_TIMER_STOP(debugTimerTelemetryWakeup);
}
void telemetryStart()
{
if (!telemetryTimer) {
telemetryTimer =
xTimerCreateStatic("Telem", 2 / RTOS_MS_PER_TICK, pdTRUE, (void*)0,
telemetryTimerCb, &telemetryTimerBuffer);
}
if (telemetryTimer) {
if( xTimerStart( telemetryTimer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
}
void telemetryStop()
{
if (telemetryTimer) {
if( xTimerStop( telemetryTimer, 5 / RTOS_MS_PER_TICK ) != pdPASS ) {
/* The timer could not be stopped. */
}
}
}
#endif
inline bool isBadAntennaDetected()
{
if (!isRasValueValid())
return false;
if (telemetryData.swrInternal.isFresh() &&
telemetryData.swrInternal.value() > FRSKY_BAD_ANTENNA_THRESHOLD)
return true;
if (telemetryData.swrExternal.isFresh() &&
telemetryData.swrExternal.value() > FRSKY_BAD_ANTENNA_THRESHOLD)
return true;
return false;
}
static inline void pollTelemetry(uint8_t module, const etx_proto_driver_t* drv, void* ctx)
{
if (!drv || !drv->processData) return;
auto mod_st = (etx_module_state_t*)ctx;
auto serial_drv = modulePortGetSerialDrv(mod_st->rx);
auto serial_ctx = modulePortGetCtx(mod_st->rx);
if (!serial_drv || !serial_ctx || !serial_drv->getByte)
return;
uint8_t* rxBuffer = getTelemetryRxBuffer(module);
uint8_t& rxBufferCount = getTelemetryRxBufferCount(module);
uint8_t data;
if (serial_drv->getByte(serial_ctx, &data) > 0) {
LOG_TELEMETRY_WRITE_START();
do {
telemetryMirrorSend(data);
drv->processData(ctx, data, rxBuffer, &rxBufferCount);
LOG_TELEMETRY_WRITE_BYTE(data);
} while (serial_drv->getByte(serial_ctx, &data) > 0);
}
}
// This can only be changed when the mixer is not
// running as the priority of the timer task is
// lower.
volatile uint8_t _telemetryIsPolling = false;
void telemetryWakeup()
{
_telemetryIsPolling = true;
for (uint8_t i = 0; i < MAX_MODULES; i++) {
auto mod = pulsesGetModuleDriver(i);
if (!mod) continue;
pollTelemetry(i, mod->drv, mod->ctx);
}
_telemetryIsPolling = false;
for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
const TelemetrySensor & sensor = g_model.telemetrySensors[i];
if (sensor.type == TELEM_TYPE_CALCULATED) {
telemetryItems[i].eval(sensor);
}
}
#if defined(VARIO)
if (TELEMETRY_STREAMING() && !IS_FAI_ENABLED()) {
varioWakeup();
}
#endif
static tmr10ms_t alarmsCheckTime = 0;
#define SCHEDULE_NEXT_ALARMS_CHECK(seconds) alarmsCheckTime = get_tmr10ms() + (100*(seconds))
if (int32_t(get_tmr10ms() - alarmsCheckTime) > 0) {
SCHEDULE_NEXT_ALARMS_CHECK(1/*second*/);
bool sensorLost = false;
for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
if (isTelemetryFieldAvailable(i)) {
TelemetryItem & item = telemetryItems[i];
if (item.timeout == 0) {
TelemetrySensor * sensor = & g_model.telemetrySensors[i];
if (sensor->unit != UNIT_DATETIME) {
item.setOld();
sensorLost = true;
}
}
}
}
if (sensorLost && TELEMETRY_STREAMING() && !g_model.disableTelemetryWarning) {
audioEvent(AU_SENSOR_LOST);
}
#if defined(PCBFRSKY)
if (isBadAntennaDetected()) {
AUDIO_RAS_RED();
POPUP_WARNING_ON_UI_TASK(STR_WARNING, STR_ANTENNAPROBLEM);
SCHEDULE_NEXT_ALARMS_CHECK(10/*seconds*/);
}
#endif
if (!g_model.disableTelemetryWarning) {
if (TELEMETRY_STREAMING()) {
if (TELEMETRY_RSSI() < g_model.rfAlarms.critical ) {
AUDIO_RSSI_RED();
SCHEDULE_NEXT_ALARMS_CHECK(10/*seconds*/);
}
else if (TELEMETRY_RSSI() < g_model.rfAlarms.warning ) {
AUDIO_RSSI_ORANGE();
SCHEDULE_NEXT_ALARMS_CHECK(10/*seconds*/);
}
}
if (TELEMETRY_STREAMING()) {
if (telemetryState == TELEMETRY_KO) {
AUDIO_TELEMETRY_BACK();
#if defined(CROSSFIRE)
// TODO: move to crossfire code
#if defined(HARDWARE_EXTERNAL_MODULE)
if (isModuleCrossfire(EXTERNAL_MODULE)) {
moduleState[EXTERNAL_MODULE].counter = CRSF_FRAME_MODELID;
}
#endif
#if defined(HARDWARE_INTERNAL_MODULE)
if (isModuleCrossfire(INTERNAL_MODULE)) {
moduleState[INTERNAL_MODULE].counter = CRSF_FRAME_MODELID;
}
#endif
#endif
}
telemetryState = TELEMETRY_OK;
}
else if (telemetryState == TELEMETRY_OK) {
telemetryState = TELEMETRY_KO;
if (!isModuleInBeepMode()) {
AUDIO_TELEMETRY_LOST();
}
}
}
}
}
void telemetryInterrupt10ms()
{
if (telemetryStreaming > 0) {
bool tick160ms = (telemetryStreaming & 0x0F) == 0;
for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
const TelemetrySensor & sensor = g_model.telemetrySensors[i];
if (sensor.type == TELEM_TYPE_CALCULATED) {
telemetryItems[i].per10ms(sensor);
}
if (tick160ms && telemetryItems[i].timeout > 0) {
telemetryItems[i].timeout--;
}
}
telemetryStreaming--;
}
else {
#if !defined(SIMU)
telemetryData.rssi.reset();
#endif
for (auto & telemetryItem: telemetryItems) {
if (telemetryItem.isAvailable()) {
telemetryItem.setOld();
}
}
}
}
void telemetryReset()
{
telemetryData.clear();
for (auto & telemetryItem : telemetryItems) {
telemetryItem.clear();
}
telemetryStreaming = 0; // reset counter only if valid telemetry packets are being detected
telemetryState = TELEMETRY_INIT;
}
#if defined(LOG_TELEMETRY) && !defined(SIMU)
extern FIL g_telemetryFile;
void logTelemetryWriteStart()
{
static tmr10ms_t lastTime = 0;
tmr10ms_t newTime = get_tmr10ms();
if (lastTime != newTime) {
struct gtm utm;
gettime(&utm);
f_printf(&g_telemetryFile, "\r\n%4d-%02d-%02d,%02d:%02d:%02d.%02d0:",
utm.tm_year + TM_YEAR_BASE, utm.tm_mon + 1, utm.tm_mday,
utm.tm_hour, utm.tm_min, utm.tm_sec, g_ms100);
lastTime = newTime;
}
}
void logTelemetryWriteByte(uint8_t data)
{
f_printf(&g_telemetryFile, " %02X", data);
}
#endif
OutputTelemetryBuffer outputTelemetryBuffer __DMA;
#if defined(LUA)
Fifo<uint8_t, LUA_TELEMETRY_INPUT_FIFO_SIZE> * luaInputTelemetryFifo = NULL;
#endif
#if defined(HARDWARE_INTERNAL_MODULE)
static ModuleSyncStatus moduleSyncStatus[NUM_MODULES];
ModuleSyncStatus &getModuleSyncStatus(uint8_t moduleIdx)
{
return moduleSyncStatus[moduleIdx];
}
#else
static ModuleSyncStatus moduleSyncStatus;
ModuleSyncStatus &getModuleSyncStatus(uint8_t moduleIdx)
{
return moduleSyncStatus;
}
#endif
ModuleSyncStatus::ModuleSyncStatus()
{
memset(this, 0, sizeof(ModuleSyncStatus));
}
void ModuleSyncStatus::update(uint16_t newRefreshRate, int16_t newInputLag)
{
if (!newRefreshRate)
return;
if (newRefreshRate < MIN_REFRESH_RATE)
newRefreshRate = newRefreshRate * (MIN_REFRESH_RATE / (newRefreshRate + 1));
else if (newRefreshRate > MAX_REFRESH_RATE)
newRefreshRate = MAX_REFRESH_RATE;
refreshRate = newRefreshRate;
inputLag = newInputLag;
currentLag = newInputLag;
lastUpdate = get_tmr10ms();
#if 0
TRACE("[SYNC] update rate = %dus; lag = %dus",refreshRate,currentLag);
#endif
}
void ModuleSyncStatus::invalidate() {
//make invalid after use
currentLag = 0;
}
uint16_t ModuleSyncStatus::getAdjustedRefreshRate()
{
int16_t lag = currentLag;
int32_t newRefreshRate = refreshRate;
if (lag == 0) {
return refreshRate;
}
newRefreshRate += lag;
if (newRefreshRate < MIN_REFRESH_RATE) {
newRefreshRate = MIN_REFRESH_RATE;
}
else if (newRefreshRate > MAX_REFRESH_RATE) {
newRefreshRate = MAX_REFRESH_RATE;
}
currentLag -= newRefreshRate - refreshRate;
#if 0
TRACE("[SYNC] mod rate = %dus; lag = %dus",newRefreshRate,currentLag);
#endif
return (uint16_t)newRefreshRate;
}
void ModuleSyncStatus::getRefreshString(char * statusText)
{
if (!isValid()) {
return;
}
char * tmp = statusText;
#if defined(DEBUG)
*tmp++ = 'L';
tmp = strAppendSigned(tmp, inputLag, 5);
tmp = strAppend(tmp, "R");
tmp = strAppendUnsigned(tmp, refreshRate, 5);
#else
tmp = strAppend(tmp, "Sync ");
tmp = strAppendUnsigned(tmp, refreshRate);
#endif
tmp = strAppend(tmp, "us");
}