From 0c5ebbc0d55aed4f95190524f9860b250aa77e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hr=C3=A1zk=C3=BD?= Date: Mon, 2 Sep 2024 23:16:16 +0200 Subject: [PATCH] Sticky brake Feature: Implement sticky brake > Sticky brake will use strong, phase-shorting brake at standstill and untill a certain ERPM is crossed (brake is disarmed). After that, regular braking current will be used until the strong brake is armed again by crossing the arm threshold (arm and disarm thresholds are potential new configuration values). --- src/conf/datatypes.h | 2 ++ src/conf/settings.xml | 44 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 28 +++++++++++++++------------ src/motor_data.c | 2 ++ src/motor_data.h | 1 + 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/conf/datatypes.h b/src/conf/datatypes.h index 30b654d..362cfec 100644 --- a/src/conf/datatypes.h +++ b/src/conf/datatypes.h @@ -205,6 +205,8 @@ typedef struct { bool startup_pushstart_enabled; bool startup_dirtylandings_enabled; float brake_current; + uint16_t brake_arm_threshold; + uint16_t brake_disarm_threshold; float ki_limit; float booster_angle; float booster_ramp; diff --git a/src/conf/settings.xml b/src/conf/settings.xml index d53f860..e79130c 100644 --- a/src/conf/settings.xml +++ b/src/conf/settings.xml @@ -1309,6 +1309,46 @@ p, li { white-space: pre-wrap; } A 7 + + Brake Arm Threshold + 2 + 1 + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Roboto'; ; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">ERPM threshold below which the brake current is armed, meaning the board will passively lock the wheel. Set to 0 to always arm if ERPM is below the Brake Disarm Threshold.</p></body></html> + CFG_DFLT_BRAKE_ARM_THRESHOLD + 1 + 0 + 2000 + 0 + 0 + 20 + 50 + ERPM + 3 + + + Brake Disarm Threshold + 2 + 1 + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Roboto'; ; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">ERPM threshold above which the board will not passively lock the wheel and apply the braking current instead.</p></body></html> + CFG_DFLT_BRAKE_DISARM_THRESHOLD + 1 + 0 + 2000 + 0 + 0 + 100 + 500 + ERPM + 3 + I Term Limit 1 @@ -3475,6 +3515,8 @@ p, li { white-space: pre-wrap; } startup_pushstart_enabled startup_dirtylandings_enabled brake_current + brake_arm_threshold + brake_disarm_threshold ki_limit booster_angle booster_ramp @@ -3656,6 +3698,8 @@ p, li { white-space: pre-wrap; } startup_dirtylandings_enabled ::sep::Holding brake_current + brake_arm_threshold + brake_disarm_threshold diff --git a/src/main.c b/src/main.c index 4a65b91..b14e96c 100644 --- a/src/main.c +++ b/src/main.c @@ -87,6 +87,8 @@ typedef struct { int beep_reason; bool beeper_enabled; + bool braking; + Leds leds; // Lights Control Module - external lights control @@ -1043,7 +1045,7 @@ static void apply_turntilt(data *d) { static void brake(data *d) { // Brake timeout logic float brake_timeout_length = 1; // Brake Timeout hard-coded to 1s - if (d->motor.abs_erpm > ERPM_MOVING_THRESHOLD || d->brake_timeout == 0) { + if (d->motor.erpm_smooth > ERPM_MOVING_THRESHOLD || d->brake_timeout == 0) { d->brake_timeout = d->current_time + brake_timeout_length; } @@ -1053,21 +1055,23 @@ static void brake(data *d) { VESC_IF->timeout_reset(); - // If brake current is set to 0 don't do anything - if (d->float_conf.brake_current == 0) { - return; + if (d->motor.erpm_smooth > d->float_conf.brake_disarm_threshold) { + d->braking = false; + } else if (d->motor.erpm_smooth < d->float_conf.brake_arm_threshold || + d->float_conf.brake_arm_threshold == 0) { + d->braking = true; } - // Use brake current over certain ERPM to prevent the board skidding to a stop when deactivated - // at speed? - if (d->motor.abs_erpm > 2000) { + if (d->braking) { + // Use DC control mode as it has better holding power + // Also improves with 6.05 shorting feature + VESC_IF->mc_set_duty(0); + d->braking = true; + } else { + // Use brake current over certain ERPM to prevent the board + // skidding to a stop when deactivated at speed VESC_IF->mc_set_brake_current(d->float_conf.brake_current); - return; } - - // Use DC control mode as it has better holding power - // Also improves with 6.05 shorting feature - VESC_IF->mc_set_duty(0); } static void set_current(data *d, float current) { diff --git a/src/motor_data.c b/src/motor_data.c index 41bdc44..e337508 100644 --- a/src/motor_data.c +++ b/src/motor_data.c @@ -25,6 +25,7 @@ void motor_data_reset(MotorData *m) { m->duty_smooth = 0; + m->erpm_smooth = 0; m->acceleration = 0; m->accel_idx = 0; @@ -47,6 +48,7 @@ void motor_data_configure(MotorData *m, float frequency) { void motor_data_update(MotorData *m) { m->erpm = VESC_IF->mc_get_rpm(); m->abs_erpm = fabsf(m->erpm); + m->erpm_smooth = m->erpm_smooth * 0.9 + m->abs_erpm * 0.1; m->erpm_sign = sign(m->erpm); m->current = VESC_IF->mc_get_tot_current_directional_filtered(); diff --git a/src/motor_data.h b/src/motor_data.h index 7901bd2..4c2aa9f 100644 --- a/src/motor_data.h +++ b/src/motor_data.h @@ -28,6 +28,7 @@ typedef struct { float erpm; float abs_erpm; float last_erpm; + float erpm_smooth; int8_t erpm_sign; float current;