Ian Wolf / Mbed OS TestBenchFlow1

Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of TestBenchFlow by Ian Wolf

Files at this revision

API Documentation at this revision

Comitter:
dmwahl
Date:
Fri Jul 07 20:52:31 2017 +0000
Child:
1:d58df8cb271d
Commit message:
Pressure sensors, display, airflow, input pots working

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
C12832A1Z.lib Show annotated file Show diff for this revision Revisions of this file
KellerDruck_pressure.lib Show annotated file Show diff for this revision Revisions of this file
PID.lib Show annotated file Show diff for this revision Revisions of this file
PWM-Coil-driver.lib Show annotated file Show diff for this revision Revisions of this file
Sensirion_SF04.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
pin_defs_F429.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832A1Z.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/sstaub/code/C12832A1Z/#41800273e715
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KellerDruck_pressure.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/dmwahl/code/KellerDruck_pressure/#c298ec31dd93
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/aberk/code/PID/#6e12a3e5af19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PWM-Coil-driver.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/dmwahl/code/PWM-Coil-driver/#3dab9e2441e3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensirion_SF04.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/dmwahl/code/Sensirion_SF04/#1243b362ece8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,155 @@
+#include "main.h"
+
+void pumpTachTrigger()
+{
+    pumpTachCounts++;
+}
+
+void pump_tach_update()
+{
+    float i = pumpTachCounts; // In case it triggers mid-calculation
+    pumpTachCounts = 0;
+    pumpRpm = (i/pumpTachPoles)*60;
+}
+
+void pump_init()
+{
+    pump.period(.001); // 1kHz PWM
+    pump = 0;
+    ledGrn.period(.001);
+    
+    InterruptIn pumpTach(pumpTachPin);
+    pumpTach.rise(&pumpTachTrigger);
+    
+    pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
+    pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
+    pump_control_PID.setMode(AUTO_MODE);
+    pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
+}
+
+void pump_pid_update(char error)
+{
+    if (pumpPressure.status != 0x40) {
+        pump = 0;
+        pump_control_PID.reset();
+    } else {
+        pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);
+
+        //Update the process variable.
+        pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
+        //PID calculation and set the new output value.
+        pump = pump_control_PID.compute();
+        //pump = 0.1;
+        ledGrn = ((float)1.0-pump.read());
+    }
+}
+
+void update_pressures()
+{
+    Timer timer;
+    timer.start();
+    char error;
+    while (true) {
+        i2c1_m.lock();
+        timer.reset();
+        error = pumpPressure.readPT();
+        int wait = (200 - timer.read_ms());
+        i2c1_m.unlock();
+        Thread::wait(wait);
+        pump_pid_update(error);
+    }
+}
+
+void update_airflow()
+{
+    Timer timer;
+    timer.start();
+    char error;
+    while (true) {
+        i2c2_m.lock();
+        timer.reset();
+        error = sfm7033.Measure(FLOW);
+        int wait = (200 - timer.read_ms());
+        i2c2_m.unlock();
+        Thread::wait(wait);
+    }
+}
+
+void print_process_values()
+{
+    //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
+    Timer timer;
+    timer.start();
+    while (true) {
+        stdio_m.lock();
+        timer.reset();
+        /*pc.printf("%.3fkPa %.2fPSI %.1fC %.1fF %02X %.2f%% %.0fRPM %.0f %s %.1f %.3f\n\r",
+                  pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC,
+                  pumpPressure.temperatureF, pumpPressure.status, pump.read()*100, pumpRpm,
+                  ((float)sfm7033.flow.i16 / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, (double)pot1*18, ((double)pot2-.002)*pumpMaxPSI);*/
+
+        pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n",
+                  pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, pumpPressure.status, pump.read()*100, pumpRpm, sfm7033.flow.u16, (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, (double)pot1*18, ((double)pot2)*pumpMaxPSI);
+        int wait = (1000 - timer.read_ms());
+        stdio_m.unlock();
+        Thread::wait(wait);
+    }
+}
+
+void update_lcd()
+{
+    Timer timer;
+    timer.start();
+    while (true) {
+        float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16));
+        flow = flow/1000;
+        stdio_m.lock();
+        timer.reset();
+        lcd.cls();
+        lcd.font((unsigned char*)ArialR12x14);
+        lcd.locate(0, 0);
+        lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18);
+        lcd.locate(0, 14);
+        lcd.printf("PV: %.0f", pumpPressure.pressurePSI);
+        lcd.locate(64, 14);
+        lcd.printf("SV: %.0f", ((double)pot2)*pumpMaxPSI);
+        int wait = (1000 - timer.read_ms());
+        stdio_m.unlock();
+        Thread::wait(wait);
+    }
+}
+
+// main() runs in its own thread in the OS
+int main()
+{
+    pump_init();
+    ledBlu = 1;
+    pc.printf("Serenity Starting up...\n\r");
+    /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax);
+    pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode);
+    pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/
+
+    // Thread to poll pressure sensors
+    update_pressures_t.set_priority(osPriorityNormal);
+    update_pressures_t.start(update_pressures);
+
+    // Thread to poll airflow sensor
+    update_airflow_t.set_priority(osPriorityNormal);
+    update_airflow_t.start(update_airflow);
+
+    // Thread to update lcd
+    update_lcd_t.set_priority(osPriorityIdle);
+    update_lcd_t.start(update_lcd);
+
+    // Thread to send process values to serial port
+    print_process_values_t.set_priority(osPriorityLow);
+    print_process_values_t.start(&print_process_values);
+
+
+
+    while (true) {
+        //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF);
+        Thread::wait(1000);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,84 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+#include "mbed.h"
+#include "keller_pressure.h"
+#include "sensirion_sf04.h"
+#include "PID.h"
+#include "coil-driver.h"
+
+// Mbed application shield display
+#include "C12832A1Z.h"
+#include "Small_7.h"
+#include "Fonts/ArialR12x14.h"
+
+C12832A1Z lcd(D11, D13, D12, D7, D10); // MOSI, SCK, Reset, A0, CS
+// LCD width and height (minus 1)
+#define lcdWidth 127
+#define lcdHeight 31
+
+// ISO/SEV pressure: 10psi, DES: 25-30psi
+#define pumpSetPointPSI 45
+#define pumpMinPSI 0
+#define pumpMaxPSI 85
+//#define pumpHystPSI 2
+
+// Gas flow sensor defines
+#define SFM7033_ADDR 0x40
+// End gas flow sensor defines
+
+// Liquid pump defines
+// PID settings
+#define pumpPIDRate .2 // Pump PID computation interval (seconds)
+float pumpKp = 4.0;
+float pumpKi = 1.0;
+#define pumpKd 0
+
+#define pumpPwmFrequency 1000 // Frequency of PWM signal supplied to pump
+#define pumpTachPoles 6 // 6 pulses per revolution
+#define pumpTachPin PE_7 // Pump tach input (green wire)
+#define pumpCtrlPin PE_10 // Pump control (white wire)
+// End Liquid pump defines
+
+Thread print_process_values_t, update_pressures_t, update_airflow_t, update_lcd_t;
+Mutex i2c1_m, i2c2_m, stdio_m;
+
+//DigitalOut myled(LED2);
+Serial pc(USBTX, USBRX, 250000); // tx, rx, baud
+
+// an I2C sub-class that provides a constructed default
+class I2CPreInit : public I2C
+{
+public:
+    I2CPreInit(PinName sda, PinName scl, int freq) : I2C(sda, scl) {
+        frequency(freq);
+    };
+};
+
+//I2CPreInit gI2C1(I2C_SDA, I2C_SCL, I2C frequency);
+I2CPreInit i2c1(PB_9, PB_8, 100000);
+I2CPreInit i2c2(PB_11, PB_10, 100000);
+
+KELLER_PRESSURE pumpPressure(i2c1, 0x40);
+
+// Sensirion gas flow sensor object (i2c object, i2c address, calibration field, resolution 9-16 bit)
+// calibration field 1: Air, 2: O2, 3: N2O
+SF04 sfm7033(i2c2, SFM7033_ADDR, 1, 12); // Gas flow sensor (SFM7033)
+
+// Mbed application shield
+AnalogIn pot1(A0);
+AnalogIn pot2(A1);
+
+PwmOut pump(pumpCtrlPin);
+//pump.period(.001);
+
+PID pump_control_PID(pumpKp, pumpKi, pumpKd, pumpPIDRate);
+
+//InterruptIn pumpTach(pumpTachPin);
+volatile int pumpTachCounts = 0;
+volatile float pumpRpm = 0;
+
+PwmOut ledRed(PE_11);
+PwmOut ledGrn(PD_15);
+DigitalOut ledBlu(PF_12);
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Jul 07 20:52:31 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#269f58d75b752a4e67a6a2d8c5c698635ffd6752