software:firmware
MakAir Firmware
main_state_machine.cpp File Reference

Auto test for end of line unit test. More...

Go to the source code of this file.

Enumerations

enum  Step {
  SETUP , STOPPED , INIT_CYCLE , BREATH ,
  TRIGGER_RAISED , END_CYCLE
}
 

Functions

void millisecondTimerMSM (HardwareTimer *)
 

Variables

MainStateMachine mainStateMachine = MainStateMachine()
 
uint32_t clockMsmTimer = 0
 
uint32_t lastMillis = 0
 
uint32_t lastMainControllerCall = 0
 
HardwareTimer * msmTimer
 
uint32_t lastMicro = 0
 
uint32_t tick = 0
 
Step msmstep = SETUP
 
Step previousmsmstep = SETUP
 

Detailed Description

Auto test for end of line unit test.

Author
Makers For Life

Definition in file main_state_machine.cpp.

Enumeration Type Documentation

◆ Step

enum Step
Enumerator
SETUP 
STOPPED 
INIT_CYCLE 
BREATH 
TRIGGER_RAISED 
END_CYCLE 

Definition at line 42 of file main_state_machine.cpp.

Function Documentation

◆ millisecondTimerMSM()

void millisecondTimerMSM ( HardwareTimer *  )

Definition at line 68 of file main_state_machine.cpp.

