How ssm-lab/dt-12v-dc-motor works
1. System shape
- This repo is a two-process edge digital twin for a 12V DC motor.
- The physical side runs on a Raspberry Pi; the analytic/visualization side runs on a separate client machine.
- The architecture is centered on one live feedback loop: sensor readings and motor state flow from the Pi to the client, while control commands can flow back to the Pi.
2. Physical edge subsystem: pi_server/
- pi_server/server.py is the hardware-facing API service.
- It reads two DS18B20 1-Wire sensors: one for ambient temperature and one for motor surface temperature.
- It also drives the L298N H-bridge through GPIO/PWM to switch the motor on/off and perform emergency shutdown.
- This is the hard real-time safety boundary: if the motor must be stopped, the Pi is the final authority.
3. Edge data acquisition and logging
- pi_server/temp_collection.py is a separate acquisition/logging utility used for calibration and experimental runs.
- It supports the offline dataset generation that produced run_A.csv through run_D.csv and run_off.csv.
- Those CSVs are not runtime inputs to the live system; they are model-fitting artifacts.
4. Client/twin subsystem: client/
- client/app.py is the operator-facing Streamlit dashboard and polling loop.
- It periodically queries the Pi server, displays live temperatures, calculated residuals, and predicted time-to-failure, and exposes manual control buttons.
- The client is also the orchestration point for safety decisions, including issuing a remote kill request.
5. Thermal model boundary
- client/thermal_model.py contains the ThermalDigitalTwin physics model.
- This is the “Ghost Motor” twin: a software-only lumped thermal model that evolves in parallel with the real motor.
- It consumes live ambient temperature and operational state, then estimates the expected motor surface temperature independently of the sensor reading.
6. Prediction and anomaly flow
- The client compares measured surface temperature against the ghost twin output to compute a residual.
- A sustained positive residual is treated as unexplained heating and is used for anomaly detection.
- In parallel, the client estimates time-to-failure from recent temperature trend and thermal limit projection.
7. Safety and control loop
- The safety mechanism is split across client and Pi: the client detects dangerous conditions, but the Pi executes the actual motor kill.
- Conditions include overheating, low predicted TTF, and manual emergency shutdown.
- This separation matters architecturally: analysis and UI stay on the client, while actuation remains local to the hardware controller.
8. Calibration and model fitting
- client/derive.py is an offline parameter derivation tool.
- Together with plot_compare.py, it supports fitting and validating the thermal model against the recorded runs.
- The calibrated constants in the README indicate the live twin is parameterized from empirical data rather than purely theoretical values.
9. Runtime data flow
- Sensor readings originate on the Pi, are exposed through FastAPI, and are polled by the Streamlit client at a short interval.
- The client updates its local twin state from those readings, computes derived metrics, then renders the dashboard.
- If conditions are unsafe, the client sends a control request back to the Pi, which immediately cuts motor power.
10. Technology and deployment boundaries
- The Pi side uses FastAPI/Uvicorn and GPIO-oriented libraries for the hardware interface.
- The client side uses Streamlit, NumPy, and plotting libraries for visualization and model computation.
- The repo is therefore split cleanly into: hardware API/control, live analytics/UI, and offline calibration/analysis.
11. Repository artifacts
- The CSV files are the historical calibration corpus.
- docs/setup.png documents the physical wiring, which is an important part of the system boundary because sensor placement and motor driver wiring directly affect the control and measurement loop.
- The root requirements.txt plus the two component-specific requirements files reinforce that the Pi and client are meant to be installed and run separately.
12. What to draw in an architecture diagram
- Draw three main nodes: the physical motor/sensors/driver, the Raspberry Pi API/controller, and the client dashboard/twin.
- Show sensor data flowing from physical hardware to the Pi, then to the client over HTTP.
- Show kill/control commands flowing from the client back to the Pi, and from the Pi to the motor driver.
- Place the thermal model, residual/anomaly logic, and TTF estimation inside the client boundary, and place GPIO/PWM actuation inside the Pi boundary.
Main entry points: app.py Dashboard · server.py Pi API · run_A.csv Run CSVs · thermal_model.py Ghost twin
Components 16
Physical rig
Motor, sensors, and power switching on the bench
- Motor rig · 12V DC motor, sensors, and wiring harness
- Ambient temp · Measures surrounding air temperature
- Motor temp · Measures motor surface temperature
- L298N driver · Switches motor power under GPIO/PWM control
Raspberry Pi
Hardware-facing API, acquisition, and actuation
- Pi API · Hardware-facing server on the Raspberry Pi
pi_server/server.py - GPIO control · Executes local motor enable and emergency stop
pi_server/server.py - Temp collection · Offline logging utility for calibration runs
pi_server/temp_collection.py
Client twin
Dashboard, thermal model, anomaly logic, and safety decisions
- Dashboard · Operator UI and polling loop
client/app.py - Ghost twin · Lumped physics model of expected motor temperature
client/thermal_model.py - Residual check · Compares measured and predicted motor temperature
client/app.py - TTF estimate · Projects time-to-failure from thermal trend
client/app.py - Safety rules · Triggers alerts and remote kill on unsafe conditions
client/app.py
Offline analysis
Calibration, parameter fitting, and recorded runs
- Derive params · Fits twin constants from recorded runs
client/derive.py - Model compare · Checks fitted model against recorded data
plot_compare.py - Run CSVs · Historical runs used for model fitting
run_A.csv - Wiring notes · Documentation for sensor placement and driver wiring
Connections 19
- Ambient temp → Pi API: ambient temp · Reads ambient temperature into the hardware API
- Motor temp → Pi API: motor temp · Reads motor surface temperature into the hardware API
- Pi API → Dashboard: telemetry · Polled live sensor and motor state from the Pi
- Dashboard → Ghost twin: update model · Feeds ambient state and operating context into the ghost twin
- Ghost twin → Residual check: predicted temp · Provides expected motor temperature for comparison
- Motor temp → Residual check: measured temp · Measured surface temperature is compared with the twin output
- Residual check → Safety rules: anomaly · Sustained positive residual contributes to unsafe condition detection
- TTF estimate → Safety rules: low TTF · Predicted time-to-failure can trigger shutdown policy
- Dashboard → TTF estimate: trend data · Uses recent temperature history to project failure horizon
- Dashboard → Pi API: kill request · Sends remote stop command when safety rules fire
- Safety rules → Dashboard: alerts · Surfaces alarms and manual emergency controls in the dashboard
- Pi API → GPIO control: actuate · Executes the final local stop/start decision on the Pi
- GPIO control → L298N driver: PWM / enable · Switches the H-bridge that powers the motor
- L298N driver → Motor rig: motor power · Delivers or cuts power to the DC motor
- Temp collection → Run CSVs: log data · Produces recorded calibration datasets
- Run CSVs → Derive params: fit data · Recorded runs are used to derive twin parameters
- Derive params → Ghost twin: constants · Calibrated parameters define the live thermal twin
- Run CSVs → Model compare: validate · Historical runs are compared against model output
- Wiring notes → Motor rig: setup reference · Documents the physical measurement and control boundary