forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc_interface.c
2358 lines (1965 loc) · 62.5 KB
/
mc_interface.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
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 2016 - 2020 Benjamin Vedder [email protected]
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 "mc_interface.h"
#include "mcpwm.h"
#include "mcpwm_foc.h"
#include "ledpwm.h"
#include "stm32f4xx_conf.h"
#include "hw.h"
#include "terminal.h"
#include "utils.h"
#include "ch.h"
#include "hal.h"
#include "commands.h"
#include "encoder.h"
#include "drv8301.h"
#include "drv8320s.h"
#include "drv8323s.h"
#include "buffer.h"
#include "gpdrive.h"
#include "comm_can.h"
#include "shutdown.h"
#include "app.h"
#include "utils.h"
#include "mempools.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
// Macros
#define DIR_MULT (motor_now()->m_conf.m_invert_direction ? -1.0 : 1.0)
// Global variables
volatile uint16_t ADC_Value[HW_ADC_CHANNELS + HW_ADC_CHANNELS_EXTRA];
volatile int ADC_curr_norm_value[6];
typedef struct {
volatile mc_configuration m_conf;
mc_fault_code m_fault_now;
int m_ignore_iterations;
unsigned int m_cycles_running;
bool m_lock_enabled;
bool m_lock_override_once;
float m_motor_current_sum;
float m_input_current_sum;
float m_motor_current_iterations;
float m_input_current_iterations;
float m_motor_id_sum;
float m_motor_iq_sum;
float m_motor_id_iterations;
float m_motor_iq_iterations;
float m_motor_vd_sum;
float m_motor_vq_sum;
float m_motor_vd_iterations;
float m_motor_vq_iterations;
float m_amp_seconds;
float m_amp_seconds_charged;
float m_watt_seconds;
float m_watt_seconds_charged;
float m_position_set;
float m_temp_fet;
float m_temp_motor;
float m_gate_driver_voltage;
float m_motor_current_unbalance;
float m_motor_current_unbalance_error_rate;
float m_f_samp_now;
} motor_if_state_t;
// Private variables
static volatile motor_if_state_t m_motor_1;
#ifdef HW_HAS_DUAL_MOTORS
static volatile motor_if_state_t m_motor_2;
#endif
// Sampling variables
#define ADC_SAMPLE_MAX_LEN 2000
__attribute__((section(".ram4"))) static volatile int16_t m_curr0_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_curr1_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph1_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph2_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph3_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_vzero_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile uint8_t m_status_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_curr_fir_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_f_sw_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int8_t m_phase_samples[ADC_SAMPLE_MAX_LEN];
static volatile int m_sample_len;
static volatile int m_sample_int;
static volatile debug_sampling_mode m_sample_mode;
static volatile debug_sampling_mode m_sample_mode_last;
static volatile int m_sample_now;
static volatile int m_sample_trigger;
static volatile float m_last_adc_duration_sample;
static volatile bool m_sample_is_second_motor;
static volatile mc_fault_code m_fault_stop_fault;
static volatile bool m_fault_stop_is_second_motor;
// Private functions
static void update_override_limits(volatile motor_if_state_t *motor, volatile mc_configuration *conf);
static void run_timer_tasks(volatile motor_if_state_t *motor);
static volatile motor_if_state_t *motor_now(void);
// Function pointers
static void(*pwn_done_func)(void) = 0;
// Threads
static THD_WORKING_AREA(timer_thread_wa, 1024);
static THD_FUNCTION(timer_thread, arg);
static THD_WORKING_AREA(sample_send_thread_wa, 512);
static THD_FUNCTION(sample_send_thread, arg);
static thread_t *sample_send_tp;
static THD_WORKING_AREA(fault_stop_thread_wa, 512);
static THD_FUNCTION(fault_stop_thread, arg);
static thread_t *fault_stop_tp;
void mc_interface_init(void) {
memset((void*)&m_motor_1, 0, sizeof(motor_if_state_t));
#ifdef HW_HAS_DUAL_MOTORS
memset((void*)&m_motor_2, 0, sizeof(motor_if_state_t));
#endif
conf_general_read_mc_configuration((mc_configuration*)&m_motor_1.m_conf, false);
#ifdef HW_HAS_DUAL_MOTORS
conf_general_read_mc_configuration((mc_configuration*)&m_motor_2.m_conf, true);
#endif
#ifdef HW_HAS_DUAL_MOTORS
m_motor_1.m_conf.motor_type = MOTOR_TYPE_FOC;
m_motor_2.m_conf.motor_type = MOTOR_TYPE_FOC;
#endif
m_last_adc_duration_sample = 0.0;
m_sample_len = 1000;
m_sample_int = 1;
m_sample_now = 0;
m_sample_trigger = 0;
m_sample_mode = DEBUG_SAMPLING_OFF;
m_sample_mode_last = DEBUG_SAMPLING_OFF;
m_sample_is_second_motor = false;
// Start threads
chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa), NORMALPRIO, timer_thread, NULL);
chThdCreateStatic(sample_send_thread_wa, sizeof(sample_send_thread_wa), NORMALPRIO - 1, sample_send_thread, NULL);
chThdCreateStatic(fault_stop_thread_wa, sizeof(fault_stop_thread_wa), HIGHPRIO - 3, fault_stop_thread, NULL);
int motor_old = mc_interface_get_motor_thread();
mc_interface_select_motor_thread(1);
#ifdef HW_HAS_DRV8301
drv8301_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8301_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8320S)
drv8320s_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8320s_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8323S)
drv8323s_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8323s_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
DRV8323S_CUSTOM_SETTINGS();
#endif
#if defined HW_HAS_DUAL_MOTORS || defined HW_HAS_DUAL_PARALLEL
mc_interface_select_motor_thread(2);
#ifdef HW_HAS_DRV8301
drv8301_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8301_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8320S)
drv8320s_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8320s_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8323S)
drv8323s_set_oc_mode(motor_now()->m_conf.m_drv8301_oc_mode);
drv8323s_set_oc_adj(motor_now()->m_conf.m_drv8301_oc_adj);
DRV8323S_CUSTOM_SETTINGS();
#endif
#endif
mc_interface_select_motor_thread(motor_old);
// Initialize encoder
#if !WS2811_ENABLE
switch (motor_now()->m_conf.m_sensor_port_mode) {
case SENSOR_PORT_MODE_ABI:
encoder_init_abi(motor_now()->m_conf.m_encoder_counts);
break;
case SENSOR_PORT_MODE_AS5047_SPI:
encoder_init_as5047p_spi();
break;
case SENSOR_PORT_MODE_AD2S1205:
encoder_init_ad2s1205_spi();
break;
case SENSOR_PORT_MODE_SINCOS:
encoder_init_sincos(motor_now()->m_conf.foc_encoder_sin_gain, motor_now()->m_conf.foc_encoder_sin_offset,
motor_now()->m_conf.foc_encoder_cos_gain, motor_now()->m_conf.foc_encoder_cos_offset,
motor_now()->m_conf.foc_encoder_sincos_filter_constant);
break;
case SENSOR_PORT_MODE_TS5700N8501:
case SENSOR_PORT_MODE_TS5700N8501_MULTITURN: {
app_configuration *appconf = mempools_alloc_appconf();
conf_general_read_app_configuration(appconf);
if (appconf->app_to_use == APP_ADC ||
appconf->app_to_use == APP_UART ||
appconf->app_to_use == APP_PPM_UART ||
appconf->app_to_use == APP_ADC_UART) {
appconf->app_to_use = APP_NONE;
conf_general_store_app_configuration(appconf);
}
mempools_free_appconf(appconf);
encoder_init_ts5700n8501();
} break;
default:
break;
}
#endif
// Initialize selected implementation
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_init(&motor_now()->m_conf);
break;
case MOTOR_TYPE_FOC:
#ifdef HW_HAS_DUAL_MOTORS
mcpwm_foc_init(&m_motor_1.m_conf, &m_motor_2.m_conf);
#else
mcpwm_foc_init(&m_motor_1.m_conf, &m_motor_1.m_conf);
#endif
break;
case MOTOR_TYPE_GPD:
gpdrive_init(&motor_now()->m_conf);
break;
default:
break;
}
}
int mc_interface_motor_now(void) {
#if defined HW_HAS_DUAL_MOTORS || defined HW_HAS_DUAL_PARALLEL
int isr_motor = mcpwm_foc_isr_motor();
int thd_motor = chThdGetSelfX()->motor_selected;
if (isr_motor > 0) {
return isr_motor;
} else if (thd_motor > 0) {
return thd_motor;
} else {
return 1;
}
#else
return 1;
#endif
}
/**
* Select motor for current thread. When a thread has a motor selected,
* the mc_interface functions will use that motor for that thread. This
* is only relevant for dual motor hardware.
*
* @param motor
* 0: no specific motor selected, the last motor will be used.
* 1: motor 1 selected (default).
* 2: motor 2 selected.
*/
void mc_interface_select_motor_thread(int motor) {
#if defined HW_HAS_DUAL_MOTORS || defined HW_HAS_DUAL_PARALLEL
if (motor == 0 || motor == 1 || motor == 2) {
chThdGetSelfX()->motor_selected = motor;
}
#else
(void)motor;
#endif
}
/**
* Get the motor selected for the current thread.
*
* @return
* 0: no specific motor selected, the last motor will be used.
* 1: motor 1 selected (default).
* 2: motor 2 selected.
*/
int mc_interface_get_motor_thread(void) {
return chThdGetSelfX()->motor_selected;
}
const volatile mc_configuration* mc_interface_get_configuration(void) {
return &motor_now()->m_conf;
}
void mc_interface_set_configuration(mc_configuration *configuration) {
volatile motor_if_state_t *motor = motor_now();
#if defined HW_HAS_DUAL_MOTORS || defined HW_HAS_DUAL_PARALLEL
configuration->motor_type = MOTOR_TYPE_FOC;
#endif
#if !WS2811_ENABLE
if (motor->m_conf.m_sensor_port_mode != configuration->m_sensor_port_mode) {
encoder_deinit();
switch (configuration->m_sensor_port_mode) {
case SENSOR_PORT_MODE_ABI:
encoder_init_abi(configuration->m_encoder_counts);
break;
case SENSOR_PORT_MODE_AS5047_SPI:
encoder_init_as5047p_spi();
break;
case SENSOR_PORT_MODE_AD2S1205:
encoder_init_ad2s1205_spi();
break;
case SENSOR_PORT_MODE_SINCOS:
encoder_init_sincos(motor->m_conf.foc_encoder_sin_gain, motor->m_conf.foc_encoder_sin_offset,
motor->m_conf.foc_encoder_cos_gain, motor->m_conf.foc_encoder_cos_offset,
motor->m_conf.foc_encoder_sincos_filter_constant);
break;
case SENSOR_PORT_MODE_TS5700N8501:
case SENSOR_PORT_MODE_TS5700N8501_MULTITURN: {
app_configuration *appconf = mempools_alloc_appconf();
*appconf = *app_get_configuration();
if (appconf->app_to_use == APP_ADC ||
appconf->app_to_use == APP_UART ||
appconf->app_to_use == APP_PPM_UART ||
appconf->app_to_use == APP_ADC_UART) {
appconf->app_to_use = APP_NONE;
conf_general_store_app_configuration(appconf);
app_set_configuration(appconf);
}
mempools_free_appconf(appconf);
encoder_init_ts5700n8501();
} break;
default:
break;
}
}
if (configuration->m_sensor_port_mode == SENSOR_PORT_MODE_ABI) {
encoder_set_counts(configuration->m_encoder_counts);
}
#endif
#ifdef HW_HAS_DRV8301
drv8301_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8301_set_oc_adj(configuration->m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8320S)
drv8320s_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8320s_set_oc_adj(configuration->m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8323S)
drv8323s_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8323s_set_oc_adj(configuration->m_drv8301_oc_adj);
#endif
#ifdef HW_HAS_DUAL_PARALLEL
mc_interface_select_motor_thread(2);
#ifdef HW_HAS_DRV8301
drv8301_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8301_set_oc_adj(configuration->m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8320S)
drv8320s_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8320s_set_oc_adj(configuration->m_drv8301_oc_adj);
#elif defined(HW_HAS_DRV8323S)
drv8323s_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8323s_set_oc_adj(configuration->m_drv8301_oc_adj);
#endif
mc_interface_select_motor_thread(1);
#endif
if (motor->m_conf.motor_type != configuration->motor_type) {
mcpwm_deinit();
mcpwm_foc_deinit();
gpdrive_deinit();
motor->m_conf = *configuration;
switch (motor->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_init(&motor->m_conf);
break;
case MOTOR_TYPE_FOC:
#ifdef HW_HAS_DUAL_MOTORS
mcpwm_foc_init(&m_motor_1.m_conf, &m_motor_2.m_conf);
#else
mcpwm_foc_init(&m_motor_1.m_conf, &m_motor_1.m_conf);
#endif
break;
case MOTOR_TYPE_GPD:
gpdrive_init(&motor->m_conf);
break;
default:
break;
}
} else {
motor->m_conf = *configuration;
}
update_override_limits(motor, &motor->m_conf);
switch (motor->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_configuration(&motor->m_conf);
break;
case MOTOR_TYPE_FOC:
#ifdef HW_HAS_DUAL_MOTORS
if (motor == &m_motor_1) {
m_motor_2.m_conf.foc_f_sw = motor->m_conf.foc_f_sw;
} else {
m_motor_1.m_conf.foc_f_sw = motor->m_conf.foc_f_sw;
}
#endif
mcpwm_foc_set_configuration(&motor->m_conf);
break;
case MOTOR_TYPE_GPD:
gpdrive_set_configuration(&motor->m_conf);
break;
default:
break;
}
}
bool mc_interface_dccal_done(void) {
bool ret = false;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_is_dccal_done();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_is_dccal_done();
break;
case MOTOR_TYPE_GPD:
ret = gpdrive_is_dccal_done();
break;
default:
break;
}
return ret;
}
/**
* Set a function that should be called after each PWM cycle.
*
* Note: this function is called from an interrupt.
*
* @param p_func
* The function to be called. 0 will not call any function.
*/
void mc_interface_set_pwm_callback(void (*p_func)(void)) {
pwn_done_func = p_func;
}
/**
* Lock the control by disabling all control commands.
*/
void mc_interface_lock(void) {
motor_now()->m_lock_enabled = true;
}
/**
* Unlock all control commands.
*/
void mc_interface_unlock(void) {
motor_now()->m_lock_enabled = false;
}
/**
* Allow just one motor control command in the locked state.
*/
void mc_interface_lock_override_once(void) {
motor_now()->m_lock_override_once = true;
}
mc_fault_code mc_interface_get_fault(void) {
return motor_now()->m_fault_now;
}
const char* mc_interface_fault_to_string(mc_fault_code fault) {
switch (fault) {
case FAULT_CODE_NONE: return "FAULT_CODE_NONE"; break;
case FAULT_CODE_OVER_VOLTAGE: return "FAULT_CODE_OVER_VOLTAGE"; break;
case FAULT_CODE_UNDER_VOLTAGE: return "FAULT_CODE_UNDER_VOLTAGE"; break;
case FAULT_CODE_DRV: return "FAULT_CODE_DRV"; break;
case FAULT_CODE_ABS_OVER_CURRENT: return "FAULT_CODE_ABS_OVER_CURRENT"; break;
case FAULT_CODE_OVER_TEMP_FET: return "FAULT_CODE_OVER_TEMP_FET"; break;
case FAULT_CODE_OVER_TEMP_MOTOR: return "FAULT_CODE_OVER_TEMP_MOTOR"; break;
case FAULT_CODE_GATE_DRIVER_OVER_VOLTAGE: return "FAULT_CODE_GATE_DRIVER_OVER_VOLTAGE"; break;
case FAULT_CODE_GATE_DRIVER_UNDER_VOLTAGE: return "FAULT_CODE_GATE_DRIVER_UNDER_VOLTAGE"; break;
case FAULT_CODE_MCU_UNDER_VOLTAGE: return "FAULT_CODE_MCU_UNDER_VOLTAGE"; break;
case FAULT_CODE_BOOTING_FROM_WATCHDOG_RESET: return "FAULT_CODE_BOOTING_FROM_WATCHDOG_RESET"; break;
case FAULT_CODE_ENCODER_SPI: return "FAULT_CODE_ENCODER_SPI"; break;
case FAULT_CODE_ENCODER_SINCOS_BELOW_MIN_AMPLITUDE: return "FAULT_CODE_ENCODER_SINCOS_BELOW_MIN_AMPLITUDE"; break;
case FAULT_CODE_ENCODER_SINCOS_ABOVE_MAX_AMPLITUDE: return "FAULT_CODE_ENCODER_SINCOS_ABOVE_MAX_AMPLITUDE"; break;
case FAULT_CODE_FLASH_CORRUPTION: return "FAULT_CODE_FLASH_CORRUPTION";
case FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_1: return "FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_1";
case FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_2: return "FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_2";
case FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_3: return "FAULT_CODE_HIGH_OFFSET_CURRENT_SENSOR_3";
case FAULT_CODE_UNBALANCED_CURRENTS: return "FAULT_CODE_UNBALANCED_CURRENTS";
case FAULT_CODE_BRK: return "FAULT_CODE_BRK";
default: return "FAULT_UNKNOWN"; break;
}
}
mc_state mc_interface_get_state(void) {
mc_state ret = MC_STATE_OFF;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_state();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_state();
break;
default:
break;
}
return ret;
}
void mc_interface_set_duty(float dutyCycle) {
if (fabsf(dutyCycle) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_duty(DIR_MULT * dutyCycle);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_duty(DIR_MULT * dutyCycle);
break;
default:
break;
}
}
void mc_interface_set_duty_noramp(float dutyCycle) {
if (fabsf(dutyCycle) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_duty_noramp(DIR_MULT * dutyCycle);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_duty_noramp(DIR_MULT * dutyCycle);
break;
default:
break;
}
}
void mc_interface_set_pid_speed(float rpm) {
if (fabsf(rpm) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_pid_speed(DIR_MULT * rpm);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_pid_speed(DIR_MULT * rpm);
break;
default:
break;
}
}
void mc_interface_set_pid_pos(float pos) {
SHUTDOWN_RESET();
if (mc_interface_try_input()) {
return;
}
motor_now()->m_position_set = pos;
pos *= DIR_MULT;
utils_norm_angle(&pos);
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_pid_pos(pos);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_pid_pos(pos);
break;
default:
break;
}
}
void mc_interface_set_current(float current) {
if (fabsf(current) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_current(DIR_MULT * current);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_current(DIR_MULT * current);
break;
default:
break;
}
}
void mc_interface_set_brake_current(float current) {
if (fabsf(current) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_brake_current(DIR_MULT * current);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_brake_current(DIR_MULT * current);
break;
case MOTOR_TYPE_GPD:
// For timeout to stop the output
gpdrive_set_mode(GPD_OUTPUT_MODE_NONE);
break;
default:
break;
}
}
/**
* Set current relative to the minimum and maximum current limits.
*
* @param current
* The relative current value, range [-1.0 1.0]
*/
void mc_interface_set_current_rel(float val) {
if (fabsf(val) > 0.001) {
SHUTDOWN_RESET();
}
mc_interface_set_current(val * motor_now()->m_conf.lo_current_motor_max_now);
}
/**
* Set brake current relative to the minimum current limit.
*
* @param current
* The relative current value, range [0.0 1.0]
*/
void mc_interface_set_brake_current_rel(float val) {
if (fabsf(val) > 0.001) {
SHUTDOWN_RESET();
}
mc_interface_set_brake_current(val * fabsf(motor_now()->m_conf.lo_current_motor_min_now));
}
/**
* Set open loop current vector to brake motor.
*
* @param current
* The current value.
*/
void mc_interface_set_handbrake(float current) {
if (fabsf(current) > 0.001) {
SHUTDOWN_RESET();
}
if (mc_interface_try_input()) {
return;
}
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
// TODO: Not implemented yet, use brake mode for now.
mcpwm_set_brake_current(current);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_handbrake(current);
break;
default:
break;
}
}
/**
* Set handbrake brake current relative to the minimum current limit.
*
* @param current
* The relative current value, range [0.0 1.0]
*/
void mc_interface_set_handbrake_rel(float val) {
if (fabsf(val) > 0.001) {
SHUTDOWN_RESET();
}
mc_interface_set_handbrake(val * fabsf(motor_now()->m_conf.lo_current_motor_min_now));
}
void mc_interface_brake_now(void) {
SHUTDOWN_RESET();
mc_interface_set_duty(0.0);
}
/**
* Disconnect the motor and let it turn freely.
*/
void mc_interface_release_motor(void) {
mc_interface_set_current(0.0);
}
/**
* Stop the motor and use braking.
*/
float mc_interface_get_duty_cycle_set(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_duty_cycle_set();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_duty_cycle_set();
break;
default:
break;
}
return DIR_MULT * ret;
}
float mc_interface_get_duty_cycle_now(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_duty_cycle_now();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_duty_cycle_now();
break;
default:
break;
}
return DIR_MULT * ret;
}
float mc_interface_get_sampling_frequency_now(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_switching_frequency_now();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_sampling_frequency_now();
break;
case MOTOR_TYPE_GPD:
ret = gpdrive_get_switching_frequency_now();
break;
default:
break;
}
return ret;
}
float mc_interface_get_rpm(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_rpm();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_rpm();
break;
default:
break;
}
return DIR_MULT * ret;
}
/**
* Get the amount of amp hours drawn from the input source.
*
* @param reset
* If true, the counter will be reset after this call.
*
* @return
* The amount of amp hours drawn.
*/
float mc_interface_get_amp_hours(bool reset) {
float val = motor_now()->m_amp_seconds / 3600;
if (reset) {
motor_now()->m_amp_seconds = 0.0;
}
return val;
}
/**
* Get the amount of amp hours fed back into the input source.
*
* @param reset
* If true, the counter will be reset after this call.
*
* @return
* The amount of amp hours fed back.
*/
float mc_interface_get_amp_hours_charged(bool reset) {
float val = motor_now()->m_amp_seconds_charged / 3600;
if (reset) {
motor_now()->m_amp_seconds_charged = 0.0;
}
return val;
}
/**
* Get the amount of watt hours drawn from the input source.
*
* @param reset
* If true, the counter will be reset after this call.
*
* @return
* The amount of watt hours drawn.
*/
float mc_interface_get_watt_hours(bool reset) {
float val = motor_now()->m_watt_seconds / 3600;
if (reset) {
motor_now()->m_watt_seconds = 0.0;
}
return val;
}
/**
* Get the amount of watt hours fed back into the input source.
*
* @param reset
* If true, the counter will be reset after this call.
*
* @return
* The amount of watt hours fed back.
*/
float mc_interface_get_watt_hours_charged(bool reset) {
float val = motor_now()->m_watt_seconds_charged / 3600;
if (reset) {
motor_now()->m_watt_seconds_charged = 0.0;
}
return val;
}
float mc_interface_get_tot_current(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current();
break;
default:
break;
}
return ret;
}
float mc_interface_get_tot_current_filtered(void) {
float ret = 0.0;
switch (motor_now()->m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current_filtered();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current_filtered();
break;
default:
break;
}
return ret;
}
float mc_interface_get_tot_current_directional(void) {
float ret = 0.0;