APM:Libraries
LowPassFilter.cpp
Go to the documentation of this file.
1 /*
2  * Example sketch to demonstrate use of LowPassFilter library.
3  * Code by Randy Mackay. DIYDrones.com
4  */
5 
6 #include <AP_HAL/AP_HAL.h>
7 #include <Filter/Filter.h> // Filter library
8 #include <Filter/LowPassFilter.h> // LowPassFilter class (inherits from Filter class)
9 
10 void setup();
11 void loop();
12 
14 
15 // create a global instance of the class
17 
18 // setup routine
19 void setup()
20 {
21  // introduction
22  hal.console->printf("ArduPilot LowPassFilter test ver 1.0\n\n");
23 
24  // set-up filter
25  low_pass_filter.set_cutoff_frequency(1.0f);
26 
27  // Wait for the serial connection
28  hal.scheduler->delay(500);
29 }
30 
31 //Main loop where the action takes place
32 void loop()
33 {
34  // reset value to 100. If not reset the filter will start at the first value entered
35  low_pass_filter.reset(0);
36 
37  for(int16_t i = 0; i < 300; i++ ) {
38 
39  // new data value
40  const float new_value = sinf((float)i * 2 * M_PI * 5 / 50.0f); // 5hz
41 
42  // output to user
43  hal.console->printf("applying: %6.4f", (double)new_value);
44 
45  // apply new value and retrieved filtered result
46  const float filtered_value = low_pass_filter.apply(new_value, 0.02f);
47 
48  // display results
49  hal.console->printf("\toutput: %6.4f\n", (double)filtered_value);
50 
51  hal.scheduler->delay(10);
52  }
53  hal.scheduler->delay(10000);
54 }
55 
56 AP_HAL_MAIN();
void reset(T value)
AP_HAL::UARTDriver * console
Definition: HAL.h:110
void setup()
void set_cutoff_frequency(float cutoff_freq)
void loop()
virtual void delay(uint16_t ms)=0
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
LowPassFilterFloat low_pass_filter
#define f(i)
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
const HAL & get_HAL()
A class to implement a low pass filter without losing precision even for int types the downside being...
AP_HAL_MAIN()
#define M_PI
Definition: definitions.h:10
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114
T apply(T sample, float dt)