forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSRV_Channel_aux.cpp
920 lines (851 loc) · 27.3 KB
/
SRV_Channel_aux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
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
/*
This program is free software: you can redistribute it and/or modify
it 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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
SRV_Channel_aux.cpp - handling of servo auxiliary functions
*/
#include "SRV_Channel.h"
#include <AP_Math/AP_Math.h>
#include <AP_HAL/AP_HAL.h>
#include <RC_Channel/RC_Channel.h>
#include <GCS_MAVLink/GCS.h>
#if NUM_SERVO_CHANNELS == 0
#pragma GCC diagnostic ignored "-Wtype-limits"
#endif
extern const AP_HAL::HAL& hal;
/// map a function to a servo channel and output it
void SRV_Channel::output_ch(void)
{
#ifndef HAL_BUILD_AP_PERIPH
int8_t passthrough_from = -1;
bool passthrough_mapped = false;
// take care of special function cases
switch(function.get())
{
case k_manual: // manual
passthrough_from = ch_num;
break;
case k_rcin1 ... k_rcin16: // rc pass-thru
passthrough_from = int8_t((int16_t)function - k_rcin1);
break;
case k_rcin1_mapped ... k_rcin16_mapped:
passthrough_from = int8_t((int16_t)function - k_rcin1_mapped);
passthrough_mapped = true;
break;
}
if (passthrough_from != -1) {
// we are doing passthrough from input to output for this channel
RC_Channel *c = rc().channel(passthrough_from);
if (c) {
if (SRV_Channels::passthrough_disabled()) {
output_pwm = c->get_radio_trim();
} else {
// non-mapped rc passthrough
int16_t radio_in = c->get_radio_in();
if (passthrough_mapped) {
if (rc().has_valid_input()) {
switch (c->get_type()) {
case RC_Channel::ControlType::ANGLE:
radio_in = pwm_from_angle(c->norm_input_dz() * 4500);
break;
case RC_Channel::ControlType::RANGE:
// convert RC normalised input from -1 to +1 range to 0 to +1 and output as range
radio_in = pwm_from_range((c->norm_input_ignore_trim() + 1.0) * 0.5 * 4500);
break;
}
} else {
// no valid input. If we are in radio
// failsafe then go to trim values (if
// configured for this channel). Otherwise
// use the last-good value
if ( ((1U<<passthrough_from) & SRV_Channels::get_rc_fs_mask()) && rc().in_rc_failsafe()) {
radio_in = pwm_from_angle(0);
} else {
radio_in = previous_radio_in;
}
}
}
if (!ign_small_rcin_changes) {
output_pwm = radio_in;
previous_radio_in = radio_in;
} else {
// check if rc input value has changed by more than the deadzone
if (abs(radio_in - previous_radio_in) > c->get_dead_zone()) {
output_pwm = radio_in;
ign_small_rcin_changes = false;
}
}
}
}
}
#endif // HAL_BUILD_AP_PERIPH
if (!(SRV_Channels::disabled_mask & (1U<<ch_num))) {
hal.rcout->write(ch_num, output_pwm);
}
}
/*
call output_ch() on all channels
*/
void SRV_Channels::output_ch_all(void)
{
uint8_t max_chan = NUM_SERVO_CHANNELS;
#if NUM_SERVO_CHANNELS >= 17
if (_singleton != nullptr && _singleton->enable_32_channels.get() <= 0) {
max_chan = 16;
}
#endif
for (uint8_t i = 0; i < max_chan; i++) {
channels[i].output_ch();
}
}
/*
return the current function for a channel
*/
SRV_Channel::Function SRV_Channels::channel_function(uint8_t channel)
{
if (channel < NUM_SERVO_CHANNELS) {
return channels[channel].function;
}
return SRV_Channel::k_none;
}
/*
setup a channels aux servo function
*/
void SRV_Channel::aux_servo_function_setup(void)
{
if (type_setup) {
return;
}
switch (function.get()) {
case k_flap:
case k_flap_auto:
case k_egg_drop:
case k_lift_release:
set_range(100);
break;
case k_heli_rsc:
case k_heli_tail_rsc:
case k_motor_tilt:
case k_boost_throttle:
case k_thrust_out:
set_range(1000);
break;
case k_aileron_with_input:
case k_elevator_with_input:
case k_aileron:
case k_elevator:
case k_dspoilerLeft1:
case k_dspoilerLeft2:
case k_dspoilerRight1:
case k_dspoilerRight2:
case k_rudder:
case k_steering:
case k_flaperon_left:
case k_flaperon_right:
case k_tiltMotorLeft:
case k_tiltMotorRight:
case k_tiltMotorRear:
case k_tiltMotorRearLeft:
case k_tiltMotorRearRight:
case k_elevon_left:
case k_elevon_right:
case k_vtail_left:
case k_vtail_right:
case k_scripting1:
case k_scripting2:
case k_scripting3:
case k_scripting4:
case k_scripting5:
case k_scripting6:
case k_scripting7:
case k_scripting8:
case k_scripting9:
case k_scripting10:
case k_scripting11:
case k_scripting12:
case k_scripting13:
case k_scripting14:
case k_scripting15:
case k_scripting16:
case k_roll_out:
case k_pitch_out:
case k_yaw_out:
case k_rcin1_mapped ... k_rcin16_mapped:
set_angle(4500);
break;
case k_throttle:
case k_throttleLeft:
case k_throttleRight:
case k_airbrake:
// fixed wing throttle
set_range(100);
break;
case k_cam_zoom:
case k_cam_focus:
case k_lights1:
case k_lights2:
case k_video_switch:
set_range(1000);
break;
default:
break;
}
}
/// setup the output range types of all functions
void SRV_Channels::update_aux_servo_function(void)
{
if (!channels) {
return;
}
function_mask.clearall();
for (uint16_t i = 0; i < SRV_Channel::k_nr_aux_servo_functions; i++) {
functions[i].channel_mask = 0;
}
invalid_mask = 0;
// set auxiliary ranges
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (!channels[i].valid_function()) {
invalid_mask |= 1U<<i;
continue;
}
const uint16_t function = channels[i].function.get();
channels[i].aux_servo_function_setup();
function_mask.set(function);
functions[function].channel_mask |= 1U<<i;
}
initialised = true;
}
/// Should be called after the servo functions have been initialized
/// called at 1Hz
void SRV_Channels::enable_aux_servos()
{
hal.rcout->set_default_rate(uint16_t(_singleton->default_rate.get()));
update_aux_servo_function();
// enable all channels that are set to a valid function. This
// includes k_none servos, which allows those to get their initial
// trim value on startup
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
// see if it is a valid function
if (c.valid_function() && !(disabled_mask & (1U<<c.ch_num))) {
hal.rcout->enable_ch(c.ch_num);
} else {
hal.rcout->disable_ch(c.ch_num);
}
// output some servo functions before we fiddle with the
// parameter values:
if (c.function == SRV_Channel::k_min) {
c.set_output_pwm(c.servo_min);
c.output_ch();
} else if (c.function == SRV_Channel::k_trim) {
c.set_output_pwm(c.servo_trim);
c.output_ch();
} else if (c.function == SRV_Channel::k_max) {
c.set_output_pwm(c.servo_max);
c.output_ch();
}
}
// propagate channel masks to the ESCS
hal.rcout->update_channel_masks();
#if HAL_SUPPORT_RCOUT_SERIAL
blheli.update();
#endif
}
/*
for channels which have been marked as digital output then the
MIN/MAX/TRIM values have no meaning for controlling output, as
the HAL handles the scaling. We still need to cope with places
in the code that may try to set a PWM value however, so to
ensure consistency we force the MIN/MAX/TRIM to be consistent
across all digital channels. We use a MIN/MAX of 1000/2000, and
set TRIM to either 1000 or 1500 depending on whether the channel
is reversible
*/
void SRV_Channels::set_digital_outputs(uint32_t dig_mask, uint32_t rev_mask) {
digital_mask |= dig_mask;
reversible_mask |= rev_mask;
// add in NeoPixel and ProfiLED functions to digital array to determine anything else
// that should be disabled
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
switch (c.function.get()) {
case SRV_Channel::k_LED_neopixel1:
case SRV_Channel::k_LED_neopixel2:
case SRV_Channel::k_LED_neopixel3:
case SRV_Channel::k_LED_neopixel4:
case SRV_Channel::k_ProfiLED_1:
case SRV_Channel::k_ProfiLED_2:
case SRV_Channel::k_ProfiLED_3:
case SRV_Channel::k_ProfiLED_Clock:
dig_mask |= 1U<<c.ch_num;
break;
default:
break;
}
}
disabled_mask = hal.rcout->get_disabled_channels(dig_mask);
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (digital_mask & (1U<<i)) {
c.servo_min.set_and_default(1000);
c.servo_max.set_and_default(2000);
if (reversible_mask & (1U<<i)) {
c.servo_trim.set_and_default(1500);
} else {
c.servo_trim.set_and_default(1000);
}
}
}
}
/// enable output channels using a channel mask
void SRV_Channels::enable_by_mask(uint32_t mask)
{
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (mask & (1U<<i)) {
hal.rcout->enable_ch(i);
}
}
}
/*
set radio_out for all channels matching the given function type
*/
void SRV_Channels::set_output_pwm(SRV_Channel::Function function, uint16_t value)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].set_output_pwm(value);
channels[i].output_ch();
}
}
}
/*
set radio_out for all channels matching the given function type
trim the output assuming a 1500 center on the given value
reverses pwm output based on channel reversed property
*/
void
SRV_Channels::set_output_pwm_trimmed(SRV_Channel::Function function, int16_t value)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
int16_t value2;
if (channels[i].get_reversed()) {
value2 = 1500 - value + channels[i].get_trim();
} else {
value2 = value - 1500 + channels[i].get_trim();
}
channels[i].set_output_pwm(constrain_int16(value2,channels[i].get_output_min(),channels[i].get_output_max()));
channels[i].output_ch();
}
}
}
/*
set and save the trim value to current output for all channels matching
the given function type
*/
void
SRV_Channels::set_trim_to_servo_out_for(SRV_Channel::Function function)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].servo_trim.set_and_save_ifchanged(channels[i].get_output_pwm());
}
}
}
#if AP_RC_CHANNEL_ENABLED
/*
copy radio_in to radio_out for a given function
*/
void
SRV_Channels::copy_radio_in_out(SRV_Channel::Function function, bool do_input_output)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
RC_Channel *c = rc().channel(channels[i].ch_num);
if (c == nullptr) {
continue;
}
channels[i].set_output_pwm(c->get_radio_in());
if (do_input_output) {
channels[i].output_ch();
}
}
}
}
/*
copy radio_in to radio_out for a channel mask
*/
void
SRV_Channels::copy_radio_in_out_mask(uint32_t mask)
{
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if ((1U<<i) & mask) {
RC_Channel *c = rc().channel(channels[i].ch_num);
if (c == nullptr) {
continue;
}
channels[i].set_output_pwm(c->get_radio_in());
}
}
}
#endif // AP_RC_CHANNEL_ENABLED
/*
setup failsafe value for an auxiliary function type to a Limit
*/
void
SRV_Channels::set_failsafe_pwm(SRV_Channel::Function function, uint16_t pwm)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
const SRV_Channel &c = channels[i];
if (c.function == function) {
hal.rcout->set_failsafe_pwm(1U<<c.ch_num, pwm);
}
}
}
/*
setup failsafe value for an auxiliary function type to a Limit
*/
void
SRV_Channels::set_failsafe_limit(SRV_Channel::Function function, SRV_Channel::Limit limit)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
const SRV_Channel &c = channels[i];
if (c.function == function) {
uint16_t pwm = c.get_limit_pwm(limit);
hal.rcout->set_failsafe_pwm(1U<<c.ch_num, pwm);
}
}
}
/*
set radio output value for an auxiliary function type to a Limit
*/
void
SRV_Channels::set_output_limit(SRV_Channel::Function function, SRV_Channel::Limit limit)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (c.function == function) {
uint16_t pwm = c.get_limit_pwm(limit);
c.set_output_pwm(pwm);
#if AP_RC_CHANNEL_ENABLED
if (c.function == SRV_Channel::k_manual) {
RC_Channel *cin = rc().channel(c.ch_num);
if (cin != nullptr) {
// in order for output_ch() to work for k_manual we
// also have to override radio_in
cin->set_radio_in(pwm);
}
}
#endif
}
}
}
/*
return true if a particular function is assigned to at least one RC channel
*/
bool
SRV_Channels::function_assigned(SRV_Channel::Function function)
{
if (!initialised) {
update_aux_servo_function();
}
return function_mask.get(uint16_t(function));
}
/*
set servo_out and angle_min/max, then calc_pwm and output a
value. This is used to move a AP_Mount servo
*/
void
SRV_Channels::move_servo(SRV_Channel::Function function,
int16_t value, int16_t angle_min, int16_t angle_max)
{
if (!function_assigned(function)) {
return;
}
if (angle_max <= angle_min) {
return;
}
float v = float(value - angle_min) / float(angle_max - angle_min);
v = constrain_float(v, 0.0f, 1.0f);
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (c.function == function) {
float v2 = c.get_reversed()? (1-v) : v;
uint16_t pwm = c.servo_min + v2 * (c.servo_max - c.servo_min);
c.set_output_pwm(pwm);
}
}
}
/*
set the default channel an auxiliary output function should be on
*/
bool SRV_Channels::set_aux_channel_default(SRV_Channel::Function function, uint8_t channel)
{
if (function_assigned(function)) {
// already assigned
return true;
}
if (channels[channel].function != SRV_Channel::k_none &&
!(channel >15 && channels[channel].function == SRV_Channel::k_GPIO)) {
if (channels[channel].function == function) {
return true;
}
hal.console->printf("Channel %u already assigned function %u\n",
(unsigned)(channel + 1),
(unsigned)channels[channel].function.get());
return false;
}
channels[channel].type_setup = false;
channels[channel].function.set_and_default(function);
channels[channel].aux_servo_function_setup();
function_mask.set((uint16_t)function);
if (SRV_Channel::valid_function(function)) {
functions[function].channel_mask |= 1U<<channel;
}
return true;
}
// find first channel that a function is assigned to
bool SRV_Channels::find_channel(SRV_Channel::Function function, uint8_t &chan)
{
// Must have populated channel masks
if (!initialised) {
update_aux_servo_function();
}
// Make sure function is valid
if (!SRV_Channel::valid_function(function)) {
return false;
}
// Get the index of the first set bit, returns 0 if no bits are set
const int first_chan = __builtin_ffs(functions[function].channel_mask);
if (first_chan == 0) {
return false;
}
// Convert to 0 indexed
chan = first_chan - 1;
return true;
}
/*
get a pointer to first auxiliary channel for a channel function
*/
SRV_Channel *SRV_Channels::get_channel_for(SRV_Channel::Function function)
{
uint8_t chan;
if (!find_channel(function, chan)) {
return nullptr;
}
return &channels[chan];
}
void SRV_Channels::set_output_scaled(SRV_Channel::Function function, float value)
{
if (SRV_Channel::valid_function(function)) {
functions[function].output_scaled = value;
SRV_Channel::have_pwm_mask &= ~functions[function].channel_mask;
}
}
float SRV_Channels::get_output_scaled(SRV_Channel::Function function)
{
if (SRV_Channel::valid_function(function)) {
return functions[function].output_scaled;
}
return 0;
}
// get slew limited scaled output for the given function type
float SRV_Channels::get_slew_limited_output_scaled(SRV_Channel::Function function)
{
if (!SRV_Channel::valid_function(function)) {
return 0.0;
}
for (slew_list *slew = _slew; slew; slew = slew->next) {
if (slew->func == function) {
if (!is_positive(slew->max_change)) {
// treat negative or zero slew rate as disabled
break;
}
return constrain_float(functions[function].output_scaled, slew->last_scaled_output - slew->max_change, slew->last_scaled_output + slew->max_change);
}
}
// no slew limiting
return functions[function].output_scaled;
}
/*
get mask of output channels for a function
*/
uint32_t SRV_Channels::get_output_channel_mask(SRV_Channel::Function function)
{
if (!initialised) {
update_aux_servo_function();
}
if (SRV_Channel::valid_function(function)) {
return functions[function].channel_mask;
}
return invalid_mask;
}
// set the trim for a function channel to given pwm
void SRV_Channels::set_trim_to_pwm_for(SRV_Channel::Function function, int16_t pwm)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].servo_trim.set_and_default(pwm);
}
}
}
// set the trim for a function channel to min output of the channel honnoring reverse unless ignore_reversed is true
void SRV_Channels::set_trim_to_min_for(SRV_Channel::Function function, bool ignore_reversed)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].servo_trim.set_and_default((channels[i].get_reversed() && !ignore_reversed)?channels[i].servo_max:channels[i].servo_min);
}
}
}
/*
set the default function for a channel
*/
void SRV_Channels::set_default_function(uint8_t chan, SRV_Channel::Function function)
{
if (chan < NUM_SERVO_CHANNELS) {
const SRV_Channel::Function old = channels[chan].function;
channels[chan].function.set_default(function);
if (old != channels[chan].function && channels[chan].function == function) {
function_mask.set((uint16_t)function);
}
}
}
void SRV_Channels::set_esc_scaling_for(SRV_Channel::Function function)
{
uint8_t chan;
if (find_channel(function, chan)) {
hal.rcout->set_esc_scaling(channels[chan].get_output_min(), channels[chan].get_output_max());
}
}
/*
auto-adjust channel trim from an integrator value. Positive v means
adjust trim up. Negative means decrease
*/
void SRV_Channels::adjust_trim(SRV_Channel::Function function, float v)
{
if (is_zero(v)) {
return;
}
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (function != c.function) {
continue;
}
float change = c.reversed?-v:v;
uint16_t new_trim = c.servo_trim;
if (c.servo_max <= c.servo_min) {
continue;
}
float trim_scaled = float(c.servo_trim - c.servo_min) / (c.servo_max - c.servo_min);
if (change > 0 && trim_scaled < 0.6f) {
new_trim++;
} else if (change < 0 && trim_scaled > 0.4f) {
new_trim--;
} else {
continue;
}
c.servo_trim.set(new_trim);
trimmed_mask |= 1U<<i;
}
}
// get pwm output for the first channel of the given function type.
bool SRV_Channels::get_output_pwm(SRV_Channel::Function function, uint16_t &value)
{
uint8_t chan;
if (!find_channel(function, chan)) {
return false;
}
if (!SRV_Channel::valid_function(function)) {
return false;
}
channels[chan].calc_pwm(functions[function].output_scaled);
value = channels[chan].get_output_pwm();
return true;
}
// set output pwm to trim for the given function
void SRV_Channels::set_output_to_trim(SRV_Channel::Function function)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].set_output_pwm(channels[i].servo_trim);
}
}
}
/*
get the normalised output for a channel function from the pwm value
of the first matching channel
*/
float SRV_Channels::get_output_norm(SRV_Channel::Function function)
{
uint8_t chan;
if (!find_channel(function, chan)) {
return 0;
}
if (SRV_Channel::valid_function(function)) {
channels[chan].calc_pwm(functions[function].output_scaled);
}
return channels[chan].get_output_norm();
}
// set normalised output (-1 to 1 with 0 at mid point of servo_min/servo_max) for the given function
void SRV_Channels::set_output_norm(SRV_Channel::Function function, float value)
{
if (!function_assigned(function)) {
return;
}
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (c.function == function) {
c.set_output_norm(value);
}
}
}
/*
limit slew rate for an output function to given rate in percent per
second. This assumes output has not yet done to the hal
*/
void SRV_Channels::set_slew_rate(SRV_Channel::Function function, float slew_rate, uint16_t range, float dt)
{
if (!SRV_Channel::valid_function(function)) {
return;
}
const float max_change = range * slew_rate * 0.01 * dt;
for (slew_list *slew = _slew; slew; slew = slew->next) {
if (slew->func == function) {
// found existing item, update max change
slew->max_change = max_change;
return;
}
}
if (!is_positive(max_change)) {
// no point in adding a disabled slew limit
return;
}
// add new item
slew_list *new_slew = NEW_NOTHROW slew_list(function);
if (new_slew == nullptr) {
return;
}
new_slew->last_scaled_output = functions[function].output_scaled;
new_slew->max_change = max_change;
new_slew->next = _slew;
_slew = new_slew;
}
// call set_angle() on matching channels
void SRV_Channels::set_angle(SRV_Channel::Function function, uint16_t angle)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].set_angle(angle);
}
}
}
// call set_range() on matching channels
void SRV_Channels::set_range(SRV_Channel::Function function, uint16_t range)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].set_range(range);
}
}
}
// set MIN parameter for a function
void SRV_Channels::set_output_min_max(SRV_Channel::Function function, uint16_t min_pwm, uint16_t max_pwm)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].set_output_min(min_pwm);
channels[i].set_output_max(max_pwm);
}
}
}
// set MIN/MAX parameter defaults for a function
void SRV_Channels::set_output_min_max_defaults(SRV_Channel::Function function, uint16_t min_pwm, uint16_t max_pwm)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
channels[i].servo_min.set_default(min_pwm);
channels[i].servo_max.set_default(max_pwm);
}
}
}
// Save MIN/MAX/REVERSED parameters for a function
void SRV_Channels::save_output_min_max(SRV_Channel::Function function, uint16_t min_pwm, uint16_t max_pwm)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
// If min is larger than max swap and set reversed
const bool reversed = min_pwm > max_pwm;
channels[i].servo_min.set_and_save(reversed ? max_pwm : min_pwm);
channels[i].servo_max.set_and_save(reversed ? min_pwm : max_pwm);
channels[i].reversed.set_and_save(reversed ? 1 : 0);
}
}
}
// constrain to output min/max for function
void SRV_Channels::constrain_pwm(SRV_Channel::Function function)
{
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (c.function == function) {
c.set_output_pwm(constrain_int16(c.output_pwm, c.servo_min, c.servo_max));
}
}
}
/*
upgrade SERVO* parameters. This does the following:
- update to 16 bit FUNCTION from AP_Int8
*/
void SRV_Channels::upgrade_parameters(void)
{
// PARAMETER_CONVERSION - Added: Jan-2020
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
// convert from AP_Int8 to AP_Int16
c.function.convert_parameter_width(AP_PARAM_INT8);
}
}
// set RC output frequency on a function output
void SRV_Channels::set_rc_frequency(SRV_Channel::Function function, uint16_t frequency_hz)
{
uint32_t mask = 0;
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
SRV_Channel &c = channels[i];
if (c.function == function) {
mask |= (1U<<c.ch_num);
}
}
if (mask != 0) {
hal.rcout->set_freq(mask, frequency_hz);
}
}