Pump Progress so far

Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of TestBenchSerenity-proto_F429ZI1 by jack kemnitz

Committer:
iwolf32
Date:
Thu Aug 31 14:10:02 2017 +0000
Revision:
8:962b78253710
Parent:
7:b385e6a3f622
Pump Progress so far

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 0:67debf2ccbc2 1 #include "main.h"
iwolf32 4:79b23d1fbcd1 2 //---------------------------
iwolf32 8:962b78253710 3
iwolf32 8:962b78253710 4 EventQueue queue;
iwolf32 8:962b78253710 5
iwolf32 8:962b78253710 6 void dutycycleincrease(){
iwolf32 4:79b23d1fbcd1 7 dutycycle=dutycycle+0.01;
iwolf32 4:79b23d1fbcd1 8 }
dmwahl 0:67debf2ccbc2 9
iwolf32 8:962b78253710 10 void dutycycledecrease(){
iwolf32 4:79b23d1fbcd1 11 dutycycle=dutycycle-0.01;
iwolf32 4:79b23d1fbcd1 12 }
iwolf32 4:79b23d1fbcd1 13
iwolf32 8:962b78253710 14 void frequencyincrease(){
iwolf32 4:79b23d1fbcd1 15 frequency=frequency+1;
iwolf32 4:79b23d1fbcd1 16 }
iwolf32 8:962b78253710 17
iwolf32 8:962b78253710 18 void frequencydecrease(){
iwolf32 4:79b23d1fbcd1 19 frequency=frequency-1;
iwolf32 4:79b23d1fbcd1 20 }
iwolf32 8:962b78253710 21
iwolf32 4:79b23d1fbcd1 22 void injectorvalvecontrol(){
iwolf32 8:962b78253710 23 while(true) {
iwolf32 8:962b78253710 24 float openvalue=(((1/frequency)*dutycycle));
iwolf32 8:962b78253710 25 float offvalue=(1/frequency)-openvalue;
iwolf32 8:962b78253710 26 injector.on();
iwolf32 8:962b78253710 27 wait(openvalue);
iwolf32 8:962b78253710 28 injector.off();
iwolf32 8:962b78253710 29 wait(offvalue);
iwolf32 8:962b78253710 30 }
iwolf32 4:79b23d1fbcd1 31 }
iwolf32 4:79b23d1fbcd1 32
iwolf32 4:79b23d1fbcd1 33 //-----------------------------
iwolf32 8:962b78253710 34 void pumpTachTrigger(){
dmwahl 0:67debf2ccbc2 35 pumpTachCounts++;
dmwahl 0:67debf2ccbc2 36 }
dmwahl 0:67debf2ccbc2 37
iwolf32 8:962b78253710 38 void pump_tach_update(){
dmwahl 0:67debf2ccbc2 39 float i = pumpTachCounts; // In case it triggers mid-calculation
212600191 7:b385e6a3f622 40 pumpTachCounts=0;
dmwahl 0:67debf2ccbc2 41 pumpRpm = (i/pumpTachPoles)*60;
dmwahl 0:67debf2ccbc2 42 }
dmwahl 0:67debf2ccbc2 43
iwolf32 8:962b78253710 44 void pump_init(){
dmwahl 0:67debf2ccbc2 45 pump.period(.001); // 1kHz PWM
dmwahl 0:67debf2ccbc2 46 pump = 0;
dmwahl 0:67debf2ccbc2 47 pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
dmwahl 0:67debf2ccbc2 48 pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
dmwahl 0:67debf2ccbc2 49 pump_control_PID.setMode(AUTO_MODE);
dmwahl 0:67debf2ccbc2 50 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
dmwahl 0:67debf2ccbc2 51 }
dmwahl 0:67debf2ccbc2 52
iwolf32 8:962b78253710 53 void pump_pid_update(char error){
dmwahl 0:67debf2ccbc2 54 if (pumpPressure.status != 0x40) {
dmwahl 0:67debf2ccbc2 55 pump = 0;
dmwahl 0:67debf2ccbc2 56 pump_control_PID.reset();
dmwahl 0:67debf2ccbc2 57 } else {
dmwahl 0:67debf2ccbc2 58 pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);
dmwahl 0:67debf2ccbc2 59
dmwahl 0:67debf2ccbc2 60 //Update the process variable.
dmwahl 0:67debf2ccbc2 61 pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
dmwahl 0:67debf2ccbc2 62 //PID calculation and set the new output value.
dmwahl 0:67debf2ccbc2 63 pump = pump_control_PID.compute();
dmwahl 0:67debf2ccbc2 64 }
dmwahl 0:67debf2ccbc2 65 }
dmwahl 0:67debf2ccbc2 66
iwolf32 8:962b78253710 67 void update_pressures(){
dmwahl 0:67debf2ccbc2 68 Timer timer;
dmwahl 0:67debf2ccbc2 69 timer.start();
dmwahl 0:67debf2ccbc2 70 char error;
dmwahl 0:67debf2ccbc2 71 while (true) {
dmwahl 0:67debf2ccbc2 72 i2c1_m.lock();
dmwahl 0:67debf2ccbc2 73 timer.reset();
dmwahl 0:67debf2ccbc2 74 error = pumpPressure.readPT();
iwolf32 4:79b23d1fbcd1 75 //error |= mixerPressure.readPT();
dmwahl 0:67debf2ccbc2 76 int wait = (200 - timer.read_ms());
dmwahl 0:67debf2ccbc2 77 i2c1_m.unlock();
dmwahl 0:67debf2ccbc2 78 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 79 pump_pid_update(error);
dmwahl 0:67debf2ccbc2 80 }
dmwahl 0:67debf2ccbc2 81 }
dmwahl 0:67debf2ccbc2 82
dmwahl 0:67debf2ccbc2 83 void print_process_values()
dmwahl 0:67debf2ccbc2 84 {
iwolf32 8:962b78253710 85 Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
dmwahl 0:67debf2ccbc2 86 Timer timer;
dmwahl 0:67debf2ccbc2 87 timer.start();
dmwahl 0:67debf2ccbc2 88 while (true) {
dmwahl 0:67debf2ccbc2 89 stdio_m.lock();
dmwahl 0:67debf2ccbc2 90 timer.reset();
iwolf32 8:962b78253710 91 pc.printf(" %.02f\t %.01f\t %.0f\t %.02f\t %i\t %.02f\r\n",
iwolf32 8:962b78253710 92 pumpPressure.pressurePSI, ((double)pot2-.002)*pumpMaxPSI, pumpRpm,
iwolf32 8:962b78253710 93 frequency, x, dutycycle);//, agentlevel;
iwolf32 8:962b78253710 94 int wait = (100 - timer.read_ms());
dmwahl 0:67debf2ccbc2 95 stdio_m.unlock();
dmwahl 0:67debf2ccbc2 96 Thread::wait(wait);
dmwahl 0:67debf2ccbc2 97 }
dmwahl 0:67debf2ccbc2 98 }
dmwahl 0:67debf2ccbc2 99
iwolf32 8:962b78253710 100
iwolf32 8:962b78253710 101 void dutycycleint()
iwolf32 8:962b78253710 102 {
iwolf32 8:962b78253710 103 stdio_m.lock();
iwolf32 8:962b78253710 104 pc.printf("Hello");
iwolf32 8:962b78253710 105 x=x+1;
iwolf32 8:962b78253710 106 stdio_m.unlock();
iwolf32 8:962b78253710 107 return;
iwolf32 8:962b78253710 108 }
iwolf32 8:962b78253710 109
dmwahl 0:67debf2ccbc2 110 // main() runs in its own thread in the OS
dmwahl 0:67debf2ccbc2 111 int main()
dmwahl 0:67debf2ccbc2 112 {
dmwahl 0:67debf2ccbc2 113 pump_init();
212600191 7:b385e6a3f622 114 pump_tach_update();
iwolf32 8:962b78253710 115 dutycycleup.rise(&dutycycleincrease);
iwolf32 8:962b78253710 116 dutycycledown.rise(&dutycycledecrease);
iwolf32 8:962b78253710 117 frequencyup.rise(&frequencyincrease);
iwolf32 8:962b78253710 118 frequencydown.rise(&frequencydecrease);
iwolf32 8:962b78253710 119 pumpTach.rise(&pumpTachTrigger);
iwolf32 8:962b78253710 120 pump_tach_ticker.attach(&pump_tach_update, 1.0);
iwolf32 8:962b78253710 121
iwolf32 8:962b78253710 122 //Interrupt for Duty Cycle
iwolf32 8:962b78253710 123 Thread eventThread;
iwolf32 8:962b78253710 124 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
iwolf32 8:962b78253710 125 pc.attach(&dutycycleint);
dmwahl 0:67debf2ccbc2 126
iwolf32 4:79b23d1fbcd1 127 //Thread to execute injector valve control
iwolf32 4:79b23d1fbcd1 128 Injector_Valve_Control.set_priority(osPriorityHigh);
iwolf32 4:79b23d1fbcd1 129 Injector_Valve_Control.start(injectorvalvecontrol);
dmwahl 1:d58df8cb271d 130
dmwahl 0:67debf2ccbc2 131 // Thread to poll pressure sensors
dmwahl 0:67debf2ccbc2 132 update_pressures_t.set_priority(osPriorityNormal);
iwolf32 8:962b78253710 133 update_pressures_t.start(update_pressures);
dmwahl 0:67debf2ccbc2 134
dmwahl 0:67debf2ccbc2 135 // Thread to send process values to serial port
iwolf32 8:962b78253710 136 // print_process_values_t.set_priority(osPriorityLow);
iwolf32 8:962b78253710 137 //print_process_values_t.start(&print_process_values);
212600191 7:b385e6a3f622 138
iwolf32 8:962b78253710 139 while(1){}
iwolf32 8:962b78253710 140 }
dmwahl 0:67debf2ccbc2 141
212600191 7:b385e6a3f622 142