Skip to content

Commit

Permalink
Sticky brake
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lukash committed Sep 5, 2024
1 parent 25f6320 commit 5fa87b6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/conf/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions src/conf/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,46 @@ p, li { white-space: pre-wrap; }
<suffix> A</suffix>
<vTx>7</vTx>
</brake_current>
<brake_arm_threshold>
<longName>Brake Arm Threshold</longName>
<type>2</type>
<transmittable>1</transmittable>
<description>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Roboto'; ; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;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.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</description>
<cDefine>CFG_DFLT_BRAKE_ARM_THRESHOLD</cDefine>
<editorScale>1</editorScale>
<editAsPercentage>0</editAsPercentage>
<maxInt>2000</maxInt>
<minInt>0</minInt>
<showDisplay>0</showDisplay>
<stepInt>20</stepInt>
<valInt>50</valInt>
<suffix> ERPM</suffix>
<vTx>3</vTx>
</brake_arm_threshold>
<brake_disarm_threshold>
<longName>Brake Disarm Threshold</longName>
<type>2</type>
<transmittable>1</transmittable>
<description>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Roboto'; ; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;ERPM threshold above which the board will not passively lock the wheel and apply the braking current instead.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</description>
<cDefine>CFG_DFLT_BRAKE_DISARM_THRESHOLD</cDefine>
<editorScale>1</editorScale>
<editAsPercentage>0</editAsPercentage>
<maxInt>2000</maxInt>
<minInt>0</minInt>
<showDisplay>0</showDisplay>
<stepInt>100</stepInt>
<valInt>500</valInt>
<suffix> ERPM</suffix>
<vTx>3</vTx>
</brake_disarm_threshold>
<ki_limit>
<longName>I Term Limit</longName>
<type>1</type>
Expand Down Expand Up @@ -3475,6 +3515,8 @@ p, li { white-space: pre-wrap; }
<ser>startup_pushstart_enabled</ser>
<ser>startup_dirtylandings_enabled</ser>
<ser>brake_current</ser>
<ser>brake_arm_threshold</ser>
<ser>brake_disarm_threshold</ser>
<ser>ki_limit</ser>
<ser>booster_angle</ser>
<ser>booster_ramp</ser>
Expand Down Expand Up @@ -3656,6 +3698,8 @@ p, li { white-space: pre-wrap; }
<param>startup_dirtylandings_enabled</param>
<param>::sep::Holding</param>
<param>brake_current</param>
<param>brake_arm_threshold</param>
<param>brake_disarm_threshold</param>
</subgroupParams>
</subgroup>
<subgroup>
Expand Down
28 changes: 16 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ typedef struct {
int beep_reason;
bool beeper_enabled;

bool braking;

Leds leds;

// Lights Control Module - external lights control
Expand Down Expand Up @@ -1042,7 +1044,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;
}

Expand All @@ -1052,21 +1054,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) {
Expand Down
2 changes: 2 additions & 0 deletions src/motor_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/motor_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {
float erpm;
float abs_erpm;
float last_erpm;
float erpm_smooth;
int8_t erpm_sign;

float current;
Expand Down

0 comments on commit 5fa87b6

Please sign in to comment.