forked from aurimasniekis/fatboy-vesc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc_interface.c
1590 lines (1310 loc) · 37.2 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 - 2018 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"
#ifdef HW_HAS_DRV8301
#include "drv8301.h"
#endif
#ifdef HW_HAS_DRV8320
#include "drv8320.h"
#endif
#include "buffer.h"
#include <math.h>
// Macros
#define DIR_MULT (m_conf.m_invert_direction ? -1.0 : 1.0)
// Global variables
volatile uint16_t ADC_Value[HW_ADC_CHANNELS];
volatile int ADC_curr_norm_value[3];
// Private variables
static volatile mc_configuration m_conf;
static mc_fault_code m_fault_now;
static int m_ignore_iterations;
static volatile unsigned int m_cycles_running;
static volatile bool m_lock_enabled;
static volatile bool m_lock_override_once;
static volatile float m_motor_current_sum;
static volatile float m_input_current_sum;
static volatile float m_motor_current_iterations;
static volatile float m_input_current_iterations;
static volatile float m_motor_id_sum;
static volatile float m_motor_iq_sum;
static volatile float m_motor_id_iterations;
static volatile float m_motor_iq_iterations;
static volatile float m_amp_seconds;
static volatile float m_amp_seconds_charged;
static volatile float m_watt_seconds;
static volatile float m_watt_seconds_charged;
static volatile float m_position_set;
static volatile float m_temp_fet;
static volatile float m_temp_motor;
// 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;
// Private functions
static void update_override_limits(volatile mc_configuration *conf);
// 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, 1024);
static THD_FUNCTION(sample_send_thread, arg);
static thread_t *sample_send_tp;
void mc_interface_init(mc_configuration *configuration) {
m_conf = *configuration;
m_fault_now = FAULT_CODE_NONE;
m_ignore_iterations = 0;
m_cycles_running = 0;
m_lock_enabled = false;
m_lock_override_once = false;
m_motor_current_sum = 0.0;
m_input_current_sum = 0.0;
m_motor_current_iterations = 0.0;
m_input_current_iterations = 0.0;
m_motor_id_sum = 0.0;
m_motor_iq_sum = 0.0;
m_motor_id_iterations = 0.0;
m_motor_iq_iterations = 0.0;
m_amp_seconds = 0.0;
m_amp_seconds_charged = 0.0;
m_watt_seconds = 0.0;
m_watt_seconds_charged = 0.0;
m_position_set = 0.0;
m_last_adc_duration_sample = 0.0;
m_temp_fet = 0.0;
m_temp_motor = 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;
// 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);
#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_DRV8320)
drv8320_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8320_set_oc_adj(configuration->m_drv8301_oc_adj);
#endif
// Initialize encoder
#if !WS2811_ENABLE
switch (m_conf.m_sensor_port_mode) {
case SENSOR_PORT_MODE_ABI:
encoder_init_abi(m_conf.m_encoder_counts);
break;
case SENSOR_PORT_MODE_AS5047_SPI:
encoder_init_as5047p_spi();
break;
default:
break;
}
#endif
// Initialize selected implementation
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_init(&m_conf);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_init(&m_conf);
break;
default:
break;
}
}
const volatile mc_configuration* mc_interface_get_configuration(void) {
return &m_conf;
}
void mc_interface_set_configuration(mc_configuration *configuration) {
#if !WS2811_ENABLE
if (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;
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_DRV8320)
drv8320_set_oc_mode(configuration->m_drv8301_oc_mode);
drv8320_set_oc_adj(configuration->m_drv8301_oc_adj);
#endif
if (m_conf.motor_type == MOTOR_TYPE_FOC
&& configuration->motor_type != MOTOR_TYPE_FOC) {
mcpwm_foc_deinit();
m_conf = *configuration;
mcpwm_init(&m_conf);
} else if (m_conf.motor_type != MOTOR_TYPE_FOC
&& configuration->motor_type == MOTOR_TYPE_FOC) {
mcpwm_deinit();
m_conf = *configuration;
mcpwm_foc_init(&m_conf);
} else {
m_conf = *configuration;
}
update_override_limits(&m_conf);
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_configuration(&m_conf);
break;
case MOTOR_TYPE_FOC:
mcpwm_foc_set_configuration(&m_conf);
break;
default:
break;
}
}
bool mc_interface_dccal_done(void) {
bool ret = false;
switch (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;
default:
break;
}
return ret;
}
/**
* Set a function that should be called after each PWM cycle.
*
* @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) {
m_lock_enabled = true;
}
/**
* Unlock all control commands.
*/
void mc_interface_unlock(void) {
m_lock_enabled = false;
}
/**
* Allow just one motor control command in the locked state.
*/
void mc_interface_lock_override_once(void) {
m_lock_override_once = true;
}
mc_fault_code mc_interface_get_fault(void) {
return 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;
default: return "FAULT_UNKNOWN"; break;
}
}
mc_state mc_interface_get_state(void) {
mc_state ret = MC_STATE_OFF;
switch (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 (mc_interface_try_input()) {
return;
}
switch (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 (mc_interface_try_input()) {
return;
}
switch (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 (mc_interface_try_input()) {
return;
}
switch (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) {
if (mc_interface_try_input()) {
return;
}
m_position_set = pos;
pos *= DIR_MULT;
utils_norm_angle(&pos);
switch (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 (mc_interface_try_input()) {
return;
}
switch (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 (mc_interface_try_input()) {
return;
}
switch (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;
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 (val > 0.0) {
mc_interface_set_current(val * m_conf.lo_current_motor_max_now);
} else {
mc_interface_set_current(val * fabsf(m_conf.lo_current_motor_min_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) {
mc_interface_set_brake_current(val * m_conf.lo_current_motor_max_now);
}
/**
* Set open loop current vector to brake motor.
*
* @param current
* The current value.
*/
void mc_interface_set_handbrake(float current) {
if (mc_interface_try_input()) {
return;
}
switch (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) {
mc_interface_set_handbrake(val * fabsf(m_conf.lo_current_motor_min_now));
}
void mc_interface_brake_now(void) {
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 (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 (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 (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;
default:
break;
}
return ret;
}
float mc_interface_get_rpm(void) {
float ret = 0.0;
switch (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 = m_amp_seconds / 3600;
if (reset) {
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 = m_amp_seconds_charged / 3600;
if (reset) {
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 = m_watt_seconds / 3600;
if (reset) {
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 = m_watt_seconds_charged / 3600;
if (reset) {
m_watt_seconds_charged = 0.0;
}
return val;
}
float mc_interface_get_tot_current(void) {
float ret = 0.0;
switch (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 (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;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current_directional();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current_directional();
break;
default:
break;
}
return DIR_MULT * ret;
}
float mc_interface_get_tot_current_directional_filtered(void) {
float ret = 0.0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current_directional_filtered();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current_directional_filtered();
break;
default:
break;
}
return DIR_MULT * ret;
}
float mc_interface_get_tot_current_in(void) {
float ret = 0.0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current_in();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current_in();
break;
default:
break;
}
return ret;
}
float mc_interface_get_tot_current_in_filtered(void) {
float ret = 0.0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tot_current_in_filtered();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tot_current_in_filtered();
break;
default:
break;
}
return ret;
}
int mc_interface_get_tachometer_value(bool reset) {
int ret = 0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tachometer_value(reset);
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tachometer_value(reset);
break;
default:
break;
}
return DIR_MULT * ret;
}
int mc_interface_get_tachometer_abs_value(bool reset) {
int ret = 0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_tachometer_abs_value(reset);
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_tachometer_abs_value(reset);
break;
default:
break;
}
return ret;
}
float mc_interface_get_last_inj_adc_isr_duration(void) {
float ret = 0.0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = mcpwm_get_last_inj_adc_isr_duration();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_last_inj_adc_isr_duration();
break;
default:
break;
}
return ret;
}
float mc_interface_read_reset_avg_motor_current(void) {
float res = m_motor_current_sum / m_motor_current_iterations;
m_motor_current_sum = 0.0;
m_motor_current_iterations = 0.0;
return res;
}
float mc_interface_read_reset_avg_input_current(void) {
float res = m_input_current_sum / m_input_current_iterations;
m_input_current_sum = 0.0;
m_input_current_iterations = 0.0;
return res;
}
/**
* Read and reset the average direct axis motor current. (FOC only)
*
* @return
* The average D axis current.
*/
float mc_interface_read_reset_avg_id(void) {
float res = m_motor_id_sum / m_motor_id_iterations;
m_motor_id_sum = 0.0;
m_motor_id_iterations = 0.0;
return DIR_MULT * res; // TODO: DIR_MULT?
}
/**
* Read and reset the average quadrature axis motor current. (FOC only)
*
* @return
* The average Q axis current.
*/
float mc_interface_read_reset_avg_iq(void) {
float res = m_motor_iq_sum / m_motor_iq_iterations;
m_motor_iq_sum = 0.0;
m_motor_iq_iterations = 0.0;
return DIR_MULT * res;
}
float mc_interface_get_pid_pos_set(void) {
return m_position_set;
}
float mc_interface_get_pid_pos_now(void) {
float ret = 0.0;
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
ret = encoder_read_deg();
break;
case MOTOR_TYPE_FOC:
ret = mcpwm_foc_get_pid_pos_now();
break;
default:
break;
}
ret *= DIR_MULT;
utils_norm_angle(&ret);
return ret;
}
float mc_interface_get_last_sample_adc_isr_duration(void) {
return m_last_adc_duration_sample;
}
void mc_interface_sample_print_data(debug_sampling_mode mode, uint16_t len, uint8_t decimation) {
if (len > ADC_SAMPLE_MAX_LEN) {
len = ADC_SAMPLE_MAX_LEN;
}
if (mode == DEBUG_SAMPLING_SEND_LAST_SAMPLES) {
chEvtSignal(sample_send_tp, (eventmask_t) 1);
} else {
m_sample_trigger = -1;
m_sample_now = 0;
m_sample_len = len;
m_sample_int = decimation;
m_sample_mode = mode;
}
}
/**
* Get filtered MOSFET temperature. The temperature is pre-calculated, so this
* functions is fast.
*
* @return
* The filtered MOSFET temperature.
*/
float mc_interface_temp_fet_filtered(void) {
return m_temp_fet;
}
/**
* Get filtered motor temperature. The temperature is pre-calculated, so this
* functions is fast.
*
* @return
* The filtered motor temperature.
*/
float mc_interface_temp_motor_filtered(void) {
return m_temp_motor;
}
// MC implementation functions
/**
* A helper function that should be called before sending commands to control
* the motor. If the state is detecting, the detection will be stopped.
*
* @return
* The amount if milliseconds left until user commands are allowed again.
*
*/
int mc_interface_try_input(void) {
// TODO: Remove this later
if (mc_interface_get_state() == MC_STATE_DETECTING) {
mcpwm_stop_pwm();
m_ignore_iterations = MCPWM_DETECT_STOP_TIME;
}
int retval = m_ignore_iterations;
if (!m_ignore_iterations && m_lock_enabled) {
if (!m_lock_override_once) {
retval = 1;
} else {
m_lock_override_once = false;
}