# havish06/savsd architecture

> Architecture diagram of the GitHub repository havish06/savsd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/havish06/savsd
- Repository: https://github.com/havish06/savsd

## Overview

1. **What this repo is**
   - This is a sensor-driven ADAS simulator, centered on a single **ego vehicle** that is continuously monitored and assisted by multiple safety modules: drowsiness, alcohol lockout, adaptive cruise, lane-change assist, blind-spot checks, and collision warnings.

2. **Top-level execution path**
   - `main.py` and `run_simulator.py` are the entry entrypoints.
   - They assemble configuration, build the simulator, and start the main control loop that advances the world, reads sensors, applies decision logic, and renders the scene.

3. **Simulation core**
   - The `simulation/` package is the orchestration layer.
   - `simulator.py` is the base simulation loop.
   - `enhanced_simulator.py` adds the “sensor-first” ADAS behaviors called out in the README, including priority-aware control ownership, safe-stop override, and the enhanced HUD.
   - `traffic_generator.py` is responsible for populating or varying surrounding traffic.

4. **World model**
   - `world/` contains the static and temporal simulation context:
     - `road.py` models lane geometry and road boundaries.
     - `traffic_light.py` represents signal state.
     - `clock.py` handles simulation time.
     - `world.py` likely ties these together into the shared state consumed by vehicles, sensors, and rendering.
   - This is the common data source for the rest of the system.

5. **Vehicle layer**
   - `vehicles/` separates the controlled car from surrounding traffic:
     - `ego_vehicle.py` is the main controlled vehicle with ADAS interventions.
     - `npc_vehicle.py` models other traffic participants.
     - `vehicle.py` is the shared vehicle abstraction.
   - The decision layer acts primarily on the ego vehicle, while NPCs provide dynamic obstacles and lane context.

6. **Sensor subsystem**
   - `sensors/` is the main perception layer and appears to be the architectural center of the project.
   - Key modules include:
     - `driver_monitor.py` for drowsiness and eye-based driver monitoring.
     - `alcohol_sensor.py` for ignition lock behavior.
     - `distance_sensor.py` for adaptive cruise / headway checks.
     - `lane_sensor.py` for lane position and lane keeping.
     - `blind_spot_sensor.py` for lane-change safety.
     - `collision_warning.py` for immediate hazard detection.
   - These sensors read world/vehicle state and produce safety-relevant signals that feed the decision logic.

7. **Decision and control arbitration**
   - `decision/state_machine.py` is the main policy layer that decides which safety module currently owns control.
   - `decision/lane_change.py` encapsulates lane-change assist behavior, including driver confirmation and abort-on-hazard behavior.
   - This layer translates raw sensor outputs into actuation decisions such as safe stop, lockout, assist engagement, or lane-change approval.

8. **Driver interaction**
   - `input/driver_input.py` handles human commands such as assist toggle, lane-change request/confirm, manual speed control, pause, reset, and exit.
   - The README’s key bindings show that the simulator supports both assisted and manual driving modes, with the decision layer mediating when assist is active.

9. **Visualization and operator feedback**
   - `visualization/renderer.py` is the presentation layer for the simulation.
   - The README indicates road boundary visualization, lane markers, and a priority-aware HUD that shows which module currently controls the vehicle.
   - There is also a live OpenCV eye-monitor window when camera mode is enabled, which belongs to the driver-monitoring feedback loop.

10. **Audio and logging**
   - `utils/audio.py` provides alert or warning output for safety events.
   - `utils/logger.py` records simulation and sensor events, likely for debugging and traceability of safety interventions.
   - These utilities are cross-cutting concerns used by sensors and the decision layer rather than standalone subsystems.

11. **Configuration boundary**
   - `config/` centralizes simulation parameters and thresholds:
     - `config.py` and `sim_config.py` likely hold runtime constants for the environment.
     - `thresholds.py` defines decision limits for sensor triggers and safety actions.
   - This package is the main tuning point for behavior without changing the core logic.

12. **Main data flow to diagram**
   - A useful architectural diagram should show this loop:
     - **Driver input + world state + NPC traffic** feed the **sensor layer**
     - Sensor outputs feed the **state machine / lane-change decision logic**
     - Decisions update the **ego vehicle**
     - The updated world is sent to the **renderer** and **audio/logger**
   - The key boundary is between **perception/sensing** and **control arbitration**, with the simulation world acting as the shared substrate.

