APM:Libraries
Filter.cpp
Go to the documentation of this file.
1 /*
2  * Example of Filter library.
3  * Code by Randy Mackay and Jason Short. DIYDrones.com
4  */
5 
6 #include <AP_HAL/AP_HAL.h>
7 #include <Filter/Filter.h> // Filter library
8 #include <Filter/ModeFilter.h> // ModeFilter class (inherits from Filter class)
9 #include <Filter/AverageFilter.h> // AverageFilter class (inherits from Filter class)
10 
11 void setup();
12 void loop();
13 void readTemp();
14 
16 
17 int16_t rangevalue[] = {31000, 31000, 50, 55, 60, 55, 10, 0, 31000};
18 
19 // create a global instance of the class instead of local to avoid memory fragmentation
20 ModeFilterInt16_Size5 mfilter(2); // buffer of 5 values, result will be from buffer element 2 (ie. the 3rd element which is the middle)
21 //AverageFilterInt16_Size5 mfilter; // buffer of 5 values. result will be average of these 5
22 
24 
26 
27 void setup()
28 {
29  // introduction
30  hal.console->printf("ArduPilot ModeFilter library test ver 1.0\n\n");
31 
32  // Wait for the serial connection
33  hal.scheduler->delay(500);
34 }
35 
36 // Read Raw Temperature values
37 void readTemp()
38 {
39  static uint8_t next_num = 0;
40  static int32_t raw_temp = 0;
41  uint8_t buf[2];
42  uint16_t _temp_sensor;
43 
44  next_num++;
45  buf[0] = next_num; //next_num;
46  buf[1] = 0xFF;
47 
48  _temp_sensor = buf[0];
49  _temp_sensor = (_temp_sensor << 8) | buf[1];
50 
51  raw_temp = _temp_filter.apply(_temp_sensor);
52 
53  // use a butter filter on the result, just so we have a
54  // butterworth filter example
55  butter.filter(raw_temp);
56 
57  hal.console->printf("RT: %lu\n", (unsigned long)raw_temp);
58 }
59 
60 // Main loop where the action takes place
61 void loop()
62 {
63  for (uint8_t j = 0; j < 0xFF; j++) {
64  readTemp();
65  hal.scheduler->delay(100);
66  }
67  hal.scheduler->delay(10000);
68 }
69 
70 AP_HAL_MAIN();
void setup()
Definition: Filter.cpp:27
AP_HAL::UARTDriver * console
Definition: HAL.h:110
virtual T apply(T sample)
Definition: AverageFilter.h:79
void loop()
Definition: Filter.cpp:61
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
Definition: Filter.cpp:15
virtual void delay(uint16_t ms)=0
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
AP_HAL_MAIN()
Definition: Butter.h:6
AverageFilterUInt16_Size4 _temp_filter
Definition: Filter.cpp:23
const HAL & get_HAL()
int16_t rangevalue[]
Definition: Filter.cpp:17
A class to apply a mode filter which is basically picking the median value from the last x samples th...
A class to provide the average of a number of samples.
ModeFilterInt16_Size5 mfilter(2)
butter50hz8_0 butter
Definition: Filter.cpp:25
float filter(float input)
Definition: Butter.h:9
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114
void readTemp()
Definition: Filter.cpp:37