Working code for pc app 12/01/2018 commit
Dependencies: mbed MS5607 mbed-dsp
Fork of Turrentine_Code by
Diff: main.cpp
- Revision:
- 3:c80aa39db5bb
- Parent:
- 2:3d3e21c907e4
- Child:
- 4:d4804771134a
--- a/main.cpp Fri Jan 12 15:40:32 2018 +0000 +++ b/main.cpp Thu Jan 25 09:21:43 2018 +0000 @@ -1,4 +1,6 @@ #include "mbed.h" +#include "MS5607SPI.h" +#include "arm_math.h" #define M_PI 3.14159265358979323846 @@ -6,42 +8,23 @@ //mbed class def Serial pc(USBTX, USBRX); // tx, rx -SPI spi(p5, p6, p7); // mosi, miso, sclk -DigitalOut cs(p8); - -PwmOut motorOn(p26); +MS5607SPI pressure_sensor(p5, p6, p7, p8); // mosi, miso, sclk, cs -Timer t; +BusOut leds(LED1, LED2, LED3, LED4); -//variable instaniation -unsigned short calData[6]; +PwmOut motor(p26); -char buffer16[3]; -short int serbuffer[2]; +Timer sample_timer; +Timer iter_timer; -char tempBuffer[4]; -unsigned int temp = 0; -char pressBuffer[4]; -unsigned int press = 0; +float p_data[N_SAMPLES]; +float p_data_raw[N_SAMPLES]; +float f_data[N_SAMPLES]; +float mag_data[N_SAMPLES / 2 + 1]; -unsigned int pData[N_SAMPLES]; -float spData[N_SAMPLES]; unsigned int tData[N_SAMPLES]; -const char cb1 = 0xA2; -const char cb2 = 0xA4; -const char cb3 = 0xA6; -const char cb4 = 0xA8; -const char cb5 = 0xAA; -const char cb6 = 0xAC; - -const char * commarr[6] = {&cb1, &cb2, &cb3, &cb4, &cb5, &cb6}; - -const char D1conv256 = 0x40; -const char D1conv512 = 0x42; -const char D2conv4096 = 0x58; -const char D2conv512 = 0x52; -const char readADC = 0x00; +arm_rfft_fast_instance_f32 rfft; float duty = 1; @@ -50,246 +33,82 @@ return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); } -//calculate temperature -int calcT(unsigned int Tval, unsigned short consts[6]) -{ - int dT = Tval - consts[4]*(2<<7); - - int T = 2000 + dT*consts[5]/(2<<22); - printf("Temp: %d C\n", T); - return T; -} - -//calculate 1st order temperature compensated pressure -int calcP(unsigned int Tval, unsigned int Pval, unsigned short consts[6]) -{ - int dT = Tval - consts[4]*(2<<7); - - long long int off = (long long)consts[1]*(2<<16) + ((long long)consts[3] *(long long)dT)/(2<<5); - - long long int sens = (long long)consts[0]*(2<<15) + ((long long)consts[2] *(long long)dT)/(2<<6); - - int P = (Pval *(sens/(2<<20)) - off)/(2<<14); - //printf("Pressure %d Pa\n\r", P); - return P; -} - int main() { + printf("Turrentine\n"); + + //Configure PC serial link pc.baud(115200); - motorOn = 0; - motorOn.period_ms(10); - - printf("Turrentine\n"); - - // Chip must be deselected - cs = 1; - - // Setup the spi for 8 bit data, high steady state clock, - // second edge capture, with a 1MHz clock rate - spi.format(8,3); - spi.frequency(500000); - spi.set_default_write_value(0x00); - - // Select the device by seting chip select low - cs = 0; - - // Send 0x1E Command to reset the chip - spi.write(0x1E); - cs = 1; - wait_ms(100); - cs = 1; - - printf("Read cal data\n"); - - //read cal data values from device into program - for (char i=0; i<6; i++) { - //spi read sequence - cs= 0; - spi.write(commarr[i], 1, buffer16, 3); - //time for SPI to write into data buffers - wait_ms(10); - cs=1; - //Put data into 16bit unsigned int in calData array - calData[i] = buffer16[1]<<8 | buffer16[2]; - } - + + //Configure motor PWM + motor = 0; + motor.period_ms(10); printf("Pump On\n"); - //turn pump on - motorOn.write(duty); - - - //dummy samples to wait for sensor response to stabilise - int a =0; - while(a < 10000) { - cs = 0; - spi.write(&D1conv256, 1, buffer16, 1); - wait_us(500); - cs = 1; - cs = 0; - spi.write(&readADC, 1, pressBuffer, 4); - cs = 1; - a++; - } + motor.write(duty); printf("Start loop\n"); - - //display calibration values to check against datasheet - - //program loop - while(1) { - //read temerature value - cs = 0; - spi.write(&D2conv4096, 1, buffer16, 1); - wait_ms(10); - cs = 1; - cs = 0; - - spi.write(&readADC, 1, tempBuffer, 4); - cs = 1; - - //read pressure value - /*cs = 0; - spi.write(&D1conv512, 1, buffer16, 1); - wait_ms(2); - cs = 1; cs = 0; - - spi.write(&readADC, 1, pressBuffer, 4); - cs = 1;*/ - - //write values from buffers to program variables - temp = tempBuffer[1]<<16 | tempBuffer[2]<<8 | tempBuffer[3]; - //press = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3]; - - int a =0; - while(a < 5000) { - cs = 0; - spi.write(&D1conv256, 1, buffer16, 1); - wait_us(500); - cs = 1; - cs = 0; - spi.write(&readADC, 1, pressBuffer, 4); - cs = 1; - a++; - } - - printf("Start Measurement\n"); - - //double cumsum = 0; - - t.reset(); - t.start(); - //loop values - for (int x = 0; x < N_SAMPLES; x++) { - cs = 0; - spi.write(&D1conv256, 1, buffer16, 1); - wait_us(500); - cs = 1; - - cs = 0; - if (x > 0) { - pData[x-1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3]; - //cumsum = cumsum + pData[x-1]; - } - spi.write(&readADC, 1, pressBuffer, 4); - tData[x] = t.read_us(); - cs = 1; - } - - pData[N_SAMPLES - 1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3]; - - //stop motor and timer - t.stop(); - //motorOn.write(0); - - printf("Stop Measurement\n"); - - //calc temperature - calcT(temp, calData); - long long sum = 0; - - //float avg = calcP(temp, cumsum/(nsample-1), calData); + int iter_counter = 0; //Iteration counter + iter_timer.start(); - //printf("avg %f\n\r", avg); - - //output date to PC - printf("$CAL %hu %hu %hu %hu %hu %hu\n", calData[0], calData[1], calData[2], calData[3], calData[4], calData[5]); - + //program loop + while(1) { + sample_timer.reset(); + sample_timer.start(); + + float temperature_raw = pressure_sensor.getRawTemperature(); + + //Capture raw pressure samples for(int i = 0; i < N_SAMPLES; i++) { - printf("$%d\n", pData[i]); - } - - printf("$COMPLETE\n"); - - /* - //for crossing detection - bool cflag1 = (avg > pData[0]); - bool cflag2 = (avg > pData[1]); - int numcross = 0; - int lastcrosspoint = 0; - unsigned int crosstart; - unsigned int crossend; - float freq = 0; - - //smooth data array 20 sample size - for (int y=0; y< nsample-1; y++) { - if (9< y && y <nsample-10) { - sum = 0; - for (int z=0; z<20; z++) { - sum = sum + pData[y-10+z]; + p_data_raw[i] = pressure_sensor.calculatePressure(pressure_sensor.getRawPressure(), temperature_raw); + p_data[i] = p_data_raw[i]; } - - spData[y] = (float) calcP(temp, sum/20, calData); - - //detect avg crossings for frequency calc - if (y>10) { - cflag2 = (avg > spData[y]); - cflag1 = (avg > spData[y-1]); - - if (cflag1 != cflag2) { - if(lastcrosspoint<=(y-10)) { - numcross++; - lastcrosspoint = y; - - if (numcross == 1) { - crosstart = tData[y]; - } - - crossend = tData[y]; - } - } + + sample_timer.stop(); + + //http://www.keil.com/pack/doc/CMSIS/DSP/html/group__RealFFT.html + //Compute the RFFT + // + //p_data is trashed in the process + //Result is packaged as [real_0, real_N/2, real_1, imag_1, real_2, imag_2 ... real_N/2-1, imag_N/2-1] + arm_rfft_fast_init_f32(&rfft, N_SAMPLES); + arm_rfft_fast_f32(&rfft, p_data, f_data, 0); + + //http://www.keil.com/pack/doc/CMSIS/DSP/html/group__cmplx__mag.html + //Convert to magntiude, skip over the DC and fundamental terms + + arm_cmplx_mag_f32(&f_data[2], &mag_data[1], (N_SAMPLES / 2) - 1); + //Fill in the first and last terms from the first entry in f_data + 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; } - //calc pressure & display list - //printf("%d\t%d\t%d\n\r", tData[y], calcP(temp, spData[y], calData), calcP(temp, pData[y], calData)); - printf("%d\t%f\n\r", tData[y],spData[y]); - } else if (10> y || y >nsample-10) { - spData[y] = (float) calcP(temp, pData[y], calData); - } - - - } - - printf("numcross = %d\n\r", numcross); - printf("crosstart = %d\n\r", crosstart); - printf("crossend = %d\n\r", crossend); - - freq = (1e6 *numcross)/(2*(crossend - crosstart)); - printf("frequency = %f\n\r", freq); - - for (int z=0; z< nsample-1; z++) { - - spData[z] = spData[z] -avg; - } - - printf("Repeat Blockage Test?\n\rPress Enter to continue\n\r"); - - while(1) { - if (pc.getc() != 0) { - break; - } - } - */ + + printf("%10.5g\n", average); + + //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; + + //break; } } \ No newline at end of file