# pks716/self_driving_car_js architecture

> Architecture diagram of the GitHub repository pks716/self_driving_car_js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pks716/self_driving_car_js
- Repository: https://github.com/pks716/self_driving_car_js

## Overview

##### Project Type & Purpose
- The project is a self-contained front-end simulation of a self-driving car.
- It uses a basic neural network (input, hidden, and output layers) to determine movement (left, right, forward, reverse).
- The simulation brings together UI components, simulation logic, sensor inputs, and neural network-driven decision making.

##### Analyzing the File Structure
- The entire project resides in the “Self Driving Car V1” directory.
- Core JavaScript files include:
- car.js: likely managing car behavior or state.
- controls.js: handling user controls or simulation inputs.
- network.js: implementing the neural network logic.
- sensor.js: simulating sensor data input for the car.
- road.js: managing road geometry or track simulation.
- main.js: serving as the entry point, orchestrating component interactions.
- visualizer.js: handling the visualization of car movements or network outputs.
- utils.js: providing generic helper functions.
- index.html and style.css form the front-end UI of the simulation.

##### Key Components
a. User Interface:
- “index.html” and “style.css”
- Acts as the presentation layer and possibly a dashboard/visual output for the simulation.

b. Simulation Core / Controller:
- “main.js” acts as the central coordinator, linking together other modules.

c. Car & Environment Modules:
- car.js: encapsulates car properties and behaviors.
- road.js: simulates the environment (the road).

d. Input & Sensor Modules:
- controls.js: facilitates user or simulation input.
- sensor.js: simulates sensor readings that feed into the neural network.

e. Decision Making (Neural Network):
- network.js: represents the internal three-layer neural network that processes inputs and produces driving decisions.
- This module takes sensor data and, following the neural network logic, maps the outputs to one of four movement directions.

f. Visualization:
- visualizer.js: renders simulation states, possibly drawing the car, roads, and sensor inputs for debugging or demonstration purposes.

g. Support Utilities:
- utils.js: handles common helper functions needed across multiple modules.

##### Relationships & Interactions
- Data Flow:
- The simulation starts at the UI level, possibly with user interactions via “controls.js” which feed into “main.js.”
- “main.js” then drives the simulation by invoking sensor readings (“sensor.js”) and the state of the simulation environment (from “road.js” and “car.js”).
- Sensor data is fed into “network.js” (the neural network), which processes it to determine the next move.
- The decision (output layer: left, right, forward, reverse) is applied to “car.js” to update the car’s state, which in turn influences the visualization.
- “visualizer.js” picks up changes and reflects them graphically in the UI.

- Inter-Module Dependencies:
- “utils.js” supports various modules.

##### Architectural Patterns
- Separation of Concerns: Each module has a specific responsibility (UI, sensor input, neural network processing, environmental simulation).
- Centralized Control: “main.js” functions as the orchestrator.
- MVC-like Elements: Though informal, you have a view (visualizer and HTML/CSS), a model (car, road, sensor), and a control (neural network and controls logic).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "User Interface Layer"
        UI1("index.html"):::ui
        UI2("style.css"):::ui
    end

    %% Visualization Layer
    subgraph "Visualization"
        V("visualizer.js"):::ui
    end

    %% Simulation Core
    subgraph "Simulation Core"
        M("main.js"):::sim
    end

    %% Input & Sensor Modules
    subgraph "Input & Sensor"
        C("controls.js"):::sim
        S("sensor.js"):::sim
    end

    %% Decision Making (Neural Network)
    subgraph "Neural Network"
        N("network.js<br>3 Layers: Input,Hidden,Output"):::neural
    end

    %% Car & Environment Modules
    subgraph "Car & Environment"
        Car("car.js"):::sim
        R("road.js"):::sim
    end

    %% Support Utilities
    U("utils.js"):::util

    %% Data Flow Connections
    UI1 -->|"UserInput"| C
    C -->|"ActivateSensors"| S
    S -->|"SensorData"| N
    N -->|"MovementCommand"| Car
    Car -->|"UpdateState"| V
    V -->|"Render"| UI1

    %% Orchestration by Simulation Core
    M -->|"InitControls"| C
    M -->|"InitSensors"| S
    M -->|"UpdateCar"| Car
    M -->|"ManageRoad"| R
    M -->|"UpdateVisualization"| V

    %% Support utilities used by various modules (dashed lines)
    M -.->|"uses"| U
    C -.->|"uses"| U
    S -.->|"uses"| U
    N -.->|"uses"| U
    Car -.->|"uses"| U
    R -.->|"uses"| U
    V -.->|"uses"| U

    %% Styles
    classDef ui fill:#CFE2F3,stroke:#000,stroke-width:1px;
    classDef sim fill:#FCE5CD,stroke:#000,stroke-width:1px;
    classDef neural fill:#D9EAD3,stroke:#000,stroke-width:1px;
    classDef util fill:#F4CCCC,stroke:#000,stroke-width:1px;

    %% Click Events
    click UI1 "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/index.html"
    click UI2 "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/style.css"
    click M "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/main.js"
    click Car "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/car.js"
    click R "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/road.js"
    click C "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/controls.js"
    click S "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/sensor.js"
    click N "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/network.js"
    click V "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/visualizer.js"
    click U "https://github.com/pks716/self_driving_car_js/blob/main/Self Driving Car V1/utils.js"
```
