Skip to content

Commit 7e024f2

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

File tree

1 file changed

+42
-50
lines changed

1 file changed

+42
-50
lines changed

src/atr.c

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -133,62 +133,54 @@ 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;
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+
bool should_transition_boost;
144+
if (atr->target * atr->setpoint >= 0) {
145+
// If Target and Setpoint are of same sign or 0, we are not transitioning
146+
should_transition_boost = false;
147+
} else if (forward ? (atr->setpoint < 0 && atr->target > 0)
148+
: (atr->setpoint > 0 && atr->target < 0)) {
149+
// Transitioning Downhill to Uphill: check margin between target and setpoint,
150+
// with a minimum ERPM threshold
151+
should_transition_boost = (fabsf(atr->target - atr->setpoint) > TT_BOOST_UPHILL_MARGIN) &&
152+
(motor->abs_erpm > 2000);
153+
} else {
154+
// Transitioning Uphill to Downhill: no transition boost
155+
should_transition_boost = false;
156+
}
157+
158+
// Winding Down: Setpoint is moving toward 0
159+
bool is_winding_down = (atr->setpoint < 0 && atr->setpoint < atr->target) ||
160+
(atr->setpoint > 0 && atr->setpoint > atr->target);
161+
162+
// Special Case: When transitioning from any Uphill to Strong Downhill,
163+
// we will use on_step_size (typically faster)
137164
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-
}
165+
if (atr->setpoint >= 0) {
166+
is_winding_down =
167+
(atr->target > -TT_BOOST_DOWNHILL_THRESHOLD) && (atr->setpoint > atr->target);
163168
}
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-
}
170+
if (atr->setpoint <= 0) {
171+
is_winding_down =
172+
(atr->target < TT_BOOST_DOWNHILL_THRESHOLD) && (atr->setpoint < atr->target);
189173
}
190174
}
191175

176+
if (is_winding_down) {
177+
// ATR is decreasing (boost if transitioning to strong Uphill ATR)
178+
atr_step_size = should_transition_boost ? atr_tb_step_size : atr->off_step_size;
179+
} else {
180+
// ATR is increasing (or transitioning quickly from Uphill to Downhill ATR)
181+
atr_step_size = atr->on_step_size * response_boost;
182+
}
183+
192184
if (motor->abs_erpm < 500) {
193185
atr_step_size /= 2;
194186
}

0 commit comments

Comments
 (0)