11 #include "../includes/pc_cmv_controller.h"
19 #include "../includes/main_controller.h"
20 #include "../includes/pressure_valve.h"
126 int32_t halfRampNumberfTick =
136 || (peakDelta > 25)) {
138 DBG_DO(Serial.println(
"BLOWER -100");)
141 DBG_DO(Serial.println(
"BLOWER 0");)
144 DBG_DO(Serial.println(
"BLOWER +0");)
148 DBG_DO(Serial.println(
"BLOWER +25"));
151 DBG_DO(Serial.println(
"BLOWER +50"));
154 DBG_DO(Serial.println(
"BLOWER +100"));
158 DBG_DO(Serial.print(
"Plateau Start time:");)
166 int32_t inspiratoryValveAperture;
167 int32_t derivative = 0;
168 int32_t smoothError = 0;
169 int32_t totalValues = 0;
170 int32_t proportionnalWeight;
171 int32_t derivativeWeight;
173 int32_t coefficientP;
174 int32_t coefficientI;
175 int32_t coefficientD;
177 int32_t temporarym_inspiratoryPidIntegral = 0;
180 int32_t error = targetPressure - currentPressure;
210 proportionnalWeight = (coefficientP * error) / 1000;
211 derivativeWeight = (coefficientD * derivative / 1000);
214 / (minAperture - maxAperture)
215 - (proportionnalWeight + derivativeWeight);
226 inspiratoryValveAperture =
229 inspiratoryValveAperture = 0;
234 temporarym_inspiratoryPidIntegral =
236 temporarym_inspiratoryPidIntegral =
240 proportionnalWeight = ((coefficientP * error) / 1000);
241 int32_t integralWeight = temporarym_inspiratoryPidIntegral;
242 derivativeWeight = coefficientD * derivative / 1000;
244 int32_t blowerCommand = proportionnalWeight + integralWeight + derivativeWeight;
245 inspiratoryValveAperture =
247 min(maxAperture, maxAperture + (minAperture - maxAperture) * blowerCommand / 1000));
251 if ((inspiratoryValveAperture != minAperture) && (inspiratoryValveAperture != maxAperture)) {
258 return inspiratoryValveAperture;
265 int32_t expiratoryValveAperture;
266 int32_t derivative = 0;
267 int32_t smoothError = 0;
268 int32_t totalValues = 0;
269 int32_t temporarym_expiratoryPidIntegral = 0;
270 int32_t proportionnalWeight;
271 int32_t derivativeWeight;
273 int32_t coefficientP;
274 int32_t coefficientI;
275 int32_t coefficientD;
317 proportionnalWeight = (coefficientP * error) / 1000;
318 derivativeWeight = (coefficientD * derivative / 1000);
320 / (maxAperture - minAperture)
321 - (proportionnalWeight + derivativeWeight);
332 expiratoryValveAperture =
335 expiratoryValveAperture = 0;
338 temporarym_expiratoryPidIntegral =
340 temporarym_expiratoryPidIntegral =
344 proportionnalWeight = ((coefficientP * error) / 1000);
345 int32_t integralWeight = temporarym_expiratoryPidIntegral;
346 derivativeWeight = coefficientD * derivative / 1000;
348 int32_t patientCommand = proportionnalWeight + integralWeight + derivativeWeight;
350 expiratoryValveAperture = max(
352 min(maxAperture, maxAperture + (maxAperture - minAperture) * patientCommand / 1000));
356 if ((expiratoryValveAperture != minAperture) && (expiratoryValveAperture != maxAperture)) {
363 return expiratoryValveAperture;
uint16_t getTargetSpeed() const
Get target speed value.
void runSpeedWithRampUp(uint16_t p_targetSpeed)
Run the blower to a given speed applying a ramp-up to prevent high current drain.
int32_t dt() const
Get the delta of time since the last cycle (in ms)
uint32_t tick() const
Get the tick number of the current cycle.
int16_t pressure() const
Get the current measured pressure.
int16_t peakPressureMeasure() const
Get the measured peak pressure.
int16_t peepCommand() const
Get the desired PEEP.
int16_t plateauPressureCommand() const
Get the desired plateau pressure.
uint32_t ticksPerInhalation() const
Get the duration of an inhalation in ticks.
int32_t pressureCommand() const
Get the pressure command.
Controller for the CMV mode.
int32_t m_expiratoryPidLastError
Error of the last computation of the expiratory PID.
int32_t m_inspiratoryPidLastError
Error of the last computation of the expiratory PID.
uint16_t m_blowerSpeed
Current blower speed.
bool m_expiratoryPidFastMode
Fast mode at start of expiration.
int32_t m_inspiratoryPidIntegral
Error of the last computation of the blower PID.
PC_CMV_Controller()
Default constructor.
int32_t m_expiratoryValveLastAperture
Last aperture of the blower valve.
void initCycle() override
Begin a new breathing cycle.
int32_t m_expiratoryPidIntegral
Integral gain of the patient PID.
int32_t m_inspiratoryPidLastErrorsIndex
Last error index in inspiratory PID.
void inhale() override
Control the inhalation.
int32_t m_inspiratoryPidLastErrors[PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN]
Last errors in inspiratory PID.
bool m_inspiratoryPidFastMode
Fast mode at start of inspiration.
void setup() override
Initialize controller.
int32_t m_expiratoryPidLastErrors[PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN]
Last errors in expiratory PID.
uint16_t m_plateauStartTime
Number of ticks when plateau is reached for the first time.
void exhale() override
Control the exhalation.
int32_t m_blowerIncrement
Current blower speed increment (to apply at the beginning of the next cycle)
int32_t PCexpiratoryPID(int32_t targetPressure, int32_t currentPressure, int32_t dt)
PID to control the patient valve during some specific steps of the cycle.
int32_t m_expiratoryPidLastErrorsIndex
Last error index in inspiratory PID.
bool m_plateauPressureReached
True if plateau pressure has been reached (but not necessarily converged)
int32_t PCinspiratoryPID(int32_t targetPressure, int32_t currentPressure, int32_t dt)
PID to control the blower valve during some specific steps of the cycle.
void endCycle() override
End the current breathing cycle.
void calculateBlowerIncrement()
Determine the blower speed to adopt for next cycle.
int32_t m_inspiratoryValveLastAperture
Last aperture of the blower valve.
uint16_t minAperture() const
Minimum valve aperture angle in degrees.
void open()
Request opening of the Pressure Valve.
uint16_t maxAperture() const
Maximum valve aperture angle in degrees.
void close()
Request closing of the Pressure Valve.
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
MainController mainController
static const int32_t PID_PATIENT_INTEGRAL_MIN
#define PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN
static const int32_t PID_BLOWER_INTEGRAL_MAX
#define DEFAULT_BLOWER_SPEED
static const int32_t PID_PATIENT_INTEGRAL_MAX
#define MAIN_CONTROLLER_COMPUTE_PERIOD_MICROSECONDS
static const int32_t PID_BLOWER_INTEGRAL_MIN
static const int32_t PID_PATIENT_SAFETY_PEEP_OFFSET
Increase target pressure by an offset (in mmH2O) for safety, to avoid going below the target pressure...
PC_CMV_Controller pcCmvController
PressureValve inspiratoryValve
PressureValve expiratoryValve