13. **Notable architectural emphasis**
   - The repo is not just a simulator; it is organized around **safety-module ownership of control**.
   - Drowsiness, alcohol lock, collision warning, and lane-change assist are treated as competing or prioritized control sources, which is why the enhanced simulator and state machine are central to the design.

Main entry points: [`decision/state_machine.py`](https://github.com/havish06/savsd/blob/main/decision/state_machine.py) (State Machine), [`world/world.py`](https://github.com/havish06/savsd/blob/main/world/world.py) (World), [`simulation/enhanced_simulator.py`](https://github.com/havish06/savsd/blob/main/simulation/enhanced_simulator.py) (Enhanced Sim), [`simulation/simulator.py`](https://github.com/havish06/savsd/blob/main/simulation/simulator.py) (Simulator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entrypoints"]
  node_main_py["main.py<br/>entrypoint<br/>[main.py]"]
  node_run_simulator_py["run_simulator.py<br/>runner<br/>[run_simulator.py]"]
end

subgraph group_sim["Simulation Core"]
  node_simulator_py["Simulator<br/>loop<br/>[simulator.py]"]
  node_enhanced_simulator_py["Enhanced Sim<br/>adas loop"]
  node_traffic_generator_py["Traffic Gen<br/>population"]
end

subgraph group_world["World Model"]
  node_world_py[("World<br/>shared state<br/>[world.py]")]
  node_road_py["Road<br/>geometry<br/>[road.py]"]
  node_clock_py["Clock<br/>timebase<br/>[clock.py]"]
  node_traffic_light_py["Traffic Light<br/>signal state<br/>[traffic_light.py]"]
end

subgraph group_vehicles["Vehicles"]
  node_vehicle_py["Vehicle<br/>base model<br/>[vehicle.py]"]
  node_ego_vehicle_py["Ego Vehicle<br/>controlled car<br/>[ego_vehicle.py]"]
  node_npc_vehicle_py["NPC Vehicle<br/>traffic actor<br/>[npc_vehicle.py]"]
end

subgraph group_sensors["Sensors"]
  node_driver_monitor_py["Driver Monitor<br/>drowsiness sensor<br/>[driver_monitor.py]"]
  node_alcohol_sensor_py["Alcohol Sensor<br/>lockout sensor<br/>[alcohol_sensor.py]"]
  node_distance_sensor_py["Distance Sensor<br/>headway sensor<br/>[distance_sensor.py]"]
  node_lane_sensor_py["Lane Sensor<br/>[lane_sensor.py]"]
  node_blind_spot_sensor_py["Blind Spot<br/>lane-change sensor"]
  node_collision_warning_py["Collision Warn<br/>hazard sensor"]
end

subgraph group_decision["Control Arbitration"]
  node_state_machine_py["State Machine<br/>policy arbiter<br/>[state_machine.py]"]
  node_lane_change_py["Lane Change<br/>assist logic<br/>[lane_change.py]"]
end

subgraph group_io["Input & Output"]
  node_driver_input_py["Driver Input<br/>controls<br/>[driver_input.py]"]
  node_renderer_py["Renderer<br/>visualization<br/>[renderer.py]"]
  node_audio_py["Audio<br/>alerts<br/>[audio.py]"]
  node_logger_py["Logger<br/>telemetry<br/>[logger.py]"]
end

subgraph group_config["Configuration"]
  node_config_py["Config<br/>[config.py]"]
  node_sim_config_py["Sim Config<br/>runtime config<br/>[sim_config.py]"]
  node_thresholds_py["Thresholds<br/>policy limits<br/>[thresholds.py]"]
end

node_main_py -->|"loads"| node_config_py
node_main_py -->|"starts"| node_enhanced_simulator_py
node_run_simulator_py -->|"starts"| node_simulator_py
node_enhanced_simulator_py -->|"extends"| node_simulator_py
node_simulator_py -->|"updates"| node_world_py
node_simulator_py -->|"spawns"| node_traffic_generator_py
node_simulator_py -->|"renders"| node_renderer_py
node_enhanced_simulator_py -->|"arbitrates"| node_state_machine_py
node_enhanced_simulator_py -->|"alerts"| node_audio_py
node_enhanced_simulator_py -->|"logs"| node_logger_py
node_world_py -->|"contains"| node_road_py
node_world_py -->|"tracks"| node_clock_py
node_world_py -->|"contains"| node_traffic_light_py
node_world_py -->|"includes"| node_ego_vehicle_py
node_world_py -->|"includes"| node_npc_vehicle_py
node_ego_vehicle_py -->|"inherits"| node_vehicle_py
node_npc_vehicle_py -->|"inherits"| node_vehicle_py
node_driver_input_py -->|"requests"| node_state_machine_py
node_driver_input_py -->|"commands"| node_ego_vehicle_py
node_driver_monitor_py -->|"signals"| node_state_machine_py
node_alcohol_sensor_py -->|"signals"| node_state_machine_py
node_distance_sensor_py -->|"signals"| node_state_machine_py
node_lane_sensor_py -->|"signals"| node_state_machine_py
node_blind_spot_sensor_py -->|"checks"| node_lane_change_py
node_collision_warning_py -->|"signals"| node_state_machine_py
node_state_machine_py -->|"delegates"| node_lane_change_py
node_state_machine_py -->|"controls"| node_ego_vehicle_py
node_lane_change_py -->|"updates"| node_ego_vehicle_py
node_renderer_py -->|"reads"| node_world_py
node_renderer_py -->|"shows"| node_state_machine_py
node_audio_py -->|"reflects"| node_collision_warning_py
node_logger_py -->|"records"| node_state_machine_py
node_config_py -->|"contains"| node_sim_config_py
node_config_py -->|"contains"| node_thresholds_py
node_sim_config_py -->|"configures"| node_enhanced_simulator_py
node_thresholds_py -->|"tunes"| node_driver_monitor_py
node_thresholds_py -->|"tunes"| node_collision_warning_py
node_thresholds_py -->|"tunes"| node_distance_sensor_py

click node_main_py "https://github.com/havish06/savsd/blob/main/main.py"
click node_run_simulator_py "https://github.com/havish06/savsd/blob/main/run_simulator.py"
click node_simulator_py "https://github.com/havish06/savsd/blob/main/simulation/simulator.py"
click node_enhanced_simulator_py "https://github.com/havish06/savsd/blob/main/simulation/enhanced_simulator.py"
click node_traffic_generator_py "https://github.com/havish06/savsd/blob/main/simulation/traffic_generator.py"
click node_world_py "https://github.com/havish06/savsd/blob/main/world/world.py"
click node_road_py "https://github.com/havish06/savsd/blob/main/world/road.py"
click node_clock_py "https://github.com/havish06/savsd/blob/main/world/clock.py"
click node_traffic_light_py "https://github.com/havish06/savsd/blob/main/world/traffic_light.py"
click node_vehicle_py "https://github.com/havish06/savsd/blob/main/vehicles/vehicle.py"
click node_ego_vehicle_py "https://github.com/havish06/savsd/blob/main/vehicles/ego_vehicle.py"
click node_npc_vehicle_py "https://github.com/havish06/savsd/blob/main/vehicles/npc_vehicle.py"
click node_driver_monitor_py "https://github.com/havish06/savsd/blob/main/sensors/driver_monitor.py"
click node_alcohol_sensor_py "https://github.com/havish06/savsd/blob/main/sensors/alcohol_sensor.py"
click node_distance_sensor_py "https://github.com/havish06/savsd/blob/main/sensors/distance_sensor.py"
click node_lane_sensor_py "https://github.com/havish06/savsd/blob/main/sensors/lane_sensor.py"
click node_blind_spot_sensor_py "https://github.com/havish06/savsd/blob/main/sensors/blind_spot_sensor.py"
click node_collision_warning_py "https://github.com/havish06/savsd/blob/main/sensors/collision_warning.py"
click node_state_machine_py "https://github.com/havish06/savsd/blob/main/decision/state_machine.py"
click node_lane_change_py "https://github.com/havish06/savsd/blob/main/decision/lane_change.py"
click node_driver_input_py "https://github.com/havish06/savsd/blob/main/input/driver_input.py"
click node_renderer_py "https://github.com/havish06/savsd/blob/main/visualization/renderer.py"
click node_audio_py "https://github.com/havish06/savsd/blob/main/utils/audio.py"
click node_logger_py "https://github.com/havish06/savsd/blob/main/utils/logger.py"
click node_config_py "https://github.com/havish06/savsd/blob/main/config/config.py"
click node_sim_config_py "https://github.com/havish06/savsd/blob/main/config/sim_config.py"
click node_thresholds_py "https://github.com/havish06/savsd/blob/main/config/thresholds.py"

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_main_py,node_run_simulator_py,node_driver_input_py,node_renderer_py,node_audio_py,node_logger_py toneBlue
class node_simulator_py,node_enhanced_simulator_py,node_traffic_generator_py,node_config_py,node_sim_config_py,node_thresholds_py toneAmber
class node_world_py,node_road_py,node_clock_py,node_traffic_light_py toneMint
class node_vehicle_py,node_ego_vehicle_py,node_npc_vehicle_py toneRose
class node_driver_monitor_py,node_alcohol_sensor_py,node_distance_sensor_py,node_lane_sensor_py,node_blind_spot_sensor_py,node_collision_warning_py toneIndigo
class node_state_machine_py,node_lane_change_py toneTeal
```

## Components

### Entrypoints

Startup scripts that assemble and launch the simulator.

- [main.py](https://github.com/havish06/savsd/blob/main/main.py): entrypoint · Primary application entrypoint. · `main.py`
- [run_simulator.py](https://github.com/havish06/savsd/blob/main/run_simulator.py): runner · Alternate launcher for the simulator. · `run_simulator.py`

### Simulation Core

Main execution loop and traffic generation.

- [Simulator](https://github.com/havish06/savsd/blob/main/simulation/simulator.py): loop · Base simulation loop and orchestration. · `simulation/simulator.py`
- [Enhanced Sim](https://github.com/havish06/savsd/blob/main/simulation/enhanced_simulator.py): adas loop · Sensor-first simulator with priority-aware control ownership and HUD. · `simulation/enhanced_simulator.py`
- [Traffic Gen](https://github.com/havish06/savsd/blob/main/simulation/traffic_generator.py): population · Creates and varies surrounding traffic. · `simulation/traffic_generator.py`

### World Model

Shared road, time, and signal state.

- [World](https://github.com/havish06/savsd/blob/main/world/world.py): shared state · Shared simulation state tying road, signals, and time together. · `world/world.py`
- [Road](https://github.com/havish06/savsd/blob/main/world/road.py): geometry · Lane layout and road boundaries. · `world/road.py`
- [Clock](https://github.com/havish06/savsd/blob/main/world/clock.py): timebase · Simulation time and progression. · `world/clock.py`
- [Traffic Light](https://github.com/havish06/savsd/blob/main/world/traffic_light.py): signal state · Traffic signal state used by the world and vehicles. · `world/traffic_light.py`

### Vehicles

Controlled ego vehicle and surrounding traffic participants.

- [Vehicle](https://github.com/havish06/savsd/blob/main/vehicles/vehicle.py): base model · Shared vehicle abstraction. · `vehicles/vehicle.py`
- [Ego Vehicle](https://github.com/havish06/savsd/blob/main/vehicles/ego_vehicle.py): controlled car · Main vehicle under ADAS control and intervention. · `vehicles/ego_vehicle.py`
- [NPC Vehicle](https://github.com/havish06/savsd/blob/main/vehicles/npc_vehicle.py): traffic actor · Other traffic participants and obstacles. · `vehicles/npc_vehicle.py`

### Sensors

Perception and safety signal generation.

- [Driver Monitor](https://github.com/havish06/savsd/blob/main/sensors/driver_monitor.py): drowsiness sensor · Eye-based monitoring for driver alertness. · `sensors/driver_monitor.py`
- [Alcohol Sensor](https://github.com/havish06/savsd/blob/main/sensors/alcohol_sensor.py): lockout sensor · Ignition lock and alcohol detection behavior. · `sensors/alcohol_sensor.py`
- [Distance Sensor](https://github.com/havish06/savsd/blob/main/sensors/distance_sensor.py): headway sensor · Adaptive cruise and following-distance checks. · `sensors/distance_sensor.py`
- [Lane Sensor](https://github.com/havish06/savsd/blob/main/sensors/lane_sensor.py): lane sensor · Lane position and lane keeping perception. · `sensors/lane_sensor.py`
- [Blind Spot](https://github.com/havish06/savsd/blob/main/sensors/blind_spot_sensor.py): lane-change sensor · Safety checks for adjacent-lane maneuvers. · `sensors/blind_spot_sensor.py`
- [Collision Warn](https://github.com/havish06/savsd/blob/main/sensors/collision_warning.py): hazard sensor · Immediate hazard detection and warning signals. · `sensors/collision_warning.py`

### Control Arbitration

Policy and assist logic that owns control when safety modules intervene.

- [State Machine](https://github.com/havish06/savsd/blob/main/decision/state_machine.py): policy arbiter · Prioritizes safety modules and selects current control owner. · `decision/state_machine.py`
- [Lane Change](https://github.com/havish06/savsd/blob/main/decision/lane_change.py): assist logic · Driver-confirmed lane-change assist with abort-on-hazard behavior. · `decision/lane_change.py`

### Input & Output

Driver commands, rendering, audio, and logging.

- [Driver Input](https://github.com/havish06/savsd/blob/main/input/driver_input.py): controls · Human commands for assist, lane change, speed, pause, reset, and exit. · `input/driver_input.py`
- [Renderer](https://github.com/havish06/savsd/blob/main/visualization/renderer.py): visualization · Renders the simulation scene and HUD. · `visualization/renderer.py`
- [Audio](https://github.com/havish06/savsd/blob/main/utils/audio.py): alerts · Safety alerts and warning output. · `utils/audio.py`
- [Logger](https://github.com/havish06/savsd/blob/main/utils/logger.py): telemetry · Records simulation and sensor events. · `utils/logger.py`

### Configuration

Runtime tuning and thresholds.

- [Config](https://github.com/havish06/savsd/blob/main/config/config.py): runtime config · Central simulation settings. · `config/config.py`
- [Sim Config](https://github.com/havish06/savsd/blob/main/config/sim_config.py): runtime config · Simulation-specific parameters. · `config/sim_config.py`
- [Thresholds](https://github.com/havish06/savsd/blob/main/config/thresholds.py): policy limits · Sensor and safety trigger limits. · `config/thresholds.py`

## Connections

- main.py → Config: loads · Reads runtime configuration.
- main.py → Enhanced Sim: starts · Bootstraps the ADAS simulation loop.
- run_simulator.py → Simulator: starts · Launches the base simulator.
- Enhanced Sim → Simulator: extends · Builds on the base loop.
- Simulator → World: updates · Advances shared world state each tick.
- Simulator → Traffic Gen: spawns · Populates and varies surrounding traffic.
- Simulator → Renderer: renders · Sends the current scene to the visualization layer.
- Enhanced Sim → State Machine: arbitrates · Delegates control ownership to the policy layer.
- Enhanced Sim → Audio: alerts · Emits safety warnings and notifications.
- Enhanced Sim → Logger: logs · Records events and intervention state.
- World → Road: contains · Holds lane geometry and road boundaries.
- World → Clock: tracks · Maintains simulation time.
- World → Traffic Light: contains · Stores signal state.
- World → Ego Vehicle: includes · Shared state for the controlled vehicle.
- World → NPC Vehicle: includes · Shared state for surrounding traffic.
- Ego Vehicle → Vehicle: inherits · Specializes the base vehicle model.
- NPC Vehicle → Vehicle: inherits · Specializes the base vehicle model.
- Driver Input → State Machine: requests · Human commands influence assist ownership and mode changes.
- Driver Input → Ego Vehicle: commands · Manual speed and control inputs can affect the ego vehicle.
- Driver Monitor → State Machine: signals · Drowsiness state can trigger intervention.
- Alcohol Sensor → State Machine: signals · Lockout conditions affect ignition/control.
- Distance Sensor → State Machine: signals · Headway risk informs adaptive cruise decisions.
- Lane Sensor → State Machine: signals · Lane position feeds lane keeping and assist logic.
- Blind Spot → Lane Change: checks · Adjacent-lane safety gate for lane changes.
- Collision Warn → State Machine: signals · Hazard detection can force safe stop or takeover.
- State Machine → Lane Change: delegates · Lane-change approval and abort flow.
- State Machine → Ego Vehicle: controls · Applies safe stop, lockout, or assist actions.
- Lane Change → Ego Vehicle: updates · Executes lane-change assist decisions.
- Renderer → World: reads · Visualizes the current simulation state.
- Renderer → State Machine: shows · Displays current control owner in the HUD.
- Audio → Collision Warn: reflects · Audio alerts correspond to safety warnings.
- Logger → State Machine: records · Persists arbitration and intervention events.
- Config → Sim Config: contains · Holds simulation settings.
- Config → Thresholds: contains · Defines policy thresholds and trigger limits.
- Sim Config → Enhanced Sim: configures · Shapes simulator behavior and modes.
- Thresholds → Driver Monitor: tunes · Sets drowsiness thresholds.
- Thresholds → Collision Warn: tunes · Sets hazard sensitivity.
- Thresholds → Distance Sensor: tunes · Sets headway and cruise limits.
