Skip to content

Commit dcd92bd

Browse files
committed
ATR: Refactor implementation
No behavior changes, just re-structuring in preparation for further updates.
1 parent 0763285 commit dcd92bd

File tree

1 file changed

+33
-53
lines changed

1 file changed

+33
-53
lines changed

src/atr.c

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -133,60 +133,40 @@ 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+
const float TT_BOOST_UPHILL_MARGIN = 2;
141+
const float TT_BOOST_DOWNHILL_THRESHOLD = 3;
142+
143+
// Winding Down: Setpoint is moving toward 0
144+
bool is_winding_down = (atr->setpoint < 0 && atr->setpoint < atr->target) ||
145+
(atr->setpoint > 0 && atr->setpoint > atr->target);
146+
147+
bool should_transition_boost;
148+
if (atr->target * atr->setpoint >= 0) {
149+
// If Target and Setpoint are of same sign or 0, we are not transitioning
150+
should_transition_boost = false;
151+
} else if (forward ? (atr->setpoint < 0 && atr->target > 0)
152+
: (atr->setpoint > 0 && atr->target < 0)) {
153+
// Transitioning Downhill to Uphill: check margin between target and setpoint,
154+
// with a minimum ERPM threshold
155+
should_transition_boost = (fabsf(atr->target - atr->setpoint) > TT_BOOST_UPHILL_MARGIN) &&
156+
(motor->abs_erpm > 2000);
157+
} else {
158+
// Transitioning Uphill to Downhill: check target magnitude (stricter condition);
159+
// Instead of transition boost, use on_step_size (typically faster than off_step_size)
160+
should_transition_boost = false;
161+
is_winding_down = fabsf(atr->target) < TT_BOOST_DOWNHILL_THRESHOLD;
162+
}
163+
164+
if (is_winding_down) {
165+
// ATR is decreasing (boost if transitioning to strong Uphill ATR)
166+
atr_step_size = should_transition_boost ? atr_tb_step_size : atr->off_step_size;
164167
} 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-
}
168+
// ATR is increasing (or transitioning quickly from Uphill to Downhill ATR)
169+
atr_step_size = atr_on_step_size;
190170
}
191171

192172
if (motor->abs_erpm < 500) {

0 commit comments

Comments
 (0)