Skip to content

Commit 8b9e3bf

Browse files
committed
Offset calibration improvements
1 parent 3b4e601 commit 8b9e3bf

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

conf_general.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 13
27+
#define FW_TEST_VERSION_NUMBER 14
2828

2929
#include "datatypes.h"
3030

mcpwm_foc.c

+29-22
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ typedef struct {
109109
mc_state m_state;
110110
mc_control_mode m_control_mode;
111111
motor_state_t m_motor_state;
112-
int m_curr_unbalance;
112+
float m_curr_unbalance;
113+
float m_currents_adc[3];
113114
bool m_phase_override;
114115
float m_phase_now_override;
115116
float m_duty_cycle_set;
@@ -594,21 +595,19 @@ void mcpwm_foc_init(volatile mc_configuration *conf_m1, volatile mc_configuratio
594595
DCCAL_OFF();
595596

596597
if (m_motor_1.m_conf->foc_offsets_cal_on_boot) {
597-
m_motor_1.m_conf->foc_offsets_current[0] = 2048;
598-
m_motor_1.m_conf->foc_offsets_current[1] = 2048;
599-
m_motor_1.m_conf->foc_offsets_current[2] = 2048;
598+
for (int i = 0;i < 3;i++) {
599+
m_motor_1.m_conf->foc_offsets_voltage[i] = 0.0;
600+
m_motor_1.m_conf->foc_offsets_voltage_undriven[i] = 0.0;
601+
m_motor_1.m_conf->foc_offsets_current[i] = 2048;
602+
600603
#ifdef HW_HAS_DUAL_MOTORS
601-
m_motor_2.m_conf->foc_offsets_current[0] = 2048;
602-
m_motor_2.m_conf->foc_offsets_current[1] = 2048;
603-
m_motor_2.m_conf->foc_offsets_current[2] = 2048;
604+
m_motor_2.m_conf->foc_offsets_voltage[i] = 0.0;
605+
m_motor_2.m_conf->foc_offsets_voltage_undriven[i] = 0.0;
606+
m_motor_2.m_conf->foc_offsets_current[i] = 2048;
604607
#endif
608+
}
605609

606610
mcpwm_foc_dc_cal(false);
607-
608-
conf_general_store_mc_configuration((mc_configuration*)m_motor_1.m_conf, false);
609-
#ifdef HW_HAS_DUAL_MOTORS
610-
conf_general_store_mc_configuration((mc_configuration*)m_motor_2.m_conf, true);
611-
#endif
612611
} else {
613612
m_dccal_done = true;
614613
}
@@ -1211,7 +1210,7 @@ float mcpwm_foc_get_abs_motor_current(void) {
12111210
* The magnitude of the phase currents unbalance.
12121211
*/
12131212
float mcpwm_foc_get_abs_motor_current_unbalance(void) {
1214-
return (float)(motor_now()->m_curr_unbalance) * FAC_CURRENT;
1213+
return motor_now()->m_curr_unbalance * FAC_CURRENT;
12151214
}
12161215

12171216
/**
@@ -2096,7 +2095,7 @@ int mcpwm_foc_dc_cal(bool cal_undriven) {
20962095

20972096
// Measure driven offsets
20982097

2099-
const float samples = 500.0;
2098+
const float samples = 1000.0;
21002099
float current_sum[3] = {0.0, 0.0, 0.0};
21012100
float voltage_sum[3] = {0.0, 0.0, 0.0};
21022101

@@ -2126,10 +2125,10 @@ int mcpwm_foc_dc_cal(bool cal_undriven) {
21262125
chThdSleepMilliseconds(10);
21272126

21282127
for (float i = 0;i < samples;i++) {
2129-
current_sum[0] += GET_CURRENT1();
2128+
current_sum[0] += m_motor_1.m_currents_adc[0];
21302129
voltage_sum[0] += ADC_VOLTS(ADC_IND_SENS1);
21312130
#ifdef HW_HAS_DUAL_MOTORS
2132-
current_sum_m2[0] += GET_CURRENT1_M2();
2131+
current_sum_m2[0] += m_motor_2.m_currents_adc[0];
21332132
voltage_sum_m2[0] += ADC_VOLTS(ADC_IND_SENS4);
21342133
#endif
21352134
chThdSleep(1);
@@ -2155,10 +2154,10 @@ int mcpwm_foc_dc_cal(bool cal_undriven) {
21552154
chThdSleep(1);
21562155

21572156
for (float i = 0;i < samples;i++) {
2158-
current_sum[1] += GET_CURRENT2();
2157+
current_sum[1] += m_motor_1.m_currents_adc[1];
21592158
voltage_sum[1] += ADC_VOLTS(ADC_IND_SENS2);
21602159
#ifdef HW_HAS_DUAL_MOTORS
2161-
current_sum_m2[1] += GET_CURRENT2_M2();
2160+
current_sum_m2[1] += m_motor_2.m_currents_adc[1];
21622161
voltage_sum_m2[1] += ADC_VOLTS(ADC_IND_SENS5);
21632162
#endif
21642163
chThdSleep(1);
@@ -2184,10 +2183,10 @@ int mcpwm_foc_dc_cal(bool cal_undriven) {
21842183
chThdSleep(1);
21852184

21862185
for (float i = 0;i < samples;i++) {
2187-
current_sum[2] += GET_CURRENT3();
2186+
current_sum[2] += m_motor_1.m_currents_adc[2];
21882187
voltage_sum[2] += ADC_VOLTS(ADC_IND_SENS3);
21892188
#ifdef HW_HAS_DUAL_MOTORS
2190-
current_sum_m2[2] += GET_CURRENT3_M2();
2189+
current_sum_m2[2] += m_motor_2.m_currents_adc[2];
21912190
voltage_sum_m2[2] += ADC_VOLTS(ADC_IND_SENS6);
21922191
#endif
21932192
chThdSleep(1);
@@ -2394,8 +2393,8 @@ void mcpwm_foc_adc_int_handler(void *p, uint32_t flags) {
23942393
#endif
23952394

23962395
#ifdef HW_HAS_DUAL_MOTORS
2397-
int curr0 = 0;
2398-
int curr1 = 0;
2396+
float curr0 = 0;
2397+
float curr1 = 0;
23992398

24002399
if (is_second_motor) {
24012400
curr0 = GET_CURRENT1_M2();
@@ -2424,6 +2423,14 @@ void mcpwm_foc_adc_int_handler(void *p, uint32_t flags) {
24242423
#endif
24252424
#endif
24262425

2426+
motor_now->m_currents_adc[0] = curr0;
2427+
motor_now->m_currents_adc[1] = curr1;
2428+
#ifdef HW_HAS_3_SHUNTS
2429+
motor_now->m_currents_adc[2] = curr2;
2430+
#else
2431+
motor_now->m_currents_adc[2] = 0.0;
2432+
#endif
2433+
24272434
curr0 -= conf_now->foc_offsets_current[0];
24282435
curr1 -= conf_now->foc_offsets_current[1];
24292436
#ifdef HW_HAS_3_SHUNTS

terminal.c

-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ void terminal_process_string(char *str) {
537537
} else {
538538
commands_printf("DC Cal Failed: %d\n", res);
539539
}
540-
541540
} else if (strcmp(argv[0], "hw_status") == 0) {
542541
commands_printf("Firmware: %d.%d", FW_VERSION_MAJOR, FW_VERSION_MINOR);
543542
#ifdef HW_NAME

0 commit comments

Comments
 (0)