Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of PUMP_SCRIPT1 by
main.cpp@8:bf86306ce7bf, 2017-09-05 (annotated)
- Committer:
- iwolf32
- Date:
- Tue Sep 05 19:27:50 2017 +0000
- Revision:
- 8:bf86306ce7bf
- Parent:
- 7:b385e6a3f622
- Child:
- 9:20092ef77dab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dmwahl | 0:67debf2ccbc2 | 1 | #include "main.h" |
iwolf32 | 4:79b23d1fbcd1 | 2 | //--------------------------- |
iwolf32 | 4:79b23d1fbcd1 | 3 | |
iwolf32 | 4:79b23d1fbcd1 | 4 | //----------------------------- |
dmwahl | 0:67debf2ccbc2 | 5 | void pumpTachTrigger() |
dmwahl | 0:67debf2ccbc2 | 6 | { |
212600191 | 7:b385e6a3f622 | 7 | |
dmwahl | 0:67debf2ccbc2 | 8 | pumpTachCounts++; |
dmwahl | 0:67debf2ccbc2 | 9 | } |
dmwahl | 0:67debf2ccbc2 | 10 | |
dmwahl | 0:67debf2ccbc2 | 11 | void pump_tach_update() |
dmwahl | 0:67debf2ccbc2 | 12 | { |
dmwahl | 0:67debf2ccbc2 | 13 | float i = pumpTachCounts; // In case it triggers mid-calculation |
212600191 | 7:b385e6a3f622 | 14 | pumpTachCounts=0; |
dmwahl | 0:67debf2ccbc2 | 15 | pumpRpm = (i/pumpTachPoles)*60; |
dmwahl | 0:67debf2ccbc2 | 16 | } |
dmwahl | 0:67debf2ccbc2 | 17 | |
dmwahl | 0:67debf2ccbc2 | 18 | void pump_init() |
dmwahl | 0:67debf2ccbc2 | 19 | { |
dmwahl | 0:67debf2ccbc2 | 20 | pump.period(.001); // 1kHz PWM |
dmwahl | 0:67debf2ccbc2 | 21 | pump = 0; |
212600191 | 7:b385e6a3f622 | 22 | |
212600191 | 7:b385e6a3f622 | 23 | |
iwolf32 | 4:79b23d1fbcd1 | 24 | //ledGrn.period(.001); |
dmwahl | 1:d58df8cb271d | 25 | |
212600191 | 7:b385e6a3f622 | 26 | |
212600191 | 7:b385e6a3f622 | 27 | |
dmwahl | 1:d58df8cb271d | 28 | |
dmwahl | 0:67debf2ccbc2 | 29 | pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI); |
dmwahl | 0:67debf2ccbc2 | 30 | pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1 |
dmwahl | 0:67debf2ccbc2 | 31 | pump_control_PID.setMode(AUTO_MODE); |
dmwahl | 0:67debf2ccbc2 | 32 | pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/ |
dmwahl | 0:67debf2ccbc2 | 33 | } |
dmwahl | 0:67debf2ccbc2 | 34 | |
dmwahl | 0:67debf2ccbc2 | 35 | void pump_pid_update(char error) |
dmwahl | 0:67debf2ccbc2 | 36 | { |
dmwahl | 0:67debf2ccbc2 | 37 | if (pumpPressure.status != 0x40) { |
dmwahl | 0:67debf2ccbc2 | 38 | pump = 0; |
dmwahl | 0:67debf2ccbc2 | 39 | pump_control_PID.reset(); |
dmwahl | 0:67debf2ccbc2 | 40 | } else { |
dmwahl | 0:67debf2ccbc2 | 41 | pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); |
dmwahl | 0:67debf2ccbc2 | 42 | |
dmwahl | 0:67debf2ccbc2 | 43 | //Update the process variable. |
dmwahl | 0:67debf2ccbc2 | 44 | pump_control_PID.setProcessValue(pumpPressure.pressurePSI); |
dmwahl | 0:67debf2ccbc2 | 45 | //PID calculation and set the new output value. |
dmwahl | 0:67debf2ccbc2 | 46 | pump = pump_control_PID.compute(); |
dmwahl | 0:67debf2ccbc2 | 47 | //pump = 0.1; |
iwolf32 | 4:79b23d1fbcd1 | 48 | //ledGrn = ((float)1.0-pump.read()); |
dmwahl | 0:67debf2ccbc2 | 49 | } |
dmwahl | 0:67debf2ccbc2 | 50 | } |
dmwahl | 0:67debf2ccbc2 | 51 | |
dmwahl | 0:67debf2ccbc2 | 52 | void update_pressures() |
dmwahl | 0:67debf2ccbc2 | 53 | { |
dmwahl | 0:67debf2ccbc2 | 54 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 55 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 56 | char error; |
dmwahl | 0:67debf2ccbc2 | 57 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 58 | i2c1_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 59 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 60 | error = pumpPressure.readPT(); |
iwolf32 | 4:79b23d1fbcd1 | 61 | //error |= mixerPressure.readPT(); |
dmwahl | 0:67debf2ccbc2 | 62 | int wait = (200 - timer.read_ms()); |
dmwahl | 0:67debf2ccbc2 | 63 | i2c1_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 64 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 65 | pump_pid_update(error); |
dmwahl | 0:67debf2ccbc2 | 66 | } |
dmwahl | 0:67debf2ccbc2 | 67 | } |
dmwahl | 0:67debf2ccbc2 | 68 | |
iwolf32 | 4:79b23d1fbcd1 | 69 | //------------------------------------------------ |
dmwahl | 1:d58df8cb271d | 70 | |
dmwahl | 0:67debf2ccbc2 | 71 | void print_process_values() |
dmwahl | 0:67debf2ccbc2 | 72 | { |
dmwahl | 0:67debf2ccbc2 | 73 | //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial |
dmwahl | 0:67debf2ccbc2 | 74 | Timer timer; |
dmwahl | 0:67debf2ccbc2 | 75 | timer.start(); |
dmwahl | 0:67debf2ccbc2 | 76 | while (true) { |
dmwahl | 0:67debf2ccbc2 | 77 | stdio_m.lock(); |
dmwahl | 0:67debf2ccbc2 | 78 | timer.reset(); |
dmwahl | 0:67debf2ccbc2 | 79 | |
iwolf32 | 8:bf86306ce7bf | 80 | pc.printf(" %.02f\t %.01f\t %.0f\r\n", |
iwolf32 | 8:bf86306ce7bf | 81 | pumpPressure.pressurePSI, ((double)pot2-.002)*pumpMaxPSI, pumpRpm); |
dmwahl | 0:67debf2ccbc2 | 82 | int wait = (1000 - timer.read_ms()); |
dmwahl | 1:d58df8cb271d | 83 | |
dmwahl | 0:67debf2ccbc2 | 84 | stdio_m.unlock(); |
dmwahl | 0:67debf2ccbc2 | 85 | Thread::wait(wait); |
dmwahl | 0:67debf2ccbc2 | 86 | } |
dmwahl | 0:67debf2ccbc2 | 87 | } |
dmwahl | 0:67debf2ccbc2 | 88 | |
dmwahl | 0:67debf2ccbc2 | 89 | // main() runs in its own thread in the OS |
dmwahl | 0:67debf2ccbc2 | 90 | int main() |
dmwahl | 0:67debf2ccbc2 | 91 | { |
dmwahl | 0:67debf2ccbc2 | 92 | pump_init(); |
212600191 | 7:b385e6a3f622 | 93 | pump_tach_update(); |
iwolf32 | 4:79b23d1fbcd1 | 94 | //ledBlu = 1; |
dmwahl | 0:67debf2ccbc2 | 95 | pc.printf("Serenity Starting up...\n\r"); |
iwolf32 | 4:79b23d1fbcd1 | 96 | //-------------------------------------------- |
iwolf32 | 8:bf86306ce7bf | 97 | |
212600191 | 7:b385e6a3f622 | 98 | pumpTach.rise(&pumpTachTrigger); |
212600191 | 7:b385e6a3f622 | 99 | pump_tach_ticker.attach(&pump_tach_update, 1.0); |
dmwahl | 0:67debf2ccbc2 | 100 | |
iwolf32 | 4:79b23d1fbcd1 | 101 | //-------------------------------------------- |
dmwahl | 1:d58df8cb271d | 102 | |
dmwahl | 0:67debf2ccbc2 | 103 | // Thread to poll pressure sensors |
dmwahl | 0:67debf2ccbc2 | 104 | update_pressures_t.set_priority(osPriorityNormal); |
iwolf32 | 4:79b23d1fbcd1 | 105 | update_pressures_t.start(update_pressures); |
dmwahl | 0:67debf2ccbc2 | 106 | |
iwolf32 | 4:79b23d1fbcd1 | 107 | // Thread to poll flow sensor |
212600191 | 5:f2cb21046678 | 108 | //update_flow1_t.set_priority(osPriorityNormal); |
212600191 | 5:f2cb21046678 | 109 | //update_flow1_t.start(update_flow1); |
iwolf32 | 4:79b23d1fbcd1 | 110 | |
iwolf32 | 4:79b23d1fbcd1 | 111 | //Thread to poll flow sensor |
212600191 | 5:f2cb21046678 | 112 | //update_flow2_t.set_priority(osPriorityNormal); |
212600191 | 5:f2cb21046678 | 113 | //update_flow2_t.start(update_flow2); |
dmwahl | 0:67debf2ccbc2 | 114 | |
dmwahl | 0:67debf2ccbc2 | 115 | // Thread to send process values to serial port |
dmwahl | 0:67debf2ccbc2 | 116 | print_process_values_t.set_priority(osPriorityLow); |
dmwahl | 0:67debf2ccbc2 | 117 | print_process_values_t.start(&print_process_values); |
212600191 | 7:b385e6a3f622 | 118 | |
212600191 | 7:b385e6a3f622 | 119 | |
dmwahl | 0:67debf2ccbc2 | 120 | |
212600191 | 7:b385e6a3f622 | 121 | |
212600191 | 7:b385e6a3f622 | 122 | |
iwolf32 | 4:79b23d1fbcd1 | 123 | |
212600191 | 7:b385e6a3f622 | 124 | |
dmwahl | 0:67debf2ccbc2 | 125 | } |
dmwahl | 0:67debf2ccbc2 | 126 |