@@ -73,10 +73,10 @@ static thread_t *app_thread;
73
73
// Config values
74
74
static volatile balance_config balance_conf ;
75
75
static volatile imu_config imu_conf ;
76
- static float startup_step_size , tiltback_step_size , torquetilt_step_size ;
76
+ static float startup_step_size , tiltback_step_size , torquetilt_step_size , turntilt_step_size ;
77
77
78
78
// Runtime values read from elsewhere
79
- static float pitch_angle , roll_angle ;
79
+ static float pitch_angle , roll_angle , abs_roll_angle , abs_roll_angle_sin ;
80
80
static float gyro [3 ];
81
81
static float duty_cycle , abs_duty_cycle ;
82
82
static float erpm , abs_erpm , avg_erpm ;
@@ -90,7 +90,9 @@ static BalanceState state;
90
90
static float proportional , integral , derivative ;
91
91
static float last_proportional , abs_proportional ;
92
92
static float pid_value ;
93
- static float setpoint , setpoint_target , setpoint_target_interpolated , torquetilt_target , torquetilt_interpolated , torquetilt_filtered_current ;
93
+ static float setpoint , setpoint_target , setpoint_target_interpolated ;
94
+ static float torquetilt_filtered_current , torquetilt_target , torquetilt_interpolated ;
95
+ static float turntilt_target , turntilt_interpolated ;
94
96
static SetpointAdjustmentType setpointAdjustmentType ;
95
97
static float yaw_proportional , yaw_integral , yaw_derivative , yaw_last_proportional , yaw_pid_value , yaw_setpoint ;
96
98
static systime_t current_time , last_time , diff_time ;
@@ -110,6 +112,7 @@ void app_balance_configure(balance_config *conf, imu_config *conf2) {
110
112
startup_step_size = balance_conf .startup_speed / balance_conf .hertz ;
111
113
tiltback_step_size = balance_conf .tiltback_speed / balance_conf .hertz ;
112
114
torquetilt_step_size = balance_conf .torquetilt_speed / balance_conf .hertz ;
115
+ turntilt_step_size = balance_conf .turntilt_speed / balance_conf .hertz ;
113
116
}
114
117
115
118
void app_balance_start (void ) {
@@ -133,6 +136,8 @@ void reset_vars(void){
133
136
torquetilt_target = 0 ;
134
137
torquetilt_interpolated = 0 ;
135
138
torquetilt_filtered_current = 0 ;
139
+ turntilt_target = 0 ;
140
+ turntilt_interpolated = 0 ;
136
141
setpointAdjustmentType = CENTERING ;
137
142
yaw_setpoint = 0 ;
138
143
state = RUNNING ;
@@ -295,7 +300,7 @@ void calculate_setpoint_interpolated(void){
295
300
}
296
301
}
297
302
298
- void apply_torque_tilt (void ){
303
+ void apply_torquetilt (void ){
299
304
// Filter current (Basic LPF)
300
305
torquetilt_filtered_current = ((1 - balance_conf .torquetilt_filter ) * motor_current ) + (balance_conf .torquetilt_filter * torquetilt_filtered_current );
301
306
// Wat is this line O_o
@@ -314,6 +319,46 @@ void apply_torque_tilt(void){
314
319
setpoint += torquetilt_interpolated ;
315
320
}
316
321
322
+ void apply_turntilt (void ){
323
+ // Calculate desired angle
324
+ turntilt_target = abs_roll_angle_sin * balance_conf .turntilt_power ;
325
+
326
+ // Apply cutzone
327
+ if (abs_roll_angle < balance_conf .turntilt_angle_cut ){
328
+ turntilt_target = 0 ;
329
+ }
330
+
331
+ // Disable at 0 speed otherwise add directionality
332
+ if (abs_erpm < 100 ){
333
+ turntilt_target = 0 ;
334
+ }else {
335
+ turntilt_target *= SIGN (erpm );
336
+ }
337
+
338
+ // Apply speed scaling
339
+ if (balance_conf .turntilt_erpm_boost_end > 0 ){
340
+ if (abs_erpm < balance_conf .turntilt_erpm_boost_end ){
341
+ turntilt_target *= 1 + ((balance_conf .turntilt_erpm_boost /100.0f ) * (abs_erpm / balance_conf .turntilt_erpm_boost_end ));
342
+ }else {
343
+ turntilt_target *= 1 + (balance_conf .turntilt_erpm_boost /100.0f );
344
+ }
345
+ }
346
+
347
+ // Limit angle to max angle
348
+ turntilt_target = fminf (turntilt_target , balance_conf .turntilt_angle_limit );
349
+
350
+ // Move towards target limited by max speed
351
+ if (fabsf (turntilt_target - turntilt_interpolated ) < turntilt_step_size ){
352
+ turntilt_interpolated = turntilt_target ;
353
+ }else if (turntilt_target - turntilt_interpolated > 0 ){
354
+ turntilt_interpolated += turntilt_step_size ;
355
+ }else {
356
+ turntilt_interpolated -= turntilt_step_size ;
357
+ }
358
+ setpoint += turntilt_interpolated ;
359
+
360
+ }
361
+
317
362
float apply_deadzone (float error ){
318
363
if (balance_conf .deadzone == 0 ){
319
364
return error ;
@@ -389,6 +434,8 @@ static THD_FUNCTION(balance_thread, arg) {
389
434
// Get the values we want
390
435
pitch_angle = imu_get_pitch () * 180.0f / M_PI ;
391
436
roll_angle = imu_get_roll () * 180.0f / M_PI ;
437
+ abs_roll_angle = fabsf (roll_angle );
438
+ abs_roll_angle_sin = sinf (abs_roll_angle * M_PI / 180.0f );
392
439
imu_get_gyro (gyro );
393
440
duty_cycle = mc_interface_get_duty_cycle_now ();
394
441
abs_duty_cycle = fabsf (duty_cycle );
@@ -464,7 +511,8 @@ static THD_FUNCTION(balance_thread, arg) {
464
511
calculate_setpoint_target ();
465
512
calculate_setpoint_interpolated ();
466
513
setpoint = setpoint_target_interpolated ;
467
- apply_torque_tilt ();
514
+ apply_torquetilt ();
515
+ apply_turntilt ();
468
516
469
517
// Do PID maths
470
518
proportional = setpoint - pitch_angle ;
0 commit comments