smartport.c 14.7 KB
Newer Older
Frank Zhao's avatar
Frank Zhao committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * SmartPort Telemetry implementation by frank26080115
 * see https://github.com/frank26080115/cleanflight/wiki/Using-Smart-Port
 */
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>

#include "platform.h"

#ifdef TELEMETRY

#include "common/axis.h"
#include "common/color.h"
#include "common/maths.h"

#include "drivers/system.h"
19
#include "drivers/sensor.h"
Frank Zhao's avatar
Frank Zhao committed
20
#include "drivers/accgyro.h"
21
#include "drivers/compass.h"
Frank Zhao's avatar
Frank Zhao committed
22
23
24
25
26
27
28
29
30
31
#include "drivers/serial.h"
#include "drivers/bus_i2c.h"
#include "drivers/gpio.h"
#include "drivers/timer.h"
#include "drivers/pwm_rx.h"
#include "drivers/adc.h"
#include "drivers/light_led.h"

#include "rx/rx.h"
#include "rx/msp.h"
32

Frank Zhao's avatar
Frank Zhao committed
33
34
35
36
37
38
#include "io/escservo.h"
#include "io/rc_controls.h"
#include "io/gps.h"
#include "io/gimbal.h"
#include "io/serial.h"
#include "io/ledstrip.h"
39

Frank Zhao's avatar
Frank Zhao committed
40
41
42
43
44
45
46
47
#include "sensors/boardalignment.h"
#include "sensors/sensors.h"
#include "sensors/battery.h"
#include "sensors/acceleration.h"
#include "sensors/barometer.h"
#include "sensors/compass.h"
#include "sensors/gyro.h"

48
#include "flight/pid.h"
49
50
51
52
#include "flight/imu.h"
#include "flight/mixer.h"
#include "flight/failsafe.h"
#include "flight/navigation.h"
53
#include "flight/altitudehold.h"
54

Frank Zhao's avatar
Frank Zhao committed
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
#include "telemetry/telemetry.h"
#include "telemetry/smartport.h"

#include "config/runtime_config.h"
#include "config/config.h"
#include "config/config_profile.h"
#include "config/config_master.h"

enum
{
    SPSTATE_UNINITIALIZED,
    SPSTATE_INITIALIZED,
    SPSTATE_WORKING,
    SPSTATE_TIMEDOUT,
};

enum
{
    FSSP_START_STOP = 0x7E,
    FSSP_DATA_FRAME = 0x10,

    // ID of sensor. Must be something that is polled by FrSky RX
    FSSP_SENSOR_ID1 = 0x1B,
    FSSP_SENSOR_ID2 = 0x0D,
    FSSP_SENSOR_ID3 = 0x34,
    FSSP_SENSOR_ID4 = 0x67,
81
82
    // there are 32 ID's polled by smartport master
    // remaining 3 bits are crc (according to comments in openTx code)
Frank Zhao's avatar
Frank Zhao committed
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
};

// these data identifiers are obtained from http://diydrones.com/forum/topics/amp-to-frsky-x8r-sport-converter
enum
{
    FSSP_DATAID_SPEED      = 0x0830 ,
    FSSP_DATAID_VFAS       = 0x0210 ,
    FSSP_DATAID_CURRENT    = 0x0200 ,
    FSSP_DATAID_RPM        = 0x050F ,
    FSSP_DATAID_ALTITUDE   = 0x0100 ,
    FSSP_DATAID_FUEL       = 0x0600 ,
    FSSP_DATAID_ADC1       = 0xF102 ,
    FSSP_DATAID_ADC2       = 0xF103 ,
    FSSP_DATAID_LATLONG    = 0x0800 ,
    FSSP_DATAID_CAP_USED   = 0x0600 ,
    FSSP_DATAID_VARIO      = 0x0110 ,
    FSSP_DATAID_CELLS      = 0x0300 ,
    FSSP_DATAID_CELLS_LAST = 0x030F ,
    FSSP_DATAID_HEADING    = 0x0840 ,
    FSSP_DATAID_ACCX       = 0x0700 ,
    FSSP_DATAID_ACCY       = 0x0710 ,
    FSSP_DATAID_ACCZ       = 0x0720 ,
    FSSP_DATAID_T1         = 0x0400 ,
    FSSP_DATAID_T2         = 0x0410 ,
    FSSP_DATAID_GPS_ALT    = 0x0820 ,
};

