-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathMspProcessor.cpp
More file actions
1691 lines (1533 loc) · 56.3 KB
/
MspProcessor.cpp
File metadata and controls
1691 lines (1533 loc) · 56.3 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Connect/MspProcessor.hpp"
#include "Hardware.h"
#include <platform.h>
#include <algorithm>
#include <limits>
#if defined(ESPFC_MULTI_CORE) && defined(ESPFC_FREE_RTOS)
#include <driver/timer.h>
#endif
#define VTXCOMMON_MSP_BANDCHAN_CHKVAL ((uint16_t)((7 << 3) + 7))
extern "C" {
#include "msp/msp_protocol.h"
#include "msp/msp_protocol_v2_common.h"
#include "msp/msp_protocol_v2_betaflight.h"
#include "io/serial_4way.h"
#include "blackbox/blackbox_io.h"
int blackboxCalculatePDenom(int rateNum, int rateDenom);
uint8_t blackboxCalculateSampleRate(uint16_t pRatio);
uint8_t blackboxGetRateDenom(void);
uint16_t blackboxGetPRatio(void);
}
namespace {
enum SerialSpeedIndex {
SERIAL_SPEED_INDEX_AUTO = 0,
SERIAL_SPEED_INDEX_9600,
SERIAL_SPEED_INDEX_19200,
SERIAL_SPEED_INDEX_38400,
SERIAL_SPEED_INDEX_57600,
SERIAL_SPEED_INDEX_115200,
SERIAL_SPEED_INDEX_230400,
SERIAL_SPEED_INDEX_250000,
SERIAL_SPEED_INDEX_400000,
SERIAL_SPEED_INDEX_460800,
SERIAL_SPEED_INDEX_500000,
SERIAL_SPEED_INDEX_921600,
SERIAL_SPEED_INDEX_1000000,
SERIAL_SPEED_INDEX_1500000,
SERIAL_SPEED_INDEX_2000000,
SERIAL_SPEED_INDEX_2470000,
};
static SerialSpeedIndex toBaudIndex(int32_t speed)
{
using namespace Espfc;
if(speed >= SERIAL_SPEED_2470000) return SERIAL_SPEED_INDEX_2470000;
if(speed >= SERIAL_SPEED_2000000) return SERIAL_SPEED_INDEX_2000000;
if(speed >= SERIAL_SPEED_1500000) return SERIAL_SPEED_INDEX_1500000;
if(speed >= SERIAL_SPEED_1000000) return SERIAL_SPEED_INDEX_1000000;
if(speed >= SERIAL_SPEED_921600) return SERIAL_SPEED_INDEX_921600;
if(speed >= SERIAL_SPEED_500000) return SERIAL_SPEED_INDEX_500000;
if(speed >= SERIAL_SPEED_460800) return SERIAL_SPEED_INDEX_460800;
if(speed >= SERIAL_SPEED_400000) return SERIAL_SPEED_INDEX_400000;
if(speed >= SERIAL_SPEED_250000) return SERIAL_SPEED_INDEX_250000;
if(speed >= SERIAL_SPEED_230400) return SERIAL_SPEED_INDEX_230400;
if(speed >= SERIAL_SPEED_115200) return SERIAL_SPEED_INDEX_115200;
if(speed >= SERIAL_SPEED_57600) return SERIAL_SPEED_INDEX_57600;
if(speed >= SERIAL_SPEED_38400) return SERIAL_SPEED_INDEX_38400;
if(speed >= SERIAL_SPEED_19200) return SERIAL_SPEED_INDEX_19200;
if(speed >= SERIAL_SPEED_9600) return SERIAL_SPEED_INDEX_9600;
return SERIAL_SPEED_INDEX_AUTO;
}
static Espfc::SerialSpeed fromBaudIndex(SerialSpeedIndex index)
{
using namespace Espfc;
switch(index)
{
case SERIAL_SPEED_INDEX_9600: return SERIAL_SPEED_9600;
case SERIAL_SPEED_INDEX_19200: return SERIAL_SPEED_19200;
case SERIAL_SPEED_INDEX_38400: return SERIAL_SPEED_38400;
case SERIAL_SPEED_INDEX_57600: return SERIAL_SPEED_57600;
case SERIAL_SPEED_INDEX_115200: return SERIAL_SPEED_115200;
case SERIAL_SPEED_INDEX_230400: return SERIAL_SPEED_230400;
case SERIAL_SPEED_INDEX_250000: return SERIAL_SPEED_250000;
case SERIAL_SPEED_INDEX_400000: return SERIAL_SPEED_400000;
case SERIAL_SPEED_INDEX_460800: return SERIAL_SPEED_460800;
case SERIAL_SPEED_INDEX_500000: return SERIAL_SPEED_500000;
case SERIAL_SPEED_INDEX_921600: return SERIAL_SPEED_921600;
case SERIAL_SPEED_INDEX_1000000: return SERIAL_SPEED_1000000;
case SERIAL_SPEED_INDEX_1500000: return SERIAL_SPEED_1500000;
case SERIAL_SPEED_INDEX_2000000: return SERIAL_SPEED_2000000;
case SERIAL_SPEED_INDEX_2470000: return SERIAL_SPEED_2470000;
case SERIAL_SPEED_INDEX_AUTO:
default:
return SERIAL_SPEED_NONE;
}
}
static uint8_t toFilterTypeDerivative(uint8_t t)
{
switch(t) {
case 0: return Espfc::FILTER_NONE;
case 1: return Espfc::FILTER_PT3;
case 2: return Espfc::FILTER_BIQUAD;
default: return Espfc::FILTER_PT3;
}
}
static uint8_t fromFilterTypeDerivative(uint8_t t)
{
switch(t) {
case Espfc::FILTER_NONE: return 0;
case Espfc::FILTER_PT3: return 1;
case Espfc::FILTER_BIQUAD: return 2;
default: return 1;
}
}
static uint8_t fromGyroDlpf(uint8_t t)
{
switch(t) {
case Espfc::GYRO_DLPF_256: return 0;
case Espfc::GYRO_DLPF_EX: return 1;
default: return 2;
}
}
static int8_t toVbatSource(uint8_t t)
{
switch(t) {
case 0: return 0; // none
case 1: return 1; // internal adc
default: return 0;
}
}
static int8_t toIbatSource(uint8_t t)
{
switch(t) {
case 0: return 0; // none
case 1: return 1; // internal adc
default: return 0;
}
}
static uint8_t toVbatVoltageLegacy(float voltage)
{
return constrain(lrintf(voltage * 10.0f), 0, 255);
}
static uint16_t toVbatVoltage(float voltage)
{
return constrain(lrintf(voltage * 100.0f), 0, 32000);
}
static uint16_t toIbatCurrent(float current)
{
return constrain(lrintf(current * 100.0f), -32000, 32000);
}
constexpr uint8_t MSP_PASSTHROUGH_ESC_4WAY = 0xff;
}
namespace Espfc {
namespace Connect {
MspProcessor::MspProcessor(Model& model): _model(model) {}
bool MspProcessor::parse(char c, MspMessage& msg)
{
_parser.parse(c, msg);
if(msg.isReady()) debugMessage(msg);
return !msg.isIdle();
}
void MspProcessor::processCommand(MspMessage& m, MspResponse& r, Device::SerialDevice& s)
{
r.cmd = m.cmd;
r.version = m.version;
r.result = 1;
switch(m.cmd)
{
case MSP_API_VERSION:
r.writeU8(MSP_PROTOCOL_VERSION);
r.writeU8(API_VERSION_MAJOR);
r.writeU8(API_VERSION_MINOR);
break;
case MSP_FC_VARIANT:
r.writeData(flightControllerIdentifier, FLIGHT_CONTROLLER_IDENTIFIER_LENGTH);
break;
case MSP_FC_VERSION:
r.writeU8(FC_VERSION_MAJOR);
r.writeU8(FC_VERSION_MINOR);
r.writeU8(FC_VERSION_PATCH_LEVEL);
break;
case MSP_BOARD_INFO:
r.writeData(boardIdentifier, BOARD_IDENTIFIER_LENGTH);
r.writeU16(0); // No other build targets currently have hardware revision detection.
r.writeU8(0); // 0 == FC
r.writeU8(0); // target capabilities
r.writeU8(strlen(targetName)); // target name
r.writeData(targetName, strlen(targetName));
r.writeU8(0); // board name
r.writeU8(0); // manufacturer name
for(size_t i = 0; i < 32; i++) r.writeU8(0); // signature
r.writeU8(255); // mcu id: unknown
// 1.42
r.writeU8(2); // configuration state: configured
// 1.43
r.writeU16(_model.state.gyro.present ? _model.state.gyro.timer.rate : 0); // sample rate
{
uint32_t problems = 0;
if(_model.state.accel.bias.x == 0 && _model.state.accel.bias.y == 0 && _model.state.accel.bias.z == 0) {
problems |= 1 << 0; // acc calibration required
}
if(_model.config.output.protocol == ESC_PROTOCOL_DISABLED) {
problems |= 1 << 1; // no motor protocol
}
r.writeU32(problems); // configuration problems
}
// 1.44
r.writeU8(0); // spi dev count
r.writeU8(0); // i2c dev count
break;
case MSP_BUILD_INFO:
r.writeData(buildDate, BUILD_DATE_LENGTH);
r.writeData(buildTime, BUILD_TIME_LENGTH);
r.writeData(shortGitRevision, GIT_SHORT_REVISION_LENGTH);
break;
case MSP_UID:
r.writeU32(getBoardId0());
r.writeU32(getBoardId1());
r.writeU32(getBoardId2());
break;
case MSP_STATUS_EX:
case MSP_STATUS:
//r.writeU16(_model.state.loopTimer.delta);
r.writeU16(_model.state.stats.loopTime());
r.writeU16(_model.state.i2cErrorCount); // i2c error count
// acc, baro, mag, gps, sonar, gyro
r.writeU16(_model.accelActive() | _model.baroActive() << 1 | _model.magActive() << 2 | _model.gpsActive() << 3 | 0 << 4 | _model.gyroActive() << 5);
r.writeU32(_model.state.mode.mask); // flight mode flags
r.writeU8(0); // pid profile
r.writeU16(lrintf(_model.state.stats.getCpuLoad()));
if (m.cmd == MSP_STATUS_EX) {
r.writeU8(1); // max profile count
r.writeU8(0); // current rate profile index
} else { // MSP_STATUS
//r.writeU16(_model.state.gyro.timer.interval); // gyro cycle time
r.writeU16(0);
}
// flight mode flags (above 32 bits)
r.writeU8(0); // count
// Write arming disable flags
r.writeU8(ARMING_DISABLED_FLAGS_COUNT); // 1 byte, flag count
r.writeU32(_model.state.mode.armingDisabledFlags); // 4 bytes, flags
r.writeU8(0); // reboot required
break;
case MSP_NAME:
r.writeString(_model.config.modelName);
break;
case MSP_SET_NAME:
memset(&_model.config.modelName, 0, MODEL_NAME_LEN + 1);
for(size_t i = 0; i < std::min((size_t)m.received, MODEL_NAME_LEN); i++)
{
_model.config.modelName[i] = m.readU8();
}
break;
case MSP_BOXNAMES:
r.writeString(F("ARM;ANGLE;AIRMODE;BEEPER;FAILSAFE;BLACKBOX;BLACKBOXERASE;"));
break;
case MSP_BOXIDS:
r.writeU8(MODE_ARMED);
r.writeU8(MODE_ANGLE);
r.writeU8(MODE_AIRMODE);
r.writeU8(MODE_BUZZER);
r.writeU8(MODE_FAILSAFE);
r.writeU8(MODE_BLACKBOX);
r.writeU8(MODE_BLACKBOX_ERASE);
break;
case MSP_MODE_RANGES:
for(size_t i = 0; i < ACTUATOR_CONDITIONS; i++)
{
r.writeU8(_model.config.conditions[i].id);
r.writeU8(_model.config.conditions[i].ch - AXIS_AUX_1);
r.writeU8((_model.config.conditions[i].min - 900) / 25);
r.writeU8((_model.config.conditions[i].max - 900) / 25);
}
break;
case MSP_MODE_RANGES_EXTRA:
r.writeU8(ACTUATOR_CONDITIONS);
for(size_t i = 0; i < ACTUATOR_CONDITIONS; i++)
{
r.writeU8(_model.config.conditions[i].id);
r.writeU8(_model.config.conditions[i].logicMode);
r.writeU8(_model.config.conditions[i].linkId);
}
break;
case MSP_SET_MODE_RANGE:
{
size_t i = m.readU8();
if(i < ACTUATOR_CONDITIONS)
{
_model.config.conditions[i].id = m.readU8();
_model.config.conditions[i].ch = m.readU8() + AXIS_AUX_1;
_model.config.conditions[i].min = m.readU8() * 25 + 900;
_model.config.conditions[i].max = m.readU8() * 25 + 900;
if(m.remain() >= 2) {
_model.config.conditions[i].logicMode = m.readU8(); // mode logic
_model.config.conditions[i].linkId = m.readU8(); // link to
}
}
else
{
r.result = -1;
}
}
break;
case MSP_ANALOG:
r.writeU8(toVbatVoltageLegacy(_model.state.battery.voltage)); // voltage in 0.1V
r.writeU16(0); // mah drawn
r.writeU16(_model.getRssi()); // rssi
r.writeU16(toIbatCurrent(_model.state.battery.current)); // amperage in 0.01A
r.writeU16(toVbatVoltage(_model.state.battery.voltage)); // voltage in 0.01V
break;
case MSP_FEATURE_CONFIG:
r.writeU32(_model.config.featureMask);
break;
case MSP_SET_FEATURE_CONFIG:
_model.config.featureMask = m.readU32();
_model.reload();
break;
case MSP_BATTERY_CONFIG:
r.writeU8(34); // vbatmincellvoltage
r.writeU8(42); // vbatmaxcellvoltage
r.writeU8((_model.config.vbat.cellWarning + 5) / 10); // vbatwarningcellvoltage
r.writeU16(0); // batteryCapacity
r.writeU8(_model.config.vbat.source); // voltageMeterSource
r.writeU8(_model.config.ibat.source); // currentMeterSource
r.writeU16(340); // vbatmincellvoltage
r.writeU16(420); // vbatmaxcellvoltage
r.writeU16(_model.config.vbat.cellWarning); // vbatwarningcellvoltage
break;
case MSP_SET_BATTERY_CONFIG:
m.readU8(); // vbatmincellvoltage
m.readU8(); // vbatmaxcellvoltage
_model.config.vbat.cellWarning = m.readU8() * 10; // vbatwarningcellvoltage
m.readU16(); // batteryCapacity
_model.config.vbat.source = toVbatSource(m.readU8()); // voltageMeterSource
_model.config.ibat.source = toIbatSource(m.readU8()); // currentMeterSource
if(m.remain() >= 6)
{
m.readU16(); // vbatmincellvoltage
m.readU16(); // vbatmaxcellvoltage
_model.config.vbat.cellWarning = m.readU16();
}
break;
case MSP_BATTERY_STATE:
// battery characteristics
r.writeU8(_model.state.battery.cells); // cell count, 0 indicates battery not detected.
r.writeU16(0); // capacity in mAh
// battery state
r.writeU8(toVbatVoltageLegacy(_model.state.battery.voltage)); // in 0.1V steps
r.writeU16(0); // milliamp hours drawn from battery
r.writeU16(toIbatCurrent(_model.state.battery.current)); // send current in 0.01 A steps, range is -320A to 320A
// battery alerts
r.writeU8(0);
r.writeU16(toVbatVoltage(_model.state.battery.voltage)); // in 0.01 steps
break;
case MSP_VOLTAGE_METERS:
for(int i = 0; i < 1; i++)
{
r.writeU8(i + 10); // meter id (10-19 vbat adc)
r.writeU8(toVbatVoltageLegacy(_model.state.battery.voltage)); // meter value
}
break;
case MSP_CURRENT_METERS:
for(int i = 0; i < 1; i++)
{
r.writeU8(i + 10); // meter id (10-19 ibat adc)
r.writeU16(0); // mah drawn
r.writeU16(constrain(toIbatCurrent(_model.state.battery.current) * 10, 0, 0xffff)); // meter value
}
break;
case MSP_VOLTAGE_METER_CONFIG:
r.writeU8(1); // num voltage sensors
for(int i = 0; i < 1; i++)
{
r.writeU8(5); // frame size (5)
r.writeU8(i + 10); // id (10-19 vbat adc)
r.writeU8(0); // type resistor divider
r.writeU8(_model.config.vbat.scale); // scale
r.writeU8(_model.config.vbat.resDiv); // resdivval
r.writeU8(_model.config.vbat.resMult); // resdivmultiplier
}
break;
case MSP_SET_VOLTAGE_METER_CONFIG:
{
int id = m.readU8();
if(id == 10 + 0) // id (10-19 vbat adc, allow only 10)
{
_model.config.vbat.scale = m.readU8();
_model.config.vbat.resDiv = m.readU8();
_model.config.vbat.resMult = m.readU8();
}
}
break;
case MSP_CURRENT_METER_CONFIG:
r.writeU8(1); // num voltage sensors
for(int i = 0; i < 1; i++)
{
r.writeU8(6); // frame size (6)
r.writeU8(i + 10); // id (10-19 ibat adc)
r.writeU8(1); // type adc
r.writeU16(_model.config.ibat.scale); // scale
r.writeU16(_model.config.ibat.offset); // offset
}
break;
case MSP_SET_CURRENT_METER_CONFIG:
{
int id = m.readU8();
if(id == 10 + 0) // id (10-19 ibat adc, allow only 10)
{
_model.config.ibat.scale = m.readU16();
_model.config.ibat.offset = m.readU16();
}
}
break;
case MSP_DATAFLASH_SUMMARY:
#ifdef USE_FLASHFS
{
uint8_t flags = flashfsIsSupported() ? 2 : 0;
flags |= flashfsIsReady() ? 1 : 0;
r.writeU8(flags);
r.writeU32(flashfsGetSectors());
r.writeU32(flashfsGetSize());
r.writeU32(flashfsGetOffset());
}
#else
r.writeU8(0);
r.writeU32(0);
r.writeU32(0);
r.writeU32(0);
#endif
break;
case MSP_DATAFLASH_ERASE:
#ifdef USE_FLASHFS
blackboxEraseAll();
#endif
break;
case MSP_DATAFLASH_READ:
#ifdef USE_FLASHFS
{
const unsigned int dataSize = m.remain();
const uint32_t readAddress = m.readU32();
uint16_t readLength;
bool allowCompression = false;
bool useLegacyFormat;
if (dataSize >= sizeof(uint32_t) + sizeof(uint16_t)) {
readLength = m.readU16();
if (m.remain()) {
allowCompression = m.readU8();
}
useLegacyFormat = false;
} else {
readLength = 128;
useLegacyFormat = true;
}
serializeFlashData(r, readAddress, readLength, useLegacyFormat, allowCompression);
}
#endif
break;
case MSP_ACC_TRIM:
r.writeU16(0); // pitch
r.writeU16(0); // roll
break;
case MSP_MIXER_CONFIG:
r.writeU8(_model.config.mixer.type); // mixerMode, QUAD_X
r.writeU8(_model.config.mixer.yawReverse); // yaw_motors_reversed
break;
case MSP_SET_MIXER_CONFIG:
_model.config.mixer.type = m.readU8(); // mixerMode, QUAD_X
_model.config.mixer.yawReverse = m.readU8(); // yaw_motors_reversed
break;
case MSP_SENSOR_CONFIG:
r.writeU8(_model.config.accel.dev); // 3 acc mpu6050
r.writeU8(_model.config.baro.dev); // 2 baro bmp085
r.writeU8(_model.config.mag.dev); // 3 mag hmc5883l
break;
case MSP_SET_SENSOR_CONFIG:
_model.config.accel.dev = m.readU8(); // 3 acc mpu6050
_model.config.baro.dev = m.readU8(); // 2 baro bmp085
_model.config.mag.dev = m.readU8(); // 3 mag hmc5883l
_model.reload();
break;
case MSP_SENSOR_ALIGNMENT:
r.writeU8(_model.config.gyro.align); // gyro align
r.writeU8(_model.config.gyro.align); // acc align, Starting with 4.0 gyro and acc alignment are the same
r.writeU8(_model.config.mag.align); // mag align
//1.41+
r.writeU8(_model.state.gyro.present ? 1 : 0); // gyro detection mask GYRO_1_MASK
r.writeU8(0); // gyro_to_use
r.writeU8(_model.config.gyro.align); // gyro 1
r.writeU8(0); // gyro 2
break;
case MSP_SET_SENSOR_ALIGNMENT:
{
uint8_t gyroAlign = m.readU8(); // gyro align
m.readU8(); // discard deprecated acc align
_model.config.mag.align = m.readU8(); // mag align
// API >= 1.41 - support the gyro_to_use and alignment for gyros 1 & 2
if(m.remain() >= 3)
{
m.readU8(); // gyro_to_use
gyroAlign = m.readU8(); // gyro 1 align
m.readU8(); // gyro 2 align
}
_model.config.gyro.align = gyroAlign;
}
break;
case MSP_CF_SERIAL_CONFIG:
for(int i = 0; i < SERIAL_UART_COUNT; i++)
{
if(_model.config.serial[i].id >= SERIAL_ID_SOFTSERIAL_1 && !_model.isFeatureActive(FEATURE_SOFTSERIAL)) continue;
r.writeU8(_model.config.serial[i].id); // identifier
r.writeU16(_model.config.serial[i].functionMask); // functionMask
r.writeU8(toBaudIndex(_model.config.serial[i].baud)); // msp_baudrateIndex
r.writeU8(0); // gps_baudrateIndex
r.writeU8(0); // telemetry_baudrateIndex
r.writeU8(toBaudIndex(_model.config.serial[i].blackboxBaud)); // blackbox_baudrateIndex
}
break;
case MSP2_COMMON_SERIAL_CONFIG:
{
uint8_t count = 0;
for (int i = 0; i < SERIAL_UART_COUNT; i++)
{
if(_model.config.serial[i].id >= SERIAL_ID_SOFTSERIAL_1 && !_model.isFeatureActive(FEATURE_SOFTSERIAL)) continue;
count++;
}
r.writeU8(count);
for (int i = 0; i < SERIAL_UART_COUNT; i++)
{
if(_model.config.serial[i].id >= SERIAL_ID_SOFTSERIAL_1 && !_model.isFeatureActive(FEATURE_SOFTSERIAL)) continue;
r.writeU8(_model.config.serial[i].id); // identifier
r.writeU32(_model.config.serial[i].functionMask); // functionMask
r.writeU8(toBaudIndex(_model.config.serial[i].baud)); // msp_baudrateIndex
r.writeU8(0); // gps_baudrateIndex
r.writeU8(0); // telemetry_baudrateIndex
r.writeU8(toBaudIndex(_model.config.serial[i].blackboxBaud)); // blackbox_baudrateIndex
}
}
break;
case MSP_SET_CF_SERIAL_CONFIG:
{
const int packetSize = 1 + 2 + 4;
while(m.remain() >= packetSize)
{
int id = m.readU8();
int k = _model.getSerialIndex((SerialPortId)id);
{
m.advance(packetSize - 1);
continue;
}
_model.config.serial[k].id = id;
_model.config.serial[k].functionMask = m.readU16();
_model.config.serial[k].baud = fromBaudIndex((SerialSpeedIndex)m.readU8());
m.readU8();
m.readU8();
_model.config.serial[k].blackboxBaud = fromBaudIndex((SerialSpeedIndex)m.readU8());
}
}
_model.reload();
break;
case MSP2_COMMON_SET_SERIAL_CONFIG:
{
size_t count = m.readU8();
(void)count; // ignore
const int packetSize = 1 + 4 + 4;
while(m.remain() >= packetSize)
{
int id = m.readU8();
int k = _model.getSerialIndex((SerialPortId)id);
if(k == -1)
{
m.advance(packetSize - 1);
continue;
}
_model.config.serial[k].id = id;
_model.config.serial[k].functionMask = m.readU32();
_model.config.serial[k].baud = fromBaudIndex((SerialSpeedIndex)m.readU8());
m.readU8();
m.readU8();
_model.config.serial[k].blackboxBaud = fromBaudIndex((SerialSpeedIndex)m.readU8());
}
}
_model.reload();
break;
case MSP_BLACKBOX_CONFIG:
r.writeU8(1); // Blackbox supported
r.writeU8(_model.config.blackbox.dev); // device serial or none
r.writeU8(1); // blackboxGetRateNum()); // unused
r.writeU8(1); // blackboxGetRateDenom());
r.writeU16(_model.config.blackbox.pDenom);//blackboxGetPRatio()); // p_denom
//r.writeU8(_model.config.blackbox.pDenom); // sample_rate
//r.writeU32(~_model.config.blackbox.fieldsMask);
break;
case MSP_SET_BLACKBOX_CONFIG:
// Don't allow config to be updated while Blackbox is logging
if (true) {
_model.config.blackbox.dev = m.readU8();
const int rateNum = m.readU8(); // was rate_num
const int rateDenom = m.readU8(); // was rate_denom
uint16_t pRatio = 0;
if (m.remain() >= 2) {
pRatio = m.readU16(); // p_denom specified, so use it directly
} else {
// p_denom not specified in MSP, so calculate it from old rateNum and rateDenom
//pRatio = blackboxCalculatePDenom(rateNum, rateDenom);
(void)(rateNum + rateDenom);
}
_model.config.blackbox.pDenom = pRatio;
/*if (m.remain() >= 1) {
_model.config.blackbox.pDenom = m.readU8();
} else if(pRatio > 0) {
_model.config.blackbox.pDenom = blackboxCalculateSampleRate(pRatio);
//_model.config.blackbox.pDenom = pRatio;
}
if (m.remain() >= 4) {
_model.config.blackbox.fieldsMask = ~m.readU32();
}*/
}
break;
case MSP_ATTITUDE:
r.writeU16(lrintf(Utils::toDeg(_model.state.attitude.euler.x) * 10.f)); // roll [decidegrees]
r.writeU16(lrintf(Utils::toDeg(_model.state.attitude.euler.y) * 10.f)); // pitch [decidegrees]
r.writeU16(lrintf(Utils::toDeg(-_model.state.attitude.euler.z))); // yaw [degrees]
break;
case MSP_ALTITUDE:
r.writeU32(lrintf(_model.state.baro.altitude * 100.f)); // alt [cm]
r.writeU16(0); // vario
break;
case MSP_BEEPER_CONFIG:
r.writeU32(~_model.config.buzzer.beeperMask); // beeper mask
r.writeU8(0); // dshot beacon tone
r.writeU32(0); // dshot beacon off flags
break;
case MSP_SET_BEEPER_CONFIG:
_model.config.buzzer.beeperMask = ~m.readU32(); // beeper mask
break;
case MSP_BOARD_ALIGNMENT_CONFIG:
r.writeU16(_model.config.boardAlignment[0]); // roll
r.writeU16(_model.config.boardAlignment[1]); // pitch
r.writeU16(_model.config.boardAlignment[2]); // yaw
break;
case MSP_SET_BOARD_ALIGNMENT_CONFIG:
_model.config.boardAlignment[0] = m.readU16();
_model.config.boardAlignment[1] = m.readU16();
_model.config.boardAlignment[2] = m.readU16();
break;
case MSP_RX_MAP:
for(size_t i = 0; i < INPUT_CHANNELS; i++)
{
r.writeU8(_model.config.input.channel[i].map);
}
break;
case MSP_SET_RX_MAP:
for(size_t i = 0; i < 8; i++)
{
_model.config.input.channel[i].map = m.readU8();
}
break;
case MSP_RSSI_CONFIG:
r.writeU8(_model.config.input.rssiChannel);
break;
case MSP_SET_RSSI_CONFIG:
_model.config.input.rssiChannel = m.readU8();
break;
case MSP_MOTOR_CONFIG:
r.writeU16(_model.config.output.minThrottle); // minthrottle
r.writeU16(_model.config.output.maxThrottle); // maxthrottle
r.writeU16(_model.config.output.minCommand); // mincommand
r.writeU8(_model.state.currentMixer.count); // motor count
// 1.42+
r.writeU8(_model.config.output.motorPoles); // motor pole count
r.writeU8(_model.config.output.dshotTelemetry); // dshot telemtery
r.writeU8(0); // esc sensor
break;
case MSP_SET_MOTOR_CONFIG:
_model.config.output.minThrottle = m.readU16(); // minthrottle
_model.config.output.maxThrottle = m.readU16(); // maxthrottle
_model.config.output.minCommand = m.readU16(); // mincommand
if(m.remain() >= 2)
{
#ifdef ESPFC_DSHOT_TELEMETRY
_model.config.output.motorPoles = m.readU8();
_model.config.output.dshotTelemetry = m.readU8();
#else
m.readU8();
m.readU8();
#endif
}
_model.reload();
break;
case MSP_MOTOR_3D_CONFIG:
r.writeU16(1406); // deadband3d_low;
r.writeU16(1514); // deadband3d_high;
r.writeU16(1460); // neutral3d;
break;
case MSP_ARMING_CONFIG:
r.writeU8(5); // auto_disarm delay
r.writeU8(0); // disarm kill switch
r.writeU8(180); // small angle
break;
case MSP_RC_DEADBAND:
r.writeU8(_model.config.input.deadband);
r.writeU8(0); // yaw deadband
r.writeU8(0); // alt hold deadband
r.writeU16(0); // deadband 3d throttle
break;
case MSP_SET_RC_DEADBAND:
_model.config.input.deadband = m.readU8();
m.readU8(); // yaw deadband
m.readU8(); // alt hod deadband
m.readU16(); // deadband 3d throttle
break;
case MSP_RX_CONFIG:
r.writeU8(_model.config.input.serialRxProvider); // serialrx_provider
r.writeU16(_model.config.input.maxCheck); //maxcheck
r.writeU16(_model.config.input.midRc); //midrc
r.writeU16(_model.config.input.minCheck); //mincheck
r.writeU8(0); // spectrum bind
r.writeU16(_model.config.input.minRc); //min_us
r.writeU16(_model.config.input.maxRc); //max_us
r.writeU8(_model.config.input.interpolationMode); // rc interpolation
r.writeU8(_model.config.input.interpolationInterval); // rc interpolation interval
r.writeU16(1500); // airmode activate threshold
r.writeU8(0); // rx spi prot
r.writeU32(0); // rx spi id
r.writeU8(0); // rx spi chan count
r.writeU8(0); // fpv camera angle
r.writeU8(2); // rc iterpolation channels: RPYT
r.writeU8(_model.config.input.filterType); // rc_smoothing_type
r.writeU8(_model.config.input.filter.freq); // rc_smoothing_input_cutoff
r.writeU8(_model.config.input.filterDerivative.freq); // rc_smoothing_derivative_cutoff
r.writeU8(0);//_model.config.input.filter.type); // rc_smoothing_input_type
r.writeU8(fromFilterTypeDerivative(_model.config.input.filterDerivative.type)); // rc_smoothing_derivative_type
r.writeU8(0); // usb type
// 1.42+
r.writeU8(_model.config.input.filterAutoFactor); // rc_smoothing_auto_factor
break;
case MSP_SET_RX_CONFIG:
_model.config.input.serialRxProvider = m.readU8(); // serialrx_provider
_model.config.input.maxCheck = m.readU16(); //maxcheck
_model.config.input.midRc = m.readU16(); //midrc
_model.config.input.minCheck = m.readU16(); //mincheck
m.readU8(); // spectrum bind
_model.config.input.minRc = m.readU16(); //min_us
_model.config.input.maxRc = m.readU16(); //max_us
if (m.remain() >= 4) {
_model.config.input.interpolationMode = m.readU8(); // rc interpolation
_model.config.input.interpolationInterval = m.readU8(); // rc interpolation interval
m.readU16(); // airmode activate threshold
}
if (m.remain() >= 6) {
m.readU8(); // rx spi prot
m.readU32(); // rx spi id
m.readU8(); // rx spi chan count
}
if (m.remain() >= 1) {
m.readU8(); // fpv camera angle
}
// 1.40+
if (m.remain() >= 6) {
m.readU8(); // rc iterpolation channels
_model.config.input.filterType = m.readU8(); // rc_smoothing_type
_model.config.input.filter.freq = m.readU8(); // rc_smoothing_input_cutoff
_model.config.input.filterDerivative.freq = m.readU8(); // rc_smoothing_derivative_cutoff
//_model.config.input.filter.type = m.readU8() == 1 ? FILTER_BIQUAD : FILTER_PT1; // rc_smoothing_input_type
m.readU8();
_model.config.input.filterDerivative.type = toFilterTypeDerivative(m.readU8()); // rc_smoothing_derivative_type
}
if (m.remain() >= 1) {
m.readU8(); // usb type
}
// 1.42+
if (m.remain() >= 1) {
_model.config.input.filterAutoFactor = m.readU8(); // rc_smoothing_auto_factor
}
_model.reload();
break;
case MSP_FAILSAFE_CONFIG:
r.writeU8(_model.config.failsafe.delay); // failsafe_delay
r.writeU8(0); // failsafe_off_delay
r.writeU16(1000); //failsafe_throttle
r.writeU8(_model.config.failsafe.killSwitch); // failsafe_kill_switch
r.writeU16(0); // failsafe_throttle_low_delay
r.writeU8(1); //failsafe_procedure; default drop
break;
case MSP_SET_FAILSAFE_CONFIG:
_model.config.failsafe.delay = m.readU8(); //failsafe_delay
m.readU8(); //failsafe_off_delay
m.readU16(); //failsafe_throttle
_model.config.failsafe.killSwitch = m.readU8(); //failsafe_kill_switch
m.readU16(); //failsafe_throttle_low_delay
m.readU8(); //failsafe_procedure
break;
case MSP_RXFAIL_CONFIG:
for (size_t i = 0; i < _model.state.input.channelCount; i++)
{
r.writeU8(_model.config.input.channel[i].fsMode);
r.writeU16(_model.config.input.channel[i].fsValue);
}
break;
case MSP_SET_RXFAIL_CONFIG:
{
size_t i = m.readU8();
if(i < INPUT_CHANNELS)
{
_model.config.input.channel[i].fsMode = m.readU8(); // mode
_model.config.input.channel[i].fsValue = m.readU16(); // pulse
}
else
{
r.result = -1;
}
}
break;
case MSP_RC:
for(size_t i = 0; i < _model.state.input.channelCount; i++)
{
r.writeU16(lrintf(_model.state.input.us[i]));
}
break;
case MSP_RC_TUNING:
r.writeU8(_model.config.input.rate[AXIS_ROLL]);
r.writeU8(_model.config.input.expo[AXIS_ROLL]);
for(size_t i = 0; i < AXIS_COUNT_RPY; i++)
{
r.writeU8(_model.config.input.superRate[i]);
}
r.writeU8(_model.config.controller.tpaScale); // dyn thr pid
r.writeU8(50); // thrMid8
r.writeU8(0); // thr expo
r.writeU16(_model.config.controller.tpaBreakpoint); // tpa breakpoint
r.writeU8(_model.config.input.expo[AXIS_YAW]); // yaw expo
r.writeU8(_model.config.input.rate[AXIS_YAW]); // yaw rate
r.writeU8(_model.config.input.rate[AXIS_PITCH]); // pitch rate
r.writeU8(_model.config.input.expo[AXIS_PITCH]); // pitch expo
// 1.41+
r.writeU8(_model.config.output.throttleLimitType); // throttle_limit_type (off)
r.writeU8(_model.config.output.throttleLimitPercent); // throtle_limit_percent (100%)
//1.42+
r.writeU16(_model.config.input.rateLimit[0]); // rate limit roll
r.writeU16(_model.config.input.rateLimit[1]); // rate limit pitch
r.writeU16(_model.config.input.rateLimit[2]); // rate limit yaw
// 1.43+
r.writeU8(_model.config.input.rateType); // rates type
break;
case MSP_SET_RC_TUNING:
if(m.remain() >= 10)
{
const uint8_t rate = m.readU8();
if(_model.config.input.rate[AXIS_PITCH] == _model.config.input.rate[AXIS_ROLL])
{
_model.config.input.rate[AXIS_PITCH] = rate;
}
_model.config.input.rate[AXIS_ROLL] = rate;
const uint8_t expo = m.readU8();
if(_model.config.input.expo[AXIS_PITCH] == _model.config.input.expo[AXIS_ROLL])
{
_model.config.input.expo[AXIS_PITCH] = expo;
}
_model.config.input.expo[AXIS_ROLL] = expo;
for(size_t i = 0; i < AXIS_COUNT_RPY; i++)
{
_model.config.input.superRate[i] = m.readU8();
}
_model.config.controller.tpaScale = Utils::clamp(m.readU8(), (uint8_t)0, (uint8_t)90); // dyn thr pid
m.readU8(); // thrMid8
m.readU8(); // thr expo
_model.config.controller.tpaBreakpoint = Utils::clamp(m.readU16(), (uint16_t)1000, (uint16_t)2000); // tpa breakpoint
if(m.remain() >= 1)
{
_model.config.input.expo[AXIS_YAW] = m.readU8(); // yaw expo
}
if(m.remain() >= 1)
{
_model.config.input.rate[AXIS_YAW] = m.readU8(); // yaw rate
}
if(m.remain() >= 1)
{
_model.config.input.rate[AXIS_PITCH] = m.readU8(); // pitch rate
}
if(m.remain() >= 1)
{
_model.config.input.expo[AXIS_PITCH] = m.readU8(); // pitch expo
}
// 1.41
if(m.remain() >= 2)
{
_model.config.output.throttleLimitType = m.readU8(); // throttle_limit_type
_model.config.output.throttleLimitPercent = m.readU8(); // throttle_limit_percent
}
// 1.42
if(m.remain() >= 6)
{
_model.config.input.rateLimit[0] = m.readU16(); // roll
_model.config.input.rateLimit[1] = m.readU16(); // pitch
_model.config.input.rateLimit[2] = m.readU16(); // yaw
}
// 1.43
if (m.remain() >= 1)
{
_model.config.input.rateType = m.readU8();
}
}
else
{
r.result = -1;
// error
}
break;
case MSP_ADVANCED_CONFIG:
r.writeU8(1); // gyroSync unused