Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04
main.cpp@4:49852dc27066, 2017-09-08 (annotated)
- Committer:
- dmwahl
- Date:
- Fri Sep 08 21:27:10 2017 +0000
- Revision:
- 4:49852dc27066
- Parent:
- 3:9ff79ea3a294
- Child:
- 5:0df92010e19b
Pressure setpoint rounded to nearest integer
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| dmwahl | 0:67debf2ccbc2 | 1 | #include "main.h" | 
| dmwahl | 0:67debf2ccbc2 | 2 | |
| dmwahl | 4:49852dc27066 | 3 | void pump_tach_trigger() | 
| dmwahl | 0:67debf2ccbc2 | 4 | { | 
| dmwahl | 4:49852dc27066 | 5 | //pumpTachCounts++; | 
| dmwahl | 4:49852dc27066 | 6 | ledRed = !ledRed; | 
| dmwahl | 0:67debf2ccbc2 | 7 | } | 
| dmwahl | 0:67debf2ccbc2 | 8 | |
| dmwahl | 0:67debf2ccbc2 | 9 | void pump_tach_update() | 
| dmwahl | 0:67debf2ccbc2 | 10 | { | 
| dmwahl | 0:67debf2ccbc2 | 11 | float i = pumpTachCounts; // In case it triggers mid-calculation | 
| dmwahl | 0:67debf2ccbc2 | 12 | pumpTachCounts = 0; | 
| dmwahl | 0:67debf2ccbc2 | 13 | pumpRpm = (i/pumpTachPoles)*60; | 
| dmwahl | 0:67debf2ccbc2 | 14 | } | 
| dmwahl | 0:67debf2ccbc2 | 15 | |
| dmwahl | 0:67debf2ccbc2 | 16 | void pump_init() | 
| dmwahl | 0:67debf2ccbc2 | 17 | { | 
| dmwahl | 0:67debf2ccbc2 | 18 | pump.period(.001); // 1kHz PWM | 
| dmwahl | 0:67debf2ccbc2 | 19 | pump = 0; | 
| dmwahl | 0:67debf2ccbc2 | 20 | ledGrn.period(.001); | 
| dmwahl | 1:d58df8cb271d | 21 | |
| dmwahl | 4:49852dc27066 | 22 | //InterruptIn pumpTach(pumpTachPin); | 
| dmwahl | 4:49852dc27066 | 23 | //pumpTach.rise(&pumpTachTrigger); | 
| dmwahl | 1:d58df8cb271d | 24 | |
| dmwahl | 0:67debf2ccbc2 | 25 | pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI); | 
| dmwahl | 0:67debf2ccbc2 | 26 | pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1 | 
| dmwahl | 0:67debf2ccbc2 | 27 | pump_control_PID.setMode(AUTO_MODE); | 
| dmwahl | 4:49852dc27066 | 28 | pump_control_PID.setSetPoint(pumpSetPointPSI); // pump setpoint based on pot 2*/ | 
| dmwahl | 0:67debf2ccbc2 | 29 | } | 
| dmwahl | 0:67debf2ccbc2 | 30 | |
| dmwahl | 0:67debf2ccbc2 | 31 | void pump_pid_update(char error) | 
| dmwahl | 0:67debf2ccbc2 | 32 | { | 
| dmwahl | 0:67debf2ccbc2 | 33 | if (pumpPressure.status != 0x40) { | 
| dmwahl | 0:67debf2ccbc2 | 34 | pump = 0; | 
| dmwahl | 0:67debf2ccbc2 | 35 | pump_control_PID.reset(); | 
| dmwahl | 0:67debf2ccbc2 | 36 | } else { | 
| dmwahl | 4:49852dc27066 | 37 | |
| dmwahl | 4:49852dc27066 | 38 | pumpSetPointPSI = floor(((double)pot2)*pumpMaxPSI); // Update pump pressure setpoint based on value of pot2 and round down | 
| dmwahl | 4:49852dc27066 | 39 | |
| dmwahl | 4:49852dc27066 | 40 | pump_control_PID.setSetPoint(pumpSetPointPSI); | 
| dmwahl | 0:67debf2ccbc2 | 41 | |
| dmwahl | 0:67debf2ccbc2 | 42 | //Update the process variable. | 
| dmwahl | 4:49852dc27066 | 43 | pump_control_PID.setProcessValue(pumpPressure.pressurePSI-mixerPressure.pressurePSI); | 
| dmwahl | 0:67debf2ccbc2 | 44 | //PID calculation and set the new output value. | 
| dmwahl | 0:67debf2ccbc2 | 45 | pump = pump_control_PID.compute(); | 
| dmwahl | 0:67debf2ccbc2 | 46 | //pump = 0.1; | 
| dmwahl | 0:67debf2ccbc2 | 47 | ledGrn = ((float)1.0-pump.read()); | 
| dmwahl | 0:67debf2ccbc2 | 48 | } | 
| dmwahl | 0:67debf2ccbc2 | 49 | } | 
| dmwahl | 0:67debf2ccbc2 | 50 | |
| dmwahl | 0:67debf2ccbc2 | 51 | void update_pressures() | 
| dmwahl | 0:67debf2ccbc2 | 52 | { | 
| dmwahl | 0:67debf2ccbc2 | 53 | Timer timer; | 
| dmwahl | 0:67debf2ccbc2 | 54 | timer.start(); | 
| dmwahl | 0:67debf2ccbc2 | 55 | char error; | 
| dmwahl | 0:67debf2ccbc2 | 56 | while (true) { | 
| dmwahl | 0:67debf2ccbc2 | 57 | i2c1_m.lock(); | 
| dmwahl | 0:67debf2ccbc2 | 58 | timer.reset(); | 
| dmwahl | 0:67debf2ccbc2 | 59 | error = pumpPressure.readPT(); | 
| dmwahl | 3:9ff79ea3a294 | 60 | error |= mixerPressure.readPT(); | 
| dmwahl | 4:49852dc27066 | 61 | int wait = (pressureWait - timer.read_ms()); | 
| dmwahl | 0:67debf2ccbc2 | 62 | i2c1_m.unlock(); | 
| dmwahl | 0:67debf2ccbc2 | 63 | Thread::wait(wait); | 
| dmwahl | 0:67debf2ccbc2 | 64 | pump_pid_update(error); | 
| dmwahl | 0:67debf2ccbc2 | 65 | } | 
| dmwahl | 0:67debf2ccbc2 | 66 | } | 
| dmwahl | 0:67debf2ccbc2 | 67 | |
| dmwahl | 0:67debf2ccbc2 | 68 | void update_airflow() | 
| dmwahl | 0:67debf2ccbc2 | 69 | { | 
| dmwahl | 0:67debf2ccbc2 | 70 | Timer timer; | 
| dmwahl | 0:67debf2ccbc2 | 71 | timer.start(); | 
| dmwahl | 0:67debf2ccbc2 | 72 | char error; | 
| dmwahl | 0:67debf2ccbc2 | 73 | while (true) { | 
| dmwahl | 0:67debf2ccbc2 | 74 | i2c2_m.lock(); | 
| dmwahl | 0:67debf2ccbc2 | 75 | timer.reset(); | 
| dmwahl | 0:67debf2ccbc2 | 76 | error = sfm7033.Measure(FLOW); | 
| dmwahl | 4:49852dc27066 | 77 | int wait = (airflowWait - timer.read_ms()); | 
| dmwahl | 0:67debf2ccbc2 | 78 | i2c2_m.unlock(); | 
| dmwahl | 0:67debf2ccbc2 | 79 | Thread::wait(wait); | 
| dmwahl | 0:67debf2ccbc2 | 80 | } | 
| dmwahl | 0:67debf2ccbc2 | 81 | } | 
| dmwahl | 0:67debf2ccbc2 | 82 | |
| dmwahl | 4:49852dc27066 | 83 | //void update_level() | 
| dmwahl | 4:49852dc27066 | 84 | //{ | 
| dmwahl | 4:49852dc27066 | 85 | // Timer timer; | 
| dmwahl | 4:49852dc27066 | 86 | // timer.start(); | 
| dmwahl | 4:49852dc27066 | 87 | // while (true) { | 
| dmwahl | 4:49852dc27066 | 88 | // i2c1_m.lock(); | 
| dmwahl | 4:49852dc27066 | 89 | // timer.reset(); | 
| dmwahl | 4:49852dc27066 | 90 | // agentlevel = (float)level; | 
| dmwahl | 4:49852dc27066 | 91 | // int wait = (1000 - timer.read_ms()); | 
| dmwahl | 4:49852dc27066 | 92 | // i2c1_m.unlock(); | 
| dmwahl | 4:49852dc27066 | 93 | // Thread::wait(wait); | 
| dmwahl | 4:49852dc27066 | 94 | // } | 
| dmwahl | 4:49852dc27066 | 95 | //} | 
| dmwahl | 1:d58df8cb271d | 96 | |
| dmwahl | 1:d58df8cb271d | 97 | //float get_level() | 
| dmwahl | 1:d58df8cb271d | 98 | //{ | 
| dmwahl | 1:d58df8cb271d | 99 | // i2c1_m.lock(); | 
| dmwahl | 1:d58df8cb271d | 100 | // float value = (float)level; | 
| dmwahl | 1:d58df8cb271d | 101 | // i2c1_m.unlock(); | 
| dmwahl | 1:d58df8cb271d | 102 | // return value; | 
| dmwahl | 1:d58df8cb271d | 103 | //} | 
| dmwahl | 1:d58df8cb271d | 104 | |
| dmwahl | 1:d58df8cb271d | 105 | |
| dmwahl | 4:49852dc27066 | 106 | |
| dmwahl | 0:67debf2ccbc2 | 107 | void print_process_values() | 
| dmwahl | 0:67debf2ccbc2 | 108 | { | 
| dmwahl | 4:49852dc27066 | 109 | Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial | 
| dmwahl | 0:67debf2ccbc2 | 110 | Timer timer; | 
| dmwahl | 0:67debf2ccbc2 | 111 | timer.start(); | 
| dmwahl | 4:49852dc27066 | 112 | while (completed == false) { | 
| dmwahl | 0:67debf2ccbc2 | 113 | stdio_m.lock(); | 
| dmwahl | 0:67debf2ccbc2 | 114 | timer.reset(); | 
| dmwahl | 4:49852dc27066 | 115 | Thread::wait(10); | 
| dmwahl | 4:49852dc27066 | 116 | //float runtime = timer.read_ms(); | 
| dmwahl | 4:49852dc27066 | 117 | pc.printf("%05.2fpsi %04.01fC %04.01fF %05.2fpsi %04.01fC %04.01fF %05.1f%% %04.0fRPM %05.0f %s %04.1f %05.2fpsi %05.2fpsi\r\n", | 
| dmwahl | 4:49852dc27066 | 118 | pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, | 
| dmwahl | 4:49852dc27066 | 119 | mixerPressure.pressurePSI, mixerPressure.temperatureC, mixerPressure.temperatureF, | 
| dmwahl | 3:9ff79ea3a294 | 120 | pump.read()*100, pumpRpm, | 
| dmwahl | 4:49852dc27066 | 121 | (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, | 
| dmwahl | 4:49852dc27066 | 122 | (double)pot1*18, pumpPressure.pressurePSI-mixerPressure.pressurePSI, pumpSetPointPSI); | 
| dmwahl | 4:49852dc27066 | 123 | int wait = (200 - timer.read_ms()); | 
| dmwahl | 1:d58df8cb271d | 124 | |
| dmwahl | 0:67debf2ccbc2 | 125 | stdio_m.unlock(); | 
| dmwahl | 0:67debf2ccbc2 | 126 | Thread::wait(wait); | 
| dmwahl | 0:67debf2ccbc2 | 127 | } | 
| dmwahl | 0:67debf2ccbc2 | 128 | } | 
| dmwahl | 0:67debf2ccbc2 | 129 | |
| dmwahl | 0:67debf2ccbc2 | 130 | void update_lcd() | 
| dmwahl | 0:67debf2ccbc2 | 131 | { | 
| dmwahl | 0:67debf2ccbc2 | 132 | Timer timer; | 
| dmwahl | 0:67debf2ccbc2 | 133 | timer.start(); | 
| dmwahl | 0:67debf2ccbc2 | 134 | while (true) { | 
| dmwahl | 0:67debf2ccbc2 | 135 | float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16)); | 
| dmwahl | 0:67debf2ccbc2 | 136 | flow = flow/1000; | 
| dmwahl | 0:67debf2ccbc2 | 137 | stdio_m.lock(); | 
| dmwahl | 0:67debf2ccbc2 | 138 | timer.reset(); | 
| dmwahl | 0:67debf2ccbc2 | 139 | lcd.cls(); | 
| dmwahl | 0:67debf2ccbc2 | 140 | lcd.font((unsigned char*)ArialR12x14); | 
| dmwahl | 0:67debf2ccbc2 | 141 | lcd.locate(0, 0); | 
| dmwahl | 0:67debf2ccbc2 | 142 | lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18); | 
| dmwahl | 0:67debf2ccbc2 | 143 | lcd.locate(0, 14); | 
| dmwahl | 4:49852dc27066 | 144 | lcd.printf("PV: %.1f", (pumpPressure.pressurePSI-mixerPressure.pressurePSI)); | 
| dmwahl | 0:67debf2ccbc2 | 145 | lcd.locate(64, 14); | 
| dmwahl | 4:49852dc27066 | 146 | lcd.printf("SV: %.1f", pumpSetPointPSI); | 
| dmwahl | 0:67debf2ccbc2 | 147 | int wait = (1000 - timer.read_ms()); | 
| dmwahl | 0:67debf2ccbc2 | 148 | stdio_m.unlock(); | 
| dmwahl | 0:67debf2ccbc2 | 149 | Thread::wait(wait); | 
| dmwahl | 0:67debf2ccbc2 | 150 | } | 
| dmwahl | 0:67debf2ccbc2 | 151 | } | 
| dmwahl | 0:67debf2ccbc2 | 152 | |
| dmwahl | 1:d58df8cb271d | 153 | void update_shutoff() | 
| dmwahl | 1:d58df8cb271d | 154 | { | 
| dmwahl | 1:d58df8cb271d | 155 | Timer timer; | 
| dmwahl | 1:d58df8cb271d | 156 | timer.start(); | 
| dmwahl | 1:d58df8cb271d | 157 | while (true) { | 
| dmwahl | 1:d58df8cb271d | 158 | float threshold = 0.1; | 
| dmwahl | 1:d58df8cb271d | 159 | timer.reset(); | 
| dmwahl | 1:d58df8cb271d | 160 | if((double)pot1 < 0.05) { | 
| dmwahl | 1:d58df8cb271d | 161 | shutoff.off(); | 
| dmwahl | 3:9ff79ea3a294 | 162 | //pc.printf("shutoff off\r\n"); | 
| dmwahl | 1:d58df8cb271d | 163 | //Thread::wait(1000); | 
| dmwahl | 1:d58df8cb271d | 164 | } | 
| dmwahl | 1:d58df8cb271d | 165 | if((double)pot1 >= 0.1) { | 
| dmwahl | 1:d58df8cb271d | 166 | shutoff.on(); | 
| dmwahl | 3:9ff79ea3a294 | 167 | //pc.printf("shutoff on\r\n"); | 
| dmwahl | 1:d58df8cb271d | 168 | //Thread::wait(1000); | 
| dmwahl | 1:d58df8cb271d | 169 | } | 
| dmwahl | 1:d58df8cb271d | 170 | int wait = (200 - timer.read_ms()); | 
| dmwahl | 1:d58df8cb271d | 171 | Thread::wait(wait); | 
| dmwahl | 1:d58df8cb271d | 172 | } | 
| dmwahl | 1:d58df8cb271d | 173 | } | 
| dmwahl | 1:d58df8cb271d | 174 | |
| dmwahl | 4:49852dc27066 | 175 | |
| dmwahl | 4:49852dc27066 | 176 | |
| dmwahl | 0:67debf2ccbc2 | 177 | // main() runs in its own thread in the OS | 
| dmwahl | 0:67debf2ccbc2 | 178 | int main() | 
| dmwahl | 0:67debf2ccbc2 | 179 | { | 
| dmwahl | 4:49852dc27066 | 180 | //t.start(); // Start runtime timer | 
| dmwahl | 0:67debf2ccbc2 | 181 | pump_init(); | 
| dmwahl | 0:67debf2ccbc2 | 182 | ledBlu = 1; | 
| dmwahl | 0:67debf2ccbc2 | 183 | pc.printf("Serenity Starting up...\n\r"); | 
| dmwahl | 4:49852dc27066 | 184 | //pumpTach.rise(&pump_tach_trigger); | 
| dmwahl | 4:49852dc27066 | 185 | |
| dmwahl | 0:67debf2ccbc2 | 186 | /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax); | 
| dmwahl | 0:67debf2ccbc2 | 187 | pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode); | 
| dmwahl | 0:67debf2ccbc2 | 188 | pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/ | 
| dmwahl | 0:67debf2ccbc2 | 189 | |
| dmwahl | 1:d58df8cb271d | 190 | |
| dmwahl | 1:d58df8cb271d | 191 | // Thread to turn shutoff valve on/off | 
| dmwahl | 1:d58df8cb271d | 192 | update_shutoff_t.set_priority(osPriorityHigh); | 
| dmwahl | 1:d58df8cb271d | 193 | update_shutoff_t.start(update_shutoff); | 
| dmwahl | 1:d58df8cb271d | 194 | |
| dmwahl | 0:67debf2ccbc2 | 195 | // Thread to poll pressure sensors | 
| dmwahl | 0:67debf2ccbc2 | 196 | update_pressures_t.set_priority(osPriorityNormal); | 
| dmwahl | 0:67debf2ccbc2 | 197 | update_pressures_t.start(update_pressures); | 
| dmwahl | 0:67debf2ccbc2 | 198 | |
| dmwahl | 0:67debf2ccbc2 | 199 | // Thread to poll airflow sensor | 
| dmwahl | 0:67debf2ccbc2 | 200 | update_airflow_t.set_priority(osPriorityNormal); | 
| dmwahl | 0:67debf2ccbc2 | 201 | update_airflow_t.start(update_airflow); | 
| dmwahl | 0:67debf2ccbc2 | 202 | |
| dmwahl | 1:d58df8cb271d | 203 | // Thread to poll level sensor | 
| dmwahl | 1:d58df8cb271d | 204 | //update_level_t.set_priority(osPriorityIdle); | 
| dmwahl | 1:d58df8cb271d | 205 | //update_level_t.start(update_level); | 
| dmwahl | 1:d58df8cb271d | 206 | |
| dmwahl | 0:67debf2ccbc2 | 207 | // Thread to update lcd | 
| dmwahl | 0:67debf2ccbc2 | 208 | update_lcd_t.set_priority(osPriorityIdle); | 
| dmwahl | 0:67debf2ccbc2 | 209 | update_lcd_t.start(update_lcd); | 
| dmwahl | 0:67debf2ccbc2 | 210 | |
| dmwahl | 0:67debf2ccbc2 | 211 | // Thread to send process values to serial port | 
| dmwahl | 0:67debf2ccbc2 | 212 | print_process_values_t.set_priority(osPriorityLow); | 
| dmwahl | 0:67debf2ccbc2 | 213 | print_process_values_t.start(&print_process_values); | 
| dmwahl | 0:67debf2ccbc2 | 214 | |
| dmwahl | 0:67debf2ccbc2 | 215 | while (true) { | 
| dmwahl | 4:49852dc27066 | 216 | //pc.printf("%d ms have passed\r\n", t.read_us()/1000); | 
| dmwahl | 0:67debf2ccbc2 | 217 | //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF); | 
| dmwahl | 3:9ff79ea3a294 | 218 | if((double)pot1 >= 0.1) { | 
| dmwahl | 3:9ff79ea3a294 | 219 | injector.on(); | 
| dmwahl | 3:9ff79ea3a294 | 220 | } | 
| dmwahl | 3:9ff79ea3a294 | 221 | //pc.printf("shutoff on\r\n"); | 
| dmwahl | 4:49852dc27066 | 222 | Thread::wait(3); | 
| dmwahl | 1:d58df8cb271d | 223 | |
| dmwahl | 3:9ff79ea3a294 | 224 | injector.off(); | 
| dmwahl | 3:9ff79ea3a294 | 225 | //pc.printf("shutoff off\r\n");*/ | 
| dmwahl | 4:49852dc27066 | 226 | Thread::wait(497); | 
| dmwahl | 0:67debf2ccbc2 | 227 | } | 
| dmwahl | 0:67debf2ccbc2 | 228 | } | 
| dmwahl | 0:67debf2ccbc2 | 229 |