Working code for pc app 12/01/2018 commit
Dependencies: mbed MS5607 mbed-dsp
Fork of Turrentine_Code by
main.cpp
- Committer:
- intrinseca
- Date:
- 2018-01-25
- Revision:
- 3:c80aa39db5bb
- Parent:
- 2:3d3e21c907e4
- Child:
- 4:d4804771134a
File content as of revision 3:c80aa39db5bb:
#include "mbed.h" #include "MS5607SPI.h" #include "arm_math.h" #define M_PI 3.14159265358979323846 #define N_SAMPLES 1024 //mbed class def Serial pc(USBTX, USBRX); // tx, rx MS5607SPI pressure_sensor(p5, p6, p7, p8); // mosi, miso, sclk, cs BusOut leds(LED1, LED2, LED3, LED4); PwmOut motor(p26); Timer sample_timer; Timer iter_timer; 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 tData[N_SAMPLES]; arm_rfft_fast_instance_f32 rfft; float duty = 1; int round(float number) { return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); } int main() { printf("Turrentine\n"); //Configure PC serial link pc.baud(115200); //Configure motor PWM motor = 0; motor.period_ms(10); printf("Pump On\n"); //turn pump on motor.write(duty); printf("Start loop\n"); int iter_counter = 0; //Iteration counter iter_timer.start(); //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++) { p_data_raw[i] = pressure_sensor.calculatePressure(pressure_sensor.getRawPressure(), temperature_raw); p_data[i] = p_data_raw[i]; } 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; } 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; } }