Skip to content

Commit ca4d338

Browse files
committed
Rework ATR Tilt Speed conditions
Fix: Rework ATR Tilt Speed conditions: > - Re-organize implementation to avoid repeated logic > - Account for ATR Thresholds in the Boost margin > - Use "On" Tilt Speed for Transition Boost if faster
1 parent ea40338 commit ca4d338

1 file changed

Lines changed: 35 additions & 53 deletions

File tree

src/atr.c

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -133,60 +133,42 @@ void atr_update(ATR *atr, const MotorData *motor, const RefloatConfig *config) {
133133
// We want to react quickly to changes, but we don't want to overreact to glitches in
134134
// acceleration data or trigger oscillations...
135135
float atr_step_size = 0;
136-
const float TT_BOOST_MARGIN = 2;
137-
if (forward) {
138-
if (atr->setpoint < 0) {
139-
// downhill
140-
if (atr->setpoint < atr->target) {
141-
// to avoid oscillations we go down slower than we go up
142-
atr_step_size = atr->off_step_size;
143-
if (atr->target > 0 && atr->target - atr->setpoint > TT_BOOST_MARGIN &&
144-
motor->abs_erpm > 2000) {
145-
// boost the speed if tilt target has reversed (and if there's a significant
146-
// margin)
147-
atr_step_size = atr->off_step_size * config->atr_transition_boost;
148-
}
149-
} else {
150-
// ATR is increasing
151-
atr_step_size = atr->on_step_size * response_boost;
152-
}
153-
} else {
154-
// uphill or other heavy resistance (grass, mud, etc)
155-
if (atr->target > -3 && atr->setpoint > atr->target) {
156-
// ATR winding down (current ATR is bigger than the target)
157-
// normal wind down case: to avoid oscillations we go down slower than we go up
158-
atr_step_size = atr->off_step_size;
159-
} else {
160-
// standard case of increasing ATR
161-
atr_step_size = atr->on_step_size * response_boost;
162-
}
163-
}
136+
137+
float atr_tb_step_size = atr->off_step_size * config->atr_transition_boost;
138+
float atr_on_step_size = atr->on_step_size * response_boost;
139+
140+
// When Transition Boost condition is met, use the highest allowed step size
141+
float atr_transition_step_size = fmaxf(atr_tb_step_size, atr_on_step_size);
142+
143+
// If ATR Thresholds are in place, they should be negated from the Transition Boost margin
144+
// for the effective condition (change in accel_diff needed) to be the same
145+
const float TT_BOOST_UPHILL_MARGIN =
146+
fmaxf(0, 2 - config->atr_threshold_down - config->atr_threshold_up);
147+
const float TT_BOOST_DOWNHILL_THRESHOLD = fmaxf(0, 3 - config->atr_threshold_down);
148+
149+
bool should_transition_boost;
150+
if (atr->target * atr->setpoint >= 0) {
151+
// If Target and Setpoint are of same sign or 0, we are not transitioning
152+
should_transition_boost = false;
153+
} else if (forward ? (atr->setpoint < 0 && atr->target > 0)
154+
: (atr->setpoint > 0 && atr->target < 0)) {
155+
// Transitioning Downhill to Uphill: check margin between target and setpoint
156+
should_transition_boost = fabsf(atr->target - atr->setpoint) >= TT_BOOST_UPHILL_MARGIN;
157+
} else {
158+
// Transitioning Uphill to Downhill: check target magnitude (stricter condition)
159+
should_transition_boost = fabsf(atr->target) >= TT_BOOST_DOWNHILL_THRESHOLD;
160+
}
161+
162+
// Winding Down: Setpoint is moving toward 0
163+
bool is_winding_down = (atr->setpoint < 0 && atr->setpoint < atr->target) ||
164+
(atr->setpoint > 0 && atr->setpoint > atr->target);
165+
166+
if (is_winding_down) {
167+
// ATR is decreasing (boost if transitioning to strong ATR of opposite sign)
168+
atr_step_size = should_transition_boost ? atr_transition_step_size : atr->off_step_size;
164169
} else {
165-
if (atr->setpoint > 0) {
166-
// downhill
167-
if (atr->setpoint > atr->target) {
168-
// to avoid oscillations we go down slower than we go up
169-
atr_step_size = atr->off_step_size;
170-
if (atr->target < 0 && atr->setpoint - atr->target > TT_BOOST_MARGIN &&
171-
motor->abs_erpm > 2000) {
172-
// boost the speed if tilt target has reversed (and if there's a significant
173-
// margin)
174-
atr_step_size = atr->off_step_size * config->atr_transition_boost;
175-
}
176-
} else {
177-
// ATR is increasing
178-
atr_step_size = atr->on_step_size * response_boost;
179-
}
180-
} else {
181-
// uphill or other heavy resistance (grass, mud, etc)
182-
if (atr->target < 3 && atr->setpoint < atr->target) {
183-
// normal wind down case: to avoid oscillations we go down slower than we go up
184-
atr_step_size = atr->off_step_size;
185-
} else {
186-
// standard case of increasing torquetilt
187-
atr_step_size = atr->on_step_size * response_boost;
188-
}
189-
}
170+
// ATR is increasing
171+
atr_step_size = atr->on_step_size * response_boost;
190172
}
191173

192174
if (motor->abs_erpm < 500) {

0 commit comments

Comments
 (0)