2020-09-19 19:03:36 -03:00
# pragma once
/* slew rate limiting filter. This is used to prevent oscillation of a
* controller by modifying the controllers output based on a maximum
* slew rate
*/
# include <stdint.h>
# include "LowPassFilter.h"
2021-04-08 23:44:20 -03:00
# define N_EVENTS 2 // number of positive and negative consecutive slew rate exceedance events recorded where a value of 2 corresponds to a complete cycle
2021-04-10 19:21:53 -03:00
# define WINDOW_MS 300 // time in msec required for a half cycle of the slowest oscillation frequency expected
2021-04-10 19:19:24 -03:00
# define MODIFIER_GAIN 1.5f // ratio of modifier reduction to slew rate exceedance ratio
2021-04-10 20:47:43 -03:00
# define DERIVATIVE_CUTOFF_FREQ 25.0f
2021-04-08 23:44:20 -03:00
2020-09-19 19:03:36 -03:00
class SlewLimiter {
public :
SlewLimiter ( const float & slew_rate_max , const float & slew_rate_tau ) ;
/*
apply filter to sample , returning multiplier between 0 and 1 to keep
output within slew rate
*/
float modifier ( float sample , float dt ) ;
2021-04-04 06:49:47 -03:00
/*
2021-04-08 23:44:20 -03:00
get last oscillation slew rate
2021-04-04 06:49:47 -03:00
*/
float get_slew_rate ( void ) const {
2021-04-10 20:47:43 -03:00
return _output_slew_rate ;
2021-04-04 06:49:47 -03:00
}
2020-09-19 19:03:36 -03:00
private :
const float & slew_rate_max ;
const float & slew_rate_tau ;
LowPassFilterFloat slew_filter ;
2021-04-10 20:47:43 -03:00
float _output_slew_rate ;
float _modifier_slew_rate ;
2020-09-19 19:03:36 -03:00
float last_sample ;
2021-04-06 07:02:59 -03:00
float _max_pos_slew_rate ;
float _max_neg_slew_rate ;
2021-04-08 23:44:20 -03:00
uint32_t _max_pos_slew_event_ms ;
uint32_t _max_neg_slew_event_ms ;
uint8_t _pos_event_index ;
uint8_t _neg_event_index ;
uint32_t _pos_event_ms [ N_EVENTS ] ;
uint32_t _neg_event_ms [ N_EVENTS ] ;
bool _pos_event_stored ;
bool _neg_event_stored ;
2020-09-19 19:03:36 -03:00
} ;