-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst_telemetry.cpp
517 lines (405 loc) · 14.5 KB
/
bst_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
/*
* TeamBlacksheep BlackSheepTelemetry (BST) Protocol
* Datatype definitions and helper functions
* Most of the datatype definitions were
* taken from the bst files in the COLIBRI_RACE
* directory in the betaflight repo and the
* bst code in the PX4 repo.
*
* ------------------------------------------------
*
* Copyright (C) 2019 Paul Kurucz
*
* License info: See the LICENSE file at the repo top level
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <Wire.h>
#include <Arduino.h>
#include "util.h"
#include "config.h"
#include "vector_open_telemetry.h"
#include "bst_telemetry.h"
/* ----------------------------------------------------- */
/* Local Defines */
/* Enable for verbose debug lovin */
//#define BST_DEBUG
/* -------------- */
#define BST_BUFFER_SIZE 128
#define BST_PROTOCOL_VERSION 0
#define API_VERSION_MAJOR 1 // increment when major changes are made
#define API_VERSION_MINOR 13 // increment when any change is made, reset to zero when major changes are released after changing API_VERSION_MAJOR
#define API_VERSION_LENGTH 2
/* Configure the CRC peripheral to use the polynomial x8 + x7 + x6 + x4 + x2 + 1 */
#define BST_CRC_POLYNOMIAL 0xD5
/* I2C Addresses */
#define I2C_ADDR_TBS_CORE_PNP_PRO 0x80
#define I2C_ADDR_RESERVED 0x8A
#define I2C_ADDR_PNP_PRO_DIDITAL_CURRENT_SENSOR 0xC0
#define I2C_ADDR_PNP_PRO_GPS 0xC2
#define I2C_ADDR_TSB_BLACKBOX 0xC4
#define I2C_ADDR_CLEANFLIGHT_FC 0xC8
#define I2C_ADDR_CROSSFIRE_UHF_RECEIVER 0xEC
/* MSP Frame address */
#define PUBLIC_ADDRESS 0x00
/* Frame Types */
#define GPS_POSITION_FRAME_ID 0x02 /* Len: 15 bytes (PX4) */
#define GPS_TIME_FRAME_ID 0x03
#define BATTERY_STATUS_FRAME_ID 0x08
#define CROSSFIRE_RSSI_FRAME_ID 0x14
#define RC_CHANNEL_FRAME_ID 0x15
#define RC_CHANNELS_PACKED_FRAME_ID 0x16 /* From: PX4, Len: 22 bytes, 11 bits per channel * 16 channels */
#define FC_ATTITUDE_FRAME_ID 0x1E /* Len: 6 bytes (PX4) */
#define CLEANFLIGHT_MODE_FRAME_ID 0x20
#define FLIGHT_MODE_FRAME_ID 0x21 /* From: PX4, */
/* CLEANFLIGHT_MODE_FRAME_ID bitfields */
#define BST_FLAG_ARMED (0x01 << 0)
#define BST_FLAG_ANGLE_MODE (0x01 << 1)
#define BST_FLAG_HORIZON_MODE (0x01 << 2)
#define BST_FLAG_BARO_MODE (0x01 << 3)
#define BST_FLAG_MAG_MODE (0x01 << 4)
#define BST_FLAG_AIR_MODE (0x01 << 5)
#define BST_FLAG_RANGEFINDER_MODE (0x01 << 6)
#define BST_FLAG_FAILSAFE_MODE (0x01 << 7)
#define BST_SENSOR_ACC (0x01 << 0)
#define BST_SENSOR_BARO (0x01 << 1)
#define BST_SENSOR_MAG (0x01 << 2)
#define BST_SENSOR_GPS (0x01 << 3)
#define BST_SENSOR_RANGEFINDER (0x01 << 4)
#define USABLE_TIMER_CHANNEL_COUNT 8
/* ----------------------------------------------------- */
/* Local Variables and function prototypes */
static uint8_t bst_crc = 0;
static uint8_t bst_write_buffer[BST_BUFFER_SIZE];
static uint8_t bst_write_buffer_ptr;
static uint8_t bst_i2c_buffer[BST_BUFFER_SIZE] = {0};
static uint8_t bst_i2c_buffer_index = 0;
static uint8_t bst_i2c_buffer_len = 0;
/* -- Timestamp variables -- */
static uint32_t bst_RPY_TimestampMS;
static uint32_t bst_GPS_TimestampMS;
static uint32_t bst_Battery_TimestampMS;
static uint32_t bst_FC_Mode_TimestampMS;
static uint32_t bst_RC_Chan_TimestampMS;
/* -- Utility functions -- */
static void bst_calc_crc(uint8_t data_in);
static void bst_reset_buffer(uint8_t address);
static void bst_buffer8(uint8_t data);
static void bst_buffer16(uint16_t data);
static void bst_buffer32(uint32_t data);
static bool bst_write_busy(void);
static bool bst_master_write(uint8_t* data);
static void bst_write_loop(void);
/* -- Data conversion and output functions -- */
/* Vector translation/write functions */
static bool bst_write_vector_gps(void);
static bool bst_write_vector_battery(void);
static bool bst_write_vector_rpy(void);
static bool bst_write_vector_fc_mode(void);
/* Test write functions */
#if 0
static bool bst_read_rc_channels(void);
static bool bst_write_rc_channels(void);
#endif
/* ----------------------------------------------------- */
#define BST_HEX_PRINT(_X) do { hex_print((_X)); } while(0)
#define BST_PRINT(...) do { Serial.print(__VA_ARGS__); } while(0)
#define BST_PRINTLN(...) do { Serial.println(__VA_ARGS__); } while(0)
#ifdef BST_DEBUG
#define BST_HEX_DPRINT(_X) do { hex_print((_X)); } while(0)
#define BST_DPRINT(...) do { Serial.print(__VA_ARGS__); } while(0)
#define BST_DPRINTLN(...) do { Serial.println(__VA_ARGS__); } while(0)
#else
#define BST_HEX_DPRINT(_X) do{ } while(0)
#define BST_DPRINT(...) do{ } while(0)
#define BST_DPRINTLN(...) do{ } while(0)
#endif
/* ----------------------------------------------------- */
void bst_init(void) {
/* I2C Init, make sure the internal pull-ups are enabled */
#if 0
pinMode (SDA, INPUT_PULLUP);
pinMode (SCL, INPUT_PULLUP);
digitalWrite(SDA, 1);
digitalWrite(SCL, 1);
#endif
/* Use Digital Pins 16 and 17 with external pull-up resistors to pull up SCL and SDA */
pinMode (16, OUTPUT);
pinMode (17, OUTPUT);
digitalWrite(16, 1);
digitalWrite(17, 1);
Wire.begin();
Wire.setClock(100000);
/* Timestamp variables to keep track of the last time we sent an update over BST */
bst_RPY_TimestampMS = 0;
bst_GPS_TimestampMS = 0;
bst_Battery_TimestampMS = 0;
bst_FC_Mode_TimestampMS = 0;
bst_RC_Chan_TimestampMS = 0;
}
void bst_handler_task(void) {
/* Handle overall I2C tasks */
bst_write_loop();
/* Let other system tasks run while I2C is busy */
if (bst_write_busy()) return;
/* ---- Queue up the next I2C transaction ----- */
// bst_write_vector_gps();
// return;
/* Telemetry update priority is based on the order things appear here */
if(!vot_telemetry_valid) return;
if(bst_RPY_TimestampMS != vot_telemetry.TimestampMS) {
bst_RPY_TimestampMS = vot_telemetry.TimestampMS;
LED_ON();
bst_write_vector_rpy();
return;
}
if(bst_GPS_TimestampMS != vot_telemetry.TimestampMS) {
bst_GPS_TimestampMS = vot_telemetry.TimestampMS;
bst_write_vector_gps();
return;
}
if(bst_Battery_TimestampMS != vot_telemetry.TimestampMS) {
bst_Battery_TimestampMS = vot_telemetry.TimestampMS;
bst_write_vector_battery();
return;
}
if(bst_FC_Mode_TimestampMS != vot_telemetry.TimestampMS) {
bst_FC_Mode_TimestampMS = vot_telemetry.TimestampMS;
bst_write_vector_fc_mode();
return;
}
#if 0
if(bst_RC_Chan_TimestampMS != vot_telemetry.TimestampMS) {
bst_RC_Chan_TimestampMS = vot_telemetry.TimestampMS;
bst_read_rc_channels();
return;
}
#endif
LED_OFF();
}
/* ----------------------------------------------------- */
static void bst_calc_crc(uint8_t data_in)
{
/* polynomial = x^8+x^7+x^6+x^4+x^2+1 = x^8+x^7+x^6+x^4+x^2+X^0 */
uint8_t polynomial = BST_CRC_POLYNOMIAL;
bool MSB_Flag;
/* Step through each bit of the BYTE (8-bits) */
for (uint8_t i = 0; i < 8; i++) {
/* Clear the Flag */
MSB_Flag = false;
/* MSB_Set = 80; */
if (bst_crc & 0x80) {
MSB_Flag = true;
}
bst_crc <<= 1;
/* MSB_Set = 80; */
if (data_in & 0x80) {
bst_crc++;
}
data_in <<= 1;
if (MSB_Flag == true) {
bst_crc ^= polynomial;
}
}
}
/* ----------------------------------------------------- */
static void bst_reset_buffer(uint8_t address)
{
bst_write_buffer[0] = address;
bst_write_buffer_ptr = 2;
}
static void bst_buffer8(uint8_t data)
{
bst_write_buffer[bst_write_buffer_ptr++] = data;
bst_write_buffer[1] = bst_write_buffer_ptr;
}
static void bst_buffer16(uint16_t data)
{
bst_buffer8((uint8_t)(data >> 8));
bst_buffer8((uint8_t)(data >> 0));
}
static void bst_buffer32(uint32_t data)
{
bst_buffer16((uint16_t)(data >> 16));
bst_buffer16((uint16_t)(data >> 0));
}
static bool bst_write_busy(void)
{
if (bst_i2c_buffer_len)
return true;
else
return false;
}
static bool bst_master_write(uint8_t* data)
{
if (bst_i2c_buffer_len==0) {
bst_crc = 0;
bst_i2c_buffer_index = 0;
bst_i2c_buffer[0] = *data;
bst_i2c_buffer[1] = *(data+1);
bst_i2c_buffer_len = bst_i2c_buffer[1] + 2;
for (uint8_t i=2; i<bst_i2c_buffer_len; i++) {
if (i==(bst_i2c_buffer_len-1)) {
bst_calc_crc(0);
bst_i2c_buffer[i] = bst_crc;
} else {
bst_i2c_buffer[i] = *(data+i);
bst_calc_crc((uint8_t)bst_i2c_buffer[i]);
}
}
return true;
}
return false;
}
static void bst_write_loop(void)
{
if (bst_i2c_buffer_len && bst_i2c_buffer_index==0) {
BST_DPRINT(F("I2C Write, bst_i2c_buffer_len="));
BST_DPRINT(bst_i2c_buffer_len, DEC);
BST_DPRINT(F(" ... "));
Wire.beginTransmission(bst_i2c_buffer[0]);
bst_i2c_buffer_index = 1;
BST_HEX_DPRINT(bst_i2c_buffer[0]);
for(int i = 1; i < bst_i2c_buffer_len; i++) {
BST_HEX_DPRINT(bst_i2c_buffer[bst_i2c_buffer_index]);
Wire.write(bst_i2c_buffer[bst_i2c_buffer_index]);
bst_i2c_buffer_index++;
}
uint8_t error = Wire.endTransmission();
switch(error) {
case 0:
BST_DPRINTLN(F("Success"));
break;
case 1:
BST_DPRINTLN(F("1-Data too long"));
break;
case 2:
BST_DPRINTLN(F("2-NACK on addr"));
break;
case 3:
BST_DPRINTLN(F("3-NACK on data"));
break;
default:
BST_DPRINT(error,DEC);
BST_DPRINTLN(F("-Other error"));
break;
}
bst_i2c_buffer_index = 0;
bst_i2c_buffer_len = 0;
}
}
static bool bst_write_vector_gps(void)
{
uint32_t lat = vot_telemetry.GPSTelemetry.LatitudeX1E7; // BST: LatitudeX1E7
uint32_t lon = vot_telemetry.GPSTelemetry.LongitudeX1E7; // BST: LongitudeX1E7
uint16_t speed = vot_telemetry.GPSTelemetry.GroundspeedKPHX10; // BST: GroundspeedKPHX10
uint16_t alt = vot_telemetry.GPSTelemetry.GPSAltitudecm / 100; // BST: GPSAltitudeM
uint16_t altitude = alt + 1000; // BST: in Meters, +1000 added as offset
uint8_t numOfSat = vot_telemetry.GPSTelemetry.SatsInUse; // BST: Number of Sats
#if 1
/* Send coordinates as 0 - 2X3.14159(PI)X10000 */
/* This is the default for auto-discovery... */
uint16_t gpsHeading = (62832 * ((uint32_t)vot_telemetry.GPSTelemetry.CourseDegrees)) / 360;
#else
/* Send coordinates as 0 - 360X100 degrees */
/* Scaling needs to be done in the Taranis. Ratio = 25.5 works */
uint16_t gpsHeading = 100 * vot_telemetry.GPSTelemetry.CourseDegrees;
#endif
#ifdef BST_CONFIG_TELEM_USE_VECTOR_AIRSPEED
speed = vot_telemetry.SensorTelemetry.AirspeedKPHX10; // BST: AirspeedKPHX10, requires optional pitot sensor
#endif
BST_DPRINT(F("Write GPS Position... "));
BST_DPRINT(altitude, DEC);
BST_DPRINT(F(", "));
BST_DPRINTLN(gpsHeading, DEC);
bst_reset_buffer(PUBLIC_ADDRESS);
bst_buffer8(GPS_POSITION_FRAME_ID);
bst_buffer32(lat); // Status: Complete
bst_buffer32(lon); // Status: Complete
bst_buffer16(speed); // Status: ( km/h * 10 )
bst_buffer16(gpsHeading); // Status: ?
bst_buffer16(altitude); // Status: OK, in Meters, +1000 added as offset
bst_buffer8(numOfSat); // Status: OK
bst_buffer8(0x00); // Status: ?
return bst_master_write(bst_write_buffer);
}
static bool bst_write_vector_battery(void)
{
uint16_t voltage = vot_telemetry.SensorTelemetry.PackVoltageX100 / 10; // BST: VoltageX10
uint16_t current = vot_telemetry.SensorTelemetry.PackCurrentX10; // BST: CurrentX10
uint32_t mAHConsumed = vot_telemetry.SensorTelemetry.mAHConsumed; // BST: mAHConsumed
BST_DPRINTLN(F("Write Battery Status..."));
bst_reset_buffer(PUBLIC_ADDRESS);
bst_buffer8(BATTERY_STATUS_FRAME_ID);
bst_buffer16(voltage); // Status: Complete
bst_buffer16(current); // Status: Complete
bst_buffer8(mAHConsumed >> 16); // Status: Complete
bst_buffer8(mAHConsumed >> 8);
bst_buffer8(mAHConsumed);
// bst_buffer8(mAHConsumed); // Battery Percentage... this only appears in CRSF code so for now we don't use this
return bst_master_write(bst_write_buffer);
}
static bool bst_write_vector_rpy(void)
{
/* Send coordinates as +/- 3.14159(PI)X10000 */
/* This is the default for auto-discovery... */
int16_t X = (31416 * ((int32_t)vot_telemetry.SensorTelemetry.Attitude.PitchDegrees)) / 180;
int16_t Y = (31416 * ((int32_t)vot_telemetry.SensorTelemetry.Attitude.RollDegrees)) / 180;
int16_t Z = (31416 * ((int32_t)vot_telemetry.SensorTelemetry.Attitude.YawDegrees)) / 180;
BST_DPRINTLN(F("Write RPY..."));
bst_reset_buffer(PUBLIC_ADDRESS);
bst_buffer8(FC_ATTITUDE_FRAME_ID);
bst_buffer16(X); // Status: Complete
bst_buffer16(Y); // Status: Complete
bst_buffer16(Z); // Status: Complete
return bst_master_write(bst_write_buffer);
}
#if 0
static bool bst_read_rc_channels(void)
{
const int len = 32;
BST_PRINT(F("Read RC Pos..."));
Wire.requestFrom(PUBLIC_ADDRESS, len); // request len bytes from slave device
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
BST_HEX_PRINT(c); // print the character
BST_PRINT(' ');
}
BST_PRINTLN(F(""));
return true;
}
static bool bst_write_rc_channels(void)
{
uint8_t i = 0;
BST_PRINTLN(F("Write RC Pos..."));
bst_reset_buffer(PUBLIC_ADDRESS);
bst_buffer8(RC_CHANNEL_FRAME_ID);
for (i = 0; i < (USABLE_TIMER_CHANNEL_COUNT-1); i++) {
bst_buffer16(100 * i);
}
return bst_master_write(bst_write_buffer);
}
#endif
static bool bst_write_vector_fc_mode(void)
{
uint8_t fm = MIN(vot_telemetry.PresentFlightMode, VECTOR_FLIGHT_MODE_MAX); // Clamp the flight mode index to the legit range
const char * fm_str = vot_flight_mode_strings[fm];
uint8_t len = MIN(15, strlen(fm_str)); // Maximum string length of 16 bytes including the NULL
BST_DPRINT(F("Write FC Mode..."));
BST_HEX_DPRINT(vot_telemetry.PresentFlightMode);
BST_DPRINTLN(F(""));
bst_reset_buffer(PUBLIC_ADDRESS);
bst_buffer8(FLIGHT_MODE_FRAME_ID);
for(int i = 0; i < len; i++) bst_buffer8(fm_str[i]);
bst_buffer8(0); // NULL Terminate the string
return bst_master_write(bst_write_buffer);
}