From 4e19cf919fdc949249d384acd70dd720cf3e9fc3 Mon Sep 17 00:00:00 2001 From: Amado Antonini Date: Wed, 25 Mar 2020 17:21:04 -0400 Subject: [PATCH 1/5] display all pressures --- e-vent.ino | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/e-vent.ino b/e-vent.ino index 408ec0b..5291fb1 100644 --- a/e-vent.ino +++ b/e-vent.ino @@ -213,8 +213,10 @@ void loop() { goToPosition(Volume, Vin); } - if(millis()-stateTimer > Tin*1000 || abs(motorPosition - Volume) < goalTol) + if(millis()-stateTimer > Tin*1000 || abs(motorPosition - Volume) < goalTol){ + displ.writePeakP(round(readPressure())); setState(PAUSE_STATE); + } } else if(state == PAUSE_STATE){ @@ -225,9 +227,7 @@ void loop() { } if(millis()-stateTimer > pauseTime){ //Finish the pressure averaging - displ.writePeakP(0); displ.writePlateauP(round(readPressure())); - displ.writePEEP(0); setState(EX_STATE); } @@ -244,7 +244,9 @@ void loop() { if(abs(motorPosition) < goalTol) roboclaw.ForwardM1(address,0); - if(millis()-stateTimer > Tex*1000) + if(millis()-stateTimer > Tex*1000){ + displ.writePEEP(round(readPressure())); setState(IN_STATE); + } } } From 2e4070dda79440ab25658c70371b7d7c2dda818a Mon Sep 17 00:00:00 2001 From: Amado Antonini Date: Wed, 25 Mar 2020 21:04:23 -0400 Subject: [PATCH 2/5] pressure peak finding --- Pressure.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ e-vent.ino | 38 ++++++++++++-------------------------- 2 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 Pressure.h diff --git a/Pressure.h b/Pressure.h new file mode 100644 index 0000000..7b79e09 --- /dev/null +++ b/Pressure.h @@ -0,0 +1,46 @@ +#ifndef Pressure_h +#define Pressure_h + +#include "Arduino.h" + +class Pressure { +public: + Pressure(int pin): sense_pin_(pin), offset_(0.0), peak_(0.0) {} + + void calibrate() { + offset_= read(); + } + + //Get pressure reading + float read() { + // read the voltage + int V = analogRead(sense_pin_); + + float Pmin = -100.0; // pressure max in mbar + float Pmax = 100.0; // pressure min in mbar + float Vmax = 1024; // max voltage in range from analogRead + float R = 32./37; // Internal 32K resistor and external 5K resistor ratio + // convert to pressure + float pres = (10 * V/Vmax * R - 1) * (Pmax-Pmin)/8. + Pmin; //mmHg + + // convert to cmH20 + pres *= 1.01972; + + float calibrated_pressure = pres - offset_; + peak_ = max(peak_, calibrated_pressure); + return calibrated_pressure; + } + + float get_peak_and_reset() { + float peak = peak_; + peak_ = 0.0; + return peak; + } + +private: + int sense_pin_; + float offset_; + float peak_; +}; + +#endif diff --git a/e-vent.ino b/e-vent.ino index 5291fb1..0994d48 100644 --- a/e-vent.ino +++ b/e-vent.ino @@ -1,6 +1,7 @@ #include #include #include "Display.h" +#include "Pressure.h" // Settings //////////// @@ -60,11 +61,13 @@ RoboClaw roboclaw(&serial,10000); int motorPosition = 0; // LCD Screen -double pressOffset = 0; const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); Display displ(&lcd); +// Pressure +Pressure pressure(PRESS_SENSE_PIN); + // Functions //////////// @@ -111,24 +114,6 @@ void readPots(){ } } -//Get pressure reading -float readPressure() { - // read the voltage - int V = analogRead(PRESS_SENSE_PIN); - - float Pmin = -100.0; // pressure max in mbar - float Pmax = 100.0; // pressure min in mbar - float Vmax = 1024; // max voltage in range from analogRead - float R = 32./37; // Internal 32K resistor and external 5K resistor ratio - // convert to pressure - float pres = (10 * V/Vmax * R - 1) * (Pmax-Pmin)/8. + Pmin; //mmHg - - //convert to cmH20 - pres *= 1.01972; - - return pres - pressOffset; -} - int readEncoder() { uint8_t robot_status; bool valid; @@ -166,7 +151,7 @@ void setup() { delay(1000); //Initialize - analogReference(EXTERNAL); // For the pressure reading + analogReference(EXTERNAL); // For the pressure and pots reading displ.begin(); setState(IN_STATE); // Initial state roboclaw.begin(38400); // Roboclaw @@ -175,7 +160,7 @@ void setup() { roboclaw.SetEncM1(address, 0); // Zero the encoder // Calibrate pressure sensor - pressOffset = readPressure(); + pressure.calibrate(); if(DEBUG){ // setup serial coms @@ -200,6 +185,9 @@ void loop() { // Update display header displ.writeHeader(); + // read pressure every cycle to keep track of peak + int current_pressure = round(pressure.read()); + if(state == DEBUG_STATE){ // Stop motor roboclaw.ForwardM1(address,0); @@ -214,7 +202,6 @@ void loop() { } if(millis()-stateTimer > Tin*1000 || abs(motorPosition - Volume) < goalTol){ - displ.writePeakP(round(readPressure())); setState(PAUSE_STATE); } } @@ -222,12 +209,10 @@ void loop() { else if(state == PAUSE_STATE){ // Entering if(enteringState){ - // Start the pressure averaging enteringState = false; } if(millis()-stateTimer > pauseTime){ - //Finish the pressure averaging - displ.writePlateauP(round(readPressure())); + displ.writePlateauP(current_pressure); setState(EX_STATE); } @@ -245,7 +230,8 @@ void loop() { roboclaw.ForwardM1(address,0); if(millis()-stateTimer > Tex*1000){ - displ.writePEEP(round(readPressure())); + displ.writePeakP(round(pressure.get_peak_and_reset())); + displ.writePEEP(current_pressure); setState(IN_STATE); } } From f0e6509cfa98be17883baa511a4c10b52649166e Mon Sep 17 00:00:00 2001 From: Amado Antonini Date: Wed, 25 Mar 2020 21:06:40 -0400 Subject: [PATCH 3/5] --amend --- Pressure.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Pressure.h b/Pressure.h index 7b79e09..ae295bc 100644 --- a/Pressure.h +++ b/Pressure.h @@ -27,7 +27,10 @@ class Pressure { pres *= 1.01972; float calibrated_pressure = pres - offset_; + + // update peak peak_ = max(peak_, calibrated_pressure); + return calibrated_pressure; } From c47b2c8e92cbe8025c0021ea48c30aac85f3c6b3 Mon Sep 17 00:00:00 2001 From: Teddy Ort Date: Thu, 26 Mar 2020 00:24:07 -0400 Subject: [PATCH 4/5] Restructured pressure --- Pressure.h | 26 ++++++++++++++++---------- e-vent.ino | 19 +++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Pressure.h b/Pressure.h index ae295bc..082a50e 100644 --- a/Pressure.h +++ b/Pressure.h @@ -5,14 +5,18 @@ class Pressure { public: - Pressure(int pin): sense_pin_(pin), offset_(0.0), peak_(0.0) {} + Pressure(int pin): sense_pin_(pin), peak_(0.0), current_(0.0) {} - void calibrate() { - offset_= read(); + void setPlateau() { + plateau_ = current_; + } + + float getPlateau() { + return plateau_; } //Get pressure reading - float read() { + void read() { // read the voltage int V = analogRead(sense_pin_); @@ -25,13 +29,15 @@ class Pressure { // convert to cmH20 pres *= 1.01972; - - float calibrated_pressure = pres - offset_; // update peak - peak_ = max(peak_, calibrated_pressure); + peak_ = max(peak_, pres); + + current_ = pres; + } - return calibrated_pressure; + float get() { + return current_; } float get_peak_and_reset() { @@ -42,8 +48,8 @@ class Pressure { private: int sense_pin_; - float offset_; - float peak_; + float peak_, plateau_, peep_; + float current_; }; #endif diff --git a/e-vent.ino b/e-vent.ino index 0994d48..a07a902 100644 --- a/e-vent.ino +++ b/e-vent.ino @@ -110,7 +110,10 @@ void readPots(){ Serial.print("\tVin:"); Serial.print(Vin); Serial.print("\tVex:"); - Serial.println(Vex); + Serial.print(Vex); + Serial.print("\tPressure:"); + Serial.print(pressure.get()); + Serial.println(); } } @@ -158,9 +161,6 @@ void setup() { roboclaw.SetM1MaxCurrent(address, 10000); // Current limit is 10A roboclaw.SetM1VelocityPID(address,Kd,Kp,Ki,qpps); // Set PID Coefficients roboclaw.SetEncM1(address, 0); // Zero the encoder - - // Calibrate pressure sensor - pressure.calibrate(); if(DEBUG){ // setup serial coms @@ -186,7 +186,7 @@ void loop() { displ.writeHeader(); // read pressure every cycle to keep track of peak - int current_pressure = round(pressure.read()); + pressure.read(); if(state == DEBUG_STATE){ // Stop motor @@ -212,7 +212,7 @@ void loop() { enteringState = false; } if(millis()-stateTimer > pauseTime){ - displ.writePlateauP(current_pressure); + pressure.setPlateau(); setState(EX_STATE); } @@ -230,8 +230,11 @@ void loop() { roboclaw.ForwardM1(address,0); if(millis()-stateTimer > Tex*1000){ - displ.writePeakP(round(pressure.get_peak_and_reset())); - displ.writePEEP(current_pressure); + float maxP = pressure.get_peak_and_reset(); + Serial.println(maxP); + displ.writePeakP(round(maxP)); + displ.writePEEP(pressure.get()); + displ.writePlateauP(pressure.getPlateau()); setState(IN_STATE); } } From c54c483ba3dff3cd3312659ce029017a0e5de63a Mon Sep 17 00:00:00 2001 From: Amado Antonini Date: Thu, 26 Mar 2020 01:04:24 -0400 Subject: [PATCH 5/5] clean display pressures --- Pressure.h | 40 +++++++++++++++++++++++++--------------- e-vent.ino | 11 +++++------ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Pressure.h b/Pressure.h index 082a50e..3afff6f 100644 --- a/Pressure.h +++ b/Pressure.h @@ -5,15 +5,13 @@ class Pressure { public: - Pressure(int pin): sense_pin_(pin), peak_(0.0), current_(0.0) {} - - void setPlateau() { - plateau_ = current_; - } - - float getPlateau() { - return plateau_; - } + Pressure(int pin): + sense_pin_(pin), + current_(0.0), + current_peak_(0.0), + peak_(0.0), + plateau_(0.0), + peep_(0.0) {} //Get pressure reading void read() { @@ -31,7 +29,7 @@ class Pressure { pres *= 1.01972; // update peak - peak_ = max(peak_, pres); + current_peak_ = max(current_peak_, pres); current_ = pres; } @@ -40,16 +38,28 @@ class Pressure { return current_; } - float get_peak_and_reset() { - float peak = peak_; - peak_ = 0.0; - return peak; + void set_peak_and_reset() { + peak_ = current_peak_; + current_peak_ = 0.0; + } + + void set_plateau() { + plateau_ = get(); } + void set_peep() { + peep_ = get(); + } + + int peak() { return round(peak_); } + int plateau() { return round(plateau_); } + int peep() { return round(peep_); } + private: int sense_pin_; - float peak_, plateau_, peep_; float current_; + float current_peak_; + float peak_, plateau_, peep_; }; #endif diff --git a/e-vent.ino b/e-vent.ino index a07a902..ab632d3 100644 --- a/e-vent.ino +++ b/e-vent.ino @@ -212,7 +212,7 @@ void loop() { enteringState = false; } if(millis()-stateTimer > pauseTime){ - pressure.setPlateau(); + pressure.set_plateau(); setState(EX_STATE); } @@ -230,11 +230,10 @@ void loop() { roboclaw.ForwardM1(address,0); if(millis()-stateTimer > Tex*1000){ - float maxP = pressure.get_peak_and_reset(); - Serial.println(maxP); - displ.writePeakP(round(maxP)); - displ.writePEEP(pressure.get()); - displ.writePlateauP(pressure.getPlateau()); + pressure.set_peak_and_reset(); + displ.writePeakP(pressure.peak()); + displ.writePEEP(pressure.peep()); + displ.writePlateauP(pressure.plateau()); setState(IN_STATE); } }