software:firmware
MakAir Firmware
respirator.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "../includes/config.h"
11 #if MODE == MODE_PROD
12 
13 // INCLUDES ==================================================================
14 
15 // External
16 #include "Arduino.h"
17 #include <HardwareSerial.h>
18 #include <IWatchdog.h>
19 #include <LiquidCrystal.h>
20 
21 // Internal
22 #include "../includes/battery.h"
23 #include "../includes/blower.h"
24 #include "../includes/buzzer.h"
25 #include "../includes/buzzer_control.h"
26 #include "../includes/calibration.h"
27 #include "../includes/cpu_load.h"
28 #include "../includes/debug.h"
29 #include "../includes/end_of_line_test.h"
30 #include "../includes/keyboard.h"
31 #include "../includes/main_controller.h"
32 #include "../includes/main_state_machine.h"
33 #include "../includes/mass_flow_meter.h"
34 #include "../includes/parameters.h"
35 #include "../includes/pressure.h"
36 #include "../includes/pressure_valve.h"
37 #include "../includes/rpi_watchdog.h"
38 #include "../includes/screen.h"
39 #include "../includes/serial_control.h"
40 #include "../includes/telemetry.h"
41 
42 // PROGRAM =====================================================================
43 
44 HardwareTimer* hardwareTimer1; // ESC command
45 HardwareTimer* hardwareTimer3; // valves command
46 
48 
49 void setup(void) {
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 }
159 
160 // cppcheck-suppress unusedFunction
161 void loop(void) { COUNT_IDLE_CYCLE; }
162 
163 #endif
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 COUNT_IDLE_CYCLE
Increment the idle cycles counter.
Definition: cpu_load.h:26
#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_TELEMETRY_SERIAL_TX
Definition: parameters.h:280
#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_TELEMETRY_SERIAL_RX
Definition: parameters.h:279
#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
void loop(void)
Definition: respirator.cpp:161
HardwareTimer * hardwareTimer3
Definition: respirator.cpp:45
HardwareSerial Serial6(PIN_TELEMETRY_SERIAL_RX, PIN_TELEMETRY_SERIAL_TX)
void setup(void)
Definition: respirator.cpp:49
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