From e0c7bac9cb0ca8501187becdecfbfde2697f51ea Mon Sep 17 00:00:00 2001 From: Teslafly <2079881+Teslafly@users.noreply.github.com> Date: Wed, 8 Mar 2023 01:45:44 -0600 Subject: [PATCH 1/3] add inductance detection command to lisp --- lispBM/README.md | 20 ++++++++ lispBM/lispif_vesc_extensions.c | 86 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/lispBM/README.md b/lispBM/README.md index 696985ba..28a1911d 100644 --- a/lispBM/README.md +++ b/lispBM/README.md @@ -2705,6 +2705,26 @@ This command is useful to update the configuration before starting the motor as --- +#### conf-measure-ind + +| Platforms | Firmware | +|---|---| +| ESC | 6.02+ | + +```clj +(conf-measure-res target-current optSamples) +``` + +Measure motor inductance with target-current. The optional argument optSamples sets the number of samples to use (default 100). + +returns: ({ld_lq_avg} {ld_lq_diff} {actual_measurement_current} fault-code) + +Can not be used when the motor is running. The measurement current is not gaurenteed to reach the target, and the actual_measurement_current parameter should be used to verify the actual current used. + +Useful for finding the saturation inductance curve of a motor. + +--- + ### EEPROM (Nonvolatile Storage) Up to 128 variables (int32 or float) can be stored in a nonvolatile memory reserved for LispBM. These variables persist between power cycles and configuration changes, but not between firmware updates. Keep in mind that the motor will be stopped briefly when writing them and that they only can be written a limited number of times (about 100 000 writes) before wear on the flash memory starts to become an issue. diff --git a/lispBM/lispif_vesc_extensions.c b/lispBM/lispif_vesc_extensions.c index ac37da54..da26014c 100644 --- a/lispBM/lispif_vesc_extensions.c +++ b/lispBM/lispif_vesc_extensions.c @@ -3008,6 +3008,91 @@ static lbm_value ext_conf_measure_res(lbm_value *args, lbm_uint argn) { return ENC_SYM_TRUE; } +typedef struct { + float current; + int samples; + lbm_cid id; +} measure_ind_args; + +static void measure_inductance_task(void *arg) { + int restart_cnt = lispif_get_restart_cnt(); + + measure_ind_args *a = (measure_ind_args*)arg; + float ld_lq_avg, ld_lq_diff, real_measurement_current = -1.0; + int fault; + + lbm_flat_value_t v; + bool ok = false; + if (lbm_start_flatten(&v, 25)) { + fault = mcpwm_foc_measure_inductance_current(a->current, a->samples, &real_measurement_current, &ld_lq_diff, &ld_lq_avg); + if (restart_cnt != lispif_get_restart_cnt()) { + f_sym(&v, ENC_SYM_EERROR); + lbm_finish_flatten(&v); + return; + } + + if(a->current == 3){ // inject fault sometimes + fault = 2; + } + + if (fault != 0) { + f_i(&v, fault); + } else { + f_cons(&v); + f_float(&v, ld_lq_avg); + f_cons(&v); + f_float(&v, ld_lq_diff); + f_cons(&v); + f_float(&v, real_measurement_current); + f_sym(&v, SYM_NIL); + } + + lbm_finish_flatten(&v); + if (lbm_unblock_ctx(a->id, &v)) { + ok = true; + } else { + lbm_free(v.buf); + } + } + + if (!ok) { + lbm_unblock_ctx_unboxed(a->id, ENC_SYM_NIL); + } +} + +static lbm_value ext_conf_measure_ind(lbm_value *args, lbm_uint argn) { + // measure inductance of motor @ current + // arg0: measurement current + // arg1: sample number. optional + // returns: ({ld_lq_avg} {ld_lq_diff} {actual_measurement_current}) or (fault-code) + if (argn != 1 && argn != 2) { + lbm_set_error_reason((char*)lbm_error_str_num_args); + return ENC_SYM_EERROR; + } + + LBM_CHECK_NUMBER_ALL(); + + if (mc_interface_get_configuration()->motor_type != MOTOR_TYPE_FOC) { + return ENC_SYM_EERROR; + } + + static measure_ind_args a; + a.current = lbm_dec_as_float(args[0]); + a.samples = 100; + if (argn == 2) { + a.samples = lbm_dec_as_u32(args[1]); + } + a.id = lbm_get_current_cid(); + + if (mc_interface_get_configuration()->l_current_max < a.current) { + return ENC_SYM_EERROR; + } + + worker_execute(measure_inductance_task, &a); + lbm_block_ctx_from_extension(); + return ENC_SYM_TRUE; +} + static lbm_value make_list(int num, ...) { va_list arguments; va_start (arguments, num); @@ -3897,6 +3982,7 @@ void lispif_load_vesc_extensions(void) { lbm_add_extension("conf-detect-foc", ext_conf_detect_foc); lbm_add_extension("conf-set-pid-offset", ext_conf_set_pid_offset); lbm_add_extension("conf-measure-res", ext_conf_measure_res); + lbm_add_extension("conf-measure-ind", ext_conf_measure_ind); // Macro expanders lbm_add_extension("me-defun", ext_me_defun); From 4c1a1f6b715183f452f734066ac392005dc9d761 Mon Sep 17 00:00:00 2001 From: Teslafly <2079881+Teslafly@users.noreply.github.com> Date: Wed, 8 Mar 2023 20:50:43 -0600 Subject: [PATCH 2/3] remove debug fault injection --- lispBM/lispif_vesc_extensions.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lispBM/lispif_vesc_extensions.c b/lispBM/lispif_vesc_extensions.c index da26014c..6b49ea80 100644 --- a/lispBM/lispif_vesc_extensions.c +++ b/lispBM/lispif_vesc_extensions.c @@ -3031,10 +3031,6 @@ static void measure_inductance_task(void *arg) { return; } - if(a->current == 3){ // inject fault sometimes - fault = 2; - } - if (fault != 0) { f_i(&v, fault); } else { From 399b0b1e599ffaa89572b5e65daa3df74d0dac64 Mon Sep 17 00:00:00 2001 From: Teslafly <2079881+Teslafly@users.noreply.github.com> Date: Wed, 8 Mar 2023 20:54:19 -0600 Subject: [PATCH 3/3] dont do any lisp things if restart cnt is not same. https://github.com/vedderb/bldc/pull/600#issuecomment-1459733432 --- lispBM/lispif_vesc_extensions.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/lispBM/lispif_vesc_extensions.c b/lispBM/lispif_vesc_extensions.c index 6b49ea80..3e8d3dc0 100644 --- a/lispBM/lispif_vesc_extensions.c +++ b/lispBM/lispif_vesc_extensions.c @@ -3026,8 +3026,6 @@ static void measure_inductance_task(void *arg) { if (lbm_start_flatten(&v, 25)) { fault = mcpwm_foc_measure_inductance_current(a->current, a->samples, &real_measurement_current, &ld_lq_diff, &ld_lq_avg); if (restart_cnt != lispif_get_restart_cnt()) { - f_sym(&v, ENC_SYM_EERROR); - lbm_finish_flatten(&v); return; }