/
rc_adjustments.c
873 lines (796 loc) · 32.8 KB
/
rc_adjustments.c
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
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "platform.h"
#include "blackbox/blackbox.h"
#include "blackbox/blackbox_fielddefs.h"
#include "build/build_config.h"
#include "common/axis.h"
#include "common/maths.h"
#include "common/utils.h"
#include "config/feature.h"
#include "drivers/time.h"
#include "fc/config.h"
#include "fc/controlrate_profile.h"
#include "fc/rc_controls.h"
#include "fc/rc.h"
#include "flight/pid.h"
#include "io/beeper.h"
#include "io/ledstrip.h"
#include "io/motors.h"
#include "io/pidaudio.h"
#include "osd/osd.h"
#include "pg/pg.h"
#include "pg/pg_ids.h"
#include "pg/rx.h"
#include "rx/rx.h"
#include "rc_adjustments.h"
#define ADJUSTMENT_RANGE_COUNT_INVALID -1
PG_REGISTER_ARRAY(adjustmentRange_t, MAX_ADJUSTMENT_RANGE_COUNT, adjustmentRanges, PG_ADJUSTMENT_RANGE_CONFIG, 1);
uint8_t pidAudioPositionToModeMap[7] = {
// on a pot with a center detent, it's easy to have center area for off/default, then three positions to the left and three to the right.
// current implementation yields RC values as below.
PID_AUDIO_PIDSUM_X, // 900 - ~1071 - Min
PID_AUDIO_PIDSUM_Y, // ~1071 - ~1242
PID_AUDIO_PIDSUM_XY, // ~1242 - ~1414
PID_AUDIO_OFF, // ~1414 - ~1585 - Center
PID_AUDIO_OFF, // ~1585 - ~1757
PID_AUDIO_OFF, // ~1757 - ~1928
PID_AUDIO_OFF, // ~1928 - 2100 - Max
// Note: Last 3 positions are currently pending implementations and use PID_AUDIO_OFF for now.
};
static int activeAdjustmentCount = ADJUSTMENT_RANGE_COUNT_INVALID;
static uint8_t activeAdjustmentArray[MAX_ADJUSTMENT_RANGE_COUNT];
static int activeAbsoluteAdjustmentCount;
static uint8_t activeAbsoluteAdjustmentArray[MAX_ADJUSTMENT_RANGE_COUNT];
static void blackboxLogInflightAdjustmentEvent(adjustmentFunction_e adjustmentFunction, int32_t newValue)
{
#ifndef USE_BLACKBOX
UNUSED(adjustmentFunction);
UNUSED(newValue);
#else
if (blackboxConfig()->device) {
flightLogEvent_inflightAdjustment_t eventData;
eventData.adjustmentFunction = adjustmentFunction;
eventData.newValue = newValue;
eventData.floatFlag = false;
blackboxLogEvent(FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT, (flightLogEventData_t*)&eventData);
}
#endif
}
#if 0
static void blackboxLogInflightAdjustmentEventFloat(adjustmentFunction_e adjustmentFunction, float newFloatValue)
{
#ifndef USE_BLACKBOX
UNUSED(adjustmentFunction);
UNUSED(newFloatValue);
#else
if (blackboxConfig()->device) {
flightLogEvent_inflightAdjustment_t eventData;
eventData.adjustmentFunction = adjustmentFunction;
eventData.newFloatValue = newFloatValue;
eventData.floatFlag = true;
blackboxLogEvent(FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT, (flightLogEventData_t*)&eventData);
}
#endif
}
#endif
STATIC_UNIT_TESTED uint8_t adjustmentStateMask = 0;
#define MARK_ADJUSTMENT_FUNCTION_AS_BUSY(adjustmentIndex) adjustmentStateMask |= (1 << adjustmentIndex)
#define MARK_ADJUSTMENT_FUNCTION_AS_READY(adjustmentIndex) adjustmentStateMask &= ~(1 << adjustmentIndex)
#define IS_ADJUSTMENT_FUNCTION_BUSY(adjustmentIndex) (adjustmentStateMask & (1 << adjustmentIndex))
// sync with adjustmentFunction_e
static const adjustmentConfig_t defaultAdjustmentConfigs[ADJUSTMENT_FUNCTION_COUNT - 1] = {
{
.adjustmentFunction = ADJUSTMENT_RC_RATE,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_RC_EXPO,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_THROTTLE_EXPO,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_RATE,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_YAW_RATE,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_P,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_I,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_D,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_YAW_P,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_YAW_I,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_YAW_D,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_RATE_PROFILE,
.mode = ADJUSTMENT_MODE_SELECT,
.data = { .switchPositions = 3 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_RATE,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_ROLL_RATE,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_P,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_I,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_D,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_ROLL_P,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_ROLL_I,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_ROLL_D,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_RC_RATE_YAW,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_ROLL_F,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_FEEDFORWARD_TRANSITION,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_HORIZON_STRENGTH,
.mode = ADJUSTMENT_MODE_SELECT,
.data = { .switchPositions = 255 }
}, {
.adjustmentFunction = ADJUSTMENT_PID_AUDIO,
.mode = ADJUSTMENT_MODE_SELECT,
.data = { .switchPositions = ARRAYLEN(pidAudioPositionToModeMap) }
}, {
.adjustmentFunction = ADJUSTMENT_PITCH_F,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_ROLL_F,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_YAW_F,
.mode = ADJUSTMENT_MODE_STEP,
.data = { .step = 1 }
}, {
.adjustmentFunction = ADJUSTMENT_OSD_PROFILE,
.mode = ADJUSTMENT_MODE_SELECT,
.data = { .switchPositions = 3 }
}, {
.adjustmentFunction = ADJUSTMENT_LED_PROFILE,
.mode = ADJUSTMENT_MODE_SELECT,
.data = { .switchPositions = 3 }
}
};
#if defined(USE_OSD) && defined(USE_OSD_ADJUSTMENTS)
static const char * const adjustmentLabels[] = {
"RC RATE",
"RC EXPO",
"THROTTLE EXPO",
"ROLL RATE",
"YAW RATE",
"PITCH/ROLL P",
"PITCH/ROLL I",
"PITCH/ROLL D",
"YAW P",
"YAW I",
"YAW D",
"RATE PROFILE",
"PITCH RATE",
"ROLL RATE",
"PITCH P",
"PITCH I",
"PITCH D",
"ROLL P",
"ROLL I",
"ROLL D",
"RC RATE YAW",
"PITCH/ROLL F",
"FF TRANSITION",
"HORIZON STRENGTH",
"ROLL RC RATE",
"PITCH RC RATE",
"ROLL RC EXPO",
"PITCH RC EXPO",
"PID AUDIO",
"PITCH F",
"ROLL F",
"YAW F",
"OSD PROFILE",
};
static int adjustmentRangeNameIndex = 0;
static int adjustmentRangeValue = -1;
#endif
#define ADJUSTMENT_FUNCTION_CONFIG_INDEX_OFFSET 1
STATIC_UNIT_TESTED adjustmentState_t adjustmentStates[MAX_SIMULTANEOUS_ADJUSTMENT_COUNT];
STATIC_UNIT_TESTED void configureAdjustment(uint8_t index, uint8_t auxSwitchChannelIndex, const adjustmentConfig_t *adjustmentConfig)
{
adjustmentState_t *adjustmentState = &adjustmentStates[index];
if (adjustmentState->config == adjustmentConfig) {
// already configured
return;
}
adjustmentState->auxChannelIndex = auxSwitchChannelIndex;
adjustmentState->config = adjustmentConfig;
adjustmentState->timeoutAt = 0;
MARK_ADJUSTMENT_FUNCTION_AS_READY(index);
}
static int applyStepAdjustment(controlRateConfig_t *controlRateConfig, uint8_t adjustmentFunction, int delta)
{
beeperConfirmationBeeps(delta > 0 ? 2 : 1);
int newValue;
switch (adjustmentFunction) {
case ADJUSTMENT_RC_RATE:
case ADJUSTMENT_ROLL_RC_RATE:
newValue = constrain((int)controlRateConfig->rcRates[FD_ROLL] + delta, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RC_RATE, newValue);
if (adjustmentFunction == ADJUSTMENT_ROLL_RC_RATE) {
break;
}
// fall through for combined ADJUSTMENT_RC_EXPO
FALLTHROUGH;
case ADJUSTMENT_PITCH_RC_RATE:
newValue = constrain((int)controlRateConfig->rcRates[FD_PITCH] + delta, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RC_RATE, newValue);
break;
case ADJUSTMENT_RC_EXPO:
case ADJUSTMENT_ROLL_RC_EXPO:
newValue = constrain((int)controlRateConfig->rcExpo[FD_ROLL] + delta, 0, CONTROL_RATE_CONFIG_RC_EXPO_MAX);
controlRateConfig->rcExpo[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RC_EXPO, newValue);
if (adjustmentFunction == ADJUSTMENT_ROLL_RC_EXPO) {
break;
}
// fall through for combined ADJUSTMENT_RC_EXPO
FALLTHROUGH;
case ADJUSTMENT_PITCH_RC_EXPO:
newValue = constrain((int)controlRateConfig->rcExpo[FD_PITCH] + delta, 0, CONTROL_RATE_CONFIG_RC_EXPO_MAX);
controlRateConfig->rcExpo[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RC_EXPO, newValue);
break;
case ADJUSTMENT_THROTTLE_EXPO:
newValue = constrain((int)controlRateConfig->thrExpo8 + delta, 0, 100); // FIXME magic numbers repeated in cli.c
controlRateConfig->thrExpo8 = newValue;
initRcProcessing();
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_THROTTLE_EXPO, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_RATE:
case ADJUSTMENT_PITCH_RATE:
newValue = constrain((int)controlRateConfig->rates[FD_PITCH] + delta, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RATE, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_RATE) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_RATE
FALLTHROUGH;
case ADJUSTMENT_ROLL_RATE:
newValue = constrain((int)controlRateConfig->rates[FD_ROLL] + delta, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RATE, newValue);
break;
case ADJUSTMENT_YAW_RATE:
newValue = constrain((int)controlRateConfig->rates[FD_YAW] + delta, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_YAW] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_RATE, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_P:
case ADJUSTMENT_PITCH_P:
newValue = constrain((int)currentPidProfile->pid[PID_PITCH].P + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_P, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_P) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_P
FALLTHROUGH;
case ADJUSTMENT_ROLL_P:
newValue = constrain((int)currentPidProfile->pid[PID_ROLL].P + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_P, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_I:
case ADJUSTMENT_PITCH_I:
newValue = constrain((int)currentPidProfile->pid[PID_PITCH].I + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_I, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_I) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_I
FALLTHROUGH;
case ADJUSTMENT_ROLL_I:
newValue = constrain((int)currentPidProfile->pid[PID_ROLL].I + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_I, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_D:
case ADJUSTMENT_PITCH_D:
newValue = constrain((int)currentPidProfile->pid[PID_PITCH].D + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_D, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_D) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_D
FALLTHROUGH;
case ADJUSTMENT_ROLL_D:
newValue = constrain((int)currentPidProfile->pid[PID_ROLL].D + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_D, newValue);
break;
case ADJUSTMENT_YAW_P:
newValue = constrain((int)currentPidProfile->pid[PID_YAW].P + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_P, newValue);
break;
case ADJUSTMENT_YAW_I:
newValue = constrain((int)currentPidProfile->pid[PID_YAW].I + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_I, newValue);
break;
case ADJUSTMENT_YAW_D:
newValue = constrain((int)currentPidProfile->pid[PID_YAW].D + delta, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_D, newValue);
break;
case ADJUSTMENT_RC_RATE_YAW:
newValue = constrain((int)controlRateConfig->rcRates[FD_YAW] + delta, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_YAW] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_RC_RATE_YAW, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_F:
case ADJUSTMENT_PITCH_F:
newValue = constrain(currentPidProfile->pid[PID_PITCH].F + delta, 0, 2000);
currentPidProfile->pid[PID_PITCH].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_F, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_F) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_F
FALLTHROUGH;
case ADJUSTMENT_ROLL_F:
newValue = constrain(currentPidProfile->pid[PID_ROLL].F + delta, 0, 2000);
currentPidProfile->pid[PID_ROLL].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_F, newValue);
break;
case ADJUSTMENT_YAW_F:
newValue = constrain(currentPidProfile->pid[PID_YAW].F + delta, 0, 2000);
currentPidProfile->pid[PID_YAW].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_F, newValue);
break;
case ADJUSTMENT_FEEDFORWARD_TRANSITION:
newValue = constrain(currentPidProfile->feedForwardTransition + delta, 1, 100); // FIXME magic numbers repeated in cli.c
currentPidProfile->feedForwardTransition = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_FEEDFORWARD_TRANSITION, newValue);
break;
default:
newValue = -1;
break;
};
return newValue;
}
static int applyAbsoluteAdjustment(controlRateConfig_t *controlRateConfig, adjustmentFunction_e adjustmentFunction, int value)
{
int newValue;
if ( !controlRateConfig || !currentPidProfile) {
return 0;
}
switch (adjustmentFunction) {
case ADJUSTMENT_RC_RATE:
case ADJUSTMENT_ROLL_RC_RATE:
newValue = constrain(value, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RC_RATE, newValue);
if (adjustmentFunction == ADJUSTMENT_ROLL_RC_RATE) {
break;
}
// fall through for combined ADJUSTMENT_RC_EXPO
FALLTHROUGH;
case ADJUSTMENT_PITCH_RC_RATE:
newValue = constrain(value, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RC_RATE, newValue);
break;
case ADJUSTMENT_RC_EXPO:
case ADJUSTMENT_ROLL_RC_EXPO:
newValue = constrain(value, 1, CONTROL_RATE_CONFIG_RC_EXPO_MAX);
controlRateConfig->rcExpo[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RC_EXPO, newValue);
if (adjustmentFunction == ADJUSTMENT_ROLL_RC_EXPO) {
break;
}
// fall through for combined ADJUSTMENT_RC_EXPO
FALLTHROUGH;
case ADJUSTMENT_PITCH_RC_EXPO:
newValue = constrain(value, 0, CONTROL_RATE_CONFIG_RC_EXPO_MAX);
controlRateConfig->rcExpo[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RC_EXPO, newValue);
break;
case ADJUSTMENT_THROTTLE_EXPO:
newValue = constrain(value, 0, 100); // FIXME magic numbers repeated in cli.c
controlRateConfig->thrExpo8 = newValue;
initRcProcessing();
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_THROTTLE_EXPO, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_RATE:
case ADJUSTMENT_PITCH_RATE:
newValue = constrain(value, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_PITCH] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_RATE, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_RATE) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_RATE
FALLTHROUGH;
case ADJUSTMENT_ROLL_RATE:
newValue = constrain(value, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_ROLL] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_RATE, newValue);
break;
case ADJUSTMENT_YAW_RATE:
newValue = constrain(value, 0, CONTROL_RATE_CONFIG_RATE_MAX);
controlRateConfig->rates[FD_YAW] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_RATE, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_P:
case ADJUSTMENT_PITCH_P:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_P, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_P) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_P
FALLTHROUGH;
case ADJUSTMENT_ROLL_P:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_P, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_I:
case ADJUSTMENT_PITCH_I:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_I, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_I) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_I
FALLTHROUGH;
case ADJUSTMENT_ROLL_I:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_I, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_D:
case ADJUSTMENT_PITCH_D:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_PITCH].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_D, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_D) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_D
FALLTHROUGH;
case ADJUSTMENT_ROLL_D:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_ROLL].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_D, newValue);
break;
case ADJUSTMENT_YAW_P:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].P = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_P, newValue);
break;
case ADJUSTMENT_YAW_I:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].I = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_I, newValue);
break;
case ADJUSTMENT_YAW_D:
newValue = constrain(value, 0, 200); // FIXME magic numbers repeated in cli.c
currentPidProfile->pid[PID_YAW].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_D, newValue);
break;
case ADJUSTMENT_RC_RATE_YAW:
newValue = constrain(value, 1, CONTROL_RATE_CONFIG_RC_RATES_MAX);
controlRateConfig->rcRates[FD_YAW] = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_RC_RATE_YAW, newValue);
break;
case ADJUSTMENT_PITCH_ROLL_F:
case ADJUSTMENT_PITCH_F:
newValue = constrain(value, 0, 2000);
currentPidProfile->pid[PID_PITCH].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_PITCH_F, newValue);
if (adjustmentFunction == ADJUSTMENT_PITCH_F) {
break;
}
// fall through for combined ADJUSTMENT_PITCH_ROLL_F
FALLTHROUGH;
case ADJUSTMENT_ROLL_F:
newValue = constrain(value, 0, 2000);
currentPidProfile->pid[PID_ROLL].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_ROLL_F, newValue);
break;
case ADJUSTMENT_YAW_F:
newValue = constrain(value, 0, 2000);
currentPidProfile->pid[PID_YAW].F = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_YAW_F, newValue);
break;
case ADJUSTMENT_FEEDFORWARD_TRANSITION:
newValue = constrain(value, 1, 100); // FIXME magic numbers repeated in cli.c
currentPidProfile->feedForwardTransition = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_FEEDFORWARD_TRANSITION, newValue);
break;
default:
newValue = -1;
break;
};
return newValue;
}
static uint8_t applySelectAdjustment(adjustmentFunction_e adjustmentFunction, uint8_t position)
{
uint8_t beeps = 0;
switch (adjustmentFunction) {
case ADJUSTMENT_RATE_PROFILE:
if (getCurrentControlRateProfileIndex() != position) {
changeControlRateProfile(position);
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_RATE_PROFILE, position);
beeps = position + 1;
}
break;
case ADJUSTMENT_HORIZON_STRENGTH:
{
uint8_t newValue = constrain(position, 0, 200); // FIXME magic numbers repeated in serial_cli.c
if (currentPidProfile->pid[PID_LEVEL].D != newValue) {
beeps = ((newValue - currentPidProfile->pid[PID_LEVEL].D) / 8) + 1;
currentPidProfile->pid[PID_LEVEL].D = newValue;
blackboxLogInflightAdjustmentEvent(ADJUSTMENT_HORIZON_STRENGTH, position);
}
}
break;
case ADJUSTMENT_PID_AUDIO:
#ifdef USE_PID_AUDIO
{
pidAudioModes_e newMode = pidAudioPositionToModeMap[position];
if (newMode != pidAudioGetMode()) {
pidAudioSetMode(newMode);
}
}
#endif
break;
case ADJUSTMENT_OSD_PROFILE:
#ifdef USE_OSD_PROFILES
if (getCurrentOsdProfileIndex() != (position + 1)) {
changeOsdProfileIndex(position+1);
}
#endif
break;
case ADJUSTMENT_LED_PROFILE:
#ifdef USE_LED_STRIP
if (getLedProfile() != position) {
setLedProfile(position);
}
#endif
break;
default:
break;
}
if (beeps) {
beeperConfirmationBeeps(beeps);
}
return position;
}
static void calcActiveAdjustmentRanges(void)
{
adjustmentRange_t defaultAdjustmentRange;
memset(&defaultAdjustmentRange, 0, sizeof(defaultAdjustmentRange));
activeAdjustmentCount = 0;
activeAbsoluteAdjustmentCount = 0;
for (int i = 0; i < MAX_ADJUSTMENT_RANGE_COUNT; i++) {
const adjustmentRange_t * const adjustmentRange = adjustmentRanges(i);
if (memcmp(adjustmentRange, &defaultAdjustmentRange, sizeof(defaultAdjustmentRange)) != 0) {
if (adjustmentRange->adjustmentCenter == 0) {
activeAdjustmentArray[activeAdjustmentCount++] = i;
} else {
activeAbsoluteAdjustmentArray[activeAbsoluteAdjustmentCount++] = i;
}
}
}
}
static void updateAdjustmentStates(void)
{
for (int index = 0; index < activeAdjustmentCount; index++) {
const adjustmentRange_t * const adjustmentRange = adjustmentRanges(activeAdjustmentArray[index]);
// Only use slots if center value has not been specified, otherwise apply values directly (scaled) from aux channel
if (isRangeActive(adjustmentRange->auxChannelIndex, &adjustmentRange->range) &&
(adjustmentRange->adjustmentCenter == 0)) {
const adjustmentConfig_t *adjustmentConfig = &defaultAdjustmentConfigs[adjustmentRange->adjustmentConfig - ADJUSTMENT_FUNCTION_CONFIG_INDEX_OFFSET];
configureAdjustment(adjustmentRange->adjustmentIndex, adjustmentRange->auxSwitchChannelIndex, adjustmentConfig);
}
}
}
#define RESET_FREQUENCY_2HZ (1000 / 2)
void processRcAdjustments(controlRateConfig_t *controlRateConfig)
{
const uint32_t now = millis();
int newValue = -1;
const bool canUseRxData = rxIsReceivingSignal();
// Recalculate the new active adjustments if required
if (activeAdjustmentCount == ADJUSTMENT_RANGE_COUNT_INVALID) {
calcActiveAdjustmentRanges();
}
updateAdjustmentStates();
// Process Increment/Decrement adjustments
for (int adjustmentIndex = 0; adjustmentIndex < MAX_SIMULTANEOUS_ADJUSTMENT_COUNT; adjustmentIndex++) {
adjustmentState_t *adjustmentState = &adjustmentStates[adjustmentIndex];
if (!adjustmentState->config) {
continue;
}
const adjustmentFunction_e adjustmentFunction = adjustmentState->config->adjustmentFunction;
if (adjustmentFunction == ADJUSTMENT_NONE) {
continue;
}
if (cmp32(now, adjustmentState->timeoutAt) >= 0) {
adjustmentState->timeoutAt = now + RESET_FREQUENCY_2HZ;
MARK_ADJUSTMENT_FUNCTION_AS_READY(adjustmentIndex);
#if defined(USE_OSD) && defined(USE_OSD_ADJUSTMENTS)
adjustmentRangeValue = -1;
#endif
}
if (!canUseRxData) {
continue;
}
const uint8_t channelIndex = NON_AUX_CHANNEL_COUNT + adjustmentState->auxChannelIndex;
if (adjustmentState->config->mode == ADJUSTMENT_MODE_STEP) {
int delta;
if (rcData[channelIndex] > rxConfig()->midrc + 200) {
delta = adjustmentState->config->data.step;
} else if (rcData[channelIndex] < rxConfig()->midrc - 200) {
delta = -adjustmentState->config->data.step;
} else {
// returning the switch to the middle immediately resets the ready state
MARK_ADJUSTMENT_FUNCTION_AS_READY(adjustmentIndex);
adjustmentState->timeoutAt = now + RESET_FREQUENCY_2HZ;
continue;
}
if (IS_ADJUSTMENT_FUNCTION_BUSY(adjustmentIndex)) {
continue;
}
newValue = applyStepAdjustment(controlRateConfig, adjustmentFunction, delta);
pidInitConfig(currentPidProfile);
} else if (adjustmentState->config->mode == ADJUSTMENT_MODE_SELECT) {
int switchPositions = adjustmentState->config->data.switchPositions;
if (adjustmentFunction == ADJUSTMENT_RATE_PROFILE && systemConfig()->rateProfile6PosSwitch) {
switchPositions = 6;
}
const uint16_t rangeWidth = (2100 - 900) / switchPositions;
const uint8_t position = (constrain(rcData[channelIndex], 900, 2100 - 1) - 900) / rangeWidth;
newValue = applySelectAdjustment(adjustmentFunction, position);
}
#if defined(USE_OSD) && defined(USE_OSD_ADJUSTMENTS)
if (newValue != -1
&& adjustmentState->config->adjustmentFunction != ADJUSTMENT_RATE_PROFILE // Rate profile already has an OSD element
#ifdef USE_OSD_PROFILES
&& adjustmentState->config->adjustmentFunction != ADJUSTMENT_OSD_PROFILE
#endif
#ifdef USE_LED_STRIP
&& adjustmentState->config->adjustmentFunction != ADJUSTMENT_LED_PROFILE
#endif
) {
adjustmentRangeNameIndex = adjustmentFunction;
adjustmentRangeValue = newValue;
}
#else
UNUSED(newValue);
#endif
MARK_ADJUSTMENT_FUNCTION_AS_BUSY(adjustmentIndex);
}
if (!canUseRxData) {
return;
}
// Process Absolute adjustments
for (int i = 0; i < activeAbsoluteAdjustmentCount; i++) {
static int16_t lastRcData[MAX_ADJUSTMENT_RANGE_COUNT] = { 0 };
int index = activeAbsoluteAdjustmentArray[i];
const adjustmentRange_t * const adjustmentRange = adjustmentRanges(index);
const uint8_t channelIndex = NON_AUX_CHANNEL_COUNT + adjustmentRange->auxSwitchChannelIndex;
const adjustmentConfig_t *adjustmentConfig = &defaultAdjustmentConfigs[adjustmentRange->adjustmentConfig - ADJUSTMENT_FUNCTION_CONFIG_INDEX_OFFSET];
// If setting is defined for step adjustment and center value has been specified, apply values directly (scaled) from aux channel
if ((rcData[channelIndex] != lastRcData[index]) &&
adjustmentRange->adjustmentCenter &&
(adjustmentConfig->mode == ADJUSTMENT_MODE_STEP) &&
isRangeActive(adjustmentRange->auxChannelIndex, &adjustmentRange->range)) {
int value = (((rcData[channelIndex] - PWM_RANGE_MIDDLE) * adjustmentRange->adjustmentScale) / (PWM_RANGE_MIDDLE - PWM_RANGE_MIN)) + adjustmentRange->adjustmentCenter;
lastRcData[index] = rcData[channelIndex];
applyAbsoluteAdjustment(controlRateConfig, adjustmentConfig->adjustmentFunction, value);
pidInitConfig(currentPidProfile);
}
}
}
void resetAdjustmentStates(void)
{
memset(adjustmentStates, 0, sizeof(adjustmentStates));
}
#if defined(USE_OSD) && defined(USE_OSD_ADJUSTMENTS)
const char *getAdjustmentsRangeName(void)
{
if (adjustmentRangeNameIndex > 0) {
return &adjustmentLabels[adjustmentRangeNameIndex - 1][0];
} else {
return NULL;
}
}
int getAdjustmentsRangeValue(void)
{
return adjustmentRangeValue;
}
#endif
void activeAdjustmentRangeReset(void)
{
activeAdjustmentCount = ADJUSTMENT_RANGE_COUNT_INVALID;
}