This repository contains the source code and configuration for an industrial-grade, edge-deployed autonomous monitoring and fault detection system. Engineered specifically for the Raspberry Pi 5 operating within strict determinism requirements, the architecture leverages true real-time multi-threading, hybrid dual AI models, physics-based signal processing, and direct hardware actuation to mitigate critical machinery failures with sub-40 millisecond latency.
The orchestrator bypasses the Python Global Interpreter Lock (GIL) by utilizing a hybrid multiprocessing and threading architecture. I/O operations (I2C polling, SQLite transactions) operate on the main priority thread, while computationally expensive tasks (FFT operations, TFLite inferences) are executed asynchronously in isolated processes. This guarantees a maximum end-to-end latency of less than 40ms.
Raw sensor telemetry is conditioned prior to inference via physics-based processing pipelines:
- Kalman Filtering: Implements a 1D state estimator to eliminate stochastic noise from sensitive analog readings (e.g., current draw).
- Z-Score Normalization: Standardizes inputs based on baseline statistics to ensure numerical stability during neural network propagation.
- Fast Fourier Transform (FFT): Converts high-frequency (1kHz) physical vibration buffers from the time domain into spectral frequency bins to detect mechanical anomalies such as bearing wear and rotor unbalance.
The anomaly detection engine relies on a concurrent dual-model approach, combined via a weighted fusion layer:
- LSTM Autoencoder (TFLite): A deep learning sequence model tasked with identifying gradual pattern drift over time by calculating reconstruction loss.
- Isolation Forest: A scikit-learn ensemble algorithm optimized for immediate point-anomaly and sudden spike detection.
- Hardware Watchdog Integration: Hooks directly into the Linux kernel via
/dev/watchdogto guarantee an autonomous hard-reboot of the edge hardware in the event of a catastrophic OS or orchestrator hang. - SQLite Event Logging: Maintains a persistent local database (
storage/fault_history.db) logging every anomaly score, recovery action, and sensor state for post-incident forensic analysis.
This software stack requires a Raspberry Pi 5.
| Sensor Module | Physical Measurement | I2C Address | Required Driver Library |
|---|---|---|---|
| MPU6050 | Tri-axial Vibration | 0x68 |
smbus2 |
| ADS1115 | Current (via ACS712) | 0x48 |
adafruit-circuitpython-ads1x15 |
| BMP280 | Pressure / Temperature | 0x76 |
adafruit-circuitpython-bmp280 |
| INA219 | Bus Voltage / Power | 0x40 |
adafruit-circuitpython-ina219 |
- DHT22 (Environmental Humidity):
GPIO 4 - Relay Channel 1 (Primary Machine Power Cutoff):
GPIO 17 - Relay Channel 2 (Secondary/Backup System Actuation):
GPIO 27 - Audio Annunciator (Buzzer):
GPIO 22 - Visual Annunciator (LED Status Panel):
GPIO 23
Enable the I2C interface and configure the hardware watchdog module on the Raspberry Pi.
sudo raspi-config # Interface Options -> I2C -> Enable
# Load Watchdog Kernel Module
sudo modprobe bcm2835_wdt
echo "bcm2835_wdt" | sudo tee -a /etc/modulesInitialize the virtual environment or install dependencies directly to the system Python environment.
pip3 install -r requirements.txtGenerate synthetic baselines, train the LSTM and Isolation Forest topologies, convert the LSTM to .tflite, and serialize the models to the ai_engine/models/ directory. This step may be performed on an x86 workstation prior to edge deployment.
python3 ai_engine/model_trainer.pyExecution requires elevated privileges to access raw GPIO memory registers and the /dev/watchdog file descriptor.
sudo -E python3 main.pyTo guarantee high availability, the orchestrator should be configured to initialize on boot and automatically recover from unexpected terminations via a systemd service.
- Create the unit file:
sudo nano /etc/systemd/system/fault-detection.service - Populate the configuration:
[Unit]
Description=High-Performance Fault Detection Edge Daemon
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/Real-Time-Fault-Detection/main.py
WorkingDirectory=/home/pi/Real-Time-Fault-Detection
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target- Enable and initialize the daemon:
sudo systemctl enable fault-detection.service
sudo systemctl start fault-detection.service
sudo systemctl status fault-detection.service