14 #include "../includes/alarm_controller.h"
15 #include "../includes/battery.h"
16 #include "../includes/blower.h"
17 #include "../includes/config.h"
18 #include "../includes/cycle.h"
19 #include "../includes/debug.h"
20 #include "../includes/parameters.h"
21 #include "../includes/pc_ac_controller.h"
22 #include "../includes/pc_cmv_controller.h"
23 #include "../includes/pc_vsai_controller.h"
24 #include "../includes/pressure_valve.h"
25 #include "../includes/telemetry.h"
26 #include "../includes/vc_ac_controller.h"
27 #include "../includes/vc_cmv_controller.h"
30 #define MAX_PRESSURE_SAMPLES 10u
105 uint16_t p_lowInspiratoryMinuteVolumeAlarmThreshold);
109 uint16_t p_highInspiratoryMinuteVolumeAlarmThreshold);
113 uint16_t p_lowExpiratoryMinuteVolumeAlarmThreshold);
117 uint16_t p_highExpiratoryMinuteVolumeAlarmThreshold);
369 inline int32_t
dt()
const {
return m_dt; }
417 void stop(uint32_t p_currentMillis);
Controls breathing cycle.
void onTargetInspiratoryFlow(uint16_t p_targetInspiratoryFlow)
Set the inspiratory flow target.
void sendStopMessageToUi()
Send a "stopped" telemetry message.
int16_t m_pressure
Measured pressure.
int32_t dt() const
Get the delta of time since the last cycle (in ms)
void onTriggerOffsetSet(uint16_t p_triggerOffset)
Set the desired offset for expiratory trigger.
void onInspiratoryDuration(uint16_t p_inspiratoryDuration)
Set the inspiration duration.
const bool triggered() const
Get the state of the inspiratory trigger.
void executeCommands()
Send the computed commands to actuators.
void updatePhase()
Update the cycle phase.
uint16_t m_PlateauMeasureCount
Count for calulating plateau value.
void onPeakPressureIncrease()
Increase the desired peak pressure.
int16_t m_tiMaxNextCommand
Max duration of inspiration in ms for next cycle.
uint32_t m_cycleNb
Number of elapsed cycles since beginning.
void updateFakeExpiratoryFlow()
Calculate expiratory flow from pressure and valve angle.
uint16_t m_expiratoryTermCommand
Actual expiratory term.
int16_t m_plateauPressureCommand
Actual desired plateau pressure.
int32_t m_highExpiratoryMinuteVolumeAlarmThresholdCommand
Threshold for high inspiratory minute volume alarm.
uint16_t tidalVolumeMeasure() const
Get the measured Tidal Volume. Updated only at the end of inspiration.
int16_t m_expiratoryTriggerFlowNextCommand
Desired expiratory trigger flow for next cycle (in percent of max flow)
int16_t m_peakPressureNextCommand
Peak pressure desired by the operator for the next cycle.
int16_t peepNextCommand() const
Get the desired PEEP for the next cycle.
void onCycleSet(uint16_t p_cpm)
Set the desired number of cycles per minute.
int16_t m_inspiratoryDurationNextCommand
Duration of inspiration.
VentilationController * m_ventilationControllerNextCommand
Ventilation controller for the next cycle.
uint32_t m_tick
Actual tick number (given by the main state machine)
int16_t m_tiMinCommand
Minimum duration of inspiration in ms.
void checkCycleAlarm()
At the end of a respiratory cycle, check if some alarms are triggered.
void onLowTidalVolumeAlarmThresholdSet(uint16_t p_lowTidalVolumeAlarmThreshold)
Set threshold on tidal volume below which an alarm is raised.
void onlowRespiratoryRateAlarmThresholdSet(uint16_t p_lowRespiratoryRateAlarmThreshold)
Set alarm threshold for low respiratory rate.
int32_t m_highInspiratoryMinuteVolumeAlarmThresholdNextCommand
Threshold for high inspiratory minute volume alarm for next cycle.
void onPeakPressureDecrease()
Decrease the desired peak pressure.
const int16_t tiMinCommand() const
Get the value of the minimum duration of inspiration in ms.
void onLeakAlarmThresholdSet(uint16_t p_leakAlarmThreshold)
Set the threshold for leak that raise the alarm.
void onCycleIncrease()
Increase the desired number of cycles per minute.
uint16_t m_cyclesPerMinuteNextCommand
Number of cycles per minute desired by the operator for the next cycle.
int16_t plateauPressureNextCommand() const
Get the desired plateau pressure for the next cycle.
const VentilationController * ventilationControllerNextCommand() const
Ventilation controller pointer for the next cycle.
int16_t * lastPressureValues()
Get last pressure values.
uint16_t cyclesPerMinuteMeasure() const
Get the desired number of cycles per minute.
void onExpiratoryTermSet(uint16_t p_expiratoryTerm)
Set the desired expiratory term.
uint16_t m_cyclesPerMinuteCommand
Actual desired number of cycles per minute.
int16_t m_lastPressureValues[MAX_PRESSURE_SAMPLES]
Last pressure values.
int16_t m_plateauDurationCommand
Plateau duration command (used in VC modes)
const int16_t tiMaxCommand() const
Get the value of the max duration of inspiration in ms.
const int16_t inspiratoryTriggerFlowCommand() const
Get the value of the inspiratory trigger flow command.
int32_t m_highRespiratoryRateAlarmThresholdNextCommand
Threshold for low respiratory rate for next cycle.
void onhighRespiratoryRateAlarmThresholdSet(uint16_t p_highRespiratoryRateAlarmThreshold)
Set alarm threshold for high respiratory rate.
int16_t m_tidalVolumeMeasure
Measured value of the Tidal volume (volume of air pushed in patient lungs in last inspiration)
int32_t patientHeight() const
int16_t peakPressureNextCommand() const
Get the desired max peak for the next cycle.
void onPatientComputePreset()
Updates patient computed params.
int16_t m_rebouncePeakPressureMeasure
Measured value of rebounce peak pressure.
int32_t m_leakAlarmThresholdCommand
Threshold for leak alarm.
int32_t m_lowExpiratoryMinuteVolumeAlarmThresholdNextCommand
Threshold for low inspiratory minute volume alarm for next cycle.
int32_t m_lowInspiratoryMinuteVolumeAlarmThresholdCommand
Threshold for low inspiratory minute volume alarm.
void endRespiratoryCycle(uint32_t p_currentMillis)
End a respiratory cycle.
void onTiMinSet(uint16_t p_tiMin)
Set min inspiratory time.
int32_t m_highTidalVolumeAlarmThresholdCommand
Threshold for high tidal Volume Alarm.
CyclePhases phase() const
Get the current cycle phase.
int32_t m_lowExpiratoryMinuteVolumeAlarmThresholdCommand
Threshold for low inspiratory minute volume alarm.
int16_t tidalVolumeNextCommand() const
Get the desired tidal Volume for the next cycle (used in VC modes)
uint32_t tick() const
Get the tick number of the current cycle.
uint16_t ticksPerCycle() const
Get the duration of a cycle in ticks.
const bool isPeepDetected()
Get if the PEEP has been detected during this cycle.
int32_t m_maxExpiratoryFlow
Measured max inspiratory flow.
int16_t m_plateauPressureMeasure
Measured value of the plateau pressure.
int16_t tidalVolumeCommand() const
uint16_t cyclesPerMinuteNextCommand() const
Get the desired number of cycles per minute for the next cycle.
int32_t m_highTidalVolumeAlarmThresholdNextCommand
Threshold for high tidal Volume Alarm next cycle.
int16_t pressure() const
Get the current measured pressure.
void onInspiratoryTriggerFlowSet(uint16_t p_inspiratoryTriggerFlow)
Set inspiratory trigger flow.
void onPlateauPressureIncrease()
Increase the desired plateau pressure.
void updateDt(int32_t p_dt)
Input the real duration since the last pressure controller computation.
void updateExpiratoryFlow(int32_t p_currentExpiratoryFlow)
Input an expiratory flow reading.
VentilationController * m_ventilationController
Ventilation controller in use (for everything related to breathing control)
void updateCurrentExpiratoryVolume(int32_t p_expiratoryVolume)
int16_t rebouncePeakPressureMeasure() const
Get the measured rebounce peak pressure.
int32_t expiratoryFlow() const
Get the current expiratoryFlow.
int32_t m_pressureCommand
Requested pressure at a given point in time.
uint16_t m_numberOfPressures
Number of the current cycle's pressures.
uint32_t m_plateauDurationMs
Duration of the plateau.
bool m_isPeepDetected
Is PEEP pressure detected in the cycle?
uint16_t m_ticksPerCycle
Number of hundredth of second per cycle.
void onCycleDecrease()
Decrease the desired number of cycles per minute.
const int32_t targetInspiratoryFlowCommand() const
get target inspiratory flow in mL/min (used in VC modes)
int16_t m_tidalVolumeCommand
Tidal volume command (used in VC modes)
void onTiMaxSet(uint16_t p_tiMax)
Set max inspiratory time.
int16_t m_tiMinNextCommand
Minimum duration of inspiration in ms for next cycle.
int16_t m_peakPressureAlarmThresholdNextCommand
Threshold for peak pressure alarmfor next cycle.
void onPeepPressureIncrease()
Increase the minimal PEEP desired.
const int16_t expiratoryTriggerFlowCommand() const
Get the value of the expiratory trigger flow command.
void updateCurrentDeliveredVolume(int32_t p_currentDeliveredVolume)
Input the current delivered volume in inspiratory branch since beginning of the respiratory cycle.
int16_t peakPressureMeasure() const
Get the measured peak pressure.
void onPeepSet(int16_t p_peep)
Set the desired PEEP.
const int16_t pressureTriggerOffsetCommand() const
Get the value of the inspiratory trigger pressure command.
int32_t currentDeliveredVolume() const
Get the measured Tidal Volume. Updated in real time.
void onPeepPressureDecrease()
Decrease the minimal PEEP desired.
uint32_t m_inspiratoryValveAngle
Blower valve angle at peak.
const int16_t pressureTriggerOffsetNextCommand() const
Get the value of the inspiratory trigger pressure command for the next cycle.
int16_t m_pressureTriggerOffsetNextCommand
Desired pressure trigger offset for the next cycle.
int16_t peepCommand() const
Get the desired PEEP.
uint32_t m_lastBreathPeriodsMsIndex
Index for the m_lastBreathPeriodsMs array.
void onPeakPressureAlarmThreshold(int16_t p_peakPressureAlarmThreshold)
Set the desired threshold for max peak pressure.
int32_t m_highExpiratoryMinuteVolumeAlarmThresholdNextCommand
Threshold for high inspiratory minute volume alarm for next cycle.
int16_t m_tiMaxCommand
Max duration of inspiration in ms.
uint32_t m_sumOfPressures
Sum of the current cycle's pressures.
void onLowExpiratoryMinuteVolumeAlarmThresholdSet(uint16_t p_lowExpiratoryMinuteVolumeAlarmThreshold)
Set alarm threshold for low expiratory minute volume.
int32_t m_lowRespiratoryRateAlarmThresholdCommand
Threshold for low respiratory rate.
int16_t m_inspiratoryTriggerFlowNextCommand
Desired inspiratory trigger flow for next cycle.
int32_t m_lowTidalVolumeAlarmThresholdCommand
Threshold for low tidal Volume Alarm.
void sendMachineState()
Send a "machine state snapshot" telemetry message.
int32_t m_highInspiratoryMinuteVolumeAlarmThresholdCommand
Threshold for high inspiratory minute volume alarm.
void updatePressure(int16_t p_currentPressure)
Input a pressure reading.
int16_t m_inhalationLastPressure
Last pressure of inhalation.
void computeTickParameters()
Compute various cycle durations given the desired number of cycles per minute.
void exhale()
Perform the pressure control and compute the actuators commands during the exhalation phase.
bool m_tidalVolumeAlreadyRead
True if Tidal volume has already been read during cycle.
const int16_t inspiratoryTriggerFlowNextCommand() const
Get the value of the inspiratory trigger flow command for the next cycle.
uint32_t m_ticksPerInhalation
Number of hundredth of second per inhalation.
int16_t peepMeasure() const
Get the measured PEEP.
int32_t m_expiratoryVolume
Volume expired by the patient during the exhalation phase.
int16_t m_peepMeasure
Measured value of the PEEP.
const int16_t tiMaxNextCommand() const
Get the value of the max duration of inspiration in ms for the next cycle.
void printDebugValues()
Print debug values, used to tweak PID, and triggers.
int32_t m_dt
Time since the last computation (in microsecond)
uint32_t m_cyclesPerMinuteMeasure
Measured number of cycles per minute.
bool m_triggered
Is inspiratory triggered or not?
void setup()
Initialize actuators.
void onPlateauPressureSet(int16_t p_plateauPressure)
Set the desired plateau pressure.
int16_t plateauPressureCommand() const
Get the desired plateau pressure.
int32_t m_currentDeliveredVolume
Current delivered volume by the blower.
const int16_t inspiratoryDurationCommand() const
Get duration of inspiration command.
CyclePhases m_phase
Current respiratory cycle phase.
int32_t m_leakAlarmThresholdNextCommand
Threshold for leak alarm for next cycle.
int16_t m_inspiratoryTriggerFlowCommand
Desired inspiratory trigger flow.
int16_t m_peakPressureMeasure
Measured value of peak pressure.
void calculateBlowerIncrement()
void initRespiratoryCycle()
Begin a respiratory cycle.
const int16_t inspiratoryDurationNextCommand() const
Get duration of inspiration command fo next cycle.
int32_t m_lowTidalVolumeAlarmThresholdNextCommand
Threshold for low tidal Volume Alarm next cycle.
int16_t m_pressureTriggerOffsetCommand
Actual pressure trigger offset.
int32_t m_targetInspiratoryFlowNextCommand
inspiratory flow required (used in VC modes) for next cycle
void onPatientHeight(int16_t p_patientHeight)
Set the desired patient height.
void onPatientGender(int16_t p_patientGender)
Set the desired patient gender.
int16_t m_plateauPressureNextCommand
Plateau pressure desired by the operator for the next cycle.
void onLowInspiratoryMinuteVolumeAlarmThresholdSet(uint16_t p_lowInspiratoryMinuteVolumeAlarmThreshold)
Set alarm threshold for low inspiratory minute volume.
int64_t m_PlateauMeasureSum
Sum for calulating plateau value.
const bool triggerModeEnabledCommand()
Get the enabling state of trigger mode.
void stop(uint32_t p_currentMillis)
Stop the breathing.
int32_t m_targetInspiratoryFlowCommand
inspiratory flow required (used in VC modes)
int16_t m_plateauPressureToDisplay
Measured value of the plateau pressure for display.
int32_t inspiratoryFlow() const
Get the current inspiratoryFlow.
void onPlateauPressureDecrease()
Decrease the desired plateau pressure.
const int16_t tiMinNextCommand() const
Get the value of the minimum duration of inspiration in ms for the next cycle.
VentilationController * m_ventilationControllersTable[NUMBER_OF_VENTILATION_MODES+1u]
Array containing pointers to different ventilation controllers.
uint16_t cyclesPerMinuteCommand() const
Get the desired number of cycles per minute.
int16_t m_peakPressureCommand
Actual desired peak pressure.
void onHighInspiratoryMinuteVolumeAlarmThresholdSet(uint16_t p_highInspiratoryMinuteVolumeAlarmThreshold)
Set alarm threshold for high inspiratory minute volume.
int16_t plateauDurationCommand() const
void onVentilationModeSet(uint16_t p_ventilationControllerMode)
Set ventilation mode.
void reachSafetyPosition()
Put actuators in safety position.
void onTargetTidalVolumeSet(uint16_t p_targetTidalVolume)
Set target tidal volume (used in VC modes)
void ticksPerInhalationSet(uint32_t p_ticksPerInhalation)
Get the duration of an inhalation in ticks.
void onHighExpiratoryMinuteVolumeAlarmThresholdSet(uint16_t p_highExpiratoryMinuteVolumeAlarmThreshold)
Set alarm threshold for high expiratory minute volume.
void compute()
Perform the pressure control.
int16_t m_expiratoryTriggerFlowCommand
Desired expiratory trigger flow (in percent of max flow)
const bool triggerModeEnabledNextCommand()
Get the enabling state of trigger mode for the next cycle.
int16_t plateauDurationNextCommand() const
uint32_t ticksPerInhalation() const
Get the duration of an inhalation in ticks.
void inhale()
Perform the pressure control and compute the actuators commands during the inhalation phase.
int32_t m_highRespiratoryRateAlarmThresholdCommand
Threshold for low respiratory rate.
int16_t m_peepCommand
Actual desired PEEP.
int16_t m_inspiratoryDurationCommand
Duration of inspiration.
uint32_t m_lastBreathPeriodsMs[NUMBER_OF_BREATH_PERIOD]
Period durations of last beathings.
int32_t pressureCommand() const
Get the pressure command.
int16_t m_plateauDurationNextCommand
Plateau duration command for next cycle.
uint32_t m_lastEndOfRespirationDateMs
Date of the last ending of a respiration.
int32_t m_lowInspiratoryMinuteVolumeAlarmThresholdNextCommand
Threshold for low inspiratory minute volume alarm for next cycle.
void onPlateauDurationSet(uint16_t p_plateauDuration)
Set the duration of Pause at the end of expiration in VC modes.
uint16_t lastPressureValuesIndex()
Get last pressure values index.
uint16_t m_lastPressureValuesIndex
Last pressure index.
const int16_t expiratoryTriggerFlowNextCommand() const
Get the value of the expiratory trigger flow command for the next cycle.
int16_t plateauPressureMeasure() const
Get the measured plateau pressure.
int16_t m_tidalVolumeNextCommand
Tidal volume command for next cycle.
int32_t m_expiratoryFlow
Measured inspiratory flow.
int16_t m_peepNextCommand
Desired PEEP for the next cycle.
const void setTrigger(bool triggerValue)
Reset the trigger to false.
int16_t peakPressureCommand() const
Get the desired max peak.
void updateTick(uint32_t p_tick)
Input a tick number.
int32_t m_maxInspiratoryFlow
Measured max inspiratory flow.
void updateInspiratoryFlow(int32_t p_currentInspiratoryFlow)
Input an inspiratory flow reading.
int32_t m_inspiratoryFlow
Measured inspiratory flow.
bool m_triggerModeEnabledNextCommand
Desired state of enabling of trigger mode for the next cycle.
VentilationModes m_ventilationControllerMode
uint16_t m_expiratoryTermNextCommand
Desired expiratory term for the next cycle.
void onExpiratoryTriggerFlowSet(uint16_t p_expiratoryTriggerFlow)
Set expiratory trigger flow.
bool m_triggerModeEnabledCommand
Actual state of enabling of trigger mode.
const int32_t targetInspiratoryFlowNextCommand() const
get target inspiratory flow in mL/min (used in VC modes) for next cycle
void onTriggerModeEnabledSet(uint16_t p_triggerEnabled)
Enable or disable expiratory trigger mode.
void onHighTidalVolumeAlarmThresholdSet(uint16_t p_highTidalVolumeAlarmThreshold)
Set threshold on tidal volume for which an alarm is raised.
MainController()
Default constructor.
int32_t m_lowRespiratoryRateAlarmThresholdNextCommand
Threshold for low respiratory rate for next cycle.
uint32_t cycleNumber() const
Get the number of past cycles since the beginning.
int16_t m_peakPressureAlarmThresholdCommand
Threshold for peak pressure alarm.
int32_t m_lastMaxExpiratoryFlow
Measured max inspiratory flow.
Abstract class for ventilation controllers.
VentilationModes
Supported ventilation modes.
#define NUMBER_OF_VENTILATION_MODES
CyclePhases
Defines the 2 main phases of the respiratory cycle.
#define MAX_PRESSURE_SAMPLES
Number of values to aggregate when computing plateau pressure.
MainController mainController
#define NUMBER_OF_BREATH_PERIOD
Number of periods used for calculating the respiratory rate.