11 #include "../includes/vc_cmv_controller.h"
18 #include "../includes/main_controller.h"
19 #include "../includes/pressure_valve.h"
86 int32_t inspirationRemainingDurationMs =
91 if (inspirationRemainingDurationMs > 20) {
122 int32_t A1MultiplyBy100 = 3318;
123 int32_t rhoMultiplyBy100 = 120;
124 int32_t twoA1SquareDotDeltaPressureMultiplyBy100 =
125 100 * 2 * (A1MultiplyBy100 * A1MultiplyBy100 / 10000)
126 * (98 * (blowerPressure - patientPressure) / 10);
130 (divider == 0) ? 0 : (twoA1SquareDotDeltaPressureMultiplyBy100 / divider);
131 int32_t sectionToOpen;
135 int32_t divider2 = ((tempRatio + 100) < 0) ? 0 : sqrt(tempRatio + 100);
136 sectionToOpen = (divider2 == 0) ? 0 : (A1MultiplyBy100 * 10 / divider2);
166 int32_t inspirationDurationMs =
172 / inspirationDurationMs;
182 int32_t expiratoryValveAperture;
183 int32_t derivative = 0;
184 int32_t smoothError = 0;
185 int32_t totalValues = 0;
186 int32_t temporaryExpiratoryPidIntegral = 0;
187 int32_t proportionnalWeight;
188 int32_t derivativeWeight;
190 int32_t coefficientP;
191 int32_t coefficientI;
192 int32_t coefficientD;
233 proportionnalWeight = (coefficientP * error) / 1000;
234 derivativeWeight = (coefficientD * derivative / 1000);
236 / (maxAperture - minAperture)
237 - (proportionnalWeight + derivativeWeight);
244 expiratoryValveAperture = 0;
246 temporaryExpiratoryPidIntegral =
248 temporaryExpiratoryPidIntegral =
252 proportionnalWeight = ((coefficientP * error) / 1000);
253 int32_t integralWeight = temporaryExpiratoryPidIntegral;
254 derivativeWeight = coefficientD * derivative / 1000;
256 int32_t patientCommand = proportionnalWeight + integralWeight + derivativeWeight;
258 expiratoryValveAperture = max(
260 min(maxAperture, maxAperture + (maxAperture - minAperture) * patientCommand / 1000));
264 if ((expiratoryValveAperture != minAperture) && (expiratoryValveAperture != maxAperture)) {
271 return expiratoryValveAperture;
uint16_t getTargetSpeed() const
Get target speed value.
int32_t getBlowerPressure(int32_t p_flow)
Given a flow in mL/min, return an estimated pressure just at the output of the blower.
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 tidalVolumeCommand() const
int16_t pressure() const
Get the current measured pressure.
const int32_t targetInspiratoryFlowCommand() const
get target inspiratory flow in mL/min (used in VC modes)
int32_t currentDeliveredVolume() const
Get the measured Tidal Volume. Updated in real time.
int16_t peepCommand() const
Get the desired PEEP.
int16_t plateauPressureCommand() const
Get the desired plateau pressure.
int32_t inspiratoryFlow() const
Get the current inspiratoryFlow.
int16_t plateauDurationCommand() const
void ticksPerInhalationSet(uint32_t p_ticksPerInhalation)
Get the duration of an inhalation in ticks.
uint32_t ticksPerInhalation() const
Get the duration of an inhalation in ticks.
int32_t pressureCommand() const
Get the pressure command.
uint16_t minAperture() const
Minimum valve aperture angle in degrees.
void openSection(int32_t p_sectionMultiplyBy100)
Request opening of the Pressure Valve with a given section (in mm^2)
uint16_t maxAperture() const
Maximum valve aperture angle in degrees.
void close()
Request closing of the Pressure Valve.
uint16_t openLinear(uint16_t p_command)
Request opening of the Pressure Valve with a given angle with linearization.
Controller for the Volume Controled mode.
uint16_t m_blowerSpeed
Current blower speed.
int32_t m_blowerTicks
Blower ticks.
void endCycle() override
End the current breathing cycle.
int32_t m_inspiratoryFlowLastValuesIndex
Last flow index.
void calculateBlower()
Determine the blower speed to adopt for 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.
VC_CMV_Controller()
Default constructor.
void exhale() override
Control the exhalation.
int32_t m_inspiratoryPidIntegral
Integral gain of the inspiratory PID.
int32_t m_expiratoryPidLastErrorsIndex
Last error index in expiratory PID.
void initCycle() override
Begin a new breathing cycle.
int32_t m_maxInspiratoryFlow
Max flow during inspiration.
int32_t m_expiratoryPidLastErrors[PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN]
Last errors in expiratory PID.
int32_t m_expiratoryPidIntegral
Integral gain of the expiratory PID.
void setup() override
Initialize controller.
bool m_expiratoryPidFastMode
Fast mode at start of expiration.
int32_t m_inspiratoryFlowLastValues[NUMBER_OF_SAMPLE_LAST_VALUES]
Last flow values.
int32_t m_expiratoryPidLastError
Error of the last computation of the PID.
int32_t m_expiratoryValveLastAperture
Last aperture of the blower valve.
int32_t m_targetFlowMultiplyBy1000
int32_t m_inspiratoryValveLastAperture
Last aperture of the inspiratory valve.
void inhale() override
Control the inhalation.
MainController mainController
static const int32_t PID_PATIENT_INTEGRAL_MIN
#define NUMBER_OF_SAMPLE_LAST_VALUES
#define VALVE_RESPONSE_TIME_MS
#define PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN
#define DEFAULT_BLOWER_SPEED
#define MAIN_CONTROLLER_COMPUTE_PERIOD_MS
static const int32_t PID_PATIENT_INTEGRAL_MAX
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...
PressureValve inspiratoryValve
PressureValve expiratoryValve
VC_CMV_Controller vcCmvController