Sheila Pham
/
IMU6050Ver1
Added for gyro and testing
Embed:
(wiki syntax)
Show/hide line numbers
filter.cpp
00001 float lowPass(float sample) 00002 { 00003 static const float a[4] = {1.00000000e+00,-2.77555756e-16,3.33333333e-01,-1.85037171e-17}; 00004 static const float b[4] = {0.16666667,0.5,0.5,0.16666667}; 00005 // x array for holding recent inputs (newest input as index 0, delay of 1 at index 1, etc. 00006 static volatile float x[4] = {0}; 00007 // x array for holding recent inputs (newest input as index 0, delay of 1 at index 1, etc. 00008 static volatile float y[4] = {0}; 00009 x[0] = sample; 00010 // Calculate the output filtered signal based on a weighted sum of previous inputs/outputs 00011 y[0] = (b[0]*x[0]+b[1]*x[1]+b[2]*x[2]+b[3]*x[3])-(a[1]*y[1]+a[2]*y[2]+a[3]*y[3]); 00012 y[0] /= a[0]; 00013 // Shift the input signals by one timestep to prepare for the next call to this function 00014 x[3] = x[2]; 00015 x[2] = x[1]; 00016 x[1] = x[0]; 00017 // Shift the previously calculated output signals by one time step to prepare for the next call to this function 00018 y[3] = y[2]; 00019 y[2] = y[1]; 00020 y[1] = y[0]; 00021 return y[0]; 00022 } 00023 00024 float boxcar(float sample) 00025 { 00026 static const int boxcarWidth = 30; // Change this value to alter boxcar length 00027 static float recentSamples[boxcarWidth] = {0}; // hold onto recent samples 00028 static int readIndex = 0; // the index of the current reading 00029 static float total = 0; // the running total 00030 static float average = 0; // the average 00031 // subtract the last reading: 00032 total = total - recentSamples[readIndex]; 00033 // add new sample to list (overwrite oldest sample) 00034 recentSamples[readIndex] = sample; 00035 // add the reading to the total: 00036 total = total + recentSamples[readIndex]; 00037 // advance to the next position in the array: 00038 readIndex = readIndex + 1; 00039 // if we're at the end of the array... 00040 if (readIndex >= boxcarWidth) { 00041 // ...wrap around to the beginning: 00042 readIndex = 0; 00043 } 00044 // calculate the average: 00045 average = total / boxcarWidth; 00046 // send it to the computer as ASCII digits 00047 return average; 00048 } 00049 float complement(float x, float y , float ratio) 00050 { 00051 return (ratio*x + (1 - ratio)*y); 00052 }
Generated on Sat Jul 16 2022 12:46:53 by 1.7.2