pachas
Dependencies: mbed QEI FastPWM
Diff: pressure_sensors.cpp
- Revision:
- 9:95fdcdc0977e
- Parent:
- 8:208d965a3bd2
- Child:
- 11:5cb7ae8bd831
diff -r 208d965a3bd2 -r 95fdcdc0977e pressure_sensors.cpp --- a/pressure_sensors.cpp Thu Apr 23 17:05:36 2020 +0000 +++ b/pressure_sensors.cpp Sun Apr 26 08:50:12 2020 +0000 @@ -14,6 +14,7 @@ int16_t pressure_01_bin, pressure_02_bin; float pressure_01_psi, pressure_02_psi; float flow_lpm, volume_ml; +float lpf_01_prev_out, lpf_02_prev_out; //float diff_pressure_psi; @@ -22,33 +23,73 @@ /* Function definition */ void Pressure_Sensors_Initialize(void){ + + int16_t sensor_01_aux, sensor_02_aux; + pressure_sensor_01.frequency(PRESSURE_SENSOR_I2C_CLK_HZ ); pressure_sensor_02.frequency(PRESSURE_SENSOR_I2C_CLK_HZ ); + + /* Perform an initial measurement */ + + pressure_sensor_01.read(PRESSURE_SENSOR_01_SRA, sensor_read_data_buffer, 2); + sensor_01_aux = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + + if(sensor_01_aux < PRESSURE_SENSOR_OUTPUT_MIN){ + sensor_01_aux = PRESSURE_SENSOR_OUTPUT_MIN; + } + + pressure_sensor_02.read(PRESSURE_SENSOR_02_SRA, sensor_read_data_buffer, 2); + sensor_02_aux = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + + if(sensor_02_aux < PRESSURE_SENSOR_OUTPUT_MIN){ + sensor_02_aux = PRESSURE_SENSOR_OUTPUT_MIN; + } + + lpf_01_prev_out = (float)sensor_01_aux; + lpf_02_prev_out = (float)sensor_02_aux; + } void Pressure_Sensors_Read(void){ + int16_t raw_pressure_01_bin, raw_pressure_02_bin; + /* Read binary values */ pressure_sensor_01.read(PRESSURE_SENSOR_01_SRA, sensor_read_data_buffer, 2); - pressure_01_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + raw_pressure_01_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + + if(raw_pressure_01_bin < PRESSURE_SENSOR_OUTPUT_MIN){ + raw_pressure_01_bin = PRESSURE_SENSOR_OUTPUT_MIN; + } + pressure_sensor_02.read(PRESSURE_SENSOR_02_SRA, sensor_read_data_buffer, 2); - pressure_02_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + raw_pressure_02_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF; + + if(raw_pressure_02_bin < PRESSURE_SENSOR_OUTPUT_MIN){ + raw_pressure_02_bin = PRESSURE_SENSOR_OUTPUT_MIN; + } + + /* Filter binary values */ + pressure_01_bin = Low_Pass_Filter_01(raw_pressure_01_bin); + pressure_02_bin = Low_Pass_Filter_02(raw_pressure_02_bin); /* Calculate pressure values in PSI */ - pressure_01_psi = (5.0 * pressure_01_bin) / 16383; - pressure_02_psi = (5.0 * pressure_02_bin) / 16383; + pressure_01_psi = (pressure_01_bin - PRESSURE_SENSOR_OUTPUT_MIN) * (PRESSURE_SENSOR_PRESSURE_MAX - PRESSURE_SENSOR_PRESSURE_MIN); + pressure_01_psi = (pressure_01_psi / (PRESSURE_SENSOR_OUTPUT_MAX - PRESSURE_SENSOR_OUTPUT_MIN)) + PRESSURE_SENSOR_PRESSURE_MIN; + //pressure_01_psi += PRESSURE_SENSOR_OFFSET_PSI; + pressure_02_psi = (pressure_02_bin - PRESSURE_SENSOR_OUTPUT_MIN) * (PRESSURE_SENSOR_PRESSURE_MAX - PRESSURE_SENSOR_PRESSURE_MIN); + pressure_02_psi = (pressure_02_psi / (PRESSURE_SENSOR_OUTPUT_MAX - PRESSURE_SENSOR_OUTPUT_MIN)) + PRESSURE_SENSOR_PRESSURE_MIN; + //pressure_02_psi += PRESSURE_SENSOR_OFFSET_PSI; +} - -} + void Calculate_Flow_From_Pressure(void){ - int16_t diff_pressure_bin; float diff_pressure_psi; - diff_pressure_bin = pressure_02_bin - pressure_01_bin; - diff_pressure_psi = (5.0 * diff_pressure_bin) / 16383; + diff_pressure_psi = pressure_02_psi - pressure_01_psi; if(diff_pressure_psi < 0){ flow_lpm = -SPIROMETER_SENSOR_CONSTANT * sqrt(-diff_pressure_psi); @@ -70,11 +111,30 @@ } -void Pressure_01_LPF(void){ +int16_t Low_Pass_Filter_01(int16_t raw_value){ + + float lpf_out, sample_period_sec, div; + + sample_period_sec = 0.001 * ((float)PRESSURE_SENSOR_READ_PERIOD_MS); + div = sample_period_sec + LPF_RC_CONSTANT_SEC; + + lpf_out = (((float)raw_value) * (sample_period_sec / div)) + (lpf_01_prev_out * (LPF_RC_CONSTANT_SEC / div)); + lpf_01_prev_out = lpf_out; + + return ((int16_t)lpf_out); } -void Pressure_02_LPF(void){ +int16_t Low_Pass_Filter_02(int16_t raw_value){ + float lpf_out, sample_period_sec, div; + + sample_period_sec = 0.001 * ((float)PRESSURE_SENSOR_READ_PERIOD_MS); + div = sample_period_sec + LPF_RC_CONSTANT_SEC; + + lpf_out = (((float)raw_value) * (sample_period_sec / div)) + (lpf_02_prev_out * (LPF_RC_CONSTANT_SEC / div)); + lpf_02_prev_out = lpf_out; + + return ((int16_t)lpf_out); }