asdf

Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of TestBenchSerenity-proto_F429ZI by Ian Wolf

Revision:
0:67debf2ccbc2
Child:
1:d58df8cb271d
--- /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);
+    }
+}
+