Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of PUMP_SCRIPT1 by Ian Wolf

Committer:
212600191
Date:
Thu Aug 03 19:07:43 2017 +0000
Revision:
7:b385e6a3f622
Parent:
5:f2cb21046678
Child:
8:bf86306ce7bf
jfkdd;

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 {
212600191 7:b385e6a3f622 37
dmwahl 0:67debf2ccbc2 38 pumpTachCounts++;
dmwahl 0:67debf2ccbc2 39 }
dmwahl 0:67debf2ccbc2 40
dmwahl 0:67debf2ccbc2 41 void pump_tach_update()
dmwahl 0:67debf2ccbc2 42 {
dmwahl 0:67debf2ccbc2 43 float i = pumpTachCounts; // In case it triggers mid-calculation
212600191 7:b385e6a3f622 44 pumpTachCounts=0;
dmwahl 0:67debf2ccbc2 45 pumpRpm = (i/pumpTachPoles)*60;
dmwahl 0:67debf2ccbc2 46 }
dmwahl 0:67debf2ccbc2 47
dmwahl 0:67debf2ccbc2 48 void pump_init()
dmwahl 0:67debf2ccbc2 49 {
dmwahl 0:67debf2ccbc2 50 pump.period(.001); // 1kHz PWM
dmwahl 0:67debf2ccbc2 51 pump = 0;
212600191 7:b385e6a3f622 52
212600191 7:b385e6a3f622 53
iwolf32 4:79b23d1fbcd1 54 //ledGrn.period(.001);
dmwahl 1:d58df8cb271d 55
212600191 7:b385e6a3f622 56
212600191 7:b385e6a3f622 57
dmwahl 1:d58df8cb271d 58
dmwahl 0:67debf2ccbc2 59 pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
dmwahl 0:67debf2ccbc2 60 pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
dmwahl 0:67debf2ccbc2 61 pump_control_PID.setMode(AUTO_MODE);
dmwahl 0:67debf2ccbc2 62 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
dmwahl 0:67debf2ccbc2 63 }
dmwahl 0:67debf2ccbc2 64
dmwahl 0:67debf2ccbc2 65 void pump_pid_update(char error)
dmwahl 0:67debf2ccbc2 66 {
dmwahl 0:67debf2ccbc2 67 if (pumpPressure.status != 0x40) {
dmwahl 0:67debf2ccbc2 68 pump = 0;
dmwahl 0:67debf2ccbc2 69 pump_control_PID.reset();
dmwahl 0:67debf2ccbc2 70 } else {
dmwahl 0:67debf2ccbc2 71 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);
dmwahl 0:67debf2ccbc2 72
dmwahl 0:67debf2ccbc2 73 //Update the process variable.
dmwahl 0:67debf2ccbc2 74 pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
dmwahl 0:67debf2ccbc2 75 //PID calculation and set the new output value.
dmwahl 0:67debf2ccbc2 76 pump = pump_control_PID.compute();
dmwahl 0:67debf2ccbc2 77 //pump = 0.1;
iwolf32 4:79b23d1fbcd1 78 //ledGrn = ((float)1.0-pump.read());
dmwahl 0:67debf2ccbc2 79 }
dmwahl 0:67debf2ccbc2 80 }
dmwahl 0:67debf2ccbc2 81
dmwahl 0:67debf2ccbc2 82 void update_pressures()
dmwahl 0:67debf2ccbc2 83 {
dmwahl 0:67debf2ccbc2 84 Timer timer;
dmwahl 0:67debf2ccbc2 85 timer.start();
dmwahl 0:67debf2ccbc2 86 char error;
dmwahl 0:67debf2ccbc2 87 while (true) {
dmwahl 0:67debf2ccbc2 88 i2c1_m.lock();
dmwahl 0:67debf2ccbc2 89 timer.reset();
dmwahl 0:67debf2ccbc2 90 error = pumpPressure.readPT();
iwolf32 4:79b23d1fbcd1 91 //error |= mixerPressure.readPT();
dmwahl 0:67debf2ccbc2 92 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 93 i2c1_m.unlock();
dmwahl 0:67debf2ccbc2 94 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 95 pump_pid_update(error);
dmwahl 0:67debf2ccbc2 96 }
dmwahl 0:67debf2ccbc2 97 }
dmwahl 0:67debf2ccbc2 98
212600191 5:f2cb21046678 99 /*void update_flow1()
dmwahl 0:67debf2ccbc2 100 {
dmwahl 0:67debf2ccbc2 101 Timer timer;
dmwahl 0:67debf2ccbc2 102 timer.start();
dmwahl 0:67debf2ccbc2 103 char error;
dmwahl 0:67debf2ccbc2 104 while (true) {
dmwahl 0:67debf2ccbc2 105 i2c2_m.lock();
dmwahl 0:67debf2ccbc2 106 timer.reset();
iwolf32 4:79b23d1fbcd1 107 error = mainflow.Measure(FLOW);
dmwahl 0:67debf2ccbc2 108 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 109 i2c2_m.unlock();
dmwahl 0:67debf2ccbc2 110 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 111 }
dmwahl 0:67debf2ccbc2 112 }
dmwahl 0:67debf2ccbc2 113
iwolf32 4:79b23d1fbcd1 114 //------------------------------------------------
iwolf32 4:79b23d1fbcd1 115 void update_flow2()
dmwahl 1:d58df8cb271d 116 {
dmwahl 1:d58df8cb271d 117 Timer timer;
dmwahl 1:d58df8cb271d 118 timer.start();
iwolf32 4:79b23d1fbcd1 119 char error;
dmwahl 1:d58df8cb271d 120 while (true) {
iwolf32 4:79b23d1fbcd1 121 i2c3_m.lock();
dmwahl 1:d58df8cb271d 122 timer.reset();
iwolf32 4:79b23d1fbcd1 123 error = loopflow.Measure(FLOW);
iwolf32 4:79b23d1fbcd1 124 int wait = (200 - timer.read_ms());
iwolf32 4:79b23d1fbcd1 125 i2c3_m.unlock();
dmwahl 1:d58df8cb271d 126 Thread::wait(wait);
dmwahl 1:d58df8cb271d 127 }
212600191 5:f2cb21046678 128 }*/
iwolf32 4:79b23d1fbcd1 129 //------------------------------------------------
dmwahl 1:d58df8cb271d 130
dmwahl 0:67debf2ccbc2 131 void print_process_values()
dmwahl 0:67debf2ccbc2 132 {
dmwahl 0:67debf2ccbc2 133 //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
dmwahl 0:67debf2ccbc2 134 Timer timer;
dmwahl 0:67debf2ccbc2 135 timer.start();
dmwahl 0:67debf2ccbc2 136 while (true) {
dmwahl 0:67debf2ccbc2 137 stdio_m.lock();
dmwahl 0:67debf2ccbc2 138 timer.reset();
dmwahl 0:67debf2ccbc2 139
212600191 7:b385e6a3f622 140 pc.printf(" %.02fprespsi %.01fsetpsi %.0fRPM %.02fHz %.02f\r\n",
212600191 7:b385e6a3f622 141 pumpPressure.pressurePSI, ((double)pot2-.002)*pumpMaxPSI, pumpRpm,
212600191 5:f2cb21046678 142 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();
212600191 7:b385e6a3f622 154 pump_tach_update();
iwolf32 4:79b23d1fbcd1 155 //ledBlu = 1;
dmwahl 0:67debf2ccbc2 156 pc.printf("Serenity Starting up...\n\r");
iwolf32 4:79b23d1fbcd1 157 //--------------------------------------------
iwolf32 4:79b23d1fbcd1 158 dutycycleup.rise(&dutycycleincrease);
iwolf32 4:79b23d1fbcd1 159 dutycycledown.rise(&dutycycledecrease);
iwolf32 4:79b23d1fbcd1 160 frequencyup.rise(&frequencyincrease);
212600191 7:b385e6a3f622 161 frequencydown.rise(&frequencydecrease);
212600191 7:b385e6a3f622 162 pumpTach.rise(&pumpTachTrigger);
212600191 7:b385e6a3f622 163 pump_tach_ticker.attach(&pump_tach_update, 1.0);
dmwahl 0:67debf2ccbc2 164
iwolf32 4:79b23d1fbcd1 165 //Thread to execute injector valve control
iwolf32 4:79b23d1fbcd1 166 Injector_Valve_Control.set_priority(osPriorityHigh);
iwolf32 4:79b23d1fbcd1 167 Injector_Valve_Control.start(injectorvalvecontrol);
iwolf32 4:79b23d1fbcd1 168 //--------------------------------------------
dmwahl 1:d58df8cb271d 169
dmwahl 0:67debf2ccbc2 170 // Thread to poll pressure sensors
dmwahl 0:67debf2ccbc2 171 update_pressures_t.set_priority(osPriorityNormal);
iwolf32 4:79b23d1fbcd1 172 update_pressures_t.start(update_pressures);
dmwahl 0:67debf2ccbc2 173
iwolf32 4:79b23d1fbcd1 174 // Thread to poll flow sensor
212600191 5:f2cb21046678 175 //update_flow1_t.set_priority(osPriorityNormal);
212600191 5:f2cb21046678 176 //update_flow1_t.start(update_flow1);
iwolf32 4:79b23d1fbcd1 177
iwolf32 4:79b23d1fbcd1 178 //Thread to poll flow sensor
212600191 5:f2cb21046678 179 //update_flow2_t.set_priority(osPriorityNormal);
212600191 5:f2cb21046678 180 //update_flow2_t.start(update_flow2);
dmwahl 0:67debf2ccbc2 181
dmwahl 0:67debf2ccbc2 182 // Thread to send process values to serial port
dmwahl 0:67debf2ccbc2 183 print_process_values_t.set_priority(osPriorityLow);
dmwahl 0:67debf2ccbc2 184 print_process_values_t.start(&print_process_values);
212600191 7:b385e6a3f622 185
212600191 7:b385e6a3f622 186
dmwahl 0:67debf2ccbc2 187
212600191 7:b385e6a3f622 188
212600191 7:b385e6a3f622 189
iwolf32 4:79b23d1fbcd1 190
212600191 7:b385e6a3f622 191
dmwahl 0:67debf2ccbc2 192 }
dmwahl 0:67debf2ccbc2 193