Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of PUMP_SCRIPT1 by Ian Wolf

Revision:
4:79b23d1fbcd1
Parent:
3:9ff79ea3a294
Child:
5:f2cb21046678
Child:
6:e3eff8a04b25
--- a/main.cpp	Mon Jul 24 15:38:39 2017 +0000
+++ b/main.cpp	Wed Jul 26 14:03:10 2017 +0000
@@ -1,5 +1,37 @@
 #include "main.h"
+//---------------------------
+void dutycycleincrease()
+{
+    dutycycle=dutycycle+0.01;
+}
 
+void dutycycledecrease()
+{
+    dutycycle=dutycycle-0.01;
+}
+
+void frequencyincrease()
+{
+    frequency=frequency+1;
+}
+void frequencydecrease()
+{
+    frequency=frequency-1;
+}
+void injectorvalvecontrol(){
+
+while(true){
+float openvalue=(((1/frequency)*dutycycle));
+float offvalue=(1/frequency)-openvalue;
+    
+injector.on();
+wait(openvalue);
+injector.off();
+wait(offvalue);
+}
+}
+
+//-----------------------------
 void pumpTachTrigger()
 {
     pumpTachCounts++;
@@ -16,7 +48,7 @@
 {
     pump.period(.001); // 1kHz PWM
     pump = 0;
-    ledGrn.period(.001);
+    //ledGrn.period(.001);
 
     InterruptIn pumpTach(pumpTachPin);
     pumpTach.rise(&pumpTachTrigger);
@@ -40,7 +72,7 @@
         //PID calculation and set the new output value.
         pump = pump_control_PID.compute();
         //pump = 0.1;
-        ledGrn = ((float)1.0-pump.read());
+        //ledGrn = ((float)1.0-pump.read());
     }
 }
 
@@ -53,7 +85,7 @@
         i2c1_m.lock();
         timer.reset();
         error = pumpPressure.readPT();
-        error |= mixerPressure.readPT();
+        //error |= mixerPressure.readPT();
         int wait = (200 - timer.read_ms());
         i2c1_m.unlock();
         Thread::wait(wait);
@@ -61,7 +93,7 @@
     }
 }
 
-void update_airflow()
+void update_flow1()
 {
     Timer timer;
     timer.start();
@@ -69,35 +101,29 @@
     while (true) {
         i2c2_m.lock();
         timer.reset();
-        error = sfm7033.Measure(FLOW);
+        error = mainflow.Measure(FLOW);
         int wait = (200 - timer.read_ms());
         i2c2_m.unlock();
         Thread::wait(wait);
     }
 }
 
-void update_level()
+//------------------------------------------------
+void update_flow2()
 {
     Timer timer;
     timer.start();
+    char error;
     while (true) {
-        i2c1_m.lock();
+        i2c3_m.lock();
         timer.reset();
-        agentlevel = (float)level;
-        int wait = (1000 - timer.read_ms());
-        i2c1_m.unlock();
+        error = loopflow.Measure(FLOW);
+        int wait = (200 - timer.read_ms());
+        i2c3_m.unlock();
         Thread::wait(wait);
     }
 }
-
-//float get_level()
-//{
-//    i2c1_m.lock();
-//    float value = (float)level;
-//    i2c1_m.unlock();
-//    return value;
-//}
-
+//------------------------------------------------   
 
 void print_process_values()
 {
@@ -108,12 +134,12 @@
         stdio_m.lock();
         timer.reset();
 
-        pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n",
-                  pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, pumpPressure.status,
-                  mixerPressure.pressureKPA, mixerPressure.pressurePSI, mixerPressure.temperatureC, mixerPressure.temperatureF, mixerPressure.status,
+        pc.printf("%.02fkPa %.02fpsi %.02fC %02X %.2f%% %.0fRPM %u %.0f %s %u %.0f %s %.3f %.02fHz %.02f\r\n",
+                  pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, 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);//, agentlevel;
+                  mainflow.flow.u16, (((float)mainflow.flow.i16) / mainflow.scaleFactor.u16), mainflow.flowUnitStr, 
+                  loopflow.flow.u16, (((float)loopflow.flow.i16) / loopflow.scaleFactor.u16), loopflow.flowUnitStr,
+                  ((double)pot2)*pumpMaxPSI, frequency, dutycycle);//, agentlevel;
         int wait = (1000 - timer.read_ms());
 
         stdio_m.unlock();
@@ -121,97 +147,42 @@
     }
 }
 
-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: %.1f", pumpPressure.pressurePSI);
-        lcd.locate(64, 14);
-        lcd.printf("SV: %.1f", ((double)pot2)*pumpMaxPSI);
-        int wait = (1000 - timer.read_ms());
-        stdio_m.unlock();
-        Thread::wait(wait);
-    }
-}
-
-void update_shutoff()
-{
-    Timer timer;
-    timer.start();
-    while (true) {
-        float threshold = 0.1;
-        timer.reset();
-        if((double)pot1 < 0.05) {
-            shutoff.off();
-            //pc.printf("shutoff off\r\n");
-            //Thread::wait(1000);
-        }
-        if((double)pot1 >= 0.1) {
-            shutoff.on();
-            //pc.printf("shutoff on\r\n");
-            //Thread::wait(1000);
-        }
-        int wait = (200 - timer.read_ms());
-        Thread::wait(wait);
-    }
-}
-
 // main() runs in its own thread in the OS
 int main()
 {
     pump_init();
-    ledBlu = 1;
+    //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());*/
+//--------------------------------------------
+dutycycleup.rise(&dutycycleincrease);
+dutycycledown.rise(&dutycycledecrease);
+frequencyup.rise(&frequencyincrease);
+frequencydown.rise(&frequencydecrease);    
 
-
-    // Thread to turn shutoff valve on/off
-    update_shutoff_t.set_priority(osPriorityHigh);
-    update_shutoff_t.start(update_shutoff);
+    //Thread to execute injector valve control
+    Injector_Valve_Control.set_priority(osPriorityHigh);
+    Injector_Valve_Control.start(injectorvalvecontrol);
+//--------------------------------------------
 
     // 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);
+    update_pressures_t.start(update_pressures); 
 
-    // Thread to poll level sensor
-    //update_level_t.set_priority(osPriorityIdle);
-    //update_level_t.start(update_level);
-
-    // Thread to update lcd
-    update_lcd_t.set_priority(osPriorityIdle);
-    update_lcd_t.start(update_lcd);
+    // Thread to poll flow sensor
+    update_flow1_t.set_priority(osPriorityNormal);
+    update_flow1_t.start(update_flow1);
+    
+    //Thread to poll flow sensor
+    update_flow2_t.set_priority(osPriorityNormal);
+    update_flow2_t.start(update_flow2);
 
     // 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);
-        if((double)pot1 >= 0.1) {
-            injector.on();
-        }
-        //pc.printf("shutoff on\r\n");
-        Thread::wait(5);
-
-        injector.off();
-        //pc.printf("shutoff off\r\n");*/
-        Thread::wait(495);
+         pump_tach_update();
+        
     }
 }