asdf
Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of TestBenchSerenity-proto_F429ZI by
main.cpp@1:d58df8cb271d, 2017-07-20 (annotated)
- Committer:
- dmwahl
- Date:
- Thu Jul 20 20:45:27 2017 +0000
- Revision:
- 1:d58df8cb271d
- Parent:
- 0:67debf2ccbc2
- Child:
- 3:9ff79ea3a294
Shutoff valve triggers when pot1 > 0.1
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 | 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 | 0:67debf2ccbc2 | 56 | int wait = (200 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 57 | i2c1_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 58 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 59 | pump_pid_update(error); |
dmwahl | 0:67debf2ccbc2 | 60 | } |
dmwahl | 0:67debf2ccbc2 | 61 | } |
dmwahl | 0:67debf2ccbc2 | 62 | |
dmwahl | 0:67debf2ccbc2 | 63 | void update_airflow() |
dmwahl | 0:67debf2ccbc2 | 64 | { |
dmwahl | 0:67debf2ccbc2 | 65 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 66 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 67 | char error; |
dmwahl | 0:67debf2ccbc2 | 68 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 69 | i2c2_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 70 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 71 | error = sfm7033.Measure(FLOW); |
dmwahl | 0:67debf2ccbc2 | 72 | int wait = (200 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 73 | i2c2_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 74 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 75 | } |
dmwahl | 0:67debf2ccbc2 | 76 | } |
dmwahl | 0:67debf2ccbc2 | 77 | |
dmwahl | 1:d58df8cb271d | 78 | void update_level() |
dmwahl | 1:d58df8cb271d | 79 | { |
dmwahl | 1:d58df8cb271d | 80 | Timer timer; |
dmwahl | 1:d58df8cb271d | 81 | timer.start(); |
dmwahl | 1:d58df8cb271d | 82 | while (true) { |
dmwahl | 1:d58df8cb271d | 83 | i2c1_m.lock(); |
dmwahl | 1:d58df8cb271d | 84 | timer.reset(); |
dmwahl | 1:d58df8cb271d | 85 | agentlevel = (float)level; |
dmwahl | 1:d58df8cb271d | 86 | int wait = (1000 - timer.read_ms()); |
dmwahl | 1:d58df8cb271d | 87 | i2c1_m.unlock(); |
dmwahl | 1:d58df8cb271d | 88 | Thread::wait(wait); |
dmwahl | 1:d58df8cb271d | 89 | } |
dmwahl | 1:d58df8cb271d | 90 | } |
dmwahl | 1:d58df8cb271d | 91 | |
dmwahl | 1:d58df8cb271d | 92 | //float get_level() |
dmwahl | 1:d58df8cb271d | 93 | //{ |
dmwahl | 1:d58df8cb271d | 94 | // i2c1_m.lock(); |
dmwahl | 1:d58df8cb271d | 95 | // float value = (float)level; |
dmwahl | 1:d58df8cb271d | 96 | // i2c1_m.unlock(); |
dmwahl | 1:d58df8cb271d | 97 | // return value; |
dmwahl | 1:d58df8cb271d | 98 | //} |
dmwahl | 1:d58df8cb271d | 99 | |
dmwahl | 1:d58df8cb271d | 100 | |
dmwahl | 0:67debf2ccbc2 | 101 | void print_process_values() |
dmwahl | 0:67debf2ccbc2 | 102 | { |
dmwahl | 0:67debf2ccbc2 | 103 | //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial |
dmwahl | 0:67debf2ccbc2 | 104 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 105 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 106 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 107 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 108 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 109 | /*pc.printf("%.3fkPa %.2fPSI %.1fC %.1fF %02X %.2f%% %.0fRPM %.0f %s %.1f %.3f\n\r", |
dmwahl | 0:67debf2ccbc2 | 110 | pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, |
dmwahl | 0:67debf2ccbc2 | 111 | pumpPressure.temperatureF, pumpPressure.status, pump.read()*100, pumpRpm, |
dmwahl | 0:67debf2ccbc2 | 112 | ((float)sfm7033.flow.i16 / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, (double)pot1*18, ((double)pot2-.002)*pumpMaxPSI);*/ |
dmwahl | 0:67debf2ccbc2 | 113 | |
dmwahl | 0:67debf2ccbc2 | 114 | pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n", |
dmwahl | 1:d58df8cb271d | 115 | pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, pumpPressure.status, pump.read()*100, pumpRpm, |
dmwahl | 1:d58df8cb271d | 116 | sfm7033.flow.u16, (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, |
dmwahl | 1:d58df8cb271d | 117 | (double)pot1*18, ((double)pot2)*pumpMaxPSI);//, agentlevel; |
dmwahl | 0:67debf2ccbc2 | 118 | int wait = (1000 - timer.read_ms()); |
dmwahl | 1:d58df8cb271d | 119 | |
dmwahl | 0:67debf2ccbc2 | 120 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 121 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 122 | } |
dmwahl | 0:67debf2ccbc2 | 123 | } |
dmwahl | 0:67debf2ccbc2 | 124 | |
dmwahl | 0:67debf2ccbc2 | 125 | void update_lcd() |
dmwahl | 0:67debf2ccbc2 | 126 | { |
dmwahl | 0:67debf2ccbc2 | 127 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 128 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 129 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 130 | float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16)); |
dmwahl | 0:67debf2ccbc2 | 131 | flow = flow/1000; |
dmwahl | 0:67debf2ccbc2 | 132 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 133 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 134 | lcd.cls(); |
dmwahl | 0:67debf2ccbc2 | 135 | lcd.font((unsigned char*)ArialR12x14); |
dmwahl | 0:67debf2ccbc2 | 136 | lcd.locate(0, 0); |
dmwahl | 0:67debf2ccbc2 | 137 | lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18); |
dmwahl | 0:67debf2ccbc2 | 138 | lcd.locate(0, 14); |
dmwahl | 1:d58df8cb271d | 139 | lcd.printf("PV: %.1f", pumpPressure.pressurePSI); |
dmwahl | 0:67debf2ccbc2 | 140 | lcd.locate(64, 14); |
dmwahl | 1:d58df8cb271d | 141 | lcd.printf("SV: %.1f", ((double)pot2)*pumpMaxPSI); |
dmwahl | 0:67debf2ccbc2 | 142 | int wait = (1000 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 143 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 144 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 145 | } |
dmwahl | 0:67debf2ccbc2 | 146 | } |
dmwahl | 0:67debf2ccbc2 | 147 | |
dmwahl | 1:d58df8cb271d | 148 | void update_shutoff() |
dmwahl | 1:d58df8cb271d | 149 | { |
dmwahl | 1:d58df8cb271d | 150 | Timer timer; |
dmwahl | 1:d58df8cb271d | 151 | timer.start(); |
dmwahl | 1:d58df8cb271d | 152 | while (true) { |
dmwahl | 1:d58df8cb271d | 153 | float threshold = 0.1; |
dmwahl | 1:d58df8cb271d | 154 | timer.reset(); |
dmwahl | 1:d58df8cb271d | 155 | if((double)pot1 < 0.05) { |
dmwahl | 1:d58df8cb271d | 156 | shutoff.off(); |
dmwahl | 1:d58df8cb271d | 157 | pc.printf("shutoff off\r\n"); |
dmwahl | 1:d58df8cb271d | 158 | //Thread::wait(1000); |
dmwahl | 1:d58df8cb271d | 159 | } |
dmwahl | 1:d58df8cb271d | 160 | if((double)pot1 >= 0.1) { |
dmwahl | 1:d58df8cb271d | 161 | shutoff.on(); |
dmwahl | 1:d58df8cb271d | 162 | pc.printf("shutoff on\r\n"); |
dmwahl | 1:d58df8cb271d | 163 | //Thread::wait(1000); |
dmwahl | 1:d58df8cb271d | 164 | } |
dmwahl | 1:d58df8cb271d | 165 | int wait = (200 - timer.read_ms()); |
dmwahl | 1:d58df8cb271d | 166 | Thread::wait(wait); |
dmwahl | 1:d58df8cb271d | 167 | } |
dmwahl | 1:d58df8cb271d | 168 | } |
dmwahl | 1:d58df8cb271d | 169 | |
dmwahl | 0:67debf2ccbc2 | 170 | // main() runs in its own thread in the OS |
dmwahl | 0:67debf2ccbc2 | 171 | int main() |
dmwahl | 0:67debf2ccbc2 | 172 | { |
dmwahl | 0:67debf2ccbc2 | 173 | pump_init(); |
dmwahl | 0:67debf2ccbc2 | 174 | ledBlu = 1; |
dmwahl | 0:67debf2ccbc2 | 175 | pc.printf("Serenity Starting up...\n\r"); |
dmwahl | 0:67debf2ccbc2 | 176 | /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax); |
dmwahl | 0:67debf2ccbc2 | 177 | pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode); |
dmwahl | 0:67debf2ccbc2 | 178 | pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/ |
dmwahl | 0:67debf2ccbc2 | 179 | |
dmwahl | 1:d58df8cb271d | 180 | |
dmwahl | 1:d58df8cb271d | 181 | // Thread to turn shutoff valve on/off |
dmwahl | 1:d58df8cb271d | 182 | update_shutoff_t.set_priority(osPriorityHigh); |
dmwahl | 1:d58df8cb271d | 183 | update_shutoff_t.start(update_shutoff); |
dmwahl | 1:d58df8cb271d | 184 | |
dmwahl | 0:67debf2ccbc2 | 185 | // Thread to poll pressure sensors |
dmwahl | 0:67debf2ccbc2 | 186 | update_pressures_t.set_priority(osPriorityNormal); |
dmwahl | 0:67debf2ccbc2 | 187 | update_pressures_t.start(update_pressures); |
dmwahl | 0:67debf2ccbc2 | 188 | |
dmwahl | 0:67debf2ccbc2 | 189 | // Thread to poll airflow sensor |
dmwahl | 0:67debf2ccbc2 | 190 | update_airflow_t.set_priority(osPriorityNormal); |
dmwahl | 0:67debf2ccbc2 | 191 | update_airflow_t.start(update_airflow); |
dmwahl | 0:67debf2ccbc2 | 192 | |
dmwahl | 1:d58df8cb271d | 193 | // Thread to poll level sensor |
dmwahl | 1:d58df8cb271d | 194 | //update_level_t.set_priority(osPriorityIdle); |
dmwahl | 1:d58df8cb271d | 195 | //update_level_t.start(update_level); |
dmwahl | 1:d58df8cb271d | 196 | |
dmwahl | 0:67debf2ccbc2 | 197 | // Thread to update lcd |
dmwahl | 0:67debf2ccbc2 | 198 | update_lcd_t.set_priority(osPriorityIdle); |
dmwahl | 0:67debf2ccbc2 | 199 | update_lcd_t.start(update_lcd); |
dmwahl | 0:67debf2ccbc2 | 200 | |
dmwahl | 0:67debf2ccbc2 | 201 | // Thread to send process values to serial port |
dmwahl | 0:67debf2ccbc2 | 202 | print_process_values_t.set_priority(osPriorityLow); |
dmwahl | 0:67debf2ccbc2 | 203 | print_process_values_t.start(&print_process_values); |
dmwahl | 0:67debf2ccbc2 | 204 | |
dmwahl | 0:67debf2ccbc2 | 205 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 206 | //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF); |
dmwahl | 1:d58df8cb271d | 207 | /*shutoff.on(); |
dmwahl | 1:d58df8cb271d | 208 | pc.printf("shutoff on\r\n"); |
dmwahl | 1:d58df8cb271d | 209 | Thread::wait(1000); |
dmwahl | 1:d58df8cb271d | 210 | |
dmwahl | 1:d58df8cb271d | 211 | shutoff.off(); |
dmwahl | 1:d58df8cb271d | 212 | pc.printf("shutoff off\r\n");*/ |
dmwahl | 0:67debf2ccbc2 | 213 | Thread::wait(1000); |
dmwahl | 0:67debf2ccbc2 | 214 | } |
dmwahl | 0:67debf2ccbc2 | 215 | } |
dmwahl | 0:67debf2ccbc2 | 216 |