MBot Software Library  v1.0
An API documentation to mbot_firmware repository
filter.h
1 
25 #ifndef RC_FILTER_H
26 #define RC_FILTER_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdint.h>
33 #include <rc/math/vector.h>
34 #include <rc/math/ring_buffer.h>
35 
43 typedef struct rc_filter_t{
46  int order;
47  double dt;
48  double gain;
52 
55  int sat_en;
56  double sat_min;
57  double sat_max;
58  int sat_flag;
60 
63  int ss_en;
64  double ss_steps;
66 
69  rc_ringbuf_t in_buf;
70  rc_ringbuf_t out_buf;
72 
75  double newest_input;
76  double newest_output;
77  uint64_t step;
81 
82 #define RC_FILTER_INITIALIZER {\
83  .order = 0,\
84  .dt = 0.0,\
85  .gain = 1.0,\
86  .num = RC_VECTOR_INITIALIZER,\
87  .den = RC_VECTOR_INITIALIZER,\
88  .sat_en = 0,\
89  .sat_min = 0.0,\
90  .sat_max = 0.0,\
91  .sat_flag = 0,\
92  .ss_en = 0,\
93  .ss_steps = 0,\
94  .in_buf = RC_RINGBUF_INITIALIZER,\
95  .out_buf = RC_RINGBUF_INITIALIZER,\
96  .newest_input = 0.0,\
97  .newest_output = 0.0,\
98  .step = 0,\
99  .initialized = 0}
100 
115 
139 int rc_filter_alloc(rc_filter_t* f, rc_vector_t num, rc_vector_t den, double dt);
140 
157 int rc_filter_alloc_from_arrays(rc_filter_t* f,double dt,double* num,int numlen,\
158  double* den,int denlen);
159 
172 
182 
194 
210 double rc_filter_march(rc_filter_t* f, double new_input);
211 
223 
239 int rc_filter_enable_saturation(rc_filter_t* f, double min, double max);
240 
253 
274 int rc_filter_enable_soft_start(rc_filter_t* f, double seconds);
275 
290 double rc_filter_previous_input(rc_filter_t* f, int steps);
291 
307 double rc_filter_previous_output(rc_filter_t* f, int steps);
308 
321 int rc_filter_prefill_inputs(rc_filter_t* f, double in);
322 
335 int rc_filter_prefill_outputs(rc_filter_t* f, double out);
336 
364 
381 int rc_filter_c2d_tustin(rc_filter_t* f, double dt,rc_vector_t num,rc_vector_t den,double w);
382 
392 
409 int rc_filter_first_order_lowpass(rc_filter_t* f, double dt, double tc);
410 
427 int rc_filter_first_order_highpass(rc_filter_t* f, double dt, double tc);
428 
443 int rc_filter_butterworth_lowpass(rc_filter_t* f, int order, double dt, double wc);
444 
459 int rc_filter_butterworth_highpass(rc_filter_t* f, int order, double dt, double wc);
460 
480 int rc_filter_moving_average(rc_filter_t* f, int samples, double dt);
481 
495 int rc_filter_integrator(rc_filter_t *f, double dt);
496 
510 int rc_filter_double_integrator(rc_filter_t* f, double dt);
511 
533 int rc_filter_pid(rc_filter_t* f,double kp,double ki,double kd,double Tf,double dt);
534 
535 
549 int rc_filter_third_order_complement(rc_filter_t* lp, rc_filter_t* hp, double freq, double damp, double dt);
550 
551 
552 #ifdef __cplusplus
553 }
554 #endif
555 
556 #endif // RC_FILTER_H
557 
double rc_filter_previous_output(rc_filter_t *f, int steps)
Returns the output 'steps' back in time. Steps = 0 returns most recent output.
Definition: filter.c:350
int rc_filter_pid(rc_filter_t *f, double kp, double ki, double kd, double Tf, double dt)
Creates a discrete-time implementation of a parallel PID controller with high-frequency rolloff using...
Definition: filter.c:777
int rc_filter_alloc_from_arrays(rc_filter_t *f, double dt, double *num, int numlen, double *den, int denlen)
Like rc_filter_alloc(), but takes arrays for the numerator and denominator coefficients instead of ve...
Definition: filter.c:100
int rc_filter_prefill_outputs(rc_filter_t *f, double out)
Fills all previous outputs of the filter as if they had been equal to 'out'.
Definition: filter.c:376
int rc_filter_double_integrator(rc_filter_t *f, double dt)
Creates a second order double integrator.
Definition: filter.c:750
int rc_filter_reset(rc_filter_t *f)
Resets all previous inputs and outputs to 0. Also resets the step counter & saturation flag.
Definition: filter.c:250
int rc_filter_normalize(rc_filter_t *f)
Normalizes a discrete time filter so that the leading demoninator coefficient is 1.
Definition: filter.c:553
int rc_filter_third_order_complement(rc_filter_t *lp, rc_filter_t *hp, double freq, double damp, double dt)
Creates a third order symmetric complementary pair of high/low pass filters.
Definition: filter.c:839
double rc_filter_march(rc_filter_t *f, double new_input)
March a filter forward one step with new input provided as an argument.
Definition: filter.c:194
struct rc_filter_t rc_filter_t
Struct containing configuration and state of a SISO filter.
int rc_filter_first_order_highpass(rc_filter_t *f, double dt, double tc)
Creates a first order high pass filter.
Definition: filter.c:604
int rc_filter_enable_soft_start(rc_filter_t *f, double seconds)
Enables soft start functionality where the output bound is gradually opened linearly from 0 to the no...
Definition: filter.c:320
int rc_filter_duplicate(rc_filter_t *f, rc_filter_t old)
duplicates a filter
Definition: filter.c:158
rc_filter_t rc_filter_empty(void)
Critical function for initializing rc_filter_t structs.
Definition: filter.c:40
int rc_filter_prefill_inputs(rc_filter_t *f, double in)
Fills all previous inputs to the filter as if they had been equal to 'in'.
Definition: filter.c:361
int rc_filter_c2d_tustin(rc_filter_t *f, double dt, rc_vector_t num, rc_vector_t den, double w)
Creates a discrete time filter with similar dynamics to a provided continuous time transfer function ...
Definition: filter.c:478
int rc_filter_first_order_lowpass(rc_filter_t *f, double dt, double tc)
Creates a first order low pass filter.
Definition: filter.c:575
int rc_filter_get_saturation_flag(rc_filter_t *f)
Checks if the filter saturated the last time step.
Definition: filter.c:310
int rc_filter_alloc(rc_filter_t *f, rc_vector_t num, rc_vector_t den, double dt)
Allocate memory for a discrete-time filter & populates it with the transfer function coefficients pro...
Definition: filter.c:46
int rc_filter_integrator(rc_filter_t *f, double dt)
Creates a first order integrator.
Definition: filter.c:723
int rc_filter_print(rc_filter_t f)
Prints the transfer function and other statistic of a filter to the screen.
Definition: filter.c:267
int rc_filter_butterworth_lowpass(rc_filter_t *f, int order, double dt, double wc)
Creates a Butterworth low pass filter of specified order and cutoff frequency.
Definition: filter.c:633
int rc_filter_free(rc_filter_t *f)
Frees the memory allocated by a filter's buffers and coefficient vectors. Also resets all filter prop...
Definition: filter.c:178
int rc_filter_multiply_three(rc_filter_t f1, rc_filter_t f2, rc_filter_t f3, rc_filter_t *out)
Creates a new filter 'out' by multiplying f1*f2*f3.
Definition: filter.c:425
int rc_filter_enable_saturation(rc_filter_t *f, double min, double max)
Enables saturation between bounds min and max.
Definition: filter.c:293
int rc_filter_multiply(rc_filter_t f1, rc_filter_t f2, rc_filter_t *out)
Creates a new filter 'out' by multiplying f1*f2.
Definition: filter.c:391
double rc_filter_previous_input(rc_filter_t *f, int steps)
Returns the input 'steps' back in time. Steps=0 returns most recent input.
Definition: filter.c:340
int rc_filter_moving_average(rc_filter_t *f, int samples, double dt)
Makes a FIR moving average filter that averages over specified number of samples.
Definition: filter.c:687
int rc_filter_butterworth_highpass(rc_filter_t *f, int order, double dt, double wc)
Creates a Butterworth high pass filter of specified order and cutoff frequency.
Definition: filter.c:659
Struct containing configuration and state of a SISO filter.
Definition: filter.h:43
double newest_input
shortcut for the most recent input
Definition: filter.h:75
double sat_max
upper saturation limit
Definition: filter.h:57
int ss_en
set to 1 by enbale_soft_start()
Definition: filter.h:63
double sat_min
lower saturation limit
Definition: filter.h:56
double newest_output
shortcut for the most recent output
Definition: filter.h:76
double gain
Additional gain multiplier, usually 1.0.
Definition: filter.h:48
double dt
timestep in seconds
Definition: filter.h:47
int order
transfer function order
Definition: filter.h:46
int sat_en
set to 1 by enable_saturation()
Definition: filter.h:55
rc_vector_t num
numerator coefficients
Definition: filter.h:49
double ss_steps
steps before full output allowed
Definition: filter.h:64
int initialized
initialization flag
Definition: filter.h:78
int sat_flag
1 if saturated on the last step
Definition: filter.h:58
rc_vector_t den
denominator coefficients
Definition: filter.h:50
uint64_t step
steps since last reset
Definition: filter.h:77
Struct containing state of a ringbuffer and pointer to dynamically allocated memory.
Definition: ring_buffer.h:34
Struct containing the state of a vector and a pointer to dynamically allocated memory to hold its con...
Definition: vector.h:41