7.05 Real-Time Sensor Monitoring, Emergency Thresholds & Hardware Fail-Safe States
💻 1. Annotated Source Code
#include <iostream> #include <stdexcept> #include "FuelLevelCritical.h" using namespace std; void checkFuelLevel(int percentage); int main() { cout << "Fuel Monitor Starting..." << endl; try { for (int i = 100; i >= 0; i -= 10) { checkFuelLevel(i); } } catch (const FuelLevelCritical& err) { cout << "ALERT: " << err.what() << endl; } return 0; } void checkFuelLevel(int percentage) { if (percentage < 10) { throw FuelLevelCritical(); } cout << "Fuel level at " << percentage << "% - within safe operating range." << endl; }
#ifndef FUEL_LEVEL_CRITICAL_H #define FUEL_LEVEL_CRITICAL_H #include <stdexcept> using namespace std; class FuelLevelCritical : public runtime_error { public: FuelLevelCritical() : runtime_error("Fuel level is critically low! Ship must refuel immediately!") { } }; #endif
📐 2. Architecture & UML Class Model
📚 3. Core C++ Concepts Deep-Dive
1. Domain-Specific Alert Hierarchies
Critical systems define distinct exception/alert tiers (e.g., FuelWarning vs FuelLevelCritical). Each tier enforces appropriate escalation behavior.
2. Exception-Driven Control Flow Hazards
Using exceptions for ordinary control flow (e.g. routine sensor polling) is an anti-pattern. Exceptions should only be reserved for truly exceptional, unrecoverable system faults.
⚡ 4. Embedded Systems & Hardware Reality
1. Safety State Machines (ISO 26262 & IEC 61508)
In safety-critical automotive/aerospace firmware, when a sensor reading breaches a critical safety envelope:
- Fail-Safe State Transition: The system immediately switches to a predefined safe state (e.g., cutting fuel pump power, opening safety contactors, engaging mechanical brakes).
- Fault Latching: The fault is latched in non-volatile memory and cannot be cleared until a full self-test or authorized service reset.
2. Hardware Analog Watchdog (AWD) in STM32 / NXP
Instead of software polling, modern microcontrollers provide hardware Analog Watchdogs on ADC channels that fire a dedicated hardware interrupt within sub-microsecond latency if voltage crosses high/low thresholds.
💡 5. Production-Ready Embedded Refactoring
Here is an embedded safety-critical monitor using a deterministic state machine with hysteresis:
#include <cstdint> enum class SafetyState : uint8_t { Nominal = 0, Warning, CriticalShutdown, FaultLatched }; class FuelSafetyMonitor { private: static constexpr uint16_t CRITICAL_THRESHOLD = 50; // 5.0 Liters static constexpr uint16_t WARNING_THRESHOLD = 150; // 15.0 Liters static constexpr uint16_t HYSTERESIS = 10; // Prevents state fluttering SafetyState state_{SafetyState::Nominal}; bool latch_engaged_{false}; public: SafetyState update(uint16_t raw_sensor_level) noexcept { if (latch_engaged_) return SafetyState::FaultLatched; if (raw_sensor_level <= CRITICAL_THRESHOLD) { state_ = SafetyState::CriticalShutdown; latch_engaged_ = true; trigger_hardware_shutdown(); } else if (raw_sensor_level <= WARNING_THRESHOLD) { state_ = SafetyState::Warning; } else if (raw_sensor_level > (WARNING_THRESHOLD + HYSTERESIS)) { state_ = SafetyState::Nominal; } return state_; } private: void trigger_hardware_shutdown() noexcept { // Assert GPIO pin to trip physical safety relay } };
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.