jj
Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of HSPFLOW by
Diff: main.cpp
- Revision:
- 5:8e47d5323647
- Parent:
- 4:79b23d1fbcd1
- Child:
- 6:c20cf19f7d9b
diff -r 79b23d1fbcd1 -r 8e47d5323647 main.cpp --- a/main.cpp Wed Jul 26 14:03:10 2017 +0000 +++ b/main.cpp Tue Aug 01 14:48:01 2017 +0000 @@ -1,188 +1,19 @@ #include "main.h" -//--------------------------- -void dutycycleincrease() -{ - dutycycle=dutycycle+0.01; -} - -void dutycycledecrease() -{ - dutycycle=dutycycle-0.01; -} -void frequencyincrease() -{ - frequency=frequency+1; -} -void frequencydecrease() -{ - frequency=frequency-1; -} -void injectorvalvecontrol(){ - -while(true){ -float openvalue=(((1/frequency)*dutycycle)); -float offvalue=(1/frequency)-openvalue; - -injector.on(); -wait(openvalue); -injector.off(); -wait(offvalue); -} -} - -//----------------------------- -void pumpTachTrigger() + +int main() { - 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()); + pc.baud(250000); + t1.start(); + pc.printf("Serenity Starting up...\n\r"); + while (true) { + float t= t1.read_ms(); + High_Speed_Pressure = analog_value.read(); // + High_Speed_Pressure = High_Speed_Pressure*100*(14.69/8.6); // Calibrate value to 1atm + mainflow.Measure(FLOW); + pc.printf("%.02f %.02f\r\n", High_Speed_Pressure, t); + //pc.printf("%0.02fpsi raw: %u %.02f", High_Speed_Pressure, mainflow.flow.u16, t);*/ + } } -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_flow1() -{ - Timer timer; - timer.start(); - char error; - while (true) { - i2c2_m.lock(); - timer.reset(); - error = mainflow.Measure(FLOW); - int wait = (200 - timer.read_ms()); - i2c2_m.unlock(); - Thread::wait(wait); - } -} - -//------------------------------------------------ -void update_flow2() -{ - Timer timer; - timer.start(); - char error; - while (true) { - i2c3_m.lock(); - timer.reset(); - error = loopflow.Measure(FLOW); - int wait = (200 - timer.read_ms()); - i2c3_m.unlock(); - Thread::wait(wait); - } -} -//------------------------------------------------ - -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 %02X %.2f%% %.0fRPM %u %.0f %s %u %.0f %s %.3f %.02fHz %.02f\r\n", - pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.status, - pump.read()*100, pumpRpm, - mainflow.flow.u16, (((float)mainflow.flow.i16) / mainflow.scaleFactor.u16), mainflow.flowUnitStr, - loopflow.flow.u16, (((float)loopflow.flow.i16) / loopflow.scaleFactor.u16), loopflow.flowUnitStr, - ((double)pot2)*pumpMaxPSI, frequency, dutycycle);//, agentlevel; - int wait = (1000 - timer.read_ms()); - - stdio_m.unlock(); - 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"); -//-------------------------------------------- -dutycycleup.rise(&dutycycleincrease); -dutycycledown.rise(&dutycycledecrease); -frequencyup.rise(&frequencyincrease); -frequencydown.rise(&frequencydecrease); - - //Thread to execute injector valve control - Injector_Valve_Control.set_priority(osPriorityHigh); - Injector_Valve_Control.start(injectorvalvecontrol); -//-------------------------------------------- - - // Thread to poll pressure sensors - update_pressures_t.set_priority(osPriorityNormal); - update_pressures_t.start(update_pressures); - - // Thread to poll flow sensor - update_flow1_t.set_priority(osPriorityNormal); - update_flow1_t.start(update_flow1); - - //Thread to poll flow sensor - update_flow2_t.set_priority(osPriorityNormal); - update_flow2_t.start(update_flow2); - - // 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) { - pump_tach_update(); - - } -} -