72 {
73  IWatchdog.reload();
74  clockMsmTimer++;
75  int32_t pressure = inspiratoryPressureSensor.read();
77 
78  if ((clockMsmTimer % 10u) == 0u) {
79  // Check if some buttons have been pushed
80  keyboardLoop();
81  // Check if battery state has changed
83  // Check serial input
85 
87  // Delay will trigger the watchdog and the machine will restart with a message
88  // on screen
89  delay(10000);
90  }
92  }
93 
94  // Because this kind of LCD screen is not reliable, we need to reset it every 5 min or
95  // so
96  if ((clockMsmTimer % 300000u) == 0u) {
97  DBG_DO(Serial.println("resetting LCD screen");)
98  resetScreen();
100  }
101 
102  // Refresh screen every 300 ms, no more
103  if ((clockMsmTimer % 300u) == 0u) {
105  }
106 
107  // Check that the UI software on the Raspberry PI has sent a heartbeat in the last 60s
108  // Otherwise restart the power
109  if ((clockMsmTimer % 1000u) == 0u) {
111  }
112 
113  if (msmstep == SETUP) {
117  msmstep = STOPPED;
118  } else if (msmstep == STOPPED) {
119  // Executed just after booting, until the first start
120  if ((clockMsmTimer % 100u) == 0u) {
121  mainController.stop(millis());
123  }
124 
125  if ((clockMsmTimer % 10u) == 0u) {
126  tick++; // Also increase ticks during stop, for alarm controller
127  }
128 
132  // set patient height to default value
133  if (mainController.patientHeight() == 0) {
135  }
136  }
137 
138  } else if (msmstep == INIT_CYCLE) {
140  lastMillis = millis();
141  lastMainControllerCall = millis();
142  tick = 0;
143  msmstep = BREATH;
144 #ifdef MASS_FLOW_METER_ENABLED
145  (void)MFM_read_milliliters(true); // Reset volume integral
146 #endif
147 #ifdef MASS_FLOW_METER_ENABLED&& MASS_FLOW_METER_SENSOR_EXPI
148  (void)MFM_expi_read_milliliters(true); // Reset volume integral
149 #endif
150 
151  } else if (msmstep == BREATH) {
152  // If breathing
153  uint32_t currentMillis = millis();
154  tick = (currentMillis - lastMillis) / MAIN_CONTROLLER_COMPUTE_PERIOD_MS;
155 
157  if (tick >= mainController.ticksPerCycle()) {
158  msmstep = END_CYCLE;
159  } else {
160  uint32_t currentMicro = micros();
161  int32_t inspiratoryflow = 0;
162  int32_t expiratoryflow = 0;
163 #ifdef MASS_FLOW_METER_ENABLED
164  inspiratoryflow = MFM_read_airflow();
166 #endif
167  mainController.updateInspiratoryFlow(inspiratoryflow);
168 
169 #ifdef MASS_FLOW_METER_ENABLED&& MASS_FLOW_METER_SENSOR_EXPI
170  expiratoryflow = MFM_expi_read_airflow();
171  mainController.updateExpiratoryFlow(expiratoryflow);
173 #else
175 #endif
176  mainController.updateDt(currentMicro - lastMicro);
177  lastMicro = currentMicro;
180  lastMainControllerCall = currentMillis;
181  tick++;
182  }
183  }
184 
185  if (mainController.triggered()) {
187  }
188 
189  // Check if machine has been paused
192  msmstep = SETUP;
193  }
194  } else if (msmstep == TRIGGER_RAISED) {
196  msmstep = END_CYCLE;
197  } else {
198  msmstep = SETUP;
199  }
200  } else if (msmstep == END_CYCLE) {
207  } else {
208  msmstep = SETUP;
209  }
210  } else {
211  // Do nothing
212  }
213 
215 }
ActivationController activationController
Instance of the activation controller.
Definition: activation.cpp:21
AlarmController alarmController
Instance of the alarm controller.
bool isBatteryDeepDischarged()
Check if battery is deeply discharged.
Definition: battery.cpp:136
void batteryLoop(uint32_t p_cycleNumber)
Handle battery events.
Definition: battery.cpp:117
bool isRunning() const
Return if breathing is activated or not.
Definition: activation.h:32
void refreshState()
Refresh the current state.
Definition: activation.cpp:49
void runAlarmEffects(uint32_t p_tick)
Run effects (buzzer, LCD message, LED) according to the currently triggered alarms.
const bool triggered() const
Get the state of the inspiratory trigger.
void updateFakeExpiratoryFlow()
Calculate expiratory flow from pressure and valve angle.
int32_t patientHeight() const
void endRespiratoryCycle(uint32_t p_currentMillis)
End a respiratory cycle.
uint16_t ticksPerCycle() const
Get the duration of a cycle in ticks.
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.
void updateCurrentExpiratoryVolume(int32_t p_expiratoryVolume)
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 updatePressure(int16_t p_currentPressure)
Input a pressure reading.
int16_t peepMeasure() const
Get the measured PEEP.
void setup()
Initialize actuators.
void initRespiratoryCycle()
Begin a respiratory cycle.
void onPatientHeight(int16_t p_patientHeight)
Set the desired patient height.
void stop(uint32_t p_currentMillis)
Stop the breathing.
void compute()
Perform the pressure control.
int16_t plateauPressureMeasure() const
Get the measured plateau pressure.
void updateTick(uint32_t p_tick)
Input a tick number.
void updateInspiratoryFlow(int32_t p_currentInspiratoryFlow)
Input an inspiratory flow reading.
uint32_t cycleNumber() const
Get the number of past cycles since the beginning.
void ScreenUpdate()
Display information on screen.
int32_t read()
Read the current pressure for the feedback control.
Definition: pressure.cpp:29
void update()
This should be called by the main state machine every 1s.
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
Definition: debug.h:24
void keyboardLoop()
Handle button events.
Definition: keyboard.cpp:223
MainController mainController
Step msmstep
uint32_t clockMsmTimer
uint32_t lastMicro
MainStateMachine mainStateMachine
uint32_t tick
Step previousmsmstep
uint32_t lastMillis
uint32_t lastMainControllerCall
int32_t MFM_read_airflow(void)
Read instant air flow.
int32_t MFM_expi_read_airflow(void)
Read instant air flow.
int32_t MFM_read_milliliters(bool reset_after_read)
Get the number of milliliters since last reset.
int32_t MFM_expi_read_milliliters(bool reset_after_read)
Get the number of milliliters since last reset for expiratory sensor.
#define MAIN_CONTROLLER_COMPUTE_PERIOD_MS
Definition: parameters.h:28
#define DEFAULT_PATIENT_HEIGHT
Definition: parameters.h:111
PressureSensor inspiratoryPressureSensor
Definition: pressure.cpp:23
RpiWatchdog rpiWatchdog
void displayMachineStopped()
Display a message when the machine is stopped.
Definition: screen.cpp:270
void clearAlarmDisplayCache()
Force clear the alarm display cache.
Definition: screen.cpp:144
void resetScreen()
Erase everything that is on the screen.
Definition: screen.cpp:50
void displayCurrentInformation(uint16_t peakPressure, uint16_t plateauPressure, uint16_t peep)
Display relevant values from the ongoing cycle.
Definition: screen.cpp:91
void serialControlLoop()
Parse input and handle changes of settings.

Variable Documentation

◆ clockMsmTimer

uint32_t clockMsmTimer = 0

Definition at line 34 of file main_state_machine.cpp.

◆ lastMainControllerCall

uint32_t lastMainControllerCall = 0

Definition at line 36 of file main_state_machine.cpp.

◆ lastMicro

uint32_t lastMicro = 0

Definition at line 38 of file main_state_machine.cpp.

◆ lastMillis

uint32_t lastMillis = 0

Definition at line 35 of file main_state_machine.cpp.

◆ mainStateMachine

MainStateMachine mainStateMachine = MainStateMachine()

Definition at line 32 of file main_state_machine.cpp.

◆ msmstep

Step msmstep = SETUP

Definition at line 44 of file main_state_machine.cpp.

◆ msmTimer

HardwareTimer* msmTimer

Definition at line 37 of file main_state_machine.cpp.

◆ previousmsmstep

Step previousmsmstep = SETUP

Definition at line 45 of file main_state_machine.cpp.

◆ tick

uint32_t tick = 0

Definition at line 39 of file main_state_machine.cpp.