Skip to content

Commit c0f75f1

Browse files
committed
Current ramping during resistance measurement
1 parent 450ed15 commit c0f75f1

39 files changed

+31
-24
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Some ISR optimization.
1414
* Updated ChibiOS to version 3.0.5.
1515
* Increased USB thread priority to avoid freeze during connect/disconnect on windows.
16+
* Smooth current ramping during resistance measurement.
1617

1718
=== FW 4.02 ===
1819
* Position PID fix (most notable on multiturn encoders).

build_all/100_250/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

build_all/46_o_47/VESC_0005ohm.bin

0 Bytes
Binary file not shown.

build_all/46_o_47/VESC_33k.bin

0 Bytes
Binary file not shown.

build_all/46_o_47/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/48/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/60/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/60_MK3/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/75_300/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/75_300_R2/VESC_default.bin

0 Bytes
Binary file not shown.
Binary file not shown.

build_all/A200S_V21/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/A200S_V22/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/AXIOM/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/DAS_RS/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/HD/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/LUNA_BBSHD/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

build_all/UAVC_OMEGA/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/UNITY/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

conf_general.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ static bool measure_r_l_imax(float current_min, float current_max,
11141114

11151115
float i_last = 0.0;
11161116
for (float i = current_start;i < current_max;i *= 1.5) {
1117-
float res_tmp = mcpwm_foc_measure_resistance(i, 5);
1117+
float res_tmp = mcpwm_foc_measure_resistance(i, 5, false);
11181118
i_last = i;
11191119

11201120
if (mc_interface_get_fault() != FAULT_CODE_NONE) {
@@ -1127,7 +1127,7 @@ static bool measure_r_l_imax(float current_min, float current_max,
11271127
}
11281128
}
11291129

1130-
*r = mcpwm_foc_measure_resistance(i_last, 100);
1130+
*r = mcpwm_foc_measure_resistance(i_last, 100, true);
11311131

11321132
mcconf->foc_motor_r = *r;
11331133
mc_interface_set_configuration(mcconf);

conf_general.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@
131131
//#define HW_SOURCE "hw_100_250.c"
132132
//#define HW_HEADER "hw_100_250.h"
133133

134-
//#define HW_SOURCE "hw_unity.c"
135-
//#define HW_HEADER "hw_unity.h"
134+
#define HW_SOURCE "hw_unity.c"
135+
#define HW_HEADER "hw_unity.h"
136136

137137
//#define HW_DUAL_CONFIG_PARALLEL
138138
//#define HW_SOURCE "hw_stormcore_100d.c"
139139
//#define HW_HEADER "hw_stormcore_100d.h"
140140

141141
//#define HW_SOURCE "hw_stormcore_60d.c"
142142
//#define HW_HEADER "hw_stormcore_60d.h"
143-
144-
#define HW_SOURCE "hw_stormcore_100s.c"
145-
#define HW_HEADER "hw_stormcore_100s.h"
143+
//
144+
//#define HW_SOURCE "hw_stormcore_100s.c"
145+
//#define HW_HEADER "hw_stormcore_100s.h"
146146
#endif
147147

148148
#ifndef HW_SOURCE

mcpwm_foc.c

+21-15
Original file line numberDiff line numberDiff line change
@@ -1637,25 +1637,30 @@ void mcpwm_foc_encoder_detect(float current, bool print, float *offset, float *r
16371637
* @param samples
16381638
* The number of samples to take.
16391639
*
1640+
* @param stop_after
1641+
* Stop motor after finishing the measurement. Otherwise, the current will
1642+
* still be applied after returning. Setting this to false is useful if you want
1643+
* to run this function again right away, without stopping the motor in between.
1644+
*
16401645
* @return
16411646
* The calculated motor resistance.
16421647
*/
1643-
float mcpwm_foc_measure_resistance(float current, int samples) {
1648+
float mcpwm_foc_measure_resistance(float current, int samples, bool stop_after) {
16441649
mc_interface_lock();
16451650

16461651
volatile motor_all_state_t *motor = motor_now();
16471652

16481653
motor->m_phase_override = true;
16491654
motor->m_phase_now_override = 0.0;
16501655
motor->m_id_set = 0.0;
1651-
for(int i = 0;i < 500;i++) {
1652-
motor->m_iq_set = (current * i) / 500.0;
1653-
chThdSleepMilliseconds(1);
1654-
}
1655-
motor->m_iq_set = current;
16561656
motor->m_control_mode = CONTROL_MODE_CURRENT;
16571657
motor->m_state = MC_STATE_RUNNING;
16581658

1659+
while (motor->m_iq_set != current) {
1660+
utils_step_towards((float*)&motor->m_iq_set, current, fabsf(current) / 500.0);
1661+
chThdSleepMilliseconds(1);
1662+
}
1663+
16591664
// Disable timeout
16601665
systime_t tout = timeout_get_timeout_msec();
16611666
float tout_c = timeout_get_brake_current();
@@ -1698,16 +1703,17 @@ float mcpwm_foc_measure_resistance(float current, int samples) {
16981703
const float voltage_avg = motor->m_samples.avg_voltage_tot / (float)motor->m_samples.sample_num;
16991704

17001705
// Stop
1701-
motor->m_id_set = 0.0;
1702-
motor->m_iq_set = 0.0;
1703-
motor->m_phase_override = false;
1704-
motor->m_control_mode = CONTROL_MODE_NONE;
1705-
motor->m_state = MC_STATE_OFF;
1706-
stop_pwm_hw(motor);
1706+
if (stop_after) {
1707+
motor->m_id_set = 0.0;
1708+
motor->m_iq_set = 0.0;
1709+
motor->m_phase_override = false;
1710+
motor->m_control_mode = CONTROL_MODE_NONE;
1711+
motor->m_state = MC_STATE_OFF;
1712+
stop_pwm_hw(motor);
1713+
}
17071714

17081715
// Enable timeout
17091716
timeout_configure(tout, tout_c);
1710-
17111717
mc_interface_unlock();
17121718

17131719
return (voltage_avg / current_avg) * (2.0 / 3.0);
@@ -1902,7 +1908,7 @@ bool mcpwm_foc_measure_res_ind(float *res, float *ind) {
19021908

19031909
float i_last = 0.0;
19041910
for (float i = 2.0;i < (motor->m_conf->l_current_max / 2.0);i *= 1.5) {
1905-
if (i > (1.0 / mcpwm_foc_measure_resistance(i, 20))) {
1911+
if (i > (1.0 / mcpwm_foc_measure_resistance(i, 20, false))) {
19061912
i_last = i;
19071913
break;
19081914
}
@@ -1916,7 +1922,7 @@ bool mcpwm_foc_measure_res_ind(float *res, float *ind) {
19161922
i_last = (motor->m_conf->l_current_max / 2.0);
19171923
#endif
19181924

1919-
*res = mcpwm_foc_measure_resistance(i_last, 200);
1925+
*res = mcpwm_foc_measure_resistance(i_last, 200, true);
19201926
motor->m_conf->foc_motor_r = *res;
19211927
*ind = mcpwm_foc_measure_inductance_current(i_last, 200, 0, 0);
19221928

mcpwm_foc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ float mcpwm_foc_get_phase_encoder(void);
7272
float mcpwm_foc_get_vd(void);
7373
float mcpwm_foc_get_vq(void);
7474
void mcpwm_foc_encoder_detect(float current, bool print, float *offset, float *ratio, bool *inverted);
75-
float mcpwm_foc_measure_resistance(float current, int samples);
75+
float mcpwm_foc_measure_resistance(float current, int samples, bool stop_after);
7676
float mcpwm_foc_measure_inductance(float duty, int samples, float *curr, float *ld_lq_diff);
7777
float mcpwm_foc_measure_inductance_current(float curr_goal, int samples, float *curr, float *ld_lq_diff);
7878
bool mcpwm_foc_measure_res_ind(float *res, float *ind);

terminal.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void terminal_process_string(char *str) {
312312
mcconf->motor_type = MOTOR_TYPE_FOC;
313313
mc_interface_set_configuration(mcconf);
314314

315-
commands_printf("Resistance: %.6f ohm\n", (double)mcpwm_foc_measure_resistance(current, 2000));
315+
commands_printf("Resistance: %.6f ohm\n", (double)mcpwm_foc_measure_resistance(current, 2000, true));
316316

317317
mc_interface_set_configuration(mcconf_old);
318318
} else {

0 commit comments

Comments
 (0)