Skip to content

Commit 1746b29

Browse files
surfdadolukash
authored andcommitted
Smoothened ATR ramping (like inputtilt ramping)
Following the example of inputtilt, ATR setpoints are now ramped slower when near the current setpoint. This allows for much higher ramping speeds without causing a jerking effect. Instead of using 5deg/sec and 3deg/sec one can now easily run 20 and 12 or even more without any perceived downsides. Feature: Smoothened/faster setpoint ramping for ATR Signed-off-by: Dado Mista <dadomista@gmail.com>
1 parent ff12ffc commit 1746b29

8 files changed

Lines changed: 64 additions & 3 deletions

File tree

src/atr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void atr_reset(ATR *atr) {
2727
atr->speed_boost = 0;
2828
atr->target = 0;
2929
atr->setpoint = 0;
30+
atr->ramped_step_size = 0;
3031

3132
smooth_target_reset(&atr->smooth_target, 0.0f);
3233
ema_filter_reset(&atr->ema_target, 0.0f, 0.0f);
@@ -203,9 +204,14 @@ void atr_update(ATR *atr, const MotorData *motor, const RefloatConfig *config, f
203204
} else if (config->target_filter.type == SFT_EMA3) {
204205
ema_filter_update(&atr->ema_target, atr->target, dt);
205206
atr->setpoint = atr->ema_target.value;
206-
} else {
207+
} else if (config->target_filter.type == SFT_THREE_STAGE) {
207208
smooth_target_update(&atr->smooth_target, atr->target);
208209
atr->setpoint = atr->smooth_target.value;
210+
} else {
211+
// Smoothen changes in tilt angle by ramping the step size
212+
smooth_rampf(
213+
&atr->setpoint, &atr->ramped_step_size, atr->target, atr_step_size, 0.05, 1.5
214+
);
209215
}
210216
}
211217

src/atr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef struct {
2727
float on_step_size;
2828
float off_step_size;
29+
float ramped_step_size;
2930

3031
float accel_diff;
3132
float speed_boost;

src/conf/datatypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ typedef struct {
182182
typedef enum {
183183
SFT_NONE = 0,
184184
SFT_THREE_STAGE,
185-
SFT_EMA3
185+
SFT_EMA3,
186+
SFT_NICO
186187
} TargetFilterType;
187188

188189
typedef struct {

src/conf/settings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,16 @@ p, li { white-space: pre-wrap; }
248248
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
249249
&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;It's defined by the EMA Half Time config option. You can use the EMA Return Multiplier to speed up transitions returning to zero (e.g. a value of 2 means the returning to zero transitions will be two times faster).&lt;/p&gt;
250250
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
251+
&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;&lt;span style=&quot; font-weight:600;&quot;&gt;Nico's&lt;/span&gt;&lt;/p&gt;
252+
&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;A smoothing filter written by Nico originally for Inputtilt, it was later proposed to be used tor ATR and Torquetilt as well. Although this filter works and is reported to work very well, it has a mathematical flaw, and hence is only included for reference. The other filters are, in fact, an attempt to have correct filter(s) that work the same or better.&lt;/p&gt;
253+
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-weight:600;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
251254
&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;&lt;span style=&quot; font-weight:600;&quot;&gt;Note: The 3-Stage and 3rd Order EMA ignore Tiltback Response Boost and Tiltback Transition Boost for ATR.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</description>
252255
<cDefine>CFG_DFLT_TARGET_FILTER_TYPE</cDefine>
253256
<valInt>1</valInt>
254257
<enumNames>None</enumNames>
255258
<enumNames>3-Stage</enumNames>
256259
<enumNames>3rd Order EMA</enumNames>
260+
<enumNames>Nico's</enumNames>
257261
</target_filter.type>
258262
<target_filter.tt_type>
259263
<longName>Filter Type (config on ATR tab)</longName>
@@ -271,6 +275,7 @@ p, li { white-space: pre-wrap; }
271275
<enumNames>None</enumNames>
272276
<enumNames>3-Stage</enumNames>
273277
<enumNames>3rd Order EMA</enumNames>
278+
<enumNames>Nico's</enumNames>
274279
</target_filter.tt_type>
275280
<target_filter.it_type>
276281
<longName>Filter Type (config on ATR tab)</longName>

src/torque_tilt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
void torque_tilt_reset(TorqueTilt *tt) {
2626
tt->setpoint = 0;
27+
tt->ramped_step_size = 0;
2728

2829
smooth_target_reset(&tt->smooth_target, 0.0f);
2930
ema_filter_reset(&tt->ema_target, 0.0f, 0.0f);
@@ -82,9 +83,12 @@ void torque_tilt_update(
8283
} else if (config->target_filter.tt_type == SFT_EMA3) {
8384
ema_filter_update(&tt->ema_target, target, dt);
8485
tt->setpoint = tt->ema_target.value;
85-
} else {
86+
} else if (config->target_filter.tt_type == SFT_THREE_STAGE) {
8687
smooth_target_update(&tt->smooth_target, target);
8788
tt->setpoint = tt->smooth_target.value;
89+
} else {
90+
// Smoothen changes in tilt angle by ramping the step size
91+
smooth_rampf(&tt->setpoint, &tt->ramped_step_size, target, step_size, 0.04, 1.5);
8892
}
8993
}
9094

src/torque_tilt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef struct {
2727
float on_step_size;
2828
float off_step_size;
29+
float ramped_step_size;
2930

3031
float setpoint;
3132

src/utils.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ void rate_limitf(float *value, float target, float step) {
3333
}
3434
}
3535

36+
// Smoothen changes in tilt angle by ramping the step size
37+
// smooth_center_window: Sets the angle away from Target that step size begins ramping down
38+
void smooth_rampf(
39+
float *value,
40+
float *ramped_step,
41+
float target,
42+
float step,
43+
float smoothing_factor,
44+
float smooth_center_window
45+
) {
46+
float tiltback_target_diff = target - *value;
47+
48+
// Within X degrees of Target Angle, start ramping down step size
49+
if (fabsf(tiltback_target_diff) < smooth_center_window) {
50+
// Target step size is reduced the closer to center you are (needed for smoothly
51+
// transitioning away from center)
52+
*ramped_step = (smoothing_factor * step * (tiltback_target_diff / 2)) +
53+
((1 - smoothing_factor) * *ramped_step);
54+
// Linearly ramped down step size is provided as minimum to prevent overshoot
55+
float centering_step = fminf(fabsf(*ramped_step), fabsf(tiltback_target_diff / 2) * step) *
56+
sign(tiltback_target_diff);
57+
if (fabsf(tiltback_target_diff) < fabsf(centering_step)) {
58+
*value = target;
59+
} else {
60+
*value += centering_step;
61+
}
62+
} else {
63+
// Ramp up step size until the configured tilt speed is reached
64+
*ramped_step = (smoothing_factor * step * sign(tiltback_target_diff)) +
65+
((1 - smoothing_factor) * *ramped_step);
66+
*value += *ramped_step;
67+
}
68+
}
69+
3670
float clampf(float value, float min, float max) {
3771
const float m = value < min ? min : value;
3872
return m > max ? max : m;

src/utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,12 @@ float clampf(float value, float min, float max);
129129
* @param step A maximum unit of change of @p value.
130130
*/
131131
void rate_limitf(float *value, float target, float step);
132+
133+
void smooth_rampf(
134+
float *value,
135+
float *ramped_step_size,
136+
float target,
137+
float step,
138+
float smoothing_factor,
139+
float smooth_center_window
140+
);

0 commit comments

Comments
 (0)