forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_general.c
2205 lines (1862 loc) · 58.8 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 - 2021 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/>.
*/
#pragma GCC push_options
#pragma GCC optimize ("Os")
#include "conf_general.h"
#include "ch.h"
#include "eeprom.h"
#include "mcpwm.h"
#include "mcpwm_foc.h"
#include "mc_interface.h"
#include "utils_math.h"
#include "utils_sys.h"
#include "stm32f4xx_conf.h"
#include "timeout.h"
#include "commands.h"
#include "encoder/encoder.h"
#include "comm_can.h"
#include "app.h"
#include "confgenerator.h"
#include "mempools.h"
#include "worker.h"
#include "crc.h"
#include "terminal.h"
#include "firmware_metadata.h"
#include <string.h>
#include <math.h>
//#define TEST_BAD_MC_CRC
//#define TEST_BAD_APP_CRC
// EEPROM settings
#define EEPROM_BASE_MCCONF 1000
#define EEPROM_BASE_APPCONF 2000
#define EEPROM_BASE_HW 3000
#define EEPROM_BASE_CUSTOM 4000
#define EEPROM_BASE_MCCONF_2 5000
#define EEPROM_BASE_BACKUP 6000
// Global variables
uint16_t VirtAddVarTab[NB_OF_VAR];
bool conf_general_permanent_nrf_found = false;
__attribute__((section(".ram4"))) volatile backup_data g_backup;
// 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;
}
for (unsigned int i = 0;i < (sizeof(backup_data) / 2);i++) {
VirtAddVarTab[ind++] = EEPROM_BASE_BACKUP + 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 backup data
bool is_ok = true;
backup_data backup_tmp;
uint8_t *data_addr = (uint8_t*)&backup_tmp;
uint16_t var;
for (unsigned int i = 0;i < (sizeof(backup_data) / 2);i++) {
if (EE_ReadVariable(EEPROM_BASE_BACKUP + i, &var) == 0) {
data_addr[2 * i] = (var >> 8) & 0xFF;
data_addr[2 * i + 1] = var & 0xFF;
} else {
is_ok = false;
break;
}
}
if (!is_ok) {
memset(data_addr, 0, sizeof(backup_data));
// If the missing data is a result of programming it might still be in RAM4. Check
// and recover the valid values one by one.
if (g_backup.odometer_init_flag == BACKUP_VAR_INIT_CODE) {
backup_tmp.odometer = g_backup.odometer;
}
if (g_backup.runtime_init_flag == BACKUP_VAR_INIT_CODE) {
backup_tmp.runtime = g_backup.runtime;
}
if (g_backup.hw_config_init_flag == BACKUP_VAR_INIT_CODE) {
memcpy((void*)backup_tmp.hw_config, (uint8_t*)g_backup.hw_config, sizeof(g_backup.hw_config));
}
}
backup_tmp.odometer_init_flag = BACKUP_VAR_INIT_CODE;
backup_tmp.runtime_init_flag = BACKUP_VAR_INIT_CODE;
backup_tmp.hw_config_init_flag = BACKUP_VAR_INIT_CODE;
g_backup = backup_tmp;
conf_general_store_backup_data();
}
/*
* Store backup data to emulated eeprom. Currently this is only done from the shutdown function, which
* only works if the hardware has a power switch. It would be possible to do this when the input voltage
* drops (e.g. on FAULT_CODE_UNDER_VOLTAGE) to not rely on a power switch. The risk with that is that
* a page swap might longer than the capacitors have voltage left, which could make cause the motor and
* app config to get lost.
*/
bool conf_general_store_backup_data(void) {
timeout_configure_IWDT_slowest();
bool is_ok = true;
uint8_t *data_addr = (uint8_t*)&g_backup;
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(backup_data) / 2);i++) {
var = (data_addr[2 * i] << 8) & 0xFF00;
var |= data_addr[2 * i + 1] & 0xFF;
if (EE_WriteVariable(EEPROM_BASE_BACKUP + i, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
FLASH_Lock();
timeout_configure_IWDT();
return is_ok;
}
/**
* 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 31.
*
* @return
* true for success, false if variable was not found.
*/
bool conf_general_read_eeprom_var_hw(eeprom_var *v, int address) {
if (address < 0 || address > (EEPROM_VARS_HW - 1)) {
return false;
}
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 127.
*
* @return
* true for success, false if variable was not found.
*/
bool conf_general_read_eeprom_var_custom(eeprom_var *v, int address) {
if (address < 0 || address > (EEPROM_VARS_CUSTOM - 1)) {
return false;
}
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 31.
*
* @return
* true for success, false if something went wrong.
*/
bool conf_general_store_eeprom_var_hw(eeprom_var *v, int address) {
if (address < 0 || address > (EEPROM_VARS_HW - 1)) {
return false;
}
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 127.
*
* @return
* true for success, false if something went wrong.
*/
bool conf_general_store_eeprom_var_custom(eeprom_var *v, int address) {
if (address < 0 || address > (EEPROM_VARS_CUSTOM - 1)) {
return false;
}
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;
}
}
// check CRC
#ifdef TEST_BAD_APP_CRC
conf->crc++;
#endif
if(conf->crc != app_calc_crc(conf)) {
is_ok = false;
// mc_interface_fault_stop(FAULT_CODE_FLASH_CORRUPTION_APP_CFG, false, false);
fault_data f;
f.fault = FAULT_CODE_FLASH_CORRUPTION_APP_CFG;
terminal_add_fault_data(&f);
}
// 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) {
int motor_old = mc_interface_get_motor_thread();
mc_interface_select_motor_thread(1);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_lock();
if (!mc_interface_wait_for_motor_release(2.0)) {
mc_interface_unlock();
mc_interface_select_motor_thread(motor_old);
return false;
}
mc_interface_select_motor_thread(2);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_lock();
if (!mc_interface_wait_for_motor_release(2.0)) {
mc_interface_unlock();
mc_interface_select_motor_thread(motor_old);
return false;
}
utils_sys_lock_cnt();
timeout_configure_IWDT_slowest();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
uint16_t var;
conf->crc = app_calc_crc(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(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_select_motor_thread(1);
mc_interface_unlock();
mc_interface_select_motor_thread(2);
mc_interface_unlock();
utils_sys_unlock_cnt();
mc_interface_select_motor_thread(motor_old);
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_motor_2) {
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
uint16_t var;
unsigned int base = is_motor_2 ? EEPROM_BASE_MCCONF_2 : EEPROM_BASE_MCCONF;
for (unsigned int i = 0;i < (sizeof(mc_configuration) / 2);i++) {
if (EE_ReadVariable(base + i, &var) == 0) {
conf_addr[2 * i] = (var >> 8) & 0xFF;
conf_addr[2 * i + 1] = var & 0xFF;
} else {
is_ok = false;
break;
}
}
// check CRC
#ifdef TEST_BAD_MC_CRC
conf->crc++;
#endif
if(conf->crc != mc_interface_calc_crc(conf, is_motor_2)) {
is_ok = false;
// mc_interface_fault_stop(FAULT_CODE_FLASH_CORRUPTION_MC_CFG, is_motor_2, false);
fault_data f;
f.fault = FAULT_CODE_FLASH_CORRUPTION_MC_CFG;
terminal_add_fault_data(&f);
}
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, bool is_motor_2) {
int motor_old = mc_interface_get_motor_thread();
mc_interface_select_motor_thread(1);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_lock();
if (!mc_interface_wait_for_motor_release(2.0)) {
mc_interface_unlock();
mc_interface_select_motor_thread(motor_old);
return false;
}
mc_interface_select_motor_thread(2);
mc_interface_unlock();
mc_interface_release_motor();
mc_interface_lock();
if (!mc_interface_wait_for_motor_release(2.0)) {
mc_interface_unlock();
mc_interface_select_motor_thread(motor_old);
return false;
}
utils_sys_lock_cnt();
timeout_configure_IWDT_slowest();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)conf;
unsigned int base = is_motor_2 ? EEPROM_BASE_MCCONF_2 : EEPROM_BASE_MCCONF;
conf->crc = mc_interface_calc_crc(conf, is_motor_2);
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(base + i, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
FLASH_Lock();
timeout_configure_IWDT();
chThdSleepMilliseconds(100);
mc_interface_select_motor_thread(1);
mc_interface_unlock();
mc_interface_select_motor_thread(2);
mc_interface_unlock();
utils_sys_unlock_cnt();
mc_interface_select_motor_thread(motor_old);
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;
mc_configuration *mcconf = mempools_alloc_mcconf();
mc_configuration *mcconf_old = mempools_alloc_mcconf();
*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();
KILL_SW_MODE tout_ksw = timeout_get_kill_sw_mode();
timeout_reset();
timeout_configure(60000, 0.0, KILL_SW_MODE_DISABLED);
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();
mc_interface_wait_for_motor_release(1.0);
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();
mc_interface_wait_for_motor_release(1.0);
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, tout_ksw);
mc_interface_set_configuration(mcconf_old);
mc_interface_unlock();
mempools_free_mcconf(mcconf);
mempools_free_mcconf(mcconf_old);
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();
mc_interface_wait_for_motor_release(1.0);
// 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, tout_ksw);
mc_interface_unlock();
mempools_free_mcconf(mcconf);
mempools_free_mcconf(mcconf_old);
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) {
mc_configuration *mcconf = mempools_alloc_mcconf();
mc_configuration *mcconf_old = mempools_alloc_mcconf();
*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_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);
mempools_free_mcconf(mcconf);
mempools_free_mcconf(mcconf_old);
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();
KILL_SW_MODE tout_ksw = timeout_get_kill_sw_mode();
timeout_reset();
timeout_configure(60000, 0.0, KILL_SW_MODE_DISABLED);
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();
mc_interface_wait_for_motor_release(1.0);
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();
mc_interface_wait_for_motor_release(1.0);
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();
mc_interface_wait_for_motor_release(1.0);
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, tout_ksw);
mc_interface_set_configuration(mcconf);
mc_interface_unlock();
mempools_free_mcconf(mcconf);
mempools_free_mcconf(mcconf_old);
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, tout_ksw);
mc_interface_set_configuration(mcconf_old);
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 / (sqrtf(3.0) * RPM2RADPS_f(avg_rpm));
mempools_free_mcconf(mcconf);
mempools_free_mcconf(mcconf_old);
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 ind
* The motor phase inductance.
*
* @param linkage
* The calculated flux linkage.
*
* @param linkage_undriven
* Flux linkage measured while the motor was undriven.
*
* @param undriven_samples
* Number of flux linkage samples while the motor was undriven.
*
* @param result
* True for success, false for anything else
*
* @return
* Fault code
*/
int conf_general_measure_flux_linkage_openloop(float current, float duty,
float erpm_per_sec, float res, float ind, float *linkage,
float *linkage_undriven, float *undriven_samples, bool *result) {
*result = false;
int fault = FAULT_CODE_NONE;
// Allow using old values when only measuring the flux linkage undriven
if (fabsf(current) <= mc_interface_get_configuration()->cc_min_current) {
if (res <= 0.0) {
res = mc_interface_get_configuration()->foc_motor_r;
}
if (ind <= 0.0) {
ind = mc_interface_get_configuration()->foc_motor_l;
}
}
// Don't let impossible values through.
if (res <= 0.0 || ind <= 0.0) {
return fault;
}
// Calculate kp and ki from supplied resistance and inductance, default to 1000us time constant.
float tc = 1500;
float bw = 1.0 / (tc * 1e-6);
float kp = ind * bw;
float ki = res * bw;
mc_configuration *mcconf = mempools_alloc_mcconf();
mc_configuration *mcconf_old = mempools_alloc_mcconf();