Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of PUMP_SCRIPT by
main.cpp@9:20092ef77dab, 2017-09-07 (annotated)
- Committer:
- iwolf32
- Date:
- Thu Sep 07 20:53:17 2017 +0000
- Revision:
- 9:20092ef77dab
- Parent:
- 8:bf86306ce7bf
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 | void pumpTachTrigger() |
iwolf32 | 9:20092ef77dab | 3 | { |
dmwahl | 0:67debf2ccbc2 | 4 | pumpTachCounts++; |
dmwahl | 0:67debf2ccbc2 | 5 | } |
dmwahl | 0:67debf2ccbc2 | 6 | |
dmwahl | 0:67debf2ccbc2 | 7 | void pump_tach_update() |
dmwahl | 0:67debf2ccbc2 | 8 | { |
dmwahl | 0:67debf2ccbc2 | 9 | float i = pumpTachCounts; // In case it triggers mid-calculation |
212600191 | 7:b385e6a3f622 | 10 | pumpTachCounts=0; |
dmwahl | 0:67debf2ccbc2 | 11 | pumpRpm = (i/pumpTachPoles)*60; |
dmwahl | 0:67debf2ccbc2 | 12 | } |
dmwahl | 0:67debf2ccbc2 | 13 | |
dmwahl | 0:67debf2ccbc2 | 14 | void pump_init() |
dmwahl | 0:67debf2ccbc2 | 15 | { |
dmwahl | 0:67debf2ccbc2 | 16 | pump.period(.001); // 1kHz PWM |
iwolf32 | 9:20092ef77dab | 17 | pump = 0; |
dmwahl | 0:67debf2ccbc2 | 18 | pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI); |
dmwahl | 0:67debf2ccbc2 | 19 | pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1 |
dmwahl | 0:67debf2ccbc2 | 20 | pump_control_PID.setMode(AUTO_MODE); |
dmwahl | 0:67debf2ccbc2 | 21 | pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/ |
dmwahl | 0:67debf2ccbc2 | 22 | } |
dmwahl | 0:67debf2ccbc2 | 23 | |
dmwahl | 0:67debf2ccbc2 | 24 | void pump_pid_update(char error) |
dmwahl | 0:67debf2ccbc2 | 25 | { |
dmwahl | 0:67debf2ccbc2 | 26 | if (pumpPressure.status != 0x40) { |
dmwahl | 0:67debf2ccbc2 | 27 | pump = 0; |
dmwahl | 0:67debf2ccbc2 | 28 | pump_control_PID.reset(); |
dmwahl | 0:67debf2ccbc2 | 29 | } else { |
dmwahl | 0:67debf2ccbc2 | 30 | pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); |
iwolf32 | 9:20092ef77dab | 31 | //Update the process variable. |
dmwahl | 0:67debf2ccbc2 | 32 | pump_control_PID.setProcessValue(pumpPressure.pressurePSI); |
dmwahl | 0:67debf2ccbc2 | 33 | //PID calculation and set the new output value. |
dmwahl | 0:67debf2ccbc2 | 34 | pump = pump_control_PID.compute(); |
dmwahl | 0:67debf2ccbc2 | 35 | } |
dmwahl | 0:67debf2ccbc2 | 36 | } |
dmwahl | 0:67debf2ccbc2 | 37 | void update_pressures() |
dmwahl | 0:67debf2ccbc2 | 38 | { |
dmwahl | 0:67debf2ccbc2 | 39 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 40 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 41 | char error; |
dmwahl | 0:67debf2ccbc2 | 42 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 43 | i2c1_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 44 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 45 | error = pumpPressure.readPT(); |
dmwahl | 0:67debf2ccbc2 | 46 | int wait = (200 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 47 | i2c1_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 48 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 49 | pump_pid_update(error); |
dmwahl | 0:67debf2ccbc2 | 50 | } |
dmwahl | 0:67debf2ccbc2 | 51 | } |
dmwahl | 0:67debf2ccbc2 | 52 | void print_process_values() |
dmwahl | 0:67debf2ccbc2 | 53 | { |
dmwahl | 0:67debf2ccbc2 | 54 | //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial |
dmwahl | 0:67debf2ccbc2 | 55 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 56 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 57 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 58 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 59 | timer.reset(); |
iwolf32 | 9:20092ef77dab | 60 | pc.printf(" %.02f\t %.01f\t %.0f\r\n", pumpPressure.pressurePSI, ((double)pot2-.002)*pumpMaxPSI, pumpRpm); |
dmwahl | 0:67debf2ccbc2 | 61 | int wait = (1000 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 62 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 63 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 64 | } |
dmwahl | 0:67debf2ccbc2 | 65 | } |
dmwahl | 0:67debf2ccbc2 | 66 | |
dmwahl | 0:67debf2ccbc2 | 67 | int main() |
dmwahl | 0:67debf2ccbc2 | 68 | { |
dmwahl | 0:67debf2ccbc2 | 69 | pump_init(); |
212600191 | 7:b385e6a3f622 | 70 | pump_tach_update(); |
iwolf32 | 9:20092ef77dab | 71 | pumpTach.rise(&pumpTachTrigger); |
iwolf32 | 9:20092ef77dab | 72 | pump_tach_ticker.attach(&pump_tach_update, 1.0); |
dmwahl | 1:d58df8cb271d | 73 | |
dmwahl | 0:67debf2ccbc2 | 74 | // Thread to poll pressure sensors |
dmwahl | 0:67debf2ccbc2 | 75 | update_pressures_t.set_priority(osPriorityNormal); |
iwolf32 | 4:79b23d1fbcd1 | 76 | update_pressures_t.start(update_pressures); |
dmwahl | 0:67debf2ccbc2 | 77 | |
dmwahl | 0:67debf2ccbc2 | 78 | // Thread to send process values to serial port |
dmwahl | 0:67debf2ccbc2 | 79 | print_process_values_t.set_priority(osPriorityLow); |
iwolf32 | 9:20092ef77dab | 80 | print_process_values_t.start(&print_process_values); |
212600191 | 7:b385e6a3f622 | 81 | |
iwolf32 | 9:20092ef77dab | 82 | } |