robobuggy / arduino / RadioBuggyMega / filter.c @ eb7af729
History | View | Annotate | Download (1.86 KB)
1 |
#include <Arduino.h> |
---|---|
2 |
#include "receiver.h" |
3 |
#include "filter.h" |
4 |
|
5 |
// This only does preliminary initialization.
|
6 |
// More initialization is done in the function body.
|
7 |
void filter_init(struct filter_state *state) { |
8 |
state->started = 0;
|
9 |
} |
10 |
|
11 |
//<<<<<<< Updated upstream
|
12 |
int filter_loop(struct filter_state *state, int val){ |
13 |
int i;
|
14 |
// First initialization: on assumption val is never -1
|
15 |
if(!(state->started)){
|
16 |
for(i = 0; i < FILTER_LEN; i++){ |
17 |
state->prev_values[i] = val; |
18 |
//=======
|
19 |
//#define SIZE 5 // the array size
|
20 |
//static char started = 0;
|
21 |
//static int last_val_sent;
|
22 |
//static int values[SIZE];
|
23 |
//static int pos;
|
24 |
//
|
25 |
///**
|
26 |
// * @brief Filters noise from raw RC readings
|
27 |
// *
|
28 |
// */
|
29 |
//int filter(int new_raw_reading){
|
30 |
// int i;
|
31 |
//
|
32 |
// // On Initialization, assume our "first" reading
|
33 |
// // occurred N times, and is our history
|
34 |
// if(!started){
|
35 |
// for(i = 0; i < SIZE; i++){
|
36 |
// values[i] = new_raw_reading;
|
37 |
//>>>>>>> Stashed changes
|
38 |
} |
39 |
state->pos = 0; // initialize the positions of the next number to be changed |
40 |
state->started = 1;
|
41 |
return val;
|
42 |
} |
43 |
|
44 |
|
45 |
//<<<<<<< Updated upstream
|
46 |
// NOT first initialization.
|
47 |
state->prev_values[state->pos] = val; // replace the number in the array
|
48 |
state->pos = (state->pos+1)%FILTER_LEN;// increment the pos number |
49 |
// calculate average of array
|
50 |
//=======
|
51 |
//
|
52 |
//
|
53 |
// values[pos] = val; // replace the number in the array
|
54 |
// pos = (pos+1)%SIZE; // increment the pos number
|
55 |
//
|
56 |
// // calculate average of previous readings
|
57 |
//>>>>>>> Stashed changes
|
58 |
int sum = 0; |
59 |
for(i = 0; i < FILTER_LEN; i++){ |
60 |
sum += state->prev_values[i]; |
61 |
} |
62 |
//<<<<<<< Updated upstream
|
63 |
int avg = sum/FILTER_LEN;
|
64 |
// return.
|
65 |
return avg;
|
66 |
//=======
|
67 |
// // Return the average.
|
68 |
// return sum / SIZE;
|
69 |
//>>>>>>> Stashed changes
|
70 |
|
71 |
} |