const uint16_t frSkyDataIdTable[] = {
    FSSP_DATAID_SPEED     ,
    FSSP_DATAID_VFAS      ,
    FSSP_DATAID_CURRENT   ,
    //FSSP_DATAID_RPM       ,
    FSSP_DATAID_ALTITUDE  ,
    FSSP_DATAID_FUEL      ,
    //FSSP_DATAID_ADC1      ,
    //FSSP_DATAID_ADC2      ,
    FSSP_DATAID_LATLONG   ,
    FSSP_DATAID_LATLONG   , // twice
    //FSSP_DATAID_CAP_USED  ,
    FSSP_DATAID_VARIO     ,
    //FSSP_DATAID_CELLS     ,
    //FSSP_DATAID_CELLS_LAST,
    FSSP_DATAID_HEADING   ,
    FSSP_DATAID_ACCX      ,
    FSSP_DATAID_ACCY      ,
    FSSP_DATAID_ACCZ      ,
    FSSP_DATAID_T1        ,
    FSSP_DATAID_T2        ,
    FSSP_DATAID_GPS_ALT   ,
    0
};

#define __USE_C99_MATH // for roundf()
136
#define SMARTPORT_BAUD 57600
137
#define SMARTPORT_UART_MODE MODE_RXTX
Frank Zhao's avatar
Frank Zhao committed
138
139
140
#define SMARTPORT_SERVICE_DELAY_MS 5 // telemetry requests comes in at roughly 12 ms intervals, keep this under that
#define SMARTPORT_NOT_CONNECTED_TIMEOUT_MS 7000

141
static serialPort_t *smartPortSerialPort = NULL; // The 'SmartPort'(tm) Port.
142
143
static serialPortConfig_t *portConfig;

Frank Zhao's avatar
Frank Zhao committed
144
static telemetryConfig_t *telemetryConfig;
145
146
static bool smartPortTelemetryEnabled =  false;
static portSharing_e smartPortPortSharing;
147

148
extern void serialInit(serialConfig_t *); // from main.c // FIXME remove this dependency
Frank Zhao's avatar
Frank Zhao committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

char smartPortState = SPSTATE_UNINITIALIZED;
static uint8_t smartPortHasRequest = 0;
static uint8_t smartPortIdCnt = 0;
static uint32_t smartPortLastRequestTime = 0;
static uint32_t smartPortLastServiceTime = 0;

static void smartPortDataReceive(uint16_t c)
{
    uint32_t now = millis();

    // look for a valid request sequence
    static uint8_t lastChar;
    if (lastChar == FSSP_START_STOP) {
        smartPortState = SPSTATE_WORKING;
        smartPortLastRequestTime = now;
Petr Ledvina's avatar
Petr Ledvina committed
165
        if (c == FSSP_SENSOR_ID1) {
Frank Zhao's avatar
Frank Zhao committed
166
167
168
169
170
171
172
173
174
175
176
177
            smartPortHasRequest = 1;
            // we only responde to these IDs
            // the X4R-SB does send other IDs, we ignore them, but take note of the time
        }
    }
    lastChar = c;
}

static void smartPortSendByte(uint8_t c, uint16_t *crcp)
{
    // smart port escape sequence
    if (c == 0x7D || c == 0x7E) {
178
        serialWrite(smartPortSerialPort, 0x7D);
Frank Zhao's avatar
Frank Zhao committed
179
180
181
        c ^= 0x20;
    }

182
    serialWrite(smartPortSerialPort, c);
Frank Zhao's avatar
Frank Zhao committed
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

    if (crcp == NULL)
        return;

    uint16_t crc = *crcp;
    crc += c;
    crc += crc >> 8;
    crc &= 0x00FF;
    *crcp = crc;
}

static void smartPortSendPackage(uint16_t id, uint32_t val)
{
    uint16_t crc = 0;
    smartPortSendByte(FSSP_DATA_FRAME, &crc);
    uint8_t *u8p = (uint8_t*)&id;
    smartPortSendByte(u8p[0], &crc);
    smartPortSendByte(u8p[1], &crc);
    u8p = (uint8_t*)&val;
    smartPortSendByte(u8p[0], &crc);
    smartPortSendByte(u8p[1], &crc);
    smartPortSendByte(u8p[2], &crc);
    smartPortSendByte(u8p[3], &crc);
    smartPortSendByte(0xFF - (uint8_t)crc, NULL);

    smartPortLastServiceTime = millis();
}

void initSmartPortTelemetry(telemetryConfig_t *initialTelemetryConfig)
{
    telemetryConfig = initialTelemetryConfig;
214
215
    portConfig = findSerialPortConfig(FUNCTION_TELEMETRY_SMARTPORT);
    smartPortPortSharing = determinePortSharing(portConfig, FUNCTION_TELEMETRY_SMARTPORT);
Frank Zhao's avatar
Frank Zhao committed
216
217
218
219
}

void freeSmartPortTelemetryPort(void)
{
220
    closeSerialPort(smartPortSerialPort);
221
    smartPortSerialPort = NULL;
222
223

    smartPortState = SPSTATE_UNINITIALIZED;
224
    smartPortTelemetryEnabled = false;
Frank Zhao's avatar
Frank Zhao committed
225
226
227
228
}

void configureSmartPortTelemetryPort(void)
{
229
230
    portOptions_t portOptions;

231
    if (!portConfig) {
Frank Zhao's avatar
Frank Zhao committed
232
233
234
        return;
    }

235
236
237
238
239
240
241
    portOptions = SERIAL_BIDIR;

    if (telemetryConfig->telemetry_inversion) {
        portOptions |= SERIAL_INVERTED;
    }

    smartPortSerialPort = openSerialPort(portConfig->identifier, FUNCTION_TELEMETRY_SMARTPORT, NULL, SMARTPORT_BAUD, SMARTPORT_UART_MODE, portOptions);
Frank Zhao's avatar
Frank Zhao committed
242

243
244
    if (!smartPortSerialPort) {
        return;
Frank Zhao's avatar
Frank Zhao committed
245
246
    }

247
    smartPortState = SPSTATE_INITIALIZED;
248
    smartPortTelemetryEnabled = true;
249
    smartPortLastRequestTime = millis();
Frank Zhao's avatar
Frank Zhao committed
250
251
}

252
bool canSendSmartPortTelemetry(void)
Frank Zhao's avatar
Frank Zhao committed
253
{
254
    return smartPortSerialPort && (smartPortState == SPSTATE_INITIALIZED || smartPortState == SPSTATE_WORKING);
Frank Zhao's avatar
Frank Zhao committed
255
256
257
258
259
260
261
}

bool isSmartPortTimedOut(void)
{
    return smartPortState >= SPSTATE_TIMEDOUT;
}

262
263
void checkSmartPortTelemetryState(void)
{
264
    bool newTelemetryEnabledValue = telemetryDetermineEnabledState(smartPortPortSharing);
265
266
267
268
269
270
271
272
273
274
275

    if (newTelemetryEnabledValue == smartPortTelemetryEnabled) {
        return;
    }

    if (newTelemetryEnabledValue)
        configureSmartPortTelemetryPort();
    else
        freeSmartPortTelemetryPort();
}

Frank Zhao's avatar
Frank Zhao committed
276
277
void handleSmartPortTelemetry(void)
{
278
279
280
281
    if (!smartPortTelemetryEnabled) {
        return;
    }

282
    if (!canSendSmartPortTelemetry()) {
Frank Zhao's avatar
Frank Zhao committed
283
        return;
284
    }
Frank Zhao's avatar
Frank Zhao committed
285

286
287
    while (serialTotalBytesWaiting(smartPortSerialPort) > 0) {
        uint8_t c = serialRead(smartPortSerialPort);
Frank Zhao's avatar
Frank Zhao committed
288
289
290
291
292
293
294
295
296
297
298
        smartPortDataReceive(c);
    }

    uint32_t now = millis();

    // if timed out, reconfigure the UART back to normal so the GUI or CLI works
    if ((now - smartPortLastRequestTime) > SMARTPORT_NOT_CONNECTED_TIMEOUT_MS) {
        smartPortState = SPSTATE_TIMEDOUT;
        return;
    }

299
    // limit the rate at which we send responses, we don't want to affect flight characteristics
Frank Zhao's avatar
Frank Zhao committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    if ((now - smartPortLastServiceTime) < SMARTPORT_SERVICE_DELAY_MS)
        return;

    if (smartPortHasRequest) {
        // we can send back any data we want, our table keeps track of the order and frequency of each data type we send
        uint16_t id = frSkyDataIdTable[smartPortIdCnt];
        if (id == 0) { // end of table reached, loop back
            smartPortIdCnt = 0;
            id = frSkyDataIdTable[smartPortIdCnt];
        }
        smartPortIdCnt++;

        int32_t tmpi;
        static uint8_t t1Cnt = 0;

        switch(id) {
316
#ifdef GPS
Frank Zhao's avatar
Frank Zhao committed
317
318
            case FSSP_DATAID_SPEED      :
                if (sensors(SENSOR_GPS) && STATE(GPS_FIX)) {
Petr Ledvina's avatar
Petr Ledvina committed
319
                    uint32_t tmpui = (GPS_speed * 36 + 36 / 2) / 100;
320
                    smartPortSendPackage(id, tmpui); // given in 0.1 m/s, provide in KM/H
Frank Zhao's avatar
Frank Zhao committed
321
322
323
                    smartPortHasRequest = 0;
                }
                break;
324
#endif
Frank Zhao's avatar
Frank Zhao committed
325
            case FSSP_DATAID_VFAS       :
326
                smartPortSendPackage(id, vbat * 10); // given in 0.1V, convert to volts
Frank Zhao's avatar
Frank Zhao committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_CURRENT    :
                smartPortSendPackage(id, amperage); // given in 10mA steps, unknown requested unit
                smartPortHasRequest = 0;
                break;
            //case FSSP_DATAID_RPM        :
            case FSSP_DATAID_ALTITUDE   :
                smartPortSendPackage(id, BaroAlt); // unknown given unit, requested 100 = 1 meter
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_FUEL       :
                smartPortSendPackage(id, mAhDrawn); // given in mAh, unknown requested unit
                smartPortHasRequest = 0;
                break;
            //case FSSP_DATAID_ADC1       :
            //case FSSP_DATAID_ADC2       :
344
#ifdef GPS
Frank Zhao's avatar
Frank Zhao committed
345
346
            case FSSP_DATAID_LATLONG    :
                if (sensors(SENSOR_GPS) && STATE(GPS_FIX)) {
Petr Ledvina's avatar
Petr Ledvina committed
347
                    uint32_t tmpui = 0;
Frank Zhao's avatar
Frank Zhao committed
348
349
                    // the same ID is sent twice, one for longitude, one for latitude
                    // the MSB of the sent uint32_t helps FrSky keep track
Petr Ledvina's avatar
Petr Ledvina committed
350
                    // the even/odd bit of our counter helps us keep track
Frank Zhao's avatar
Frank Zhao committed
351
                    if (smartPortIdCnt & 1) {
352
353
354
                        tmpui = abs(GPS_coord[LON]);  // now we have unsigned value and one bit to spare
                        tmpui = (tmpui + tmpui / 2) / 25 | 0x80000000;  // 6/100 = 1.5/25, division by power of 2 is fast
                        if (GPS_coord[LON] < 0) tmpui |= 0x40000000;
Frank Zhao's avatar
Frank Zhao committed
355
356
                    }
                    else {
357
358
359
                        tmpui = abs(GPS_coord[LAT]);  // now we have unsigned value and one bit to spare
                        tmpui = (tmpui + tmpui / 2) / 25;  // 6/100 = 1.5/25, division by power of 2 is fast
                        if (GPS_coord[LAT] < 0) tmpui |= 0x40000000;
Frank Zhao's avatar
Frank Zhao committed
360
361
362
363
364
                    }
                    smartPortSendPackage(id, tmpui);
                    smartPortHasRequest = 0;
                }
                break;
365
#endif
Frank Zhao's avatar
Frank Zhao committed
366
367
368
369
370
371
            //case FSSP_DATAID_CAP_USED   :
            case FSSP_DATAID_VARIO      :
                smartPortSendPackage(id, vario); // unknown given unit but requested in 100 = 1m/s
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_HEADING    :
372
                smartPortSendPackage(id, heading * 100); // given in deg, requested in 10000 = 100 deg
Frank Zhao's avatar
Frank Zhao committed
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
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_ACCX       :
                smartPortSendPackage(id, accSmooth[X] / 44);
                // unknown input and unknown output unit
                // we can only show 00.00 format, another digit won't display right on Taranis
                // dividing by roughly 44 will give acceleration in G units
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_ACCY       :
                smartPortSendPackage(id, accSmooth[Y] / 44);
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_ACCZ       :
                smartPortSendPackage(id, accSmooth[Z] / 44);
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_T1         :
                // we send all the flags as decimal digits for easy reading

                // the t1Cnt simply allows the telemetry view to show at least some changes
                t1Cnt++;
                if (t1Cnt >= 4) {
                    t1Cnt = 1;
                }
                tmpi = t1Cnt * 10000; // start off with at least one digit so the most significant 0 won't be cut off
                // the Taranis seems to be able to fit 5 digits on the screen
                // the Taranis seems to consider this number a signed 16 bit integer

                if (ARMING_FLAG(OK_TO_ARM))
                    tmpi += 1;
                if (ARMING_FLAG(PREVENT_ARMING))
                    tmpi += 2;
                if (ARMING_FLAG(ARMED))
                    tmpi += 4;

                if (FLIGHT_MODE(ANGLE_MODE))
                    tmpi += 10;
                if (FLIGHT_MODE(HORIZON_MODE))
                    tmpi += 20;
                if (FLIGHT_MODE(AUTOTUNE_MODE))
                    tmpi += 40;
                if (FLIGHT_MODE(PASSTHRU_MODE))
                    tmpi += 40;

                if (FLIGHT_MODE(MAG_MODE))
                    tmpi += 100;
                if (FLIGHT_MODE(BARO_MODE))
                    tmpi += 200;
                if (FLIGHT_MODE(SONAR_MODE))
                    tmpi += 400;

                if (FLIGHT_MODE(GPS_HOLD_MODE))
                    tmpi += 1000;
                if (FLIGHT_MODE(GPS_HOME_MODE))
                    tmpi += 2000;
                if (FLIGHT_MODE(HEADFREE_MODE))
                    tmpi += 4000;

                smartPortSendPackage(id, (uint32_t)tmpi);
                smartPortHasRequest = 0;
                break;
            case FSSP_DATAID_T2         :
                if (sensors(SENSOR_GPS)) {
437
#ifdef GPS
Frank Zhao's avatar
Frank Zhao committed
438
439
440
                    // provide GPS lock status
                    smartPortSendPackage(id, (STATE(GPS_FIX) ? 1000 : 0) + (STATE(GPS_FIX_HOME) ? 2000 : 0) + GPS_numSat);
                    smartPortHasRequest = 0;
441
#endif
Frank Zhao's avatar
Frank Zhao committed
442
443
444
445
446
447
                }
                else {
                    smartPortSendPackage(id, 0);
                    smartPortHasRequest = 0;
                }
                break;
448
#ifdef GPS
Frank Zhao's avatar
Frank Zhao committed
449
450
            case FSSP_DATAID_GPS_ALT    :
                if (sensors(SENSOR_GPS) && STATE(GPS_FIX)) {
451
                    smartPortSendPackage(id, GPS_altitude * 100); // given in 0.1m , requested in 10 = 1m (should be in mm, probably a bug in opentx, tested on 2.0.1.7)
Frank Zhao's avatar
Frank Zhao committed
452
453
454
                    smartPortHasRequest = 0;
                }
                break;
455
#endif
Frank Zhao's avatar
Frank Zhao committed
456
457
458
459
460
461
462
463
            default:
                break;
                // if nothing is sent, smartPortHasRequest isn't cleared, we already incremented the counter, just wait for the next loop
        }
    }
}

#endif