Capstone project files
Dependencies: mbed-dsp mbed capstone_display_2
Diff: main.cpp
- Revision:
- 2:8ae58834937f
- Parent:
- 1:4fe83f71b889
- Child:
- 3:30dcfcf9412c
--- a/main.cpp Wed Feb 05 02:49:00 2014 +0000 +++ b/main.cpp Mon Mar 10 23:33:38 2014 +0000 @@ -1,13 +1,124 @@ #include "mbed.h" +#include "FIR_f32.h" #include "arm_math.h" -#include "arm_fir_f32.c" //the full path is cmsis_dsp/FilteringFunctions/arm_fir_f32.c +#define f_sampling 2000 //the sampling frequency +#define NumTaps 27 //the number of FIR coefficients +#define BlockSize 1024 //the size of the buffer + +Serial pc(USBTX, USBRX); //USB serial for PC, to be removed later +AnalogIn input(p15); //pin 15 for analog reading +float32_t waveform[BlockSize]; //array of input data +float32_t postFilterData[BlockSize]; //array of filtered data +bool fullRead; //whether the MBED has finish +bool waitForNext; + +//the filter coefficients for a band pass filter, consider changing to doubles if not precise enough +float32_t pCoeffs[NumTaps] = + { 0.012000000000000, 0.012462263166161, -0.019562318415964, -0.026175892863747, + 0.031654803781611, 0.050648026372209, -0.032547136829180, -0.070997780956819, + 0.032992306874347, 0.094643188024724, -0.020568171368385, -0.106071176200193, + 0.009515198320277, 0.114090808482376, 0.009515198320275, -0.106071176200193, + -0.020568171368382, 0.094643188024728, 0.032992306874351, -0.070997780956815, + -0.032547136829177, 0.050648026372211, 0.031654803781612, -0.026175892863746, + -0.019562318415964, 0.012462263166161, 0.012000000000000 }; +float32_t pState[NumTaps + BlockSize - 1]; + + +int index_g; //tracks the index for the waveform array -DigitalOut myled(LED1); + +void readPoint() +{ + waitForNext = false; +} + + +/** +* This function reads one full set of analog data into the uC +*/ +void readSamples() +{ + Ticker sampler; //allows for precision data reading + waitForNext = true; + sampler.attach_us(&readPoint, (int) (1000000/f_sampling) ); //read in data according the sampling freq + for (int i = 0; i < BlockSize; i++) + { + while (waitForNext); //wait until the ticker calls for the next sample + waveform[i] = input.read(); + waitForNext = true; + } + sampler.detach(); +} + int main() { - //to initialize the filter stuff use the kind of form below (see line 104 in the arm_fir_f32.c file for documentation) - //arm_fir_instance_f32 S = {numTaps, pState, pCoeffs}; + //to initialize the filter stuff use init functions (see line 89 in the arm_fir_f32.c file for documentation) + //the initialization function is in a seperate file called arm_fir_init_f32.c + + /* +* <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. +* Samples in the state buffer are stored in the following order. +* \par +* <pre> +* {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} +* </pre> +* \par + + */ + arm_fir_instance_f32* filter = new arm_fir_instance_f32(); + int state = 0; + uint16_t numTaps = NumTaps; + uint32_t blockSize = BlockSize; + while(1) + { + //pc.printf("While loop\n\r"); + switch(state) + { + case 0: //initialization - while(1) { - } + //pc.printf("pre-filter init\n\r"); + arm_fir_init_f32(filter, numTaps, pCoeffs, pState, blockSize); + //pc.printf("Pre-attachment"); + + state = 1; + pc.printf("Done with init.\n\r"); + break; + + case 1: //read data, take samples + pc.printf("Reading data.\n\r"); + readSamples(); + state = 2; + break; + + case 2: //printout to pc connection + //pc.printf("into print\n\r"); + /* + for (int i = 0; i < BlockSize; i++) + { + pc.printf("Waveform contents:%f\n\r", waveform[i]); + } + */ + state = 3; + break; + case 3: //filter? + pc.printf("Filtering?\n\r"); + arm_fir_f32(filter, waveform, postFilterData, blockSize); + state = 6; + break; + case 4: //FFT? + break; + case 5: //output, write to display and PWM tone + break; + case 6: //calculate the average voltage + double sum = 0; + for (int i = 0; i < BlockSize; i++) sum += postFilterData[i]; + double average = sum/BlockSize*3.3; //*3.3 V_ref (array stored as fractions of V_ref) + pc.printf("Average = %f\n\r", average); + wait_ms(500); + state = 1; + break; + default: + break; + } + } //end of (infinite) while loop }