software:firmware
MakAir Firmware
calibration.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "../includes/config.h"
11 
12 // INCLUDES ==================================================================
13 
14 // Internal
15 #include "../includes/blower.h"
16 #include "../includes/buzzer.h"
17 #include "../includes/calibration.h"
18 #include "../includes/keyboard.h"
19 #include "../includes/mass_flow_meter.h"
20 #include "../includes/pressure.h"
21 #include "../includes/pressure_valve.h"
22 #include "../includes/screen.h"
23 #include "../includes/telemetry.h"
24 
25 // External
26 #include "Arduino.h"
27 
28 // PROGRAM =====================================================================
29 
32 int32_t minOffsetValue = 0;
33 int32_t maxOffsetValue = 0;
34 uint32_t restartWaitTimer = 0;
35 bool startButtonPressed = false;
36 bool calibationStarted = false;
37 bool calibrationValid = false;
38 #define RESTART_READ_KEYBOARD_DELAY 100
39 
41  // Restart calibration process when invalid
42  while (calibrationValid == false) {
43  delay(1000);
44  resetScreen();
45  calibationStarted = true;
46  // RCM-SW-17 (Christmas tree at startup)
48  digitalWrite(PIN_LED_START, LED_START_ACTIVE);
49  digitalWrite(PIN_LED_GREEN, LED_GREEN_ACTIVE);
50  digitalWrite(PIN_LED_RED, LED_RED_ACTIVE);
51  digitalWrite(PIN_LED_YELLOW, LED_YELLOW_ACTIVE);
53  digitalWrite(PIN_LED_START, LED_START_INACTIVE);
54  digitalWrite(PIN_LED_GREEN, LED_GREEN_INACTIVE);
55  digitalWrite(PIN_LED_RED, LED_RED_INACTIVE);
56  digitalWrite(PIN_LED_YELLOW, LED_YELLOW_INACTIVE);
58 
61 
62  int32_t inspiratoryPressureSensorOffset = 0;
63  resetScreen();
64  if (pressureOffsetCount != 0u) {
65  inspiratoryPressureSensorOffset =
66  pressureOffsetSum / static_cast<int32_t>(pressureOffsetCount);
67  } else {
68  inspiratoryPressureSensorOffset = 0;
69  }
70 
71  // Happens when patient is plugged at startup
72  if (((maxOffsetValue - minOffsetValue) >= 10)
73  || (inspiratoryPressureSensorOffset >= MAX_PRESSURE_OFFSET)) {
74  // Invalid calibration
75  calibrationValid = false;
78 
79  // Wait for user to press start to restart calibration
80  restartWaitTimer = 0;
81 
82  while (startButtonPressed == false) {
84 
85  // Heartbeat fatal error periodically
86  if ((restartWaitTimer % 1000u) == 0u) {
88  }
89 
91  }
92  } else {
93  calibrationValid = true;
94  inspiratoryPressureSensor.setPressureSensorOffset(inspiratoryPressureSensorOffset);
95  }
96 
97  if (calibrationValid) {
98 #ifdef MASS_FLOW_METER_ENABLED
99  int32_t flowMeterFlowAtStarting = MFM_read_airflow();
100  (void)MFM_calibrateZero();
101 #else
102  int32_t flowMeterFlowAtStarting = 0;
103 #endif
108  delay(500);
110  delay(1000);
111 #ifdef MASS_FLOW_METER_ENABLED
112  int32_t flowMeterFlowWithBlowerOn = MFM_read_airflow();
113 #else
114  int32_t flowMeterFlowWithBlowerOn = 30000;
115 #endif
116 
117  blower.stop();
118 
119  // Happens when flow meter fails
120  bool isMassFlowMeterOutOfRange = ((flowMeterFlowAtStarting < -1000)
121  || (flowMeterFlowAtStarting > 1000));
122 
123  if ((isMassFlowMeterOutOfRange == true)
124  || ((flowMeterFlowWithBlowerOn < 20000) || (flowMeterFlowWithBlowerOn > 100000))) {
125  // Invalid calibration
126  calibrationValid = false;
127  displayFlowMeterFail(flowMeterFlowAtStarting, flowMeterFlowWithBlowerOn);
128 
130 
131  // Wait for user to press start to restart calibration
132  restartWaitTimer = 0;
133 
134  while (startButtonPressed == false) {
136 
137  // Heartbeat fatal error periodically
138  if ((restartWaitTimer % 1000u) == 0u) {
139  // MFM reports an out-of-range value, it might not be connected
140  if (isMassFlowMeterOutOfRange == true) {
141  // MFM failure (eg. not connected)
143  } else {
144  // Other calibration errors
145  sendCalibrationFatalError(inspiratoryPressureSensorOffset,
147  flowMeterFlowAtStarting,
148  flowMeterFlowWithBlowerOn);
149  }
150  }
151 
153  }
154  } else {
155  calibrationValid = true;
156  }
157 
158  displayPressureOffset(inspiratoryPressureSensorOffset);
159  delay(1000);
160 #ifdef MASS_FLOW_METER_ENABLED
162  delay(1000);
163 #endif
164  }
165  // Reset values to default state
166  calibationStarted = false;
167  startButtonPressed = false;
168  }
169 }
170 
172  uint16_t start = millis();
175  pressureOffsetSum = 0;
177 
178  // Open valves
183 
184  while ((millis() - start) < ms) {
185  // Measure 1 pressure per ms we wait
186  if ((millis() - start) > pressureOffsetCount) {
192  }
193  continue;
194  }
195 }
196 
198  keyboardLoop();
200 }
201 
203 
Blower blower
Definition: blower.cpp:20
void Buzzer_High_Prio_Start(void)
Activate the buzzer pattern for high priority alarms.
Definition: buzzer.cpp:186
void Buzzer_Boot_Start(void)
Activate boot bip.
Definition: buzzer.cpp:194
bool startButtonPressed
Definition: calibration.cpp:35
void Calibration_Init()
Initialization of calibration process.
Definition: calibration.cpp:40
int32_t maxOffsetValue
Definition: calibration.cpp:33
void Calibration_Wait_Measure_Pressure(uint16_t ms)
Block execution for a given duration.
uint32_t pressureOffsetCount
Definition: calibration.cpp:31
uint32_t restartWaitTimer
Definition: calibration.cpp:34
int32_t minOffsetValue
Definition: calibration.cpp:32
#define RESTART_READ_KEYBOARD_DELAY
Definition: calibration.cpp:38
void Calibration_Read_Keyboard_Delayed()
Read keyboard duing calibration process (delayed)
bool Calibration_Started()
Check if calibration mode is started.
bool calibrationValid
Definition: calibration.cpp:37
int32_t pressureOffsetSum
Definition: calibration.cpp:30
void Calibration_Restart()
Restart calibration process.
bool calibationStarted
Definition: calibration.cpp:36
void stop()
Stops the blower.
Definition: blower.cpp:105
void runSpeed(uint16_t p_runSpeed)
Run the blower to a given speed.
Definition: blower.cpp:69
int32_t read()
Read the current pressure for the feedback control.
Definition: pressure.cpp:29
void setPressureSensorOffset(int32_t p_pressureSensorOffest)
Set the pressure sensor offset.
Definition: pressure.h:30
void execute()
Command the valve to go to the requested aperture.
void open()
Request opening of the Pressure Valve.
void close()
Request closing of the Pressure Valve.
int32_t pressureValue
void keyboardLoop()
Handle button events.
Definition: keyboard.cpp:223
int32_t MFM_getOffset(void)
Get massflow meter offset.
int8_t MFM_calibrateZero(void)
If the massflow meter needs to be calibrated, this function will be usefull.
int32_t MFM_read_airflow(void)
Read instant air flow.
#define PIN_LED_GREEN
Definition: parameters.h:253
#define PIN_LED_RED
Definition: parameters.h:255
#define LED_START_INACTIVE
Definition: parameters.h:258
#define PIN_LED_YELLOW
Definition: parameters.h:254
#define PIN_LED_START
Definition: parameters.h:252
#define LED_GREEN_ACTIVE
Definition: parameters.h:263
#define DEFAULT_BLOWER_SPEED
Definition: parameters.h:194
#define LED_YELLOW_INACTIVE
Definition: parameters.h:262
#define LED_GREEN_INACTIVE
Definition: parameters.h:264
#define LED_RED_INACTIVE
Definition: parameters.h:260
#define LED_RED_ACTIVE
Definition: parameters.h:259
#define LED_YELLOW_ACTIVE
Definition: parameters.h:261
#define LED_START_ACTIVE
Definition: parameters.h:257
#define MAX_PRESSURE_OFFSET
Definition: parameters.h:123
PressureSensor inspiratoryPressureSensor
Definition: pressure.cpp:23
PressureValve inspiratoryValve
PressureValve expiratoryValve
void displayPressureOffsetUnstable(uint32_t p_minOffsetValue, uint32_t p_maxOffsetValue)
Display error when pressure offset is unstable.
Definition: screen.cpp:219
void displayFlowMeterOffset(int32_t p_flowMeterFlowOffset)
Display flow meter offset.
Definition: screen.cpp:212
void displayPressureOffset(int32_t p_inspiratoryPressureSensorOffset)
Display pressure offset.
Definition: screen.cpp:186
void displayPatientMustBeUnplugged()
Display the "calibration in progress" message.
Definition: screen.cpp:236
void resetScreen()
Erase everything that is on the screen.
Definition: screen.cpp:50
void displayFlowMeterFail(int32_t p_flowMeterFlowAtStarting, int32_t p_flowMeterFlowWithBlowerOn)
Display error when flow meter fails.
Definition: screen.cpp:194
void sendMassFlowMeterFatalError(void)
Send a "mass flow meter" fatal error.
Definition: telemetry.cpp:1321
void sendCalibrationFatalError(int16_t pressureOffsetValue, int16_t minPressureValue, int16_t maxPressureValue, int16_t flowAtStartingValue, int16_t flowWithBlowerOnValue)
Definition: telemetry.cpp:1193
void sendInconsistentPressureFatalError(uint16_t pressureValue)
Definition: telemetry.cpp:1359