Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of HSPFLOWFINAL by jack kemnitz

Committer:
dmwahl
Date:
Mon Jul 24 15:38:39 2017 +0000
Revision:
3:9ff79ea3a294
Parent:
1:d58df8cb271d
Child:
4:79b23d1fbcd1
Shutoff and injector valve running

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 0:67debf2ccbc2 1 #include "main.h"
dmwahl 0:67debf2ccbc2 2
dmwahl 0:67debf2ccbc2 3 void pumpTachTrigger()
dmwahl 0:67debf2ccbc2 4 {
dmwahl 0:67debf2ccbc2 5 pumpTachCounts++;
dmwahl 0:67debf2ccbc2 6 }
dmwahl 0:67debf2ccbc2 7
dmwahl 0:67debf2ccbc2 8 void pump_tach_update()
dmwahl 0:67debf2ccbc2 9 {
dmwahl 0:67debf2ccbc2 10 float i = pumpTachCounts; // In case it triggers mid-calculation
dmwahl 0:67debf2ccbc2 11 pumpTachCounts = 0;
dmwahl 0:67debf2ccbc2 12 pumpRpm = (i/pumpTachPoles)*60;
dmwahl 0:67debf2ccbc2 13 }
dmwahl 0:67debf2ccbc2 14
dmwahl 0:67debf2ccbc2 15 void pump_init()
dmwahl 0:67debf2ccbc2 16 {
dmwahl 0:67debf2ccbc2 17 pump.period(.001); // 1kHz PWM
dmwahl 0:67debf2ccbc2 18 pump = 0;
dmwahl 0:67debf2ccbc2 19 ledGrn.period(.001);
dmwahl 1:d58df8cb271d 20
dmwahl 0:67debf2ccbc2 21 InterruptIn pumpTach(pumpTachPin);
dmwahl 0:67debf2ccbc2 22 pumpTach.rise(&pumpTachTrigger);
dmwahl 1:d58df8cb271d 23
dmwahl 0:67debf2ccbc2 24 pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
dmwahl 0:67debf2ccbc2 25 pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
dmwahl 0:67debf2ccbc2 26 pump_control_PID.setMode(AUTO_MODE);
dmwahl 0:67debf2ccbc2 27 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
dmwahl 0:67debf2ccbc2 28 }
dmwahl 0:67debf2ccbc2 29
dmwahl 0:67debf2ccbc2 30 void pump_pid_update(char error)
dmwahl 0:67debf2ccbc2 31 {
dmwahl 0:67debf2ccbc2 32 if (pumpPressure.status != 0x40) {
dmwahl 0:67debf2ccbc2 33 pump = 0;
dmwahl 0:67debf2ccbc2 34 pump_control_PID.reset();
dmwahl 0:67debf2ccbc2 35 } else {
dmwahl 0:67debf2ccbc2 36 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);
dmwahl 0:67debf2ccbc2 37
dmwahl 0:67debf2ccbc2 38 //Update the process variable.
dmwahl 0:67debf2ccbc2 39 pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
dmwahl 0:67debf2ccbc2 40 //PID calculation and set the new output value.
dmwahl 0:67debf2ccbc2 41 pump = pump_control_PID.compute();
dmwahl 0:67debf2ccbc2 42 //pump = 0.1;
dmwahl 0:67debf2ccbc2 43 ledGrn = ((float)1.0-pump.read());
dmwahl 0:67debf2ccbc2 44 }
dmwahl 0:67debf2ccbc2 45 }
dmwahl 0:67debf2ccbc2 46
dmwahl 0:67debf2ccbc2 47 void update_pressures()
dmwahl 0:67debf2ccbc2 48 {
dmwahl 0:67debf2ccbc2 49 Timer timer;
dmwahl 0:67debf2ccbc2 50 timer.start();
dmwahl 0:67debf2ccbc2 51 char error;
dmwahl 0:67debf2ccbc2 52 while (true) {
dmwahl 0:67debf2ccbc2 53 i2c1_m.lock();
dmwahl 0:67debf2ccbc2 54 timer.reset();
dmwahl 0:67debf2ccbc2 55 error = pumpPressure.readPT();
dmwahl 3:9ff79ea3a294 56 error |= mixerPressure.readPT();
dmwahl 0:67debf2ccbc2 57 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 58 i2c1_m.unlock();
dmwahl 0:67debf2ccbc2 59 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 60 pump_pid_update(error);
dmwahl 0:67debf2ccbc2 61 }
dmwahl 0:67debf2ccbc2 62 }
dmwahl 0:67debf2ccbc2 63
dmwahl 0:67debf2ccbc2 64 void update_airflow()
dmwahl 0:67debf2ccbc2 65 {
dmwahl 0:67debf2ccbc2 66 Timer timer;
dmwahl 0:67debf2ccbc2 67 timer.start();
dmwahl 0:67debf2ccbc2 68 char error;
dmwahl 0:67debf2ccbc2 69 while (true) {
dmwahl 0:67debf2ccbc2 70 i2c2_m.lock();
dmwahl 0:67debf2ccbc2 71 timer.reset();
dmwahl 0:67debf2ccbc2 72 error = sfm7033.Measure(FLOW);
dmwahl 0:67debf2ccbc2 73 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 74 i2c2_m.unlock();
dmwahl 0:67debf2ccbc2 75 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 76 }
dmwahl 0:67debf2ccbc2 77 }
dmwahl 0:67debf2ccbc2 78
dmwahl 1:d58df8cb271d 79 void update_level()
dmwahl 1:d58df8cb271d 80 {
dmwahl 1:d58df8cb271d 81 Timer timer;
dmwahl 1:d58df8cb271d 82 timer.start();
dmwahl 1:d58df8cb271d 83 while (true) {
dmwahl 1:d58df8cb271d 84 i2c1_m.lock();
dmwahl 1:d58df8cb271d 85 timer.reset();
dmwahl 1:d58df8cb271d 86 agentlevel = (float)level;
dmwahl 1:d58df8cb271d 87 int wait = (1000 - timer.read_ms());
dmwahl 1:d58df8cb271d 88 i2c1_m.unlock();
dmwahl 1:d58df8cb271d 89 Thread::wait(wait);
dmwahl 1:d58df8cb271d 90 }
dmwahl 1:d58df8cb271d 91 }
dmwahl 1:d58df8cb271d 92
dmwahl 1:d58df8cb271d 93 //float get_level()
dmwahl 1:d58df8cb271d 94 //{
dmwahl 1:d58df8cb271d 95 // i2c1_m.lock();
dmwahl 1:d58df8cb271d 96 // float value = (float)level;
dmwahl 1:d58df8cb271d 97 // i2c1_m.unlock();
dmwahl 1:d58df8cb271d 98 // return value;
dmwahl 1:d58df8cb271d 99 //}
dmwahl 1:d58df8cb271d 100
dmwahl 1:d58df8cb271d 101
dmwahl 0:67debf2ccbc2 102 void print_process_values()
dmwahl 0:67debf2ccbc2 103 {
dmwahl 0:67debf2ccbc2 104 //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
dmwahl 0:67debf2ccbc2 105 Timer timer;
dmwahl 0:67debf2ccbc2 106 timer.start();
dmwahl 0:67debf2ccbc2 107 while (true) {
dmwahl 0:67debf2ccbc2 108 stdio_m.lock();
dmwahl 0:67debf2ccbc2 109 timer.reset();
dmwahl 0:67debf2ccbc2 110
dmwahl 3:9ff79ea3a294 111 pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n",
dmwahl 3:9ff79ea3a294 112 pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, pumpPressure.status,
dmwahl 3:9ff79ea3a294 113 mixerPressure.pressureKPA, mixerPressure.pressurePSI, mixerPressure.temperatureC, mixerPressure.temperatureF, mixerPressure.status,
dmwahl 3:9ff79ea3a294 114 pump.read()*100, pumpRpm,
dmwahl 1:d58df8cb271d 115 sfm7033.flow.u16, (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr,
dmwahl 1:d58df8cb271d 116 (double)pot1*18, ((double)pot2)*pumpMaxPSI);//, agentlevel;
dmwahl 0:67debf2ccbc2 117 int wait = (1000 - timer.read_ms());
dmwahl 1:d58df8cb271d 118
dmwahl 0:67debf2ccbc2 119 stdio_m.unlock();
dmwahl 0:67debf2ccbc2 120 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 121 }
dmwahl 0:67debf2ccbc2 122 }
dmwahl 0:67debf2ccbc2 123
dmwahl 0:67debf2ccbc2 124 void update_lcd()
dmwahl 0:67debf2ccbc2 125 {
dmwahl 0:67debf2ccbc2 126 Timer timer;
dmwahl 0:67debf2ccbc2 127 timer.start();
dmwahl 0:67debf2ccbc2 128 while (true) {
dmwahl 0:67debf2ccbc2 129 float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16));
dmwahl 0:67debf2ccbc2 130 flow = flow/1000;
dmwahl 0:67debf2ccbc2 131 stdio_m.lock();
dmwahl 0:67debf2ccbc2 132 timer.reset();
dmwahl 0:67debf2ccbc2 133 lcd.cls();
dmwahl 0:67debf2ccbc2 134 lcd.font((unsigned char*)ArialR12x14);
dmwahl 0:67debf2ccbc2 135 lcd.locate(0, 0);
dmwahl 0:67debf2ccbc2 136 lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18);
dmwahl 0:67debf2ccbc2 137 lcd.locate(0, 14);
dmwahl 1:d58df8cb271d 138 lcd.printf("PV: %.1f", pumpPressure.pressurePSI);
dmwahl 0:67debf2ccbc2 139 lcd.locate(64, 14);
dmwahl 1:d58df8cb271d 140 lcd.printf("SV: %.1f", ((double)pot2)*pumpMaxPSI);
dmwahl 0:67debf2ccbc2 141 int wait = (1000 - timer.read_ms());
dmwahl 0:67debf2ccbc2 142 stdio_m.unlock();
dmwahl 0:67debf2ccbc2 143 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 144 }
dmwahl 0:67debf2ccbc2 145 }
dmwahl 0:67debf2ccbc2 146
dmwahl 1:d58df8cb271d 147 void update_shutoff()
dmwahl 1:d58df8cb271d 148 {
dmwahl 1:d58df8cb271d 149 Timer timer;
dmwahl 1:d58df8cb271d 150 timer.start();
dmwahl 1:d58df8cb271d 151 while (true) {
dmwahl 1:d58df8cb271d 152 float threshold = 0.1;
dmwahl 1:d58df8cb271d 153 timer.reset();
dmwahl 1:d58df8cb271d 154 if((double)pot1 < 0.05) {
dmwahl 1:d58df8cb271d 155 shutoff.off();
dmwahl 3:9ff79ea3a294 156 //pc.printf("shutoff off\r\n");
dmwahl 1:d58df8cb271d 157 //Thread::wait(1000);
dmwahl 1:d58df8cb271d 158 }
dmwahl 1:d58df8cb271d 159 if((double)pot1 >= 0.1) {
dmwahl 1:d58df8cb271d 160 shutoff.on();
dmwahl 3:9ff79ea3a294 161 //pc.printf("shutoff on\r\n");
dmwahl 1:d58df8cb271d 162 //Thread::wait(1000);
dmwahl 1:d58df8cb271d 163 }
dmwahl 1:d58df8cb271d 164 int wait = (200 - timer.read_ms());
dmwahl 1:d58df8cb271d 165 Thread::wait(wait);
dmwahl 1:d58df8cb271d 166 }
dmwahl 1:d58df8cb271d 167 }
dmwahl 1:d58df8cb271d 168
dmwahl 0:67debf2ccbc2 169 // main() runs in its own thread in the OS
dmwahl 0:67debf2ccbc2 170 int main()
dmwahl 0:67debf2ccbc2 171 {
dmwahl 0:67debf2ccbc2 172 pump_init();
dmwahl 0:67debf2ccbc2 173 ledBlu = 1;
dmwahl 0:67debf2ccbc2 174 pc.printf("Serenity Starting up...\n\r");
dmwahl 0:67debf2ccbc2 175 /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax);
dmwahl 0:67debf2ccbc2 176 pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode);
dmwahl 0:67debf2ccbc2 177 pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/
dmwahl 0:67debf2ccbc2 178
dmwahl 1:d58df8cb271d 179
dmwahl 1:d58df8cb271d 180 // Thread to turn shutoff valve on/off
dmwahl 1:d58df8cb271d 181 update_shutoff_t.set_priority(osPriorityHigh);
dmwahl 1:d58df8cb271d 182 update_shutoff_t.start(update_shutoff);
dmwahl 1:d58df8cb271d 183
dmwahl 0:67debf2ccbc2 184 // Thread to poll pressure sensors
dmwahl 0:67debf2ccbc2 185 update_pressures_t.set_priority(osPriorityNormal);
dmwahl 0:67debf2ccbc2 186 update_pressures_t.start(update_pressures);
dmwahl 0:67debf2ccbc2 187
dmwahl 0:67debf2ccbc2 188 // Thread to poll airflow sensor
dmwahl 0:67debf2ccbc2 189 update_airflow_t.set_priority(osPriorityNormal);
dmwahl 0:67debf2ccbc2 190 update_airflow_t.start(update_airflow);
dmwahl 0:67debf2ccbc2 191
dmwahl 1:d58df8cb271d 192 // Thread to poll level sensor
dmwahl 1:d58df8cb271d 193 //update_level_t.set_priority(osPriorityIdle);
dmwahl 1:d58df8cb271d 194 //update_level_t.start(update_level);
dmwahl 1:d58df8cb271d 195
dmwahl 0:67debf2ccbc2 196 // Thread to update lcd
dmwahl 0:67debf2ccbc2 197 update_lcd_t.set_priority(osPriorityIdle);
dmwahl 0:67debf2ccbc2 198 update_lcd_t.start(update_lcd);
dmwahl 0:67debf2ccbc2 199
dmwahl 0:67debf2ccbc2 200 // Thread to send process values to serial port
dmwahl 0:67debf2ccbc2 201 print_process_values_t.set_priority(osPriorityLow);
dmwahl 0:67debf2ccbc2 202 print_process_values_t.start(&print_process_values);
dmwahl 0:67debf2ccbc2 203
dmwahl 0:67debf2ccbc2 204 while (true) {
dmwahl 0:67debf2ccbc2 205 //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF);
dmwahl 3:9ff79ea3a294 206 if((double)pot1 >= 0.1) {
dmwahl 3:9ff79ea3a294 207 injector.on();
dmwahl 3:9ff79ea3a294 208 }
dmwahl 3:9ff79ea3a294 209 //pc.printf("shutoff on\r\n");
dmwahl 3:9ff79ea3a294 210 Thread::wait(5);
dmwahl 1:d58df8cb271d 211
dmwahl 3:9ff79ea3a294 212 injector.off();
dmwahl 3:9ff79ea3a294 213 //pc.printf("shutoff off\r\n");*/
dmwahl 3:9ff79ea3a294 214 Thread::wait(495);
dmwahl 0:67debf2ccbc2 215 }
dmwahl 0:67debf2ccbc2 216 }
dmwahl 0:67debf2ccbc2 217