Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of PUMP_SCRIPT1 by
main.cpp
- Committer:
- dmwahl
- Date:
- 2017-07-24
- Revision:
- 3:9ff79ea3a294
- Parent:
- 1:d58df8cb271d
- Child:
- 4:79b23d1fbcd1
File content as of revision 3:9ff79ea3a294:
#include "main.h" void pumpTachTrigger() { pumpTachCounts++; } void pump_tach_update() { float i = pumpTachCounts; // In case it triggers mid-calculation pumpTachCounts = 0; pumpRpm = (i/pumpTachPoles)*60; } void pump_init() { pump.period(.001); // 1kHz PWM pump = 0; ledGrn.period(.001); InterruptIn pumpTach(pumpTachPin); pumpTach.rise(&pumpTachTrigger); pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI); pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1 pump_control_PID.setMode(AUTO_MODE); pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/ } void pump_pid_update(char error) { if (pumpPressure.status != 0x40) { pump = 0; pump_control_PID.reset(); } else { pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); //Update the process variable. pump_control_PID.setProcessValue(pumpPressure.pressurePSI); //PID calculation and set the new output value. pump = pump_control_PID.compute(); //pump = 0.1; ledGrn = ((float)1.0-pump.read()); } } void update_pressures() { Timer timer; timer.start(); char error; while (true) { i2c1_m.lock(); timer.reset(); error = pumpPressure.readPT(); error |= mixerPressure.readPT(); int wait = (200 - timer.read_ms()); i2c1_m.unlock(); Thread::wait(wait); pump_pid_update(error); } } void update_airflow() { Timer timer; timer.start(); char error; while (true) { i2c2_m.lock(); timer.reset(); error = sfm7033.Measure(FLOW); int wait = (200 - timer.read_ms()); i2c2_m.unlock(); Thread::wait(wait); } } void update_level() { Timer timer; timer.start(); while (true) { i2c1_m.lock(); timer.reset(); agentlevel = (float)level; int wait = (1000 - timer.read_ms()); i2c1_m.unlock(); Thread::wait(wait); } } //float get_level() //{ // i2c1_m.lock(); // float value = (float)level; // i2c1_m.unlock(); // return value; //} void print_process_values() { //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial Timer timer; timer.start(); while (true) { stdio_m.lock(); timer.reset(); pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF %02X %.02fkPa %.02fpsi %.02fC %.02fF %02X %.2f%% %.0fRPM %u %.0f %s %.1f %.3f\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF, pumpPressure.status, mixerPressure.pressureKPA, mixerPressure.pressurePSI, mixerPressure.temperatureC, mixerPressure.temperatureF, mixerPressure.status, pump.read()*100, pumpRpm, sfm7033.flow.u16, (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16), sfm7033.flowUnitStr, (double)pot1*18, ((double)pot2)*pumpMaxPSI);//, agentlevel; int wait = (1000 - timer.read_ms()); stdio_m.unlock(); Thread::wait(wait); } } void update_lcd() { Timer timer; timer.start(); while (true) { float flow = ((((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16) < 0 ? 0 : (((float)sfm7033.flow.i16 / 2) / sfm7033.scaleFactor.u16)); flow = flow/1000; stdio_m.lock(); timer.reset(); lcd.cls(); lcd.font((unsigned char*)ArialR12x14); lcd.locate(0, 0); lcd.printf("%.2f slpm AA: %.1f", flow, (double)pot1*18); lcd.locate(0, 14); lcd.printf("PV: %.1f", pumpPressure.pressurePSI); lcd.locate(64, 14); lcd.printf("SV: %.1f", ((double)pot2)*pumpMaxPSI); int wait = (1000 - timer.read_ms()); stdio_m.unlock(); Thread::wait(wait); } } void update_shutoff() { Timer timer; timer.start(); while (true) { float threshold = 0.1; timer.reset(); if((double)pot1 < 0.05) { shutoff.off(); //pc.printf("shutoff off\r\n"); //Thread::wait(1000); } if((double)pot1 >= 0.1) { shutoff.on(); //pc.printf("shutoff on\r\n"); //Thread::wait(1000); } int wait = (200 - timer.read_ms()); Thread::wait(wait); } } // main() runs in its own thread in the OS int main() { pump_init(); ledBlu = 1; pc.printf("Serenity Starting up...\n\r"); /*pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpPressure.pmin, pumpPressure.pmax); pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpPressure.year, pumpPressure.month, pumpPressure.day, pumpPressure.mode); pc.printf("Status: 0x%x\r\n", pumpPressure.getStatus());*/ // Thread to turn shutoff valve on/off update_shutoff_t.set_priority(osPriorityHigh); update_shutoff_t.start(update_shutoff); // Thread to poll pressure sensors update_pressures_t.set_priority(osPriorityNormal); update_pressures_t.start(update_pressures); // Thread to poll airflow sensor update_airflow_t.set_priority(osPriorityNormal); update_airflow_t.start(update_airflow); // Thread to poll level sensor //update_level_t.set_priority(osPriorityIdle); //update_level_t.start(update_level); // Thread to update lcd update_lcd_t.set_priority(osPriorityIdle); update_lcd_t.start(update_lcd); // Thread to send process values to serial port print_process_values_t.set_priority(osPriorityLow); print_process_values_t.start(&print_process_values); while (true) { //pc.printf("%.02fkPa %.02fpsi %.02fC %.02fF\r\n", pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.temperatureF); if((double)pot1 >= 0.1) { injector.on(); } //pc.printf("shutoff on\r\n"); Thread::wait(5); injector.off(); //pc.printf("shutoff off\r\n");*/ Thread::wait(495); } }