forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_general.c
1315 lines (1106 loc) · 33.5 KB
/
conf_general.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 - 2019 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 "conf_general.h"
#include "ch.h"
#include "eeprom.h"
#include "mcpwm.h"
#include "mcpwm_foc.h"
#include "mc_interface.h"
#include "utils.h"
#include "stm32f4xx_conf.h"
#include "timeout.h"
#include "commands.h"
#include "encoder.h"
#include "comm_can.h"
#include "app.h"
#include "confgenerator.h"
#include <string.h>
#include <math.h>
// EEPROM settings
#define EEPROM_BASE_MCCONF 1000
#define EEPROM_BASE_APPCONF 2000
#define EEPROM_BASE_HW 3000
#define EEPROM_BASE_CUSTOM 4000
// Global variables
uint16_t VirtAddVarTab[NB_OF_VAR];
bool conf_general_permanent_nrf_found = false;
// Private variables
static mc_configuration mcconf, mcconf_old, mcconf_old_second;
// Private functions
static bool read_eeprom_var(eeprom_var *v, int address, uint16_t base);
static bool store_eeprom_var(eeprom_var *v, int address, uint16_t base);
void conf_general_init(void) {
// First, make sure that all relevant virtual addresses are assigned for page swapping.
memset(VirtAddVarTab, 0, sizeof(VirtAddVarTab));
int ind = 0;
for (unsigned int i = 0;i < (sizeof(mc_configuration) / 2);i++) {
VirtAddVarTab[ind++] = EEPROM_BASE_MCCONF + i;
}
for (unsigned int i = 0;i < (sizeof(app_configuration) / 2);i++) {
VirtAddVarTab[ind++] = EEPROM_BASE_APPCONF + i;
}
for (unsigned int i = 0;i < (EEPROM_VARS_HW * 2);i++) {
VirtAddVarTab[ind++] = EEPROM_BASE_HW + i;
}
for (unsigned int i = 0;i < (EEPROM_VARS_CUSTOM * 2);i++) {
VirtAddVarTab[ind++] = EEPROM_BASE_CUSTOM + i;
}
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
EE_Init();
FLASH_Lock();
}
/**
* Read hw-specific variable from emulated EEPROM.
*
* @param v
* The variable to read the result from.
*
* @param address
* Mapped address in EEPROM. Range 0 to 63.
*
* @return
* true for success, false if variable was not found.
*/
bool conf_general_read_eeprom_var_hw(eeprom_var *v, int address) {
return read_eeprom_var(v, address, EEPROM_BASE_HW);
}
/**
* Read custom variable from emulated EEPROM.
*
* @param v
* The variable to read the result from.
*
* @param address
* Mapped address in EEPROM. Range 0 to 63.
*
* @return
* true for success, false if variable was not found.
*/
bool conf_general_read_eeprom_var_custom(eeprom_var *v, int address) {
return read_eeprom_var(v, address, EEPROM_BASE_CUSTOM);
}
/**
* Store hw-specific variable to emulated EEPROM.
*
* @param v
* The variable to store the result in.
*
* @param address
* Mapped address in EEPROM. Range 0 to 63.
*
* @return
* true for success, false if something went wrong.
*/
bool conf_general_store_eeprom_var_hw(eeprom_var *v, int address) {
return store_eeprom_var(v, address, EEPROM_BASE_HW);
}
/**
* Store custom variable to emulated EEPROM.
*
* @param v
* The variable to store the result in.
*
* @param address
* Mapped address in EEPROM. Range 0 to 63.
*
* @return
* true for success, false if something went wrong.
*/
bool conf_general_store_eeprom_var_custom(eeprom_var *v, int address) {
return store_eeprom_var(v, address, EEPROM_BASE_CUSTOM);
}
static bool read_eeprom_var(eeprom_var *v, int address, uint16_t base) {
bool is_ok = true;
uint16_t var0, var1;
if (EE_ReadVariable(base + 2 * address, &var0) == 0 &&
EE_ReadVariable(base + 2 * address + 1, &var1) == 0) {
uint32_t res = ((uint32_t)var0) << 16 | var1;
v->as_u32 = res;
} else {
is_ok = false;
}
return is_ok;
}
static bool store_eeprom_var(eeprom_var *v, int address, uint16_t base) {
bool is_ok = true;
uint16_t var0, var1;
var0 = v->as_u32 >> 16;
var1 = v->as_u32 & 0xFFFF;
timeout_configure_IWDT_slowest();
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
if (EE_WriteVariable(base + address * 2, var0) != FLASH_COMPLETE) {
is_ok = false;
}
if (is_ok) {
if (EE_WriteVariable(base + address * 2 + 1, var1) != FLASH_COMPLETE) {
is_ok = false;
}
}
FLASH_Lock();
timeout_configure_IWDT();
return is_ok;
}
/**
* Read app_configuration from EEPROM. If this fails, default values will be used.
*
* @param conf
* A pointer to a app_configuration struct to write the read configuration to.
*/
void conf_general_read_app_configuration(app_configuration *conf) {
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
uint16_t var;
for (unsigned int i = 0;i < (sizeof(app_configuration) / 2);i++) {
if (EE_ReadVariable(EEPROM_BASE_APPCONF + i, &var) == 0) {
conf_addr[2 * i] = (var >> 8) & 0xFF;
conf_addr[2 * i + 1] = var & 0xFF;
} else {
is_ok = false;
break;
}
}
// Set the default configuration
if (!is_ok) {
confgenerator_set_defaults_appconf(conf);
}
}
/**
* Write app_configuration to EEPROM.
*
* @param conf
* A pointer to the configuration that should be stored.
*/
bool conf_general_store_app_configuration(app_configuration *conf) {
mc_interface_unlock();
mc_interface_release_motor();
utils_sys_lock_cnt();
mc_interface_lock();
timeout_configure_IWDT_slowest();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
uint16_t var;
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
for (unsigned int i = 0;i < (sizeof(app_configuration) / 2);i++) {
var = (conf_addr[2 * i] << 8) & 0xFF00;
var |= conf_addr[2 * i + 1] & 0xFF;
if (EE_WriteVariable(EEPROM_BASE_APPCONF + i, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
FLASH_Lock();
timeout_configure_IWDT();
chThdSleepMilliseconds(100);
mc_interface_unlock();
utils_sys_unlock_cnt();
return is_ok;
}
/**
* Read mc_configuration from EEPROM. If this fails, default values will be used.
*
* @param conf
* A pointer to a mc_configuration struct to write the read configuration to.
*/
void conf_general_read_mc_configuration(mc_configuration *conf) {
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
uint16_t var;
for (unsigned int i = 0;i < (sizeof(mc_configuration) / 2);i++) {
if (EE_ReadVariable(EEPROM_BASE_MCCONF + i, &var) == 0) {
conf_addr[2 * i] = (var >> 8) & 0xFF;
conf_addr[2 * i + 1] = var & 0xFF;
} else {
is_ok = false;
break;
}
}
if (!is_ok) {
confgenerator_set_defaults_mcconf(conf);
}
}
/**
* Write mc_configuration to EEPROM.
*
* @param conf
* A pointer to the configuration that should be stored.
*/
bool conf_general_store_mc_configuration(mc_configuration *conf) {
mc_interface_unlock();
mc_interface_release_motor();
utils_sys_lock_cnt();
mc_interface_lock();
timeout_configure_IWDT_slowest();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
for (unsigned int i = 0;i < (sizeof(mc_configuration) / 2);i++) {
uint16_t var = (conf_addr[2 * i] << 8) & 0xFF00;
var |= conf_addr[2 * i + 1] & 0xFF;
if (EE_WriteVariable(EEPROM_BASE_MCCONF + i, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
FLASH_Lock();
timeout_configure_IWDT();
chThdSleepMilliseconds(100);
mc_interface_unlock();
utils_sys_unlock_cnt();
return is_ok;
}
bool conf_general_detect_motor_param(float current, float min_rpm, float low_duty,
float *int_limit, float *bemf_coupling_k, int8_t *hall_table, int *hall_res) {
int ok_steps = 0;
const float spinup_to_duty = 0.5;
mcconf = *mc_interface_get_configuration();
mcconf_old = mcconf;
mcconf.motor_type = MOTOR_TYPE_BLDC;
mcconf.sensor_mode = SENSOR_MODE_SENSORLESS;
mcconf.comm_mode = COMM_MODE_INTEGRATE;
mcconf.sl_phase_advance_at_br = 1.0;
mcconf.sl_min_erpm = min_rpm;
mcconf.sl_bemf_coupling_k = 300;
mcconf.sl_cycle_int_limit = 50;
mcconf.sl_min_erpm_cycle_int_limit = 1100;
mcconf.m_invert_direction = false;
mc_interface_set_configuration(&mcconf);
// Wait maximum 5s for fault code to disappear
for (int i = 0;i < 500;i++) {
if (mc_interface_get_fault() == FAULT_CODE_NONE) {
break;
}
chThdSleepMilliseconds(10);
}
// Wait one second for things to get ready after
// the fault disappears. (will fry things otherwise...)
chThdSleepMilliseconds(1000);
// Disable timeout
systime_t tout = timeout_get_timeout_msec();
float tout_c = timeout_get_brake_current();
timeout_reset();
timeout_configure(60000, 0.0);
mc_interface_lock();
mc_interface_lock_override_once();
mc_interface_set_current(current);
// Try to spin up the motor. Up to three attempts with different settings are made.
bool started = false;
for (int i = 0;i < 3;i++) {
if (i == 1) {
mc_interface_lock_override_once();
mc_interface_release_motor();
mcconf.sl_min_erpm = 2 * min_rpm;
mcconf.sl_cycle_int_limit = 20;
mc_interface_lock_override_once();
mc_interface_set_configuration(&mcconf);
chThdSleepMilliseconds(1000);
mc_interface_lock_override_once();
mc_interface_set_current(current);
} else if (i == 2) {
mc_interface_lock_override_once();
mc_interface_release_motor();
mcconf.sl_min_erpm = 4 * min_rpm;
mcconf.comm_mode = COMM_MODE_DELAY;
mc_interface_lock_override_once();
mc_interface_set_configuration(&mcconf);
chThdSleepMilliseconds(1000);
mc_interface_lock_override_once();
mc_interface_set_current(current);
}
int cnt = 0;
bool switch_done = false;
started = true;
while (mc_interface_get_duty_cycle_now() < spinup_to_duty) {
chThdSleepMilliseconds(1);
cnt++;
if (mc_interface_get_duty_cycle_now() >= (spinup_to_duty / 2.0) && !switch_done) {
mcpwm_switch_comm_mode(COMM_MODE_DELAY);
switch_done = true;
}
if (cnt > 2000 && !switch_done) {
started = false;
break;
}
if (cnt >= 5000) {
started = false;
break;
}
}
if (switch_done) {
break;
}
}
if (!started) {
mc_interface_set_current(0.0);
timeout_configure(tout, tout_c);
mc_interface_set_configuration(&mcconf_old);
mc_interface_unlock();
return false;
}
ok_steps++;
// Reset hall sensor samples
mcpwm_reset_hall_detect_table();
// Run for a while to get hall sensor samples
mc_interface_lock_override_once();
mc_interface_set_duty(spinup_to_duty);
chThdSleepMilliseconds(400);
// Release the motor and wait a few commutations
mc_interface_lock_override_once();
mc_interface_set_current(0.0);
int tacho = mc_interface_get_tachometer_value(0);
for (int i = 0;i < 2000;i++) {
if ((mc_interface_get_tachometer_value(0) - tacho) < 3) {
chThdSleepMilliseconds(1);
} else {
ok_steps++;
break;
}
}
// Average the cycle integrator for 50 commutations
mcpwm_read_reset_avg_cycle_integrator();
tacho = mc_interface_get_tachometer_value(false);
for (int i = 0;i < 3000;i++) {
if ((mc_interface_get_tachometer_value(false) - tacho) < 50) {
chThdSleepMilliseconds(1);
} else {
ok_steps++;
break;
}
}
// Get hall detect result
*hall_res = mcpwm_get_hall_detect_result(hall_table);
*int_limit = mcpwm_read_reset_avg_cycle_integrator();
// Wait for the motor to slow down
for (int i = 0;i < 5000;i++) {
if (mc_interface_get_duty_cycle_now() > low_duty) {
chThdSleepMilliseconds(1);
} else {
ok_steps++;
break;
}
}
mc_interface_lock_override_once();
mc_interface_set_duty(low_duty);
// Average the cycle integrator for 100 commutations
mcpwm_read_reset_avg_cycle_integrator();
tacho = mc_interface_get_tachometer_value(0);
float rpm_sum = 0.0;
float rpm_iterations = 0.0;
for (int i = 0;i < 3000;i++) {
if ((mc_interface_get_tachometer_value(0) - tacho) < 100) {
rpm_sum += mc_interface_get_rpm();
rpm_iterations += 1;
chThdSleepMilliseconds(1);
} else {
ok_steps++;
break;
}
}
float avg_cycle_integrator_running = mcpwm_read_reset_avg_cycle_integrator();
float rpm = rpm_sum / rpm_iterations;
mc_interface_lock_override_once();
mc_interface_release_motor();
// Try to figure out the coupling factor
avg_cycle_integrator_running -= *int_limit;
avg_cycle_integrator_running /= (float)ADC_Value[ADC_IND_VIN_SENS];
avg_cycle_integrator_running *= rpm;
*bemf_coupling_k = avg_cycle_integrator_running;
// Restore settings
mc_interface_set_configuration(&mcconf_old);
timeout_configure(tout, tout_c);
mc_interface_unlock();
return ok_steps == 5 ? true : false;
}
/**
* Try to measure the motor flux linkage.
*
* @param current
* The current so spin up the motor with.
*
* @param duty
* The duty cycle to maintain.
*
* @param min_erpm
* The minimum ERPM for the delay commutation mode.
*
* @param res
* The motor phase resistance.
*
* @param linkage
* The calculated flux linkage.
*
* @return
* True for success, false otherwise.
*/
bool conf_general_measure_flux_linkage(float current, float duty,
float min_erpm, float res, float *linkage) {
mcconf = *mc_interface_get_configuration();
mcconf_old_second = mcconf;
mcconf.motor_type = MOTOR_TYPE_BLDC;
mcconf.sensor_mode = SENSOR_MODE_SENSORLESS;
mcconf.comm_mode = COMM_MODE_INTEGRATE;
mcconf.sl_phase_advance_at_br = 1.0;
mcconf.sl_min_erpm = min_erpm;
mcconf.m_bldc_f_sw_min = 10000.0;
mcconf.sl_bemf_coupling_k = 300;
mcconf.sl_cycle_int_limit = 50;
mcconf.sl_min_erpm_cycle_int_limit = 1100;
mc_interface_set_configuration(&mcconf);
// Wait maximum 5s for fault code to disappear
for (int i = 0;i < 500;i++) {
if (mc_interface_get_fault() == FAULT_CODE_NONE) {
break;
}
chThdSleepMilliseconds(10);
}
if (mc_interface_get_fault() != FAULT_CODE_NONE) {
mc_interface_set_configuration(&mcconf_old_second);
return false;
}
// Wait one second for things to get ready after
// the fault disapears.
chThdSleepMilliseconds(1000);
// Disable timeout
systime_t tout = timeout_get_timeout_msec();
float tout_c = timeout_get_brake_current();
timeout_reset();
timeout_configure(60000, 0.0);
mc_interface_lock();
mc_interface_lock_override_once();
mc_interface_set_current(current);
// Try to spin up the motor. Up to three attempts with different settings are made.
bool started = false;
for (int i = 0;i < 4;i++) {
if (i == 1) {
mc_interface_lock_override_once();
mc_interface_release_motor();
mcconf.sl_cycle_int_limit = 250;
mc_interface_lock_override_once();
mc_interface_set_configuration(&mcconf);
chThdSleepMilliseconds(1000);
mc_interface_lock_override_once();
mc_interface_set_current(current);
} else if (i == 2) {
mc_interface_lock_override_once();
mc_interface_release_motor();
mcconf.sl_min_erpm = 2 * min_erpm;
mcconf.sl_cycle_int_limit = 20;
mc_interface_lock_override_once();
mc_interface_set_configuration(&mcconf);
chThdSleepMilliseconds(1000);
mc_interface_lock_override_once();
mc_interface_set_current(current);
} else if (i == 3) {
mc_interface_lock_override_once();
mc_interface_release_motor();
mcconf.sl_min_erpm = 4 * min_erpm;
mcconf.comm_mode = COMM_MODE_DELAY;
mc_interface_lock_override_once();
mc_interface_set_configuration(&mcconf);
chThdSleepMilliseconds(1000);
mc_interface_lock_override_once();
mc_interface_set_current(current);
}
int cnt = 0;
bool switch_done = false;
started = true;
while (mc_interface_get_duty_cycle_now() < duty) {
chThdSleepMilliseconds(1);
cnt++;
if (mc_interface_get_duty_cycle_now() >= (duty / 2.0) && !switch_done) {
mcpwm_switch_comm_mode(COMM_MODE_DELAY);
switch_done = true;
}
if (cnt > 2000 && !switch_done) {
started = false;
break;
}
if (cnt >= 5000) {
started = false;
break;
}
}
if (switch_done) {
break;
}
}
if (!started) {
mc_interface_set_current(0.0);
timeout_configure(tout, tout_c);
mc_interface_set_configuration(&mcconf_old_second);
mc_interface_unlock();
return false;
}
mc_interface_lock_override_once();
mc_interface_set_duty(duty);
float avg_voltage = 0.0;
float avg_rpm = 0.0;
float avg_current = 0.0;
float samples = 0.0;
for (int i = 0;i < 2000;i++) {
avg_voltage += GET_INPUT_VOLTAGE() * mc_interface_get_duty_cycle_now();
avg_rpm += mc_interface_get_rpm();
avg_current += mc_interface_get_tot_current();
samples += 1.0;
chThdSleepMilliseconds(1.0);
}
timeout_configure(tout, tout_c);
mc_interface_set_configuration(&mcconf_old_second);
mc_interface_unlock();
mc_interface_set_current(0.0);
avg_voltage /= samples;
avg_rpm /= samples;
avg_current /= samples;
avg_voltage -= avg_current * res * 2.0;
*linkage = avg_voltage * 60.0 / (sqrtf(3.0) * 2.0 * M_PI * avg_rpm);
return true;
}
/* Calculate DTG register */
uint8_t conf_general_calculate_deadtime(float deadtime_ns, float core_clock_freq) {
uint8_t DTG = 0;
float timebase = 1.0 / (core_clock_freq / 1000000.0) * 1000.0;
if (deadtime_ns <= (timebase * 127.0)) {
DTG = deadtime_ns / timebase;
} else {
if (deadtime_ns <= ((63.0 + 64.0) * 2.0 * timebase)) {
DTG = deadtime_ns / (2.0 * timebase) - 64.0;
DTG |= 0x80;
} else {
if (deadtime_ns <= ((31.0 + 32.0) * 8.0 * timebase)) {
DTG = deadtime_ns / (8.0 * timebase) - 32.0;
DTG |= 0xC0;
} else {
if (deadtime_ns <= ((31.0 + 32) * 16 * timebase)) {
DTG = deadtime_ns / (16.0 * timebase) - 32.0;
DTG |= 0xE0;
} else {
// Deadtime requested is longer than max achievable. Set deadtime at
// longest possible value
DTG = 0xFF;
assert_param(1); //catch this
}
}
}
}
return DTG;
}
/**
* Try to measure the motor flux linkage using open loop FOC control.
*
* @param current
* The Q-axis current to spin up the motor.
*
* @param duty
* Duty cycle % to measure at
*
* @param erpm_per_sec
* Acceleration rate
*
* @param res
* The motor phase resistance.
*
* @param linkage
* The calculated flux linkage.
*
* @return
* True for success, false otherwise.
*/
bool conf_general_measure_flux_linkage_openloop(float current, float duty,
float erpm_per_sec, float res, float *linkage) {
bool result = false;
mcconf = *mc_interface_get_configuration();
mcconf_old_second = mcconf;
mcconf.motor_type = MOTOR_TYPE_FOC;
mcconf.foc_sensor_mode = FOC_SENSOR_MODE_SENSORLESS;
mcconf.foc_current_kp = 0.0005;
mcconf.foc_current_ki = 1.0;
mc_interface_set_configuration(&mcconf);
// Wait maximum 5s for fault code to disappear
for (int i = 0;i < 500;i++) {
if (mc_interface_get_fault() == FAULT_CODE_NONE) {
break;
}
chThdSleepMilliseconds(10);
}
if (mc_interface_get_fault() != FAULT_CODE_NONE) {
mc_interface_set_configuration(&mcconf_old_second);
return false;
}
// Wait one second for things to get ready after
// the fault disapears.
chThdSleepMilliseconds(1000);
// Disable timeout
systime_t tout = timeout_get_timeout_msec();
float tout_c = timeout_get_brake_current();
timeout_reset();
timeout_configure(60000, 0.0);
mc_interface_lock();
int cnt = 0;
float rpm_now = 0;
// Start by locking the motor
mcpwm_foc_set_openloop(current, rpm_now);
float duty_still = 0;
float samples = 0;
for (int i = 0;i < 1000;i++) {
duty_still += fabsf(mc_interface_get_duty_cycle_now());
samples += 1.0;
chThdSleepMilliseconds(1);
}
duty_still /= samples;
float duty_max = 0.0;
const int max_time = 15000;
while (fabsf(mc_interface_get_duty_cycle_now()) < duty) {
mcpwm_foc_set_openloop(current, mcconf.m_invert_direction ? -rpm_now : rpm_now);
rpm_now += erpm_per_sec / 1000.0;
chThdSleepMilliseconds(1);
cnt++;
float duty_now = fabsf(mc_interface_get_duty_cycle_now());
if (duty_now > duty_max) {
duty_max = duty_now;
}
if (cnt >= max_time) {
*linkage = -1.0;
break;
}
if (cnt > 4000 && duty_now < (duty_max * 0.7)) {
cnt = max_time;
*linkage = -2.0;
break;
}
if (cnt > 4000 && duty < duty_still * 1.1) {
cnt = max_time;
*linkage = -3.0;
break;
}
if (rpm_now >= 12000) {
break;
}
}
chThdSleepMilliseconds(1000);
if (cnt < max_time) {
float vq_avg = 0.0;
float vd_avg = 0.0;
float iq_avg = 0.0;
float id_avg = 0.0;
float samples2 = 0.0;
for (int i = 0;i < 1000;i++) {
vq_avg += mcpwm_foc_get_vq();
vd_avg += mcpwm_foc_get_vd();
iq_avg += mcpwm_foc_get_iq();
id_avg += mcpwm_foc_get_id();
samples2 += 1.0;
chThdSleepMilliseconds(1);
}
vq_avg /= samples2;
vd_avg /= samples2;
iq_avg /= samples2;
id_avg /= samples2;
*linkage = (sqrtf(SQ(vq_avg) + SQ(vd_avg)) - res *
sqrtf(SQ(iq_avg) + SQ(id_avg))) / (rpm_now * ((2.0 * M_PI) / 60.0));
result = true;
}
timeout_configure(tout, tout_c);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_set_configuration(&mcconf_old_second);
return result;
}
/**
* Automatically detect sensors and apply settings in FOC mode.
*
* @param current
* Current to use for detection.
*
* @param store_mcconf_on_success
* Store motor configuration in emulated EEPROM if the detection succeeds.
*
* @param send_mcconf_on_success
* Send motor configuration if the detection succeeds.
*
* @return
* 2: AS5147 detected successfully
* 1: Hall sensors detected successfully
* 0: No sensors detected and sensorless mode applied successfully
* -1: Detection failed
*/
int conf_general_autodetect_apply_sensors_foc(float current,
bool store_mcconf_on_success, bool send_mcconf_on_success) {
int result = -1;
mcconf = *mc_interface_get_configuration();
mcconf_old_second = mcconf;
mcconf.motor_type = MOTOR_TYPE_FOC;
mcconf.foc_sensor_mode = FOC_SENSOR_MODE_SENSORLESS;
mcconf.foc_current_kp = 0.0005;
mcconf.foc_current_ki = 1.0;
mc_interface_set_configuration(&mcconf);
// Wait maximum 5s for fault code to disappear
for (int i = 0;i < 500;i++) {
if (mc_interface_get_fault() == FAULT_CODE_NONE) {
break;
}
chThdSleepMilliseconds(10);
}
if (mc_interface_get_fault() != FAULT_CODE_NONE) {
mc_interface_set_configuration(&mcconf_old_second);
return -1;
}
// Wait one second for things to get ready after
// the fault disapears.
chThdSleepMilliseconds(1000);
// Disable timeout
systime_t tout = timeout_get_timeout_msec();
float tout_c = timeout_get_brake_current();
timeout_reset();
timeout_configure(60000, 0.0);
mc_interface_lock();
// Hall sensors
mcconf.m_sensor_port_mode = SENSOR_PORT_MODE_HALL;
mc_interface_set_configuration(&mcconf);
uint8_t hall_table[8];
bool res = mcpwm_foc_hall_detect(current, hall_table);
// Disable timeout and lock, as hall detection will undo the lock
timeout_reset();
timeout_configure(60000, 0.0);
mc_interface_lock();
if (res) {
mcconf_old_second.m_sensor_port_mode = SENSOR_PORT_MODE_HALL;
mcconf_old_second.foc_sensor_mode = FOC_SENSOR_MODE_HALL;
for (int i = 0;i < 8;i++) {
mcconf_old_second.foc_hall_table[i] = hall_table[i];
}
result = 1;
}
// AS5047 encoder
if (!res) {
mcconf.m_sensor_port_mode = SENSOR_PORT_MODE_AS5047_SPI;
mc_interface_set_configuration(&mcconf);
mcpwm_foc_set_openloop_phase(current, 0.0);
chThdSleepMilliseconds(1000);
float phase_start = encoder_read_deg();
float phase_mid = 0.0;
float phase_end = 0.0;
for (int i = 0;i < 180.0;i++) {
mcpwm_foc_set_openloop_phase(current, i);
chThdSleepMilliseconds(5);
if (i == 90) {
phase_mid = encoder_read_deg();
}
}
phase_end = encoder_read_deg();
float diff = fabsf(utils_angle_difference(phase_start, phase_end));
float diff_mid = fabsf(utils_angle_difference(phase_mid, phase_end));
if (diff > 2.0 && (diff_mid - diff / 2.0) < (diff / 4)) {
float offset, ratio;
bool inverted;
mcpwm_foc_encoder_detect(current, false, &offset, &ratio, &inverted);
mcconf_old_second.m_sensor_port_mode = SENSOR_PORT_MODE_AS5047_SPI;
mcconf_old_second.foc_sensor_mode = FOC_SENSOR_MODE_ENCODER;
mcconf_old_second.foc_encoder_offset = offset;
mcconf_old_second.foc_encoder_ratio = ratio;
mcconf_old_second.foc_encoder_inverted = inverted;
res = true;
result = 2;
}
}
// Sensorless
if (!res) {
mcconf_old_second.foc_sensor_mode = FOC_SENSOR_MODE_SENSORLESS;
result = 0;
res = true;
}
timeout_configure(tout, tout_c);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_set_configuration(&mcconf_old_second);