# ssm-lab/dt-12v-dc-motor architecture

> Architecture diagram of the GitHub repository ssm-lab/dt-12v-dc-motor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ssm-lab/dt-12v-dc-motor
- Repository: https://github.com/ssm-lab/dt-12v-dc-motor

## Overview

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: [`client/app.py`](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py) (Dashboard), [`pi_server/server.py`](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/server.py) (Pi API), [`run_A.csv`](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/run_A.csv) (Run CSVs), [`client/thermal_model.py`](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/thermal_model.py) (Ghost twin)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_physical["Physical rig"]
  node_motor_rig["Motor rig<br/>physical system"]
  node_ambient_sensor(("Ambient temp<br/>DS18B20 sensor"))
  node_surface_sensor(("Motor temp<br/>DS18B20 sensor"))
  node_l298n_driver["L298N driver<br/>H-bridge"]
end

subgraph group_pi["Raspberry Pi"]
  node_pi_server["Pi API<br/>FastAPI service<br/>[server.py]"]
  node_gpio_actuation["GPIO control<br/>PWM safety<br/>[server.py]"]
  node_temp_collection["Temp collection<br/>acquisition tool<br/>[temp_collection.py]"]
end

subgraph group_client["Client twin"]
  node_client_app["Dashboard<br/>Streamlit app<br/>[app.py]"]
  node_thermal_model{{"Ghost twin<br/>thermal model<br/>[thermal_model.py]"}}
  node_residual_logic(("Residual check<br/>anomaly logic<br/>[app.py]"))
  node_ttf_estimator(("TTF estimate<br/>prediction<br/>[app.py]"))
  node_safety_logic["Safety rules<br/>control policy<br/>[app.py]"]
end

subgraph group_offline["Offline analysis"]
  node_derive_tool["Derive params<br/>offline fitter<br/>[derive.py]"]
  node_plot_compare["Model compare<br/>validation tool<br/>[plot_compare.py]"]
  node_cal_runs[("Run CSVs<br/>calibration data<br/>[run_A.csv]")]
  node_wiring_doc["Wiring notes<br/>deployment doc"]
end

node_ambient_sensor -->|"ambient temp"| node_pi_server
node_surface_sensor -->|"motor temp"| node_pi_server
node_pi_server -->|"telemetry"| node_client_app
node_client_app -->|"update model"| node_thermal_model
node_thermal_model -->|"predicted temp"| node_residual_logic
node_surface_sensor -->|"measured temp"| node_residual_logic
node_residual_logic -->|"anomaly"| node_safety_logic
node_ttf_estimator -->|"low TTF"| node_safety_logic
node_client_app -->|"trend data"| node_ttf_estimator
node_client_app -->|"kill request"| node_pi_server
node_safety_logic -->|"alerts"| node_client_app
node_pi_server -->|"actuate"| node_gpio_actuation
node_gpio_actuation -->|"PWM / enable"| node_l298n_driver
node_l298n_driver -->|"motor power"| node_motor_rig
node_temp_collection -->|"log data"| node_cal_runs
node_cal_runs -->|"fit data"| node_derive_tool
node_derive_tool -->|"constants"| node_thermal_model
node_cal_runs -->|"validate"| node_plot_compare
node_wiring_doc -.->|"setup reference"| node_motor_rig

click node_pi_server "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/server.py"
click node_gpio_actuation "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/server.py"
click node_temp_collection "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/temp_collection.py"
click node_client_app "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py"
click node_thermal_model "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/thermal_model.py"
click node_residual_logic "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py"
click node_ttf_estimator "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py"
click node_safety_logic "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py"
click node_derive_tool "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/derive.py"
click node_plot_compare "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/plot_compare.py"
click node_cal_runs "https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/run_A.csv"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_motor_rig,node_ambient_sensor,node_surface_sensor,node_l298n_driver toneBlue
class node_pi_server,node_gpio_actuation,node_temp_collection toneAmber
class node_client_app,node_thermal_model,node_residual_logic,node_ttf_estimator,node_safety_logic toneMint
class node_derive_tool,node_plot_compare,node_cal_runs,node_wiring_doc toneRose
```

## Components

### Physical rig

Motor, sensors, and power switching on the bench

- **Motor rig**: physical system · 12V DC motor, sensors, and wiring harness
- **Ambient temp**: DS18B20 sensor · Measures surrounding air temperature
- **Motor temp**: DS18B20 sensor · Measures motor surface temperature
- **L298N driver**: H-bridge · Switches motor power under GPIO/PWM control

### Raspberry Pi

Hardware-facing API, acquisition, and actuation

- [Pi API](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/server.py): FastAPI service · Hardware-facing server on the Raspberry Pi · `pi_server/server.py`
- [GPIO control](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/server.py): PWM safety · Executes local motor enable and emergency stop · `pi_server/server.py`
- [Temp collection](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/pi_server/temp_collection.py): acquisition tool · Offline logging utility for calibration runs · `pi_server/temp_collection.py`

### Client twin

Dashboard, thermal model, anomaly logic, and safety decisions

- [Dashboard](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py): Streamlit app · Operator UI and polling loop · `client/app.py`
- [Ghost twin](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/thermal_model.py): thermal model · Lumped physics model of expected motor temperature · `client/thermal_model.py`
- [Residual check](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py): anomaly logic · Compares measured and predicted motor temperature · `client/app.py`
- [TTF estimate](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py): prediction · Projects time-to-failure from thermal trend · `client/app.py`
- [Safety rules](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/app.py): control policy · Triggers alerts and remote kill on unsafe conditions · `client/app.py`

### Offline analysis

Calibration, parameter fitting, and recorded runs

- [Derive params](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/client/derive.py): offline fitter · Fits twin constants from recorded runs · `client/derive.py`
- [Model compare](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/plot_compare.py): validation tool · Checks fitted model against recorded data · `plot_compare.py`
- [Run CSVs](https://github.com/ssm-lab/dt-12v-dc-motor/blob/main/run_A.csv): calibration data · Historical runs used for model fitting · `run_A.csv`
- **Wiring notes**: deployment doc · Documentation for sensor placement and driver wiring

## Connections

- 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
