-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat(1).c
More file actions
1991 lines (1757 loc) · 70.5 KB
/
float(1).c
File metadata and controls
1991 lines (1757 loc) · 70.5 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
/*
Copyright 2019 - 2022 Mitch Lustig
Copyright 2022 Benjamin Vedder benjamin@vedder.se
This file is part of the VESC firmware.
The VESC firmware 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.
The VESC firmware 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/>.
*/
#include "vesc_c_if.h"
#include "conf/datatypes.h"
#include "conf/confparser.h"
#include "conf/confxml.h"
#include "conf/buffer.h"
#include "conf/conf_default.h"
#include <math.h>
#include <string.h>
#define ACCEL_ARRAY_SIZE 40 // For ATR Acceleration average
#define ACCEL_ARRAY_SIZE1 10 // For Tration Control acceleration average
#define ERPM_ARRAY_SIZE1 250 // For traction control erpm tracking
HEADER
// Return the sign of the argument. -1.0 if negative, 1.0 if zero or positive.
#define SIGN(x) (((x) < 0.0) ? -1.0 : 1.0)
#define DEG2RAD_f(deg) ((deg) * (float)(M_PI / 180.0))
#define RAD2DEG_f(rad) ((rad) * (float)(180.0 / M_PI))
#define UNUSED(x) (void)(x)
// Data type
typedef enum {
STARTUP = 0,
RUNNING = 1,
RUNNING_TILTBACK = 2,
RUNNING_WHEELSLIP = 3,
RUNNING_UPSIDEDOWN = 4,
FAULT_ANGLE_PITCH = 6, // skipped 5 for compatibility
FAULT_ANGLE_ROLL = 7,
FAULT_SWITCH_HALF = 8,
FAULT_SWITCH_FULL = 9,
FAULT_DUTY = 10, // unused but kept for compatibility
FAULT_STARTUP = 11,
FAULT_REVERSE = 12,
FAULT_QUICKSTOP = 13,
DISABLED = 15
} FloatState;
typedef enum {
CENTERING = 0,
REVERSESTOP,
TILTBACK_NONE,
TILTBACK_DUTY,
TILTBACK_HV,
TILTBACK_LV,
TILTBACK_TEMP
} SetpointAdjustmentType;
typedef enum {
OFF = 0,
HALF,
ON
} SwitchState;
// This is all persistent state of the application, which will be allocated in init. It
// is put here because variables can only be read-only when this program is loaded
// in flash without virtual memory in RAM (as all RAM already is dedicated to the
// main firmware and managed from there). This is probably the main limitation of
// loading applications in runtime, but it is not too bad to work around.
typedef struct {
lib_thread thread; // Balance Thread
float_config float_conf;
// Firmware version, passed in from Lisp
int fw_version_major, fw_version_minor, fw_version_beta;
// Buzzer
int beep_num_left;
int beep_duration;
int beep_countdown;
bool buzzer_enabled;
// Config values
float loop_time_seconds;
unsigned int start_counter_clicks, start_counter_clicks_max;
float startup_pitch_trickmargin, startup_pitch_tolerance;
float startup_step_size;
float tiltback_duty_step_size, tiltback_hv_step_size, tiltback_lv_step_size, tiltback_return_step_size;
float inputtilt_ramped_step_size, inputtilt_step_size;
float mc_max_temp_fet, mc_max_temp_mot;
float mc_current_max, mc_current_min, max_continuous_current;
bool current_beeping;
bool duty_beeping;
// Feature: True Pitch
ATTITUDE_INFO m_att_ref;
// Runtime values read from elsewhere
float pitch_angle, last_pitch_angle, roll_angle, abs_roll_angle, abs_roll_angle_sin, last_gyro_y;
float true_pitch_angle;
float gyro[3];
float duty_cycle, abs_duty_cycle;
float erpm, abs_erpm, avg_erpm;
float motor_current;
float adc1, adc2;
float throttle_val;
float max_duty_with_margin;
SwitchState switch_state;
// Feature: ATR (Adaptive Torque Response)
float acceleration, last_erpm;
float accelhist[ACCEL_ARRAY_SIZE];
float accelavg;
int accelidx;
bool braking;
// Rumtime state values
FloatState state;
float proportional;
float pid_prop, pid_rate, pid_mod;
float last_proportional, abs_proportional;
float pid_value;
float setpoint, setpoint_target, setpoint_target_interpolated;
float applied_booster_current;
float inputtilt_interpolated;
float filtered_current;
SetpointAdjustmentType setpointAdjustmentType;
float current_time, last_time, diff_time, loop_overshoot; // Seconds
float disengage_timer, nag_timer; // Seconds
float idle_voltage;
float filtered_loop_overshoot, loop_overshoot_alpha, filtered_diff_time;
float fault_angle_pitch_timer, fault_angle_roll_timer, fault_switch_timer, fault_switch_half_timer; // Seconds
float motor_timeout_seconds;
float brake_timeout; // Seconds
float overcurrent_timer, tb_highvoltage_timer;
float switch_warn_buzz_erpm;
float quickstop_erpm;
bool traction_control;
// Feature: Simple start
bool enable_simple_start;
// Feature: Soft Start
float softstart_pid_limit, softstart_ramp_step_size;
// Brake Amp Rate Limiting:
float pid_brake_increment;
// Odometer
float odo_timer;
int odometer_dirty;
uint64_t odometer;
//Sitcky Tilt
float stickytilt_val;
bool stickytilton;
float stickytilt_maxval;
float last_throttle_val;
bool stickytiltoff;
// Feature: Surge
float surge_timer; //Timer to monitor surge cycle and period
bool surge; //Identifies surge state which drives duty to max
float differential; //Pitch differential
float surge_period; //.75 //Period between each surge, in seconds. Prevents runaway and instability.
float surge_cycle; //.30 //Length of surge, in seconds
float surge_startanglespeed; //50 //Nose speed that can initiate a surge
float surge_difflimit; //2 //Pitch required at start angle speed to initiate surge, in degrees
float surge_diffcount; //Counter to watch for continuous start angle speed or else reset to zero
float surge_currentmargin; //1.5 //Higher current margin ends surge later, min value of 1
float debug1;
float debug2;
float debug3;
float debug4;
float debug5;
float debug6;
float debug7;
float debug8;
float debug9;
float debug10;
//Traction Control
float wheelslip_timeron;
float wheelslip_timeroff;
bool wheelslip_highaccelon1;
bool wheelslip_highaccelon2;
float wheelslip_accelstart;
float wheelslip_accelend;
float wheelslip_accelstartval;
float accelhist1[ACCEL_ARRAY_SIZE1];
float accelavg1;
int accelidx1;
float erpmhist1[ERPM_ARRAY_SIZE1];
float erpmavg1;
int erpmidx1;
// Drop Detection
bool wheelslip_drop;
float wheelslip_droptimeron;
float wheelslip_droptimeroff;
float last_accel_z;
float accel_z;
float accel[3];
float wheelslip_dropcount;
float applied_accel_z_reduction;
float wheelslip_droplimit;
// Angle P Scaling
float x_est_last1;
float P_last1;
float x_est_last2;
float P_last2;
float prop_smooth;
// Log values
float float_setpoint, float_inputtilt;
} data;
static void brake(data *d);
static void set_current(data *d, float current);
/**
* BUZZER / BEEPER on Servo Pin
*/
const VESC_PIN buzzer_pin = VESC_PIN_PPM;
#define EXT_BUZZER_ON() VESC_IF->io_write(buzzer_pin, 1)
#define EXT_BUZZER_OFF() VESC_IF->io_write(buzzer_pin, 0)
void buzzer_init()
{
VESC_IF->io_set_mode(buzzer_pin, VESC_PIN_MODE_OUTPUT);
}
void buzzer_update(data *d)
{
if (d->buzzer_enabled && (d->beep_num_left > 0)) {
d->beep_countdown--;
if (d->beep_countdown <= 0) {
d->beep_countdown = d->beep_duration;
d->beep_num_left--;
if (d->beep_num_left & 0x1)
EXT_BUZZER_ON();
else
EXT_BUZZER_OFF();
}
}
}
void buzzer_enable(data *d, bool enable)
{
d->buzzer_enabled = enable;
if (!enable) {
EXT_BUZZER_OFF();
}
}
void beep_alert(data *d, int num_beeps, bool longbeep)
{
if (!d->buzzer_enabled)
return;
if (d->beep_num_left == 0) {
d->beep_num_left = num_beeps * 2 + 1;
d->beep_duration = longbeep ? 300 : 80;
d->beep_countdown = d->beep_duration;
}
}
void beep_off(data *d, bool force)
{
// don't mess with the buzzer if we're in the process of doing a multi-beep
if (force || (d->beep_num_left == 0))
EXT_BUZZER_OFF();
}
void beep_on(data *d, bool force)
{
if (!d->buzzer_enabled)
return;
// don't mess with the buzzer if we're in the process of doing a multi-beep
if (force || (d->beep_num_left == 0))
EXT_BUZZER_ON();
}
// First start only, set initial state
static void app_init(data *d) {
if (d->state != DISABLED) {
d->state = STARTUP;
}
d->buzzer_enabled = true;
// Allow saving of odometer
d->odometer_dirty = 0;
d->odometer = VESC_IF->mc_get_odometer();
}
static void configure(data *d) {
// This timer is used to determine how long the board has been disengaged / idle
d->disengage_timer = d->current_time;
// Set calculated values from config
d->loop_time_seconds = 1.0 / d->float_conf.hertz;
d->motor_timeout_seconds = d->loop_time_seconds * 20; // Times 20 for a nice long grace period
d->startup_step_size = d->float_conf.startup_speed / d->float_conf.hertz;
d->tiltback_duty_step_size = d->float_conf.tiltback_duty_speed / d->float_conf.hertz;
d->tiltback_hv_step_size = d->float_conf.tiltback_hv_speed / d->float_conf.hertz;
d->tiltback_lv_step_size = d->float_conf.tiltback_lv_speed / d->float_conf.hertz;
d->tiltback_return_step_size = d->float_conf.tiltback_return_speed / d->float_conf.hertz;
d->inputtilt_step_size = d->float_conf.inputtilt_speed / d->float_conf.hertz;
// Feature: Stealthy start vs normal start (noticeable click when engaging) - 0-20A
d->start_counter_clicks_max = 3;
// Feature: Soft Start
d->softstart_ramp_step_size = (float)100 / d->float_conf.hertz;
// Feature: Dirty Landings
d->startup_pitch_trickmargin = d->float_conf.startup_dirtylandings_enabled ? 10 : 0;
// Overwrite App CFG Mahony KP to Float CFG Value
if (VESC_IF->get_cfg_float(CFG_PARAM_IMU_mahony_kp) != d->float_conf.mahony_kp) {
VESC_IF->set_cfg_float(CFG_PARAM_IMU_mahony_kp, d->float_conf.mahony_kp);
}
d->mc_max_temp_fet = VESC_IF->get_cfg_float(CFG_PARAM_l_temp_fet_start) - 3;
d->mc_max_temp_mot = VESC_IF->get_cfg_float(CFG_PARAM_l_temp_motor_start) - 3;
d->mc_current_max = VESC_IF->get_cfg_float(CFG_PARAM_l_current_max);
int mcm = d->mc_current_max;
float mc_max_reduce = d->mc_current_max - mcm;
if (mc_max_reduce >= 0.5) {
// reduce the max current by X% to save that for torque tilt situations
// less than 60 peak amps makes no sense though so I'm not allowing it
d->mc_current_max = fmaxf(mc_max_reduce * d->mc_current_max, 60);
}
// min current is a positive value here!
d->mc_current_min = fabsf(VESC_IF->get_cfg_float(CFG_PARAM_l_current_min));
mcm = d->mc_current_min;
float mc_min_reduce = fabsf(d->mc_current_min - mcm);
if (mc_min_reduce >= 0.5) {
// reduce the max current by X% to save that for torque tilt situations
// less than 50 peak breaking amps makes no sense though so I'm not allowing it
d->mc_current_min = fmaxf(mc_min_reduce * d->mc_current_min, 50);
}
// Decimals of abs-max specify max continuous current
float max_abs = VESC_IF->get_cfg_float(CFG_PARAM_l_abs_current_max);
int mabs = max_abs;
d->max_continuous_current = (max_abs - mabs) * 100;
if (d->max_continuous_current < 25) {
// anything below 25A is suspicious and will be ignored!
d->max_continuous_current = d->mc_current_max;
}
// Maximum amps change when braking
d->pid_brake_increment = 5;
if (d->pid_brake_increment < 0.1) {
d->pid_brake_increment = 5;
}
d->max_duty_with_margin = VESC_IF->get_cfg_float(CFG_PARAM_l_max_duty) - 0.1;
// Init Filters
float loop_time_filter = 3.0; // Originally Parameter, now hard-coded to 3Hz
d->loop_overshoot_alpha = 2.0 * M_PI * ((float)1.0 / (float)d->float_conf.hertz) *
loop_time_filter / (2.0 * M_PI * (1.0 / (float)d->float_conf.hertz) *
loop_time_filter + 1.0);
// Allows smoothing of Remote Tilt
d->inputtilt_ramped_step_size = 0;
// Speed above which to warn users about an impending full switch fault
d->switch_warn_buzz_erpm = d->float_conf.is_footbuzz_enabled ? 2000 : 100000;
// Speed below which we check for quickstop conditions
d->quickstop_erpm = 200;
// Reset loop time variables
d->last_time = 0.0;
d->filtered_loop_overshoot = 0.0;
d->buzzer_enabled = d->float_conf.is_buzzer_enabled;
if (d->float_conf.float_disable) {
d->state = DISABLED;
beep_alert(d, 3, false);
}
else {
d->state = STARTUP;
beep_alert(d, 1, false);
}
}
static void reset_vars(data *d) {
// Clear accumulated values.
d->last_proportional = 0;
// Set values for startup
d->setpoint = d->pitch_angle;
d->setpoint_target_interpolated = d->pitch_angle;
d->setpoint_target = 0;
d->applied_booster_current = 0;
if (d->inputtilt_interpolated != d->stickytilt_val || !(VESC_IF->get_ppm_age() < 1)) { // Persistent sticky tilt value if we are at value with remote connected
d->inputtilt_interpolated = 0; // Reset other values
}
d->setpointAdjustmentType = CENTERING;
d->state = RUNNING;
d->current_time = 0;
d->last_time = 0;
d->diff_time = 0;
d->brake_timeout = 0;
d->traction_control = false;
d->pid_value = 0;
d->pid_mod = 0;
d->pid_prop = 0;
d->pid_integral = 0;
d->softstart_pid_limit = 0;
d->startup_pitch_tolerance = d->float_conf.startup_pitch_tolerance;
for (int i = 0; i < ACCEL_ARRAY_SIZE; i++)
d->accelhist[i] = 0;
d->accelidx = 0;
d->accelavg = 0;
for (int i = 0; i < ACCEL_ARRAY_SIZE1; i++)
d->accelhist1[i] = 0;
d->accelidx1 = 0;
d->accelavg1 = 0;
for (int i = 0; i < ERPM_ARRAY_SIZE1; i++)
d->erpmhist1[i] = 0;
d->erpmidx1 = 0;
d->erpmavg1 = 0;
// Feature: click on start
d->start_counter_clicks = d->start_counter_clicks_max;
//Angle P Scaling
d->x_est_last1 = 0;
d->P_last1 = 0;
d->x_est_last2 = 0;
d->P_last2 = 0;
}
/**
* check_odometer: see if we need to write back the odometer during fault state
*/
static void check_odometer(data *d)
{
// Make odometer persistent if we've gone 200m or more
if (d->odometer_dirty > 0) {
float stored_odo = VESC_IF->mc_get_odometer();
if ((stored_odo > d->odometer + 200) || (stored_odo < d->odometer - 10000)) {
if (d->odometer_dirty == 1) {
// Wait 10 seconds before writing to avoid writing if immediately continuing to ride
d->odo_timer = d->current_time;
d->odometer_dirty++;
}
else if ((d->current_time - d->odo_timer) > 10) {
VESC_IF->store_backup_data();
d->odometer = VESC_IF->mc_get_odometer();
d->odometer_dirty = 0;
}
}
}
}
static float get_setpoint_adjustment_step_size(data *d) {
switch(d->setpointAdjustmentType){
case (CENTERING):
return d->startup_step_size;
case (TILTBACK_DUTY):
return d->tiltback_duty_step_size;
case (TILTBACK_HV):
case (TILTBACK_TEMP):
return d->tiltback_hv_step_size;
case (TILTBACK_LV):
return d->tiltback_lv_step_size;
case (TILTBACK_NONE):
return d->tiltback_return_step_size;
default:
;
}
return 0;
}
// Read ADCs and determine switch state
static SwitchState check_adcs(data *d) {
SwitchState sw_state;
// Calculate switch state from ADC values
if(d->float_conf.fault_adc1 == 0 && d->float_conf.fault_adc2 == 0){ // No Switch
sw_state = ON;
}else if(d->float_conf.fault_adc2 == 0){ // Single switch on ADC1
if(d->adc1 > d->float_conf.fault_adc1){
sw_state = ON;
} else {
sw_state = OFF;
}
}else if(d->float_conf.fault_adc1 == 0){ // Single switch on ADC2
if(d->adc2 > d->float_conf.fault_adc2){
sw_state = ON;
} else {
sw_state = OFF;
}
}else{ // Double switch
if(d->adc1 > d->float_conf.fault_adc1 && d->adc2 > d->float_conf.fault_adc2){
sw_state = ON;
}else if(d->adc1 > d->float_conf.fault_adc1 || d->adc2 > d->float_conf.fault_adc2){
// 5 seconds after stopping we allow starting with a single sensor (e.g. for jump starts)
bool is_simple_start = d->float_conf.startup_simplestart_enabled &&
(d->current_time - d->disengage_timer > 5);
if (d->float_conf.fault_is_dual_switch || is_simple_start)
sw_state = ON;
else
sw_state = HALF;
}else{
sw_state = OFF;
}
}
if ((sw_state == OFF) && (d->state <= RUNNING_TILTBACK)) {
if (d->abs_erpm > d->switch_warn_buzz_erpm) {
// If we're at riding speed and the switch is off => ALERT the user
// set force=true since this could indicate an imminent shutdown/nosedive
beep_on(d, true);
}
else {
// if we drop below riding speed stop buzzing
beep_off(d, false);
}
}
else {
// if the switch comes back on we stop buzzing
beep_off(d, false);
}
return sw_state;
}
// Fault checking order does not really matter. From a UX perspective, switch should be before angle.
static bool check_faults(data *d){
// Aggressive reverse stop in case the board runs off when upside down
if (d->is_upside_down) {
if (d->erpm > 1000) {
// erpms are also reversed when upside down!
if (((d->current_time - d->fault_switch_timer) * 1000 > 100) ||
(d->erpm > 2000) ||
((d->state == RUNNING_WHEELSLIP) && (d->current_time - d->delay_upside_down_fault > 1) &&
((d->current_time - d->fault_switch_timer) * 1000 > 30)) ) {
// Trigger FAULT_REVERSE when board is going reverse AND
// going > 2mph for more than 100ms
// going > 4mph
// detecting wheelslip (aka excorcist wiggle) after the first second
d->state = FAULT_REVERSE;
return true;
}
}
else {
d->fault_switch_timer = d->current_time;
if (d->erpm > 300) {
// erpms are also reversed when upside down!
if ((d->current_time - d->fault_angle_roll_timer) * 1000 > 500){
d->state = FAULT_REVERSE;
return true;
}
}
else {
d->fault_angle_roll_timer = d->current_time;
}
}
if (d->switch_state == ON) {
// allow turning it off by engaging foot sensors
d->state = FAULT_SWITCH_HALF;
return true;
}
}
else {
bool disable_switch_faults = d->float_conf.fault_moving_fault_disabled &&
d->erpm > (d->float_conf.fault_adc_half_erpm * 2) && // Rolling forward (not backwards!)
fabsf(d->roll_angle) < 40; // Not tipped over
// Check switch
// Switch fully open
if (d->switch_state == OFF) {
if (!disable_switch_faults) {
if((1000.0 * (d->current_time - d->fault_switch_timer)) > d->float_conf.fault_delay_switch_full){
d->state = FAULT_SWITCH_FULL;
return true;
}
// low speed (below 6 x half-fault threshold speed):
else if ((d->abs_erpm < d->float_conf.fault_adc_half_erpm * 6)
&& (1000.0 * (d->current_time - d->fault_switch_timer) > d->float_conf.fault_delay_switch_half)){
d->state = FAULT_SWITCH_FULL;
return true;
}
}
if ((d->abs_erpm < d->quickstop_erpm) && (fabsf(d->true_pitch_angle) > 14) && (fabsf(d->inputtilt_interpolated) < 30) && (SIGN(d->true_pitch_angle) == SIGN(d->erpm))) {
// QUICK STOP
d->state = FAULT_QUICKSTOP;
return true;
}
} else {
d->fault_switch_timer = d->current_time;
}
// Switch partially open and stopped
if(!d->float_conf.fault_is_dual_switch) {
if((d->switch_state == HALF || d->switch_state == OFF) && fabsf(d->erpm) < d->float_conf.fault_adc_half_erpm){
if ((1000.0 * (d->current_time - d->fault_switch_half_timer)) > d->float_conf.fault_delay_switch_half){
d->state = FAULT_SWITCH_HALF;
return true;
}
} else {
d->fault_switch_half_timer = d->current_time;
}
}
// Check roll angle
if (fabsf(d->roll_angle) > d->float_conf.fault_roll) {
if ((1000.0 * (d->current_time - d->fault_angle_roll_timer)) > d->float_conf.fault_delay_roll) {
d->state = FAULT_ANGLE_ROLL;
return true;
}
} else {
d->fault_angle_roll_timer = d->current_time;
if (d->float_conf.fault_darkride_enabled) {
if((fabsf(d->roll_angle) > 100) && (fabsf(d->roll_angle) < 135)) {
d->state = FAULT_ANGLE_ROLL;
return true;
}
}
}
}
// Check pitch angle
if ((fabsf(d->true_pitch_angle) > d->float_conf.fault_pitch) && (fabsf(d->inputtilt_interpolated) < 30)) {
if ((1000.0 * (d->current_time - d->fault_angle_pitch_timer)) > d->float_conf.fault_delay_pitch) {
d->state = FAULT_ANGLE_PITCH;
return true;
}
} else {
d->fault_angle_pitch_timer = d->current_time;
}
// *Removed Duty Cycle Fault*
return false;
}
static void calculate_setpoint_target(data *d) {
float input_voltage = VESC_IF->mc_get_input_voltage_filtered();
if (input_voltage < d->float_conf.tiltback_hv) {
d->tb_highvoltage_timer = d->current_time;
}
if (d->setpointAdjustmentType == CENTERING && d->setpoint_target_interpolated != d->setpoint_target) {
// Ignore tiltback during centering sequence
d->state = RUNNING;
} else if (d->state == RUNNING_WHEELSLIP) {
d->setpointAdjustmentType = TILTBACK_NONE;
} else if (d->abs_duty_cycle > d->float_conf.tiltback_duty) {
if (d->erpm > 0) {
d->setpoint_target = d->float_conf.tiltback_duty_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_duty_angle;
}
d->setpointAdjustmentType = TILTBACK_DUTY;
d->state = RUNNING_TILTBACK;
} else if (d->abs_duty_cycle > 0.05 && input_voltage > d->float_conf.tiltback_hv) {
beep_alert(d, 3, false); // Triple-beep
if (((d->current_time - d->tb_highvoltage_timer) > .5) ||
(input_voltage > d->float_conf.tiltback_hv + 1)) {
// 500ms have passed or voltage is another volt higher, time for some tiltback
if (d->erpm > 0){
d->setpoint_target = d->float_conf.tiltback_hv_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_hv_angle;
}
d->setpointAdjustmentType = TILTBACK_HV;
d->state = RUNNING_TILTBACK;
}
else {
// The rider has 500ms to react to the triple-beep, or maybe it was just a short spike
d->setpointAdjustmentType = TILTBACK_NONE;
d->state = RUNNING;
}
} else if(VESC_IF->mc_temp_fet_filtered() > d->mc_max_temp_fet){
// Use the angle from Low-Voltage tiltback, but slower speed from High-Voltage tiltback
beep_alert(d, 3, true); // Triple-beep (long beeps)
if(VESC_IF->mc_temp_fet_filtered() > (d->mc_max_temp_fet + 1)) {
if(d->erpm > 0){
d->setpoint_target = d->float_conf.tiltback_lv_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_lv_angle;
}
d->setpointAdjustmentType = TILTBACK_TEMP;
d->state = RUNNING_TILTBACK;
}
else {
// The rider has 1 degree Celsius left before we start tilting back
d->setpointAdjustmentType = TILTBACK_NONE;
d->state = RUNNING;
}
} else if(VESC_IF->mc_temp_motor_filtered() > d->mc_max_temp_mot){
// Use the angle from Low-Voltage tiltback, but slower speed from High-Voltage tiltback
beep_alert(d, 3, true); // Triple-beep (long beeps)
if(VESC_IF->mc_temp_motor_filtered() > (d->mc_max_temp_mot + 1)) {
if(d->erpm > 0){
d->setpoint_target = d->float_conf.tiltback_lv_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_lv_angle;
}
d->setpointAdjustmentType = TILTBACK_TEMP;
d->state = RUNNING_TILTBACK;
}
else {
// The rider has 1 degree Celsius left before we start tilting back
d->setpointAdjustmentType = TILTBACK_NONE;
d->state = RUNNING;
}
} else if (d->abs_duty_cycle > 0.05 && input_voltage < d->float_conf.tiltback_lv) {
beep_alert(d, 3, false); // Triple-beep
float abs_motor_current = fabsf(d->motor_current);
float vdelta = d->float_conf.tiltback_lv - input_voltage;
float ratio = vdelta * 20 / abs_motor_current;
// When to do LV tiltback:
// a) we're 2V below lv threshold
// b) motor current is small (we cannot assume vsag)
// c) we have more than 20A per Volt of difference (we tolerate some amount of vsag)
if ((vdelta > 2) || (abs_motor_current < 5) || (ratio > 1)) {
if (d->erpm > 0) {
d->setpoint_target = d->float_conf.tiltback_lv_angle;
} else {
d->setpoint_target = -d->float_conf.tiltback_lv_angle;
}
d->setpointAdjustmentType = TILTBACK_LV;
d->state = RUNNING_TILTBACK;
}
else {
d->setpointAdjustmentType = TILTBACK_NONE;
d->setpoint_target = 0;
d->state = RUNNING;
}
} else {
// Normal running
if (d->float_conf.fault_reversestop_enabled && (d->erpm < -200) && !d->is_upside_down) {
d->setpointAdjustmentType = REVERSESTOP;
d->reverse_timer = d->current_time;
d->reverse_total_erpm = 0;
}
else {
d->setpointAdjustmentType = TILTBACK_NONE;
}
d->setpoint_target = 0;
d->state = RUNNING;
}
if ((d->state == RUNNING_WHEELSLIP) && (d->abs_duty_cycle > d->max_duty_with_margin)) {
d->setpoint_target = 0;
}
if (d->is_upside_down && (d->state == RUNNING)) {
d->state = RUNNING_UPSIDEDOWN;
if (!d->is_upside_down_started) {
// right after flipping when first engaging dark ride we add a 1 second grace period
// before aggressively checking for board wiggle (based on acceleration)
d->is_upside_down_started = true;
d->delay_upside_down_fault = d->current_time;
}
}
if (d->setpointAdjustmentType == TILTBACK_DUTY) {
if (d->float_conf.is_dutybuzz_enabled || (d->float_conf.tiltback_duty_angle == 0)) {
beep_on(d, true);
d->duty_beeping = true;
}
}
else {
if (d->duty_beeping) {
beep_off(d, false);
}
}
}
static void calculate_setpoint_interpolated(data *d) {
if (d->setpoint_target_interpolated != d->setpoint_target) {
// If we are less than one step size away, go all the way
if (fabsf(d->setpoint_target - d->setpoint_target_interpolated) < get_setpoint_adjustment_step_size(d)) {
d->setpoint_target_interpolated = d->setpoint_target;
} else if (d->setpoint_target - d->setpoint_target_interpolated > 0) {
d->setpoint_target_interpolated += get_setpoint_adjustment_step_size(d);
} else {
d->setpoint_target_interpolated -= get_setpoint_adjustment_step_size(d);
}
}
}
static void apply_noseangling(data *d){
if (d->state != RUNNING_WHEELSLIP) {
// Nose angle adjustment, add variable then constant tiltback
float noseangling_target = 0;
if (fabsf(d->erpm) > d->float_conf.tiltback_constant_erpm) {
noseangling_target += d->float_conf.tiltback_constant * SIGN(d->erpm);
}
if (fabsf(noseangling_target - d->noseangling_interpolated) < d->noseangling_step_size) {
d->noseangling_interpolated = noseangling_target;
} else if (noseangling_target - d->noseangling_interpolated > 0) {
d->noseangling_interpolated += d->noseangling_step_size;
} else {
d->noseangling_interpolated -= d->noseangling_step_size;
}
}
d->setpoint += d->noseangling_interpolated;
}
static void apply_inputtilt(data *d){ // Input Tiltback
float input_tiltback_target;
// Scale by Max Angle
input_tiltback_target = d->throttle_val * d->float_conf.inputtilt_angle_limit;
//Sticky Tilt Input Start
int truncanglelimit = d->float_conf.inputtilt_angle_limit; // Cast the angle limit to remove the decimals
if (d->float_conf.inputtilt_angle_limit - truncanglelimit > 0) { // If we have any decimals allow sticky tilt
float stickytilt_val1 = 3.0; // Value that defines where tilt will stick for both nose up and down. Can be made UI input later.
float stickytilt_val2 = 6.0; // Value of 0 or above max disables. Max value <= d->float_conf.inputtilt_angle_limit.
//Val1 is the default value and value 2 is the step up value that engages when you flick the throttle toward sitcky tilt direction while engaged at val1 (but not to max)
float stickytilt_maxcurrent = 20; //Current limit that prevents changing from val2 to val1 for safety
// Monitor the throttle to start sticky tilt
if ((fabsf(d->throttle_val) - fabsf(d->last_throttle_val) > .001) || // If the throttle is travelling away from center
(fabsf(d->throttle_val) > 0.95)) { // Or close to max
d->stickytilt_maxval = SIGN(d->throttle_val) * fmaxf(fabsf(d->throttle_val), fabsf(d->stickytilt_maxval)); // Monitor the maximum throttle value
}
// Check for conditions to start stop and swap sticky tilt
if ((d->throttle_val == 0) && // The throttle is at the center
(fabsf(d->stickytilt_maxval) > 0.01)) { // And a throttle action just happened
if ((!d->stickytiltoff) && // Don't apply sticky tilt if we just left sticky tilt
(fabsf(d->stickytilt_maxval) < .95)) { //Check that we have not pushed beyond this limit
if (d->stickytilton) {//if sticky tilt is activated, switch values
if (((fabsf(d->atr_filtered_current) < stickytilt_maxcurrent) &&
(fabsf(d->stickytilt_val) == stickytilt_val2)) || //If we are val2 we must be below max current to change
(fabsf(d->stickytilt_val) == stickytilt_val1)) { //If we are at val1 the current restriction is not required
d->stickytilt_val = SIGN(d->stickytilt_maxval) * ((fabsf(d->stickytilt_val) == stickytilt_val1) ? stickytilt_val2 : stickytilt_val1); //switch sticky tilt values from 1 to 2
}
} else { //else apply sticky tilt value 1
d->stickytilton = true;
d->stickytilt_val = SIGN(d->stickytilt_maxval) * stickytilt_val1; // Apply val 1 for initial sticky tilt application
}
}
d->stickytiltoff = false; // We can turn off this flag after 1 cycle of throttle ==0. Avoids getting stuck on sticky tilt when turning sticky tilt off
d->stickytilt_maxval = 0; // Reset
}
if (d->stickytilton) { //Apply sticky tilt. Check for exit condition
//Apply sticky tilt value or throttle values higher than sticky tilt value
if ((SIGN(d->inputtilt_interpolated) == SIGN(input_tiltback_target)) || (d->throttle_val == 0)) { // If the throttle is at zero or pushed to the direction of the sticky tilt value.
if (fabsf(d->stickytilt_val) >= fabsf(input_tiltback_target)) { // If sticky tilt value greater than throttle value keep at sticky value
input_tiltback_target = d->stickytilt_val; // apply our sticky tilt value
} //else we will apply the normal throttle value calculated at the beginning of apply_inputtilt() in the direction of sticky tilt
} else { //else we will apply the normal throttle value calculated at the beginning of apply_inputtilt() in the opposite direction of sticky tilt and exit sticky tilt
d->stickytiltoff = true;
d->stickytilton = false;
}
}
d->last_throttle_val = d->throttle_val;
}
//Sticky Tilt Input End
float input_tiltback_target_diff = input_tiltback_target - d->inputtilt_interpolated;
if (d->float_conf.inputtilt_smoothing_factor > 0) { // Smoothen changes in tilt angle by ramping the step size
float smoothing_factor = 0.02;
for (int i = 1; i < d->float_conf.inputtilt_smoothing_factor; i++) {
smoothing_factor /= 2;
}
float smooth_center_window = 1.5 + (0.5 * d->float_conf.inputtilt_smoothing_factor); // Sets the angle away from Target that step size begins ramping down
if (fabsf(input_tiltback_target_diff) < smooth_center_window) { // Within X degrees of Target Angle, start ramping down step size
d->inputtilt_ramped_step_size = (smoothing_factor * d->inputtilt_step_size * (input_tiltback_target_diff / 2)) + ((1 - smoothing_factor) * d->inputtilt_ramped_step_size); // Target step size is reduced the closer to center you are (needed for smoothly transitioning away from center)
float centering_step_size = fminf(fabsf(d->inputtilt_ramped_step_size), fabsf(input_tiltback_target_diff / 2) * d->inputtilt_step_size) * SIGN(input_tiltback_target_diff); // Linearly ramped down step size is provided as minimum to prevent overshoot
if (fabsf(input_tiltback_target_diff) < fabsf(centering_step_size)) {
d->inputtilt_interpolated = input_tiltback_target;
} else {
d->inputtilt_interpolated += centering_step_size;
}
} else { // Ramp up step size until the configured tilt speed is reached
d->inputtilt_ramped_step_size = (smoothing_factor * d->inputtilt_step_size * SIGN(input_tiltback_target_diff)) + ((1 - smoothing_factor) * d->inputtilt_ramped_step_size);
d->inputtilt_interpolated += d->inputtilt_ramped_step_size;
}
} else { // Constant step size; no smoothing
if (fabsf(input_tiltback_target_diff) < d->inputtilt_step_size){
d->inputtilt_interpolated = input_tiltback_target;
} else {
d->inputtilt_interpolated += d->inputtilt_step_size * SIGN(input_tiltback_target_diff);
}
}
d->setpoint += d->inputtilt_interpolated;
}
static void brake(data *d) {
// Brake timeout logic
float brake_timeout_length = 1; // Brake Timeout hard-coded to 1s
if ((d->abs_erpm > 1 || d->brake_timeout == 0)) {
d->brake_timeout = d->current_time + brake_timeout_length;
}
if (d->brake_timeout != 0 && d->current_time > d->brake_timeout) {
return;
}
// Reset the timeout
VESC_IF->timeout_reset();
// Set current
VESC_IF->mc_set_brake_current(d->float_conf.brake_current);
}
static void set_current(data *d, float current){
// Limit current output to configured max output
if (current > 0 && current > VESC_IF->get_cfg_float(CFG_PARAM_l_current_max)) {
current = VESC_IF->get_cfg_float(CFG_PARAM_l_current_max);
} else if(current < 0 && current < VESC_IF->get_cfg_float(CFG_PARAM_l_current_min)) {
current = VESC_IF->get_cfg_float(CFG_PARAM_l_current_min);
}
// Reset the timeout
VESC_IF->timeout_reset();
// Set the current delay
VESC_IF->mc_set_current_off_delay(d->motor_timeout_seconds);
// Set Current
VESC_IF->mc_set_current(current);
}
static void set_dutycycle(data *d, float dutycycle){
// Limit duty output to configured max output
if (dutycycle > VESC_IF->get_cfg_float(CFG_PARAM_l_max_duty)) {
dutycycle = VESC_IF->get_cfg_float(CFG_PARAM_l_max_duty);
} else if(dutycycle < 0 && dutycycle < (-1) * VESC_IF->get_cfg_float(CFG_PARAM_l_max_duty)) {
dutycycle = (-1) * VESC_IF->get_cfg_float(CFG_PARAM_l_max_duty);
}
// Reset the timeout
VESC_IF->timeout_reset();
// Set the current delay
VESC_IF->mc_set_current_off_delay(d->motor_timeout_seconds);
// Set Duty
//VESC_IF->mc_set_duty(dutycycle);
VESC_IF->mc_set_duty_noramp(dutycycle);
}
static void check_surge(data *d, float new_pid_value){
//Start Surge Code
d->surge_period = 0.75; //.75 //Period between each surge, in seconds. Prevents runaway and instability.
d->surge_cycle = 0.30; //.30 //Length of surge, in seconds
d->surge_startanglespeed = 50; //fmaxf(10, (float)d->float_conf.turntilt_start_erpm/10); //Nose speed that can initiate a surge
d->surge_difflimit = 3; //fmaxf(0.1, d->float_conf.turntilt_speed); //Pitch required at start angle speed to initiate surge
d->surge_currentmargin = 1.3; //fmaxf(0.5, (float)d->float_conf.turntilt_erpm_boost/100); //Higher current margin ends surge later
//Counter for high nose angle speed
if (d->differential * SIGN(d->erpm) > d->surge_startanglespeed / d->float_conf.hertz){
d->surge_diffcount += d->differential * SIGN(d->erpm); // Add until diff limit.
} else if (d->differential * SIGN(d->erpm) < 0) { //Pitch is travelling back to center
d->surge_diffcount = 0; // reset
}
//Initialize Surge Cycle
if ((d->surge_diffcount >= d->surge_difflimit) && //Nose dip condition
(SIGN(d->erpm) * d->proportional - d->surge_difflimit > 0) && //Minimum angle for acceleration
(!d->braking) && //Not braking