Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180
Fork of TestBenchSerenity-proto_F429ZI by
Revision 5:34584c78fbac, committed 2017-07-31
- Comitter:
- iwolf32
- Date:
- Mon Jul 31 20:55:31 2017 +0000
- Parent:
- 4:79b23d1fbcd1
- Commit message:
- REV1;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Jul 26 14:03:10 2017 +0000
+++ b/main.cpp Mon Jul 31 20:55:31 2017 +0000
@@ -1,82 +1,7 @@
#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()
-{
- 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()
+//Liquid Flow Sensor Adjacent to the Pump
+void update_flow2()
{
Timer timer;
timer.start();
@@ -84,47 +9,21 @@
while (true) {
i2c1_m.lock();
timer.reset();
- error = pumpPressure.readPT();
- //error |= mixerPressure.readPT();
- int wait = (200 - timer.read_ms());
+ error = mainflow.Measure(FLOW);
+ //int wait = (200 - timer.read_ms());
i2c1_m.unlock();
- Thread::wait(wait);
- pump_pid_update(error);
+ //Thread::wait(wait);
}
}
-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);
- }
+//High Speed Pressure Sensor
+void highspeedpressure(){
+ while(true) {
+ High_Speed_Pressure = analog_value.read(); //
+ High_Speed_Pressure = High_Speed_Pressure*100*(14.69/8.6); // Calibrate value to 1atm
+ wait_ms(1);
+ }
}
-
-//------------------------------------------------
-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
@@ -134,44 +33,26 @@
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());
-
+ pc.printf("%0.02fpsi raw: %u %.02f\r\n",
+ High_Speed_Pressure, mainflow.flow.u16, t);
+ //pc.printf("%.0f %s (raw: %i) Temp: %.1fC Vdd: %.2f\n\r", ((float)mainflow.flow.i16 / mainflow.scaleFactor.u16), mainflow.flowUnitStr, mainflow.flow.i16, (float)mainflow.temperature.i16/10, (float)mainflow.vdd.u16/1000);
+ //int wait = 1;//(1000- timer.read_ms());
stdio_m.unlock();
- Thread::wait(wait);
+ //Thread::wait(wait);
}
}
// main() runs in its own thread in the OS
int main()
{
- pump_init();
+ t1.start();
//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 high speed pressure sensor
+ HighSpeedPressure.set_priority(osPriorityNormal);
+ HighSpeedPressure.start(highspeedpressure);
+
//Thread to poll flow sensor
update_flow2_t.set_priority(osPriorityNormal);
update_flow2_t.start(update_flow2);
@@ -181,8 +62,8 @@
print_process_values_t.start(&print_process_values);
while (true) {
- pump_tach_update();
-
+ t= t1.read_ms();
+
}
}
--- a/main.h Wed Jul 26 14:03:10 2017 +0000 +++ b/main.h Mon Jul 31 20:55:31 2017 +0000 @@ -7,33 +7,22 @@ #include "PID.h" #include "coil-driver.h" #include "VL6180.h" -/*Start of Where Additional Code was Added ---------------------------------------------------------------------------------------------------*/ -//Coil shutoff(A1, 5000, 40, 6); // Shutoff valve, 5ms spike time, 25kHz PWM at 15% duty cycle -Coil injector(A0, 200, 40, 3); // Injector valve, 200us spike time, 25kHz PWM at 7.5% duty cycle -InterruptIn dutycycleup(A2); -InterruptIn dutycycledown(D4); -InterruptIn frequencyup(A5); -InterruptIn frequencydown(A4); +//Timer From Start of the Script +Timer t1; +float t; -Thread Injector_Valve_Control; - -double frequency=5.00; -double dutycycle=0.75; -float openvalue=(((1/frequency)*dutycycle)); -float offvalue=(1/frequency)-openvalue; - -/* ---------------------------------------------------------------------------------------------------*/ - +//High Speed Pressure +AnalogIn analog_value(A3); +Thread HighSpeedPressure; +float High_Speed_Pressure=0.00; // Mbed application shield display #include "C12832A1Z.h" #include "Small_7.h" #include "Fonts/ArialR12x14.h" -C12832A1Z lcd(D11, D13, D12, D7, D10); // MOSI, SCK, Reset, A0, CS +//C12832A1Z lcd(D11, D13, D12, D7, D10); // MOSI, SCK, Reset, A0, CS // LCD width and height (minus 1) #define lcdWidth 127 #define lcdHeight 31 @@ -45,8 +34,8 @@ //#define pumpHystPSI 2 // Gas flow sensor defines -#define SFM7033_ADDR 0x40 -#define SFM7034_ADDR 0x40 +#define MAINFLOW_ADDR 0x40 +#define LOOPFLOW_ADDR 0x40 // End gas flow sensor defines // Liquid pump defines @@ -55,17 +44,15 @@ float pumpKp = 4.0; float pumpKi = 1.0; #define pumpKd 0 - #define pumpPwmFrequency 1000 // Frequency of PWM signal supplied to pump #define pumpTachPoles 6 // 6 pulses per revolution -#define pumpTachPin PB_15 // Pump tach input (green wire) +#define pumpTachPin PA_10 // Pump tach input (green wire) #define pumpCtrlPin PB_13 // Pump control (white wire) // End Liquid pump defines -Thread print_process_values_t, update_pressures_t, update_flow1_t, update_flow2_t, update_level_t, update_lcd_t, update_shutoff_t; +Thread print_process_values_t, update_pressures_t, update_flow1_t, update_flow2_t, update_shutoff_t; Mutex i2c1_m, i2c2_m, i2c3_m, stdio_m; -//DigitalOut myled(LED2); Serial pc(USBTX, USBRX, 250000); // tx, rx, baud // an I2C sub-class that provides a constructed default @@ -78,34 +65,16 @@ }; //I2CPreInit gI2C1(I2C_SDA, I2C_SCL, I2C frequency); -I2CPreInit i2c1(PB_9, PB_8, 100000); -I2CPreInit i2c2(PB_3, PB_10, 100000); -I2CPreInit i2c3(PB_4, PA_8, 100000); - -KELLER_PRESSURE pumpPressure(i2c1, 0x40); +I2CPreInit i2c1(PB_3, PB_10, 100000); // Sensirion gas flow sensor object (i2c object, i2c address, calibration field, resolution 9-16 bit) -// calibration field 1: Air, 2: O2, 3: N2O -SF04 mainflow(i2c2, SFM7033_ADDR, 0, 9); // Gas flow sensor (SFM7033) -SF04 loopflow(i2c3, SFM7034_ADDR, 0, 9); +SF04 mainflow(i2c1, MAINFLOW_ADDR, 0, 16); // Mbed application shield -//AnalogIn pot1(A0); AnalogIn pot2(A1); - PwmOut pump(pumpCtrlPin); -//pump.period(.001); PID pump_control_PID(pumpKp, pumpKi, pumpKd, pumpPIDRate); -//InterruptIn pumpTach(pumpTachPin); -volatile int pumpTachCounts = 0; -volatile float pumpRpm = 0; - -//PwmOut ledRed(PE_11); -//PwmOut ledGrn(PD_15); -//DigitalOut ledBlu(PF_12); - -// Level sensor -//VL6180 level(i2c1); //I2C object -//float agentlevel = 0; +VL6180 level(i2c1); //I2C object +float agentlevel = 0; #endif \ No newline at end of file
