2323#include <math.h>
2424
2525void torque_tilt_reset (TorqueTilt * tt ) {
26+ tt -> target_offset = 0 ;
2627 tt -> offset = 0 ;
2728 tt -> ramped_step_size = 0 ;
2829
@@ -49,8 +50,8 @@ void torque_tilt_configure(TorqueTilt *tt, const RefloatConfig *config) {
4950 );
5051}
5152
52- void torque_tilt_update (
53- TorqueTilt * tt , const MotorData * motor , const RefloatConfig * config , float dt
53+ static float calculate_torque_tilt_target (
54+ TorqueTilt * tt , const MotorData * motor , const RefloatConfig * config
5455) {
5556 float strength =
5657 motor -> braking ? config -> torquetilt_strength_regen : config -> torquetilt_strength ;
@@ -60,7 +61,7 @@ void torque_tilt_update(
6061 // multiply it by "power" to get our desired angle, and min with the limit
6162 // to respect boundaries. Finally multiply it by motor current sign to get
6263 // directionality back.
63- float target_offset =
64+ tt -> target_offset =
6465 fminf (
6566 fmaxf ((fabsf (motor -> atr_filtered_current ) - config -> torquetilt_start_current ), 0 ) *
6667 strength ,
@@ -69,8 +70,8 @@ void torque_tilt_update(
6970 sign (motor -> atr_filtered_current );
7071
7172 float step_size = 0 ;
72- if ((tt -> offset - target_offset > 0 && target_offset > 0 ) ||
73- (tt -> offset - target_offset < 0 && target_offset < 0 )) {
73+ if ((tt -> offset - tt -> target_offset > 0 && tt -> target_offset > 0 ) ||
74+ (tt -> offset - tt -> target_offset < 0 && tt -> target_offset < 0 )) {
7475 step_size = tt -> off_step_size ;
7576 } else {
7677 step_size = tt -> on_step_size ;
@@ -80,20 +81,30 @@ void torque_tilt_update(
8081 step_size /= 2 ;
8182 }
8283
84+ return step_size ;
85+ }
86+
87+ void torque_tilt_update (
88+ TorqueTilt * tt , const MotorData * motor , const RefloatConfig * config , bool wheelslip , float dt
89+ ) {
90+ float step_size = tt -> off_step_size ;
91+
92+ if (!wheelslip ) {
93+ step_size = calculate_torque_tilt_target (tt , motor , config );
94+ } else {
95+ tt -> target_offset *= 0.99 ;
96+ }
97+
8398 if (config -> target_filter .tt_type == SFT_NONE ) {
84- rate_limitf (& tt -> offset , target_offset , step_size );
99+ rate_limitf (& tt -> offset , tt -> target_offset , step_size );
85100 } else if (config -> target_filter .tt_type == SFT_EMA3 ) {
86- ema_filter_update (& tt -> ema_target , target_offset , dt );
101+ ema_filter_update (& tt -> ema_target , tt -> target_offset , dt );
87102 tt -> offset = tt -> ema_target .value ;
88103 } else if (config -> target_filter .tt_type == SFT_THREE_STAGE ) {
89- smooth_target_update (& tt -> smooth_target , target_offset );
104+ smooth_target_update (& tt -> smooth_target , tt -> target_offset );
90105 tt -> offset = tt -> smooth_target .value ;
91106 } else {
92107 // Smoothen changes in tilt angle by ramping the step size
93- smooth_rampf (& tt -> offset , & tt -> ramped_step_size , target_offset , step_size , 0.04 , 1.5 );
108+ smooth_rampf (& tt -> offset , & tt -> ramped_step_size , tt -> target_offset , step_size , 0.04 , 1.5 );
94109 }
95110}
96-
97- void torque_tilt_winddown (TorqueTilt * tt ) {
98- tt -> offset *= 0.995 ;
99- }
0 commit comments