Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of PUMP_SCRIPT1 by Ian Wolf

Committer:
iwolf32
Date:
Wed Jul 26 14:03:10 2017 +0000
Revision:
4:79b23d1fbcd1
Parent:
3:9ff79ea3a294
Child:
5:f2cb21046678
Child:
6:e3eff8a04b25
REV2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 0:67debf2ccbc2 1 #include "main.h"
iwolf32 4:79b23d1fbcd1 2 //---------------------------
iwolf32 4:79b23d1fbcd1 3 void dutycycleincrease()
iwolf32 4:79b23d1fbcd1 4 {
iwolf32 4:79b23d1fbcd1 5 dutycycle=dutycycle+0.01;
iwolf32 4:79b23d1fbcd1 6 }
dmwahl 0:67debf2ccbc2 7
iwolf32 4:79b23d1fbcd1 8 void dutycycledecrease()
iwolf32 4:79b23d1fbcd1 9 {
iwolf32 4:79b23d1fbcd1 10 dutycycle=dutycycle-0.01;
iwolf32 4:79b23d1fbcd1 11 }
iwolf32 4:79b23d1fbcd1 12
iwolf32 4:79b23d1fbcd1 13 void frequencyincrease()
iwolf32 4:79b23d1fbcd1 14 {
iwolf32 4:79b23d1fbcd1 15 frequency=frequency+1;
iwolf32 4:79b23d1fbcd1 16 }
iwolf32 4:79b23d1fbcd1 17 void frequencydecrease()
iwolf32 4:79b23d1fbcd1 18 {
iwolf32 4:79b23d1fbcd1 19 frequency=frequency-1;
iwolf32 4:79b23d1fbcd1 20 }
iwolf32 4:79b23d1fbcd1 21 void injectorvalvecontrol(){
iwolf32 4:79b23d1fbcd1 22
iwolf32 4:79b23d1fbcd1 23 while(true){
iwolf32 4:79b23d1fbcd1 24 float openvalue=(((1/frequency)*dutycycle));
iwolf32 4:79b23d1fbcd1 25 float offvalue=(1/frequency)-openvalue;
iwolf32 4:79b23d1fbcd1 26
iwolf32 4:79b23d1fbcd1 27 injector.on();
iwolf32 4:79b23d1fbcd1 28 wait(openvalue);
iwolf32 4:79b23d1fbcd1 29 injector.off();
iwolf32 4:79b23d1fbcd1 30 wait(offvalue);
iwolf32 4:79b23d1fbcd1 31 }
iwolf32 4:79b23d1fbcd1 32 }
iwolf32 4:79b23d1fbcd1 33
iwolf32 4:79b23d1fbcd1 34 //-----------------------------
dmwahl 0:67debf2ccbc2 35 void pumpTachTrigger()
dmwahl 0:67debf2ccbc2 36 {
dmwahl 0:67debf2ccbc2 37 pumpTachCounts++;
dmwahl 0:67debf2ccbc2 38 }
dmwahl 0:67debf2ccbc2 39
dmwahl 0:67debf2ccbc2 40 void pump_tach_update()
dmwahl 0:67debf2ccbc2 41 {
dmwahl 0:67debf2ccbc2 42 float i = pumpTachCounts; // In case it triggers mid-calculation
dmwahl 0:67debf2ccbc2 43 pumpTachCounts = 0;
dmwahl 0:67debf2ccbc2 44 pumpRpm = (i/pumpTachPoles)*60;
dmwahl 0:67debf2ccbc2 45 }
dmwahl 0:67debf2ccbc2 46
dmwahl 0:67debf2ccbc2 47 void pump_init()
dmwahl 0:67debf2ccbc2 48 {
dmwahl 0:67debf2ccbc2 49 pump.period(.001); // 1kHz PWM
dmwahl 0:67debf2ccbc2 50 pump = 0;
iwolf32 4:79b23d1fbcd1 51 //ledGrn.period(.001);
dmwahl 1:d58df8cb271d 52
dmwahl 0:67debf2ccbc2 53 InterruptIn pumpTach(pumpTachPin);
dmwahl 0:67debf2ccbc2 54 pumpTach.rise(&pumpTachTrigger);
dmwahl 1:d58df8cb271d 55
dmwahl 0:67debf2ccbc2 56 pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
dmwahl 0:67debf2ccbc2 57 pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
dmwahl 0:67debf2ccbc2 58 pump_control_PID.setMode(AUTO_MODE);
dmwahl 0:67debf2ccbc2 59 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
dmwahl 0:67debf2ccbc2 60 }
dmwahl 0:67debf2ccbc2 61
dmwahl 0:67debf2ccbc2 62 void pump_pid_update(char error)
dmwahl 0:67debf2ccbc2 63 {
dmwahl 0:67debf2ccbc2 64 if (pumpPressure.status != 0x40) {
dmwahl 0:67debf2ccbc2 65 pump = 0;
dmwahl 0:67debf2ccbc2 66 pump_control_PID.reset();
dmwahl 0:67debf2ccbc2 67 } else {
dmwahl 0:67debf2ccbc2 68 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);
dmwahl 0:67debf2ccbc2 69
dmwahl 0:67debf2ccbc2 70 //Update the process variable.
dmwahl 0:67debf2ccbc2 71 pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
dmwahl 0:67debf2ccbc2 72 //PID calculation and set the new output value.
dmwahl 0:67debf2ccbc2 73 pump = pump_control_PID.compute();
dmwahl 0:67debf2ccbc2 74 //pump = 0.1;
iwolf32 4:79b23d1fbcd1 75 //ledGrn = ((float)1.0-pump.read());
dmwahl 0:67debf2ccbc2 76 }
dmwahl 0:67debf2ccbc2 77 }
dmwahl 0:67debf2ccbc2 78
dmwahl 0:67debf2ccbc2 79 void update_pressures()
dmwahl 0:67debf2ccbc2 80 {
dmwahl 0:67debf2ccbc2 81 Timer timer;
dmwahl 0:67debf2ccbc2 82 timer.start();
dmwahl 0:67debf2ccbc2 83 char error;
dmwahl 0:67debf2ccbc2 84 while (true) {
dmwahl 0:67debf2ccbc2 85 i2c1_m.lock();
dmwahl 0:67debf2ccbc2 86 timer.reset();
dmwahl 0:67debf2ccbc2 87 error = pumpPressure.readPT();
iwolf32 4:79b23d1fbcd1 88 //error |= mixerPressure.readPT();
dmwahl 0:67debf2ccbc2 89 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 90 i2c1_m.unlock();
dmwahl 0:67debf2ccbc2 91 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 92 pump_pid_update(error);
dmwahl 0:67debf2ccbc2 93 }
dmwahl 0:67debf2ccbc2 94 }
dmwahl 0:67debf2ccbc2 95
iwolf32 4:79b23d1fbcd1 96 void update_flow1()
dmwahl 0:67debf2ccbc2 97 {
dmwahl 0:67debf2ccbc2 98 Timer timer;
dmwahl 0:67debf2ccbc2 99 timer.start();
dmwahl 0:67debf2ccbc2 100 char error;
dmwahl 0:67debf2ccbc2 101 while (true) {
dmwahl 0:67debf2ccbc2 102 i2c2_m.lock();
dmwahl 0:67debf2ccbc2 103 timer.reset();
iwolf32 4:79b23d1fbcd1 104 error = mainflow.Measure(FLOW);
dmwahl 0:67debf2ccbc2 105 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 106 i2c2_m.unlock();
dmwahl 0:67debf2ccbc2 107 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 108 }
dmwahl 0:67debf2ccbc2 109 }
dmwahl 0:67debf2ccbc2 110
iwolf32 4:79b23d1fbcd1 111 //------------------------------------------------
iwolf32 4:79b23d1fbcd1 112 void update_flow2()
dmwahl 1:d58df8cb271d 113 {
dmwahl 1:d58df8cb271d 114 Timer timer;
dmwahl 1:d58df8cb271d 115 timer.start();
iwolf32 4:79b23d1fbcd1 116 char error;
dmwahl 1:d58df8cb271d 117 while (true) {
iwolf32 4:79b23d1fbcd1 118 i2c3_m.lock();
dmwahl 1:d58df8cb271d 119 timer.reset();
iwolf32 4:79b23d1fbcd1 120 error = loopflow.Measure(FLOW);
iwolf32 4:79b23d1fbcd1 121 int wait = (200 - timer.read_ms());
iwolf32 4:79b23d1fbcd1 122 i2c3_m.unlock();
dmwahl 1:d58df8cb271d 123 Thread::wait(wait);
dmwahl 1:d58df8cb271d 124 }
dmwahl 1:d58df8cb271d 125 }
iwolf32 4:79b23d1fbcd1 126 //------------------------------------------------
dmwahl 1:d58df8cb271d 127
dmwahl 0:67debf2ccbc2 128 void print_process_values()
dmwahl 0:67debf2ccbc2 129 {
dmwahl 0:67debf2ccbc2 130 //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
dmwahl 0:67debf2ccbc2 131 Timer timer;
dmwahl 0:67debf2ccbc2 132 timer.start();
dmwahl 0:67debf2ccbc2 133 while (true) {
dmwahl 0:67debf2ccbc2 134 stdio_m.lock();
dmwahl 0:67debf2ccbc2 135 timer.reset();
dmwahl 0:67debf2ccbc2 136
iwolf32 4:79b23d1fbcd1 137 pc.printf("%.02fkPa %.02fpsi %.02fC %02X %.2f%% %.0fRPM %u %.0f %s %u %.0f %s %.3f %.02fHz %.02f\r\n",
iwolf32 4:79b23d1fbcd1 138 pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.status,
dmwahl 3:9ff79ea3a294 139 pump.read()*100, pumpRpm,
iwolf32 4:79b23d1fbcd1 140 mainflow.flow.u16, (((float)mainflow.flow.i16) / mainflow.scaleFactor.u16), mainflow.flowUnitStr,
iwolf32 4:79b23d1fbcd1 141 loopflow.flow.u16, (((float)loopflow.flow.i16) / loopflow.scaleFactor.u16), loopflow.flowUnitStr,
iwolf32 4:79b23d1fbcd1 142 ((double)pot2)*pumpMaxPSI, frequency, dutycycle);//, agentlevel;
dmwahl 0:67debf2ccbc2 143 int wait = (1000 - timer.read_ms());
dmwahl 1:d58df8cb271d 144
dmwahl 0:67debf2ccbc2 145 stdio_m.unlock();
dmwahl 0:67debf2ccbc2 146 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 147 }
dmwahl 0:67debf2ccbc2 148 }
dmwahl 0:67debf2ccbc2 149
dmwahl 0:67debf2ccbc2 150 // main() runs in its own thread in the OS
dmwahl 0:67debf2ccbc2 151 int main()
dmwahl 0:67debf2ccbc2 152 {
dmwahl 0:67debf2ccbc2 153 pump_init();
iwolf32 4:79b23d1fbcd1 154 //ledBlu = 1;
dmwahl 0:67debf2ccbc2 155 pc.printf("Serenity Starting up...\n\r");
iwolf32 4:79b23d1fbcd1 156 //--------------------------------------------
iwolf32 4:79b23d1fbcd1 157 dutycycleup.rise(&dutycycleincrease);
iwolf32 4:79b23d1fbcd1 158 dutycycledown.rise(&dutycycledecrease);
iwolf32 4:79b23d1fbcd1 159 frequencyup.rise(&frequencyincrease);
iwolf32 4:79b23d1fbcd1 160 frequencydown.rise(&frequencydecrease);
dmwahl 0:67debf2ccbc2 161
iwolf32 4:79b23d1fbcd1 162 //Thread to execute injector valve control
iwolf32 4:79b23d1fbcd1 163 Injector_Valve_Control.set_priority(osPriorityHigh);
iwolf32 4:79b23d1fbcd1 164 Injector_Valve_Control.start(injectorvalvecontrol);
iwolf32 4:79b23d1fbcd1 165 //--------------------------------------------
dmwahl 1:d58df8cb271d 166
dmwahl 0:67debf2ccbc2 167 // Thread to poll pressure sensors
dmwahl 0:67debf2ccbc2 168 update_pressures_t.set_priority(osPriorityNormal);
iwolf32 4:79b23d1fbcd1 169 update_pressures_t.start(update_pressures);
dmwahl 0:67debf2ccbc2 170
iwolf32 4:79b23d1fbcd1 171 // Thread to poll flow sensor
iwolf32 4:79b23d1fbcd1 172 update_flow1_t.set_priority(osPriorityNormal);
iwolf32 4:79b23d1fbcd1 173 update_flow1_t.start(update_flow1);
iwolf32 4:79b23d1fbcd1 174
iwolf32 4:79b23d1fbcd1 175 //Thread to poll flow sensor
iwolf32 4:79b23d1fbcd1 176 update_flow2_t.set_priority(osPriorityNormal);
iwolf32 4:79b23d1fbcd1 177 update_flow2_t.start(update_flow2);
dmwahl 0:67debf2ccbc2 178
dmwahl 0:67debf2ccbc2 179 // Thread to send process values to serial port
dmwahl 0:67debf2ccbc2 180 print_process_values_t.set_priority(osPriorityLow);
dmwahl 0:67debf2ccbc2 181 print_process_values_t.start(&print_process_values);
dmwahl 0:67debf2ccbc2 182
dmwahl 0:67debf2ccbc2 183 while (true) {
iwolf32 4:79b23d1fbcd1 184 pump_tach_update();
iwolf32 4:79b23d1fbcd1 185
dmwahl 0:67debf2ccbc2 186 }
dmwahl 0:67debf2ccbc2 187 }
dmwahl 0:67debf2ccbc2 188