Working code for pc app 12/01/2018 commit
Dependencies: mbed MS5607 mbed-dsp
Fork of Turrentine_Code by
Diff: main.cpp
- Revision:
- 4:d4804771134a
- Parent:
- 3:c80aa39db5bb
- Child:
- 5:44cfc3e81d1c
--- a/main.cpp Thu Jan 25 09:21:43 2018 +0000 +++ b/main.cpp Thu Jan 25 10:20:46 2018 +0000 @@ -6,6 +6,15 @@ #define N_SAMPLES 1024 +#define PEAK_SEARCH_START 1 //first bin to include in peak search +#define PEAK_SEARCH_END 50 //last bin to include in peak search +#define SUM_AROUND_PEAK 1 //+/- this many bins + +#define REGRESSION_SLOPE 53.77986f //from Excel fit to test data +#define REGRESSION_INTERCEPT -80.07f + +float thresholds[] = {10.f, 30.f, 50.f, 70.f}; //estimated t90 thresholds to turn on LEDs + //mbed class def Serial pc(USBTX, USBRX); // tx, rx MS5607SPI pressure_sensor(p5, p6, p7, p8); // mosi, miso, sclk, cs @@ -52,6 +61,8 @@ int iter_counter = 0; //Iteration counter iter_timer.start(); + printf("fsmpl titer mean_press fmax sum_peak t90\n"); + //program loop while(1) { sample_timer.reset(); @@ -83,31 +94,48 @@ mag_data[0] = f_data[0]; mag_data[N_SAMPLES / 2] = f_data[1]; - //Output iteration data - iter_counter++; - printf("%10.5g %5d %5d\n", f_data[0] / N_SAMPLES, 1000000 / (sample_timer.read_us() / N_SAMPLES), iter_timer.read_ms() / iter_counter); - //Output raw data /*for(int i = 0; i < N_SAMPLES; i++) { printf("%f %f\n", p_data_raw[i], f_data[i]); }*/ - //Analysis - average from bin 15 to bin 25 - //Print out subset of data - float average = 0; - for(int i = 15; i < 25; i++) { - printf("%10.5g ", mag_data[i]); - average += mag_data[i] / 10; + //Find peak in spectrum + float max_mag = -1; + int max_mag_i = -1; + for(int i = PEAK_SEARCH_START + SUM_AROUND_PEAK; i < PEAK_SEARCH_END; i++){ + //printf("%10f ", mag_data[i]); + if(mag_data[i] > max_mag) { + max_mag_i = i; + max_mag = mag_data[i]; + } + } + //printf("\n"); + + //Sum surrounding + float sum = 0.f; + for(int i = max_mag_i - SUM_AROUND_PEAK; i < (max_mag_i + SUM_AROUND_PEAK + 1); i++) { + sum += mag_data[i]; + //printf("%10f ", mag_data[i]); } - printf("%10.5g\n", average); + //printf("\n"); + + //Scale by N_SAMPLES to recover pressure in Pa + sum /= N_SAMPLES; + + //Apply inverse of logarithmic regression from test data + float t90 = exp((sum + 80.07) / 53.77986); + + + //Output iteration data + iter_counter++; + printf("%5d %5d %10.0f %4d %10.5e %3.0f\n", 1000000 / (sample_timer.read_us() / N_SAMPLES), iter_timer.read_ms() / iter_counter, f_data[0] / N_SAMPLES, max_mag_i, sum, t90); //Set LEDs based on thresholds - leds[0] = average > 1000.f; - leds[1] = average > 2000.f; - leds[2] = average > 3000.f; - leds[3] = average > 4000.f; + for(int i = 0; i < 4; i++) { + leds[i] = t90 >= thresholds[i]; + } //break; }