software:firmware
MakAir Firmware
respirator.cpp File Reference

Entry point of ventilator program. More...

#include "../includes/config.h"
#include "Arduino.h"
#include <HardwareSerial.h>
#include <IWatchdog.h>
#include <LiquidCrystal.h>
#include "../includes/battery.h"
#include "../includes/blower.h"
#include "../includes/buzzer.h"
#include "../includes/buzzer_control.h"
#include "../includes/calibration.h"
#include "../includes/cpu_load.h"
#include "../includes/debug.h"
#include "../includes/end_of_line_test.h"
#include "../includes/keyboard.h"
#include "../includes/main_controller.h"
#include "../includes/main_state_machine.h"
#include "../includes/mass_flow_meter.h"
#include "../includes/parameters.h"
#include "../includes/pressure.h"
#include "../includes/pressure_valve.h"
#include "../includes/rpi_watchdog.h"
#include "../includes/screen.h"
#include "../includes/serial_control.h"
#include "../includes/telemetry.h"

Go to the source code of this file.

Functions

HardwareSerial Serial6 (PIN_TELEMETRY_SERIAL_RX, PIN_TELEMETRY_SERIAL_TX)
 
void setup (void)
 
void loop (void)
 

Variables

HardwareTimer * hardwareTimer1
 
HardwareTimer * hardwareTimer3
 

Detailed Description

Entry point of ventilator program.

Author
Makers For Life

Definition in file respirator.cpp.

Function Documentation

◆ loop()

void loop ( void  )

Definition at line 161 of file respirator.cpp.

161 { COUNT_IDLE_CYCLE; }
#define COUNT_IDLE_CYCLE
Increment the idle cycles counter.
Definition: cpu_load.h:26

◆ Serial6()

HardwareSerial Serial6 ( PIN_TELEMETRY_SERIAL_RX  ,
PIN_TELEMETRY_SERIAL_TX   
)

◆ setup()

void setup ( void  )

Definition at line 49 of file respirator.cpp.

49  {
50  // Nothing should be sent to Serial in production, but this will avoid crashing the program if
51  // some Serial.print() was forgotten
52  Serial.begin(115200);
53  DBG_DO(Serial.println("Booting the system...");)
54 
55  startScreen();
56 
57  initBattery();
60 
61  // Heartbeat fatal error periodically
62  while (true) {
64  delay(1000);
65  }
66  }
67 
68  initTelemetry();
70 
71  // Timer for valves
72  hardwareTimer3 = new HardwareTimer(TIM3);
73  hardwareTimer3->setOverflow(VALVE_PERIOD, MICROSEC_FORMAT);
74 
75  // Valves setup
79  hardwareTimer3->resume();
83  hardwareTimer3->resume();
84 
85  // Blower setup
86  hardwareTimer1 = new HardwareTimer(TIM1);
87  hardwareTimer1->setOverflow(ESC_PPM_PERIOD, MICROSEC_FORMAT);
89  blower.setup();
90 
91  // Init controllers
94 
95  // Init sensors
97 #ifdef MASS_FLOW_METER_ENABLED
98  (void)MFM_init();
99 #endif
100 
101  // Setup pins of the microcontroller
102  pinMode(PIN_PRESSURE_SENSOR, INPUT);
103  pinMode(PIN_BATTERY, INPUT);
104  pinMode(PIN_ENABLE_PWR_RASP, OUTPUT);
105  pinMode(PIN_LED_START, OUTPUT);
106  pinMode(PIN_LED_RED, OUTPUT);
107  pinMode(PIN_LED_YELLOW, OUTPUT);
108  pinMode(PIN_LED_GREEN, OUTPUT);
109  pinMode(PB12, INPUT);
110 
111  // Turn on the Raspberry Pi power
112  digitalWrite(PIN_ENABLE_PWR_RASP, PWR_RASP_ACTIVE);
113 #if DEBUG != 0
115 #endif
116 
117  // Activate test mode if a service button is pressed
118  // The end of line test mode cannot be activated later on.
119  // Autotest inputs: the service button on PB12, top right of the board's rear side
120  if (HIGH == digitalRead(PB12)) {
121  eolTest.activate();
123  while (HIGH == digitalRead(PB12)) {
124  continue;
125  }
126  }
127 
128  // Catch potential Watchdog reset
129  // cppcheck-suppress misra-c2012-14.4 ; IWatchdog.isReset() returns a boolean
130  if (IWatchdog.isReset(true)) {
131  // Run a high priority alarm
133  Buzzer_Init();
136 
137  // Heartbeat fatal error periodically
138  while (true) {
140  delay(1000);
141  }
142  }
143 
144  initKeyboard();
146  Buzzer_Init();
148 
149  if (!eolTest.isRunning()) {
151 
152  // Init the watchdog timer. It must be reloaded frequently otherwise MCU resests
153  IWatchdog.begin(WATCHDOG_TIMEOUT);
154  IWatchdog.reload();
155  } else {
157  }
158 }
AlarmController alarmController
Instance of the alarm controller.
uint32_t getBatteryLevelX100()
Returns battery level x100 for better accuracy.
Definition: battery.cpp:129
bool isBatteryDeepDischarged()
Check if battery is deeply discharged.
Definition: battery.cpp:136
void initBattery()
Initialize battery abstraction.
Definition: battery.cpp:33
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_Init()
Initialization of HardwareTimer for buzzer.
Definition: buzzer.cpp:128
#define WATCHDOG_TIMEOUT
Watchdog timeout in microseconds.
Definition: buzzer.h:11
void BuzzerControl_Init(void)
Initialization of HardwareTimer for buzzer.
void Calibration_Init()
Initialization of calibration process.
Definition: calibration.cpp:40
Manage alarm features.
Controls a blower.
Definition: blower.h:27
void setup()
Initialize the hardware timer used to control the blower.
Definition: blower.cpp:36
void setupAndStart()
Run test mode.
void activate()
Enable test mode.
bool isRunning()
Check if test mode is enabled.
Controls breathing cycle.
void setupAndStart()
Run the state machine.
Offset aware reading class for the pressure sensor.
Definition: pressure.h:13
Controls a pressure valve.
void setup()
Initialize this valve.
void disable()
Disable countdown mode (used for debug)
#define DBG_DO(statement)
Expand arbitrary code only when in debug mode.
Definition: debug.h:24
EolTest eolTest
void initKeyboard()
Initialize keyboard abstraction.
Definition: keyboard.cpp:78
MainController mainController
MainStateMachine mainStateMachine
bool MFM_init(void)
Initialize Mass Flow Meter.
#define PIN_LED_GREEN
Definition: parameters.h:253
#define PIN_LED_RED
Definition: parameters.h:255
#define ESC_PPM_PERIOD
Definition: parameters.h:181
#define PIN_LED_YELLOW
Definition: parameters.h:254
#define PIN_LED_START
Definition: parameters.h:252
#define PIN_PRESSURE_SENSOR
Definition: parameters.h:273
#define VALVE_PERIOD
Definition: parameters.h:171
#define TIM_CHANNEL_ESC_BLOWER
Definition: parameters.h:276
#define PIN_ENABLE_PWR_RASP
Definition: parameters.h:281
#define VALVE_CLOSED_STATE
Angle when closed.
Definition: parameters.h:170
#define VALVE_OPEN_STATE
Angle when opened.
Definition: parameters.h:167
#define PIN_ESC_BLOWER
Definition: parameters.h:275
#define TIM_CHANNEL_EXPIRATORY_VALVE
Definition: parameters.h:180
#define TIM_CHANNEL_INSPIRATORY_VALVE
Definition: parameters.h:179
#define PWR_RASP_ACTIVE
Definition: parameters.h:282
#define PIN_INSPIRATORY_VALVE
Definition: parameters.h:177
#define PIN_BATTERY
Definition: parameters.h:278
#define PIN_EXPIRATORY_VALVE
Definition: parameters.h:178
PressureSensor inspiratoryPressureSensor
Definition: pressure.cpp:23
PressureValve inspiratoryValve
PressureValve expiratoryValve
HardwareTimer * hardwareTimer3
Definition: respirator.cpp:45
HardwareTimer * hardwareTimer1
Definition: respirator.cpp:44
RpiWatchdog rpiWatchdog
void displayBatteryDeepDischarge()
Display error when battery level is too discharged.
Definition: screen.cpp:260
void displayWatchdogError()
Display error when machine was restarted by watchdog.
Definition: screen.cpp:250
void displayEndOfLineTestMode()
Show that EOL mode was triggered.
Definition: screen.cpp:245
void startScreen()
Start the screen.
Definition: screen.cpp:42
void sendWatchdogRestartFatalError(void)
Send a "watchdog restart" fatal error.
Definition: telemetry.cpp:1155
void sendBootMessage()
Send a "boot" message.
Definition: telemetry.cpp:114
void sendBatteryDeeplyDischargedFatalError(uint16_t batteryLevelValue)
Send a "battery deeply discharged" fatal error.
Definition: telemetry.cpp:1275
void initTelemetry(void)
Prepare Serial6 to send telemetry data.
Definition: telemetry.cpp:109

Variable Documentation

◆ hardwareTimer1

HardwareTimer* hardwareTimer1

Definition at line 44 of file respirator.cpp.

◆ hardwareTimer3

HardwareTimer* hardwareTimer3

Definition at line 45 of file respirator.cpp.