2323#include <math.h>
2424
2525void torque_tilt_reset (TorqueTilt * tt ) {
26+ tt -> target = 0 ;
2627 tt -> setpoint = 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,15 +61,16 @@ 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 =
64+ tt -> target =
6465 fminf (
6566 fmaxf ((fabsf (motor -> filt_current ) - config -> torquetilt_start_current ), 0 ) * strength ,
6667 config -> torquetilt_angle_limit
6768 ) *
6869 sign (motor -> filt_current );
6970
7071 float step_size = 0 ;
71- if ((tt -> setpoint - target > 0 && target > 0 ) || (tt -> setpoint - target < 0 && target < 0 )) {
72+ if ((tt -> setpoint - tt -> target > 0 && tt -> target > 0 ) ||
73+ (tt -> setpoint - tt -> target < 0 && tt -> target < 0 )) {
7274 step_size = tt -> off_step_size ;
7375 } else {
7476 step_size = tt -> on_step_size ;
@@ -78,20 +80,30 @@ void torque_tilt_update(
7880 step_size /= 2 ;
7981 }
8082
83+ return step_size ;
84+ }
85+
86+ void torque_tilt_update (
87+ TorqueTilt * tt , const MotorData * motor , const RefloatConfig * config , bool wheelslip , float dt
88+ ) {
89+ float step_size = tt -> off_step_size ;
90+
91+ if (!wheelslip ) {
92+ step_size = calculate_torque_tilt_target (tt , motor , config );
93+ } else {
94+ tt -> target *= 0.99 ;
95+ }
96+
8197 if (config -> target_filter .tt_type == SFT_NONE ) {
82- rate_limitf (& tt -> setpoint , target , step_size );
98+ rate_limitf (& tt -> setpoint , tt -> target , step_size );
8399 } else if (config -> target_filter .tt_type == SFT_EMA3 ) {
84- ema_filter_update (& tt -> ema_target , target , dt );
100+ ema_filter_update (& tt -> ema_target , tt -> target , dt );
85101 tt -> setpoint = tt -> ema_target .value ;
86102 } else if (config -> target_filter .tt_type == SFT_THREE_STAGE ) {
87- smooth_target_update (& tt -> smooth_target , target );
103+ smooth_target_update (& tt -> smooth_target , tt -> target );
88104 tt -> setpoint = tt -> smooth_target .value ;
89105 } else {
90106 // 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 );
107+ smooth_rampf (& tt -> setpoint , & tt -> ramped_step_size , tt -> target , step_size , 0.04 , 1.5 );
92108 }
93109}
94-
95- void torque_tilt_winddown (TorqueTilt * tt ) {
96- tt -> setpoint *= 0.995 ;
97- }
0 commit comments