asdf
Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of TestBenchSerenity-proto_F429ZI by
main.cpp@0:67debf2ccbc2, 2017-07-07 (annotated)
- Committer:
- dmwahl
- Date:
- Fri Jul 07 20:52:31 2017 +0000
- Revision:
- 0:67debf2ccbc2
- Child:
- 1:d58df8cb271d
Pressure sensors, display, airflow, input pots working
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 | 0:67debf2ccbc2 | 20 | |
dmwahl | 0:67debf2ccbc2 | 21 | InterruptIn pumpTach(pumpTachPin); |
dmwahl | 0:67debf2ccbc2 | 22 | pumpTach.rise(&pumpTachTrigger); |
dmwahl | 0:67debf2ccbc2 | 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 | 0:67debf2ccbc2 | 78 | void print_process_values() |
dmwahl | 0:67debf2ccbc2 | 79 | { |
dmwahl | 0:67debf2ccbc2 | 80 | //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial |
dmwahl | 0:67debf2ccbc2 | 81 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 82 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 83 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 84 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 85 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 86 | /*pc.printf("%.3fkPa %.2fPSI %.1fC %.1fF %02X %.2f%% %.0fRPM %.0f %s %.1f %.3f\n\r", |
dmwahl | 0:67debf2ccbc2 | 87 | pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, |
dmwahl | 0:67debf2ccbc2 | 88 | pumpPressure.temperatureF, pumpPressure.status, pump.read()*100, pumpRpm, |
dmwahl | 0:67debf2ccbc2 | 89 | ((float)sfm7033.flow.i16 / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, (double)pot1*18, ((double)pot2-.002)*pumpMaxPSI);*/ |
dmwahl | 0:67debf2ccbc2 | 90 | |
dmwahl | 0:67debf2ccbc2 | 91 | pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n", |
dmwahl | 0:67debf2ccbc2 | 92 | 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); |
dmwahl | 0:67debf2ccbc2 | 93 | int wait = (1000 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 94 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 95 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 96 | } |
dmwahl | 0:67debf2ccbc2 | 97 | } |
dmwahl | 0:67debf2ccbc2 | 98 | |
dmwahl | 0:67debf2ccbc2 | 99 | void update_lcd() |
dmwahl | 0:67debf2ccbc2 | 100 | { |
dmwahl | 0:67debf2ccbc2 | 101 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 102 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 103 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 104 | float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16)); |
dmwahl | 0:67debf2ccbc2 | 105 | flow = flow/1000; |
dmwahl | 0:67debf2ccbc2 | 106 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 107 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 108 | lcd.cls(); |
dmwahl | 0:67debf2ccbc2 | 109 | lcd.font((unsigned char*)ArialR12x14); |
dmwahl | 0:67debf2ccbc2 | 110 | lcd.locate(0, 0); |
dmwahl | 0:67debf2ccbc2 | 111 | lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18); |
dmwahl | 0:67debf2ccbc2 | 112 | lcd.locate(0, 14); |
dmwahl | 0:67debf2ccbc2 | 113 | lcd.printf("PV: %.0f", pumpPressure.pressurePSI); |
dmwahl | 0:67debf2ccbc2 | 114 | lcd.locate(64, 14); |
dmwahl | 0:67debf2ccbc2 | 115 | lcd.printf("SV: %.0f", ((double)pot2)*pumpMaxPSI); |
dmwahl | 0:67debf2ccbc2 | 116 | int wait = (1000 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 117 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 118 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 119 | } |
dmwahl | 0:67debf2ccbc2 | 120 | } |
dmwahl | 0:67debf2ccbc2 | 121 | |
dmwahl | 0:67debf2ccbc2 | 122 | // main() runs in its own thread in the OS |
dmwahl | 0:67debf2ccbc2 | 123 | int main() |
dmwahl | 0:67debf2ccbc2 | 124 | { |
dmwahl | 0:67debf2ccbc2 | 125 | pump_init(); |
dmwahl | 0:67debf2ccbc2 | 126 | ledBlu = 1; |
dmwahl | 0:67debf2ccbc2 | 127 | pc.printf("Serenity Starting up...\n\r"); |
dmwahl | 0:67debf2ccbc2 | 128 | /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax); |
dmwahl | 0:67debf2ccbc2 | 129 | pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode); |
dmwahl | 0:67debf2ccbc2 | 130 | pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/ |
dmwahl | 0:67debf2ccbc2 | 131 | |
dmwahl | 0:67debf2ccbc2 | 132 | // Thread to poll pressure sensors |
dmwahl | 0:67debf2ccbc2 | 133 | update_pressures_t.set_priority(osPriorityNormal); |
dmwahl | 0:67debf2ccbc2 | 134 | update_pressures_t.start(update_pressures); |
dmwahl | 0:67debf2ccbc2 | 135 | |
dmwahl | 0:67debf2ccbc2 | 136 | // Thread to poll airflow sensor |
dmwahl | 0:67debf2ccbc2 | 137 | update_airflow_t.set_priority(osPriorityNormal); |
dmwahl | 0:67debf2ccbc2 | 138 | update_airflow_t.start(update_airflow); |
dmwahl | 0:67debf2ccbc2 | 139 | |
dmwahl | 0:67debf2ccbc2 | 140 | // Thread to update lcd |
dmwahl | 0:67debf2ccbc2 | 141 | update_lcd_t.set_priority(osPriorityIdle); |
dmwahl | 0:67debf2ccbc2 | 142 | update_lcd_t.start(update_lcd); |
dmwahl | 0:67debf2ccbc2 | 143 | |
dmwahl | 0:67debf2ccbc2 | 144 | // Thread to send process values to serial port |
dmwahl | 0:67debf2ccbc2 | 145 | print_process_values_t.set_priority(osPriorityLow); |
dmwahl | 0:67debf2ccbc2 | 146 | print_process_values_t.start(&print_process_values); |
dmwahl | 0:67debf2ccbc2 | 147 | |
dmwahl | 0:67debf2ccbc2 | 148 | |
dmwahl | 0:67debf2ccbc2 | 149 | |
dmwahl | 0:67debf2ccbc2 | 150 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 151 | //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF); |
dmwahl | 0:67debf2ccbc2 | 152 | Thread::wait(1000); |
dmwahl | 0:67debf2ccbc2 | 153 | } |
dmwahl | 0:67debf2ccbc2 | 154 | } |
dmwahl | 0:67debf2ccbc2 | 155 |