Skip to content

Commit f6e78a0

Browse files
committed
Fixed measured MTPA mode, send ld_lq_diff detection result
1 parent fd48dc2 commit f6e78a0

9 files changed

+39
-21
lines changed

commands.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,8 @@ static THD_FUNCTION(blocking_thread, arg) {
19191919

19201920
float r = 0.0;
19211921
float l = 0.0;
1922-
bool res = mcpwm_foc_measure_res_ind(&r, &l);
1922+
float ld_lq_diff = 0.0;
1923+
bool res = mcpwm_foc_measure_res_ind(&r, &l, &ld_lq_diff);
19231924
mc_interface_set_configuration(mcconf_old);
19241925

19251926
if (!res) {
@@ -1931,6 +1932,7 @@ static THD_FUNCTION(blocking_thread, arg) {
19311932
send_buffer[ind++] = COMM_DETECT_MOTOR_R_L;
19321933
buffer_append_float32(send_buffer, r, 1e6, &ind);
19331934
buffer_append_float32(send_buffer, l, 1e3, &ind);
1935+
buffer_append_float32(send_buffer, ld_lq_diff, 1e3, &ind);
19341936
if (send_func_blocking) {
19351937
send_func_blocking(send_buffer, ind);
19361938
}

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 65
27+
#define FW_TEST_VERSION_NUMBER 68
2828

2929
#include "datatypes.h"
3030

confgenerator.c

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ int32_t confgenerator_serialize_mcconf(uint8_t *buffer, const mc_configuration *
176176
buffer[ind++] = conf->si_battery_type;
177177
buffer[ind++] = (uint8_t)conf->si_battery_cells;
178178
buffer_append_float32_auto(buffer, conf->si_battery_ah, &ind);
179+
buffer_append_float32_auto(buffer, conf->si_motor_nl_current, &ind);
179180
buffer[ind++] = conf->bms.type;
180181
buffer_append_float16(buffer, conf->bms.t_limit_start, 100, &ind);
181182
buffer_append_float16(buffer, conf->bms.t_limit_end, 100, &ind);
@@ -542,6 +543,7 @@ bool confgenerator_deserialize_mcconf(const uint8_t *buffer, mc_configuration *c
542543
conf->si_battery_type = buffer[ind++];
543544
conf->si_battery_cells = buffer[ind++];
544545
conf->si_battery_ah = buffer_get_float32_auto(buffer, &ind);
546+
conf->si_motor_nl_current = buffer_get_float32_auto(buffer, &ind);
545547
conf->bms.type = buffer[ind++];
546548
conf->bms.t_limit_start = buffer_get_float16(buffer, 100, &ind);
547549
conf->bms.t_limit_end = buffer_get_float16(buffer, 100, &ind);
@@ -904,6 +906,7 @@ void confgenerator_set_defaults_mcconf(mc_configuration *conf) {
904906
conf->si_battery_type = MCCONF_SI_BATTERY_TYPE;
905907
conf->si_battery_cells = MCCONF_SI_BATTERY_CELLS;
906908
conf->si_battery_ah = MCCONF_SI_BATTERY_AH;
909+
conf->si_motor_nl_current = MCCONF_SI_MOTOR_NL_CURRENT;
907910
conf->bms.type = MCCONF_BMS_TYPE;
908911
conf->bms.t_limit_start = MCCONF_BMS_T_LIMIT_START;
909912
conf->bms.t_limit_end = MCCONF_BMS_T_LIMIT_END;

confgenerator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <stdbool.h>
99

1010
// Constants
11-
#define MCCONF_SIGNATURE 526273576
11+
#define MCCONF_SIGNATURE 2686986464
1212
#define APPCONF_SIGNATURE 763356168
1313

1414
// Functions

datatypes.h

+1
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ typedef struct {
486486
BATTERY_TYPE si_battery_type;
487487
int si_battery_cells;
488488
float si_battery_ah;
489+
float si_motor_nl_current;
489490

490491
// BMS Configuration
491492
bms_config bms;

mcconf/mcconf_default.h

+3
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@
544544
#ifndef MCCONF_SI_BATTERY_AH
545545
#define MCCONF_SI_BATTERY_AH 6.0 // Battery amp hours
546546
#endif
547+
#ifndef MCCONF_SI_MOTOR_NL_CURRENT
548+
#define MCCONF_SI_MOTOR_NL_CURRENT 1.0 // Motor no load current
549+
#endif
547550

548551
// BMS
549552
#ifndef MCCONF_BMS_TYPE

mcpwm_foc.c

+23-15
Original file line numberDiff line numberDiff line change
@@ -1983,10 +1983,13 @@ float mcpwm_foc_measure_inductance_current(float curr_goal, int samples, float *
19831983
* @param ind
19841984
* The measured inductance in microhenry.
19851985
*
1986+
* @param ld_lq_diff
1987+
* The measured difference in D axis and Q axis inductance.
1988+
*
19861989
* @return
19871990
* True if the measurement succeeded, false otherwise.
19881991
*/
1989-
bool mcpwm_foc_measure_res_ind(float *res, float *ind) {
1992+
bool mcpwm_foc_measure_res_ind(float *res, float *ind, float *ld_lq_diff) {
19901993
volatile motor_all_state_t *motor = motor_now();
19911994

19921995
const float f_sw_old = motor->m_conf->foc_f_sw;
@@ -2015,7 +2018,7 @@ bool mcpwm_foc_measure_res_ind(float *res, float *ind) {
20152018

20162019
*res = mcpwm_foc_measure_resistance(i_last, 200, true);
20172020
motor->m_conf->foc_motor_r = *res;
2018-
*ind = mcpwm_foc_measure_inductance_current(i_last, 200, 0, 0);
2021+
*ind = mcpwm_foc_measure_inductance_current(i_last, 200, 0, ld_lq_diff);
20192022

20202023
motor->m_conf->foc_f_sw = f_sw_old;
20212024
motor->m_conf->foc_current_kp = kp_old;
@@ -2859,7 +2862,7 @@ void mcpwm_foc_adc_int_handler(void *p, uint32_t flags) {
28592862

28602863
float iq_ref = iq_set_tmp;
28612864
if (conf_now->foc_mtpa_mode == MTPA_MODE_IQ_MEASURED) {
2862-
iq_ref = motor_now->m_motor_state.iq_filter;
2865+
iq_ref = utils_min_abs(iq_set_tmp, motor_now->m_motor_state.iq_filter);
28632866
}
28642867

28652868
id_set_tmp = (lambda - sqrtf(SQ(lambda) + 8.0 * SQ(ld_lq_diff) * SQ(iq_ref))) / (4.0 * ld_lq_diff);
@@ -3715,7 +3718,9 @@ static void control_current(volatile motor_all_state_t *motor, float dt) {
37153718
// Park transform: transforms the currents from stator to the rotor reference frame
37163719
state_m->id = c * state_m->i_alpha + s * state_m->i_beta;
37173720
state_m->iq = c * state_m->i_beta - s * state_m->i_alpha;
3718-
UTILS_LP_FAST(state_m->id_filter, state_m->id, conf_now->foc_current_filter_const); //Low passed currents are used for less time critical parts, not for the feedback
3721+
3722+
// Low passed currents are used for less time critical parts, not for the feedback
3723+
UTILS_LP_FAST(state_m->id_filter, state_m->id, conf_now->foc_current_filter_const);
37193724
UTILS_LP_FAST(state_m->iq_filter, state_m->iq, conf_now->foc_current_filter_const);
37203725

37213726
float d_gain_scale = 1.0;
@@ -3749,10 +3754,11 @@ static void control_current(volatile motor_all_state_t *motor, float dt) {
37493754
state_m->vd_int += Ierr_d * (ki * d_gain_scale * dt);
37503755
state_m->vq_int += Ierr_q * (ki * dt);
37513756

3752-
// Decoupling. Using feedforward this compensates for the fact that the equations of a PMSM are not really decoupled (the d axis current has impact on q axis voltage and visa-versa):
3753-
// Resistance Inductance Cross terms Back-EMF (see www.mathworks.com/help/physmod/sps/ref/pmsm.html)
3754-
//vd = Rs*id + Ld*did/dt − ωe*iq*Lq
3755-
//vq = Rs*iq + Lq*diq/dt + ωe*id*Ld + ωe*ψm
3757+
// Decoupling. Using feedforward this compensates for the fact that the equations of a PMSM
3758+
// are not really decoupled (the d axis current has impact on q axis voltage and visa-versa):
3759+
// Resistance Inductance Cross terms Back-EMF (see www.mathworks.com/help/physmod/sps/ref/pmsm.html)
3760+
// vd = Rs*id + Ld*did/dt − ωe*iq*Lq
3761+
// vq = Rs*iq + Lq*diq/dt + ωe*id*Ld + ωe*ψm
37563762
float dec_vd = 0.0;
37573763
float dec_vq = 0.0;
37583764
float dec_bemf = 0.0;
@@ -3763,7 +3769,7 @@ static void control_current(volatile motor_all_state_t *motor, float dt) {
37633769

37643770
switch (conf_now->foc_cc_decoupling) {
37653771
case FOC_CC_DECOUPLING_CROSS:
3766-
dec_vd = state_m->iq_filter * motor->m_speed_est_fast * lq; //m_speed_est_fast is ωe in [rad/s]
3772+
dec_vd = state_m->iq_filter * motor->m_speed_est_fast * lq; // m_speed_est_fast is ωe in [rad/s]
37673773
dec_vq = state_m->id_filter * motor->m_speed_est_fast * ld;
37683774
break;
37693775

@@ -3785,7 +3791,8 @@ static void control_current(volatile motor_all_state_t *motor, float dt) {
37853791
state_m->vd -= dec_vd; //Negative sign as in the PMSM equations
37863792
state_m->vq += dec_vq + dec_bemf;
37873793

3788-
//Calculate the max length of the voltage space vector without overmodulation. Is simply 1/sqrt(3) * v_bus. See https://microchipdeveloper.com/mct5001:start. Adds margin with max_duty.
3794+
// Calculate the max length of the voltage space vector without overmodulation.
3795+
// Is simply 1/sqrt(3) * v_bus. See https://microchipdeveloper.com/mct5001:start. Adds margin with max_duty.
37893796
float max_v_mag = ONE_BY_SQRT3 * max_duty * state_m->v_bus;
37903797

37913798
// Saturation and anti-windup. Notice that the d-axis has priority as it controls field
@@ -3905,7 +3912,8 @@ static void control_current(volatile motor_all_state_t *motor, float dt) {
39053912
uint32_t duty1, duty2, duty3, top;
39063913
top = TIM1->ARR;
39073914

3908-
// Calculate the duty cycles for all the phases. This also injects a zero modulation signal to be able to fully utilize the bus voltage. See https://microchipdeveloper.com/mct5001:start.
3915+
// Calculate the duty cycles for all the phases. This also injects a zero modulation signal to
3916+
// be able to fully utilize the bus voltage. See https://microchipdeveloper.com/mct5001:start
39093917
svm(mod_alpha, mod_beta, top, &duty1, &duty2, &duty3, (uint32_t*)&state_m->svm_sector);
39103918

39113919
if (motor == &m_motor_1) {
@@ -3980,8 +3988,8 @@ static void update_valpha_vbeta(volatile motor_all_state_t *motor, float mod_alp
39803988
const float ib_filter = -0.5 * i_alpha_filter + SQRT3_BY_2 * i_beta_filter;
39813989
const float ic_filter = -0.5 * i_alpha_filter - SQRT3_BY_2 * i_beta_filter;
39823990

3983-
/* mod_alpha_sign = 2/3*sign(ia) - 1/3*sign(ib) - 1/3*sign(ic) */
3984-
/* mod_beta_sign = 1/sqrt(3)*sign(ib) - 1/sqrt(3)*sign(ic) */
3991+
// mod_alpha_sign = 2/3*sign(ia) - 1/3*sign(ib) - 1/3*sign(ic)
3992+
// mod_beta_sign = 1/sqrt(3)*sign(ib) - 1/sqrt(3)*sign(ic)
39853993
const float mod_alpha_filter_sgn = (1.0 / 3.0) * (2.0 * SIGN(ia_filter) - SIGN(ib_filter) - SIGN(ic_filter));
39863994
const float mod_beta_filter_sgn = ONE_BY_SQRT3 * (SIGN(ib_filter) - SIGN(ic_filter));
39873995

@@ -3998,8 +4006,8 @@ static void update_valpha_vbeta(volatile motor_all_state_t *motor, float mod_alp
39984006
state_m->mod_alpha_measured = mod_alpha;
39994007
state_m->mod_beta_measured = mod_beta;
40004008

4001-
/* v_alpha = 2/3*Va - 1/3*Vb - 1/3*Vc */
4002-
/* v_beta = 1/sqrt(3)*Vb - 1/sqrt(3)*Vc */
4009+
// v_alpha = 2/3*Va - 1/3*Vb - 1/3*Vc
4010+
// v_beta = 1/sqrt(3)*Vb - 1/sqrt(3)*Vc
40034011
float v_alpha = (1.0 / 3.0) * (2.0 * Va - Vb - Vc);
40044012
float v_beta = ONE_BY_SQRT3 * (Vb - Vc);
40054013

mcpwm_foc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void mcpwm_foc_encoder_detect(float current, bool print, float *offset, float *r
7777
float mcpwm_foc_measure_resistance(float current, int samples, bool stop_after);
7878
float mcpwm_foc_measure_inductance(float duty, int samples, float *curr, float *ld_lq_diff);
7979
float mcpwm_foc_measure_inductance_current(float curr_goal, int samples, float *curr, float *ld_lq_diff);
80-
bool mcpwm_foc_measure_res_ind(float *res, float *ind);
80+
bool mcpwm_foc_measure_res_ind(float *res, float *ind, float *ld_lq_diff);
8181
bool mcpwm_foc_hall_detect(float current, uint8_t *hall_table);
8282
int mcpwm_foc_dc_cal(bool cal_undriven);
8383
void mcpwm_foc_print_state(void);

terminal.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,10 @@ void terminal_process_string(char *str) {
422422

423423
float res = 0.0;
424424
float ind = 0.0;
425-
mcpwm_foc_measure_res_ind(&res, &ind);
425+
float ld_lq_diff = 0.0;
426+
mcpwm_foc_measure_res_ind(&res, &ind, &ld_lq_diff);
426427
commands_printf("Resistance: %.6f ohm", (double)res);
427-
commands_printf("Inductance: %.2f microhenry\n", (double)ind);
428+
commands_printf("Inductance: %.2f uH (Lq-Ld: %.2f uH)\n", (double)ind, (double)ld_lq_diff);
428429

429430
mc_interface_set_configuration(mcconf_old);
430431

0 commit comments

Comments
 (0)