11 #include "../includes/pc_vsai_controller.h"
18 #include "../includes/main_controller.h"
19 #include "../includes/pressure_valve.h"
124 int32_t tiMinInTick =
132 >
static_cast<int64_t
>(tiMinInTick))) {
175 int16_t rebouncePeakDelta =
177 DBG_DO(Serial.print(
"peakPressure:");)
179 DBG_DO(Serial.print(
"plateauPressureCommand:");)
185 bool veryHighRebounce = (peakDelta > 60) || ((rebouncePeakDelta < -60) && (peakDelta >= 0));
186 bool highRebounce = (peakDelta > 40) || ((rebouncePeakDelta < -40) && (peakDelta >= 0));
187 bool lowRebounce = (peakDelta > 20) || ((rebouncePeakDelta < -15) && (peakDelta >= 0));
188 bool veryLowRebounce = (peakDelta > 10) || ((rebouncePeakDelta < -10) && (peakDelta >= 0));
193 if (veryHighRebounce) {
195 }
else if (highRebounce) {
213 }
else if (!lowRebounce) {
220 DBG_DO(Serial.print(
"BLOWER"));
222 DBG_DO(Serial.print(
"m_inspiratorySlope:");)
230 int32_t inspiratoryValveAperture;
231 int32_t derivative = 0;
232 int32_t smoothError = 0;
233 int32_t totalValues = 0;
234 int32_t proportionnalWeight;
235 int32_t derivativeWeight;
237 int32_t coefficientP;
238 int32_t coefficientI;
239 int32_t coefficientD;
241 int32_t temporarym_inspiratoryPidIntegral = 0;
244 int32_t error = targetPressure - currentPressure;
274 proportionnalWeight = (coefficientP * error) / 1000;
275 derivativeWeight = (coefficientD * derivative / 1000);
278 / (minAperture - maxAperture)
279 - (proportionnalWeight + derivativeWeight);
290 inspiratoryValveAperture =
295 inspiratoryValveAperture = 0;
300 temporarym_inspiratoryPidIntegral =
302 temporarym_inspiratoryPidIntegral =
306 proportionnalWeight = ((coefficientP * error) / 1000);
307 int32_t integralWeight = temporarym_inspiratoryPidIntegral;
308 derivativeWeight = coefficientD * derivative / 1000;
310 int32_t blowerCommand = proportionnalWeight + integralWeight + derivativeWeight;
311 inspiratoryValveAperture =
313 min(maxAperture, maxAperture + (minAperture - maxAperture) * blowerCommand / 1000));
317 if ((inspiratoryValveAperture != minAperture) && (inspiratoryValveAperture != maxAperture)) {
324 return inspiratoryValveAperture;
331 int32_t expiratoryValveAperture;
332 int32_t derivative = 0;
333 int32_t smoothError = 0;
334 int32_t totalValues = 0;
335 int32_t temporarym_expiratoryPidIntegral = 0;
336 int32_t proportionnalWeight;
337 int32_t derivativeWeight;
339 int32_t coefficientP;
340 int32_t coefficientI;
341 int32_t coefficientD;
382 proportionnalWeight = (coefficientP * error) / 1000;
383 derivativeWeight = (coefficientD * derivative / 1000);
385 / (maxAperture - minAperture)
386 - (proportionnalWeight + derivativeWeight);
393 expiratoryValveAperture = 0;
395 temporarym_expiratoryPidIntegral =
397 temporarym_expiratoryPidIntegral =
401 proportionnalWeight = ((coefficientP * error) / 1000);
402 int32_t integralWeight = temporarym_expiratoryPidIntegral;
403 derivativeWeight = coefficientD * derivative / 1000;
405 int32_t patientCommand = proportionnalWeight + integralWeight + derivativeWeight;
407 expiratoryValveAperture = max(
409 min(maxAperture, maxAperture + (maxAperture - minAperture) * patientCommand / 1000));
413 if ((expiratoryValveAperture != minAperture) && (expiratoryValveAperture != maxAperture)) {
420 return expiratoryValveAperture;
uint16_t getSpeed() const
Get speed value.
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)
const int16_t tiMinCommand() const
Get the value of the minimum duration of inspiration in ms.
int16_t * lastPressureValues()
Get last pressure values.
uint32_t tick() const
Get the tick number of the current cycle.
int16_t pressure() const
Get the current measured pressure.
int16_t rebouncePeakPressureMeasure() const
Get the measured rebounce peak pressure.
const int16_t expiratoryTriggerFlowCommand() const
Get the value of the expiratory trigger flow command.
int16_t peakPressureMeasure() const
Get the measured peak pressure.
const int16_t pressureTriggerOffsetCommand() const
Get the value of the inspiratory trigger pressure command.
int16_t peepCommand() const
Get the desired PEEP.
int16_t peepMeasure() const
Get the measured PEEP.
int16_t plateauPressureCommand() const
Get the desired plateau pressure.
int32_t inspiratoryFlow() const
Get the current inspiratoryFlow.
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.
const void setTrigger(bool triggerValue)
Reset the trigger to false.
Controller for the VSAI mode.
int32_t m_expiratoryPidLastErrors[PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN]
Last errors in expiratory PID.
bool m_plateauPressureReached
True if plateau pressure has been reached (but not necessarily converged)
void setup() override
Initialize controller.
bool m_reOpenInspiratoryValve
True if we want to reopen the inspiratory valve to create a circulation flow able to detect inspirato...
int32_t m_maxInspiratoryFlow
Max flow during inspiration.
int32_t m_inspiratoryPidLastError
Error of the last computation of the blower PID.
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.
uint16_t m_blowerSpeed
Current blower speed.
PC_VSAI_Controller()
Default constructor.
void endCycle() override
End the current breathing cycle.
uint16_t m_triggerWindow
Number of ticks from which it is possible to trigger a new inspiration.
void inhale() override
Control the inhalation.
int32_t m_expiratoryPidIntegral
Integral gain of the patient PID.
int32_t m_expiratoryPidLastErrorsIndex
Last error index in expiratory PID.
int32_t m_blowerIncrement
Current blower speed increment (to apply at the beginning of the next cycle)
int32_t m_inspiratoryPidIntegral
Error of the last computation of the blower PID.
int32_t m_inspiratoryPidLastErrorsIndex
Last error index in inspiratory PID.
int32_t m_inspiratoryPidLastErrors[PC_NUMBER_OF_SAMPLE_DERIVATIVE_MOVING_MEAN]
Last errors in inspiratory PID.
void calculateBlowerIncrement()
Determine the blower speed to adopt for next cycle.
bool m_inspiratoryPidFastMode
Fast mode at start of inspiration.
bool m_expiratoryPidFastMode
Fast mode at start of expiration.
void initCycle() override
Begin a new breathing cycle.
int32_t m_inspiratorySlope
Slope of the inspiration open loop (in mmH2O/s)
int32_t m_inspiratoryValveLastAperture
Last aperture of the blower valve.
int32_t m_expiratoryValveLastAperture
Last aperture of the blower valve.
int32_t m_expiratoryPidLastError
Error of the last computation of the PID.
void exhale() override
Control the exhalation.
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.
uint16_t minAperture() const
Minimum valve aperture angle in degrees.
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.
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
MainController mainController
#define MAX_PRESSURE_SAMPLES
Number of values to aggregate when computing plateau pressure.
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
#define MAIN_CONTROLLER_COMPUTE_PERIOD_MS
#define CONST_MIN_PEAK_PRESSURE
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_VSAI_Controller pcVsaiController
PressureValve inspiratoryValve
PressureValve expiratoryValve