# peaniz/military-defense-simulation architecture

> Architecture diagram of the GitHub repository peaniz/military-defense-simulation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/peaniz/military-defense-simulation
- Repository: https://github.com/peaniz/military-defense-simulation

## Overview

##### Overview of the Project
- This project is a web-based radar and object tracking system that integrates real-time data from an Arduino (via a radar sensor) with a web interface. The system supports two modes – Radar Mode (displaying sensor data) and Tracking Mode (tracking faces or hands via MediaPipe).
- The project includes a FastAPI backend in Python, a frontend built with HTML/CSS/JavaScript, and Arduino firmware (RadarAndFace.ino) for interacting with the sensor hardware.

##### Main Components
a. Arduino Hardware:
- Represented by the RadarAndFace.ino file, which is responsible for reading sensor data (angle, distance) and receiving coordinate inputs for servo control in Tracking Mode.

b. Python Backend (FastAPI Server):
- Represented by app.py and radar.py. This layer handles API endpoints, WebSocket connections, and serial communication with the Arduino.
- Elements such as the serial port communication (configured to COM port as discussed in the README) and integration with the Arduino.
- This component as "Backend Server (FastAPI)" with a note on its role as the orchestrator between hardware and the web client.

c. Web Frontend (Client Interface):
- Composed of the HTML template (index.html), CSS (styles.css), and several JavaScript files (detection.js, main.js, radar-display.js, radar.js, ui.js, and websocket.js) contained in the static folder.
- This layer is responsible for rendering the user interface, displaying the radar, and presenting tracking data via real-time WebSocket updates.

##### Relationships and Interactions
- The Arduino communicates sensor data (in Radar Mode) to the Backend Server via a Serial/COM port. In Tracking Mode, the Backend sends coordinate data to the Arduino.
- The FastAPI backend sets up WebSocket connections with the Web Client for real-time data updates (both for radar and tracking information).
- The Web Client fetches the initial HTML/CSS/JS content from the server and maintains a persistent WebSocket connection for dynamic updates.
- Directional flows:
- Arduino → Backend: Sensor data incoming via serial communication.
- Backend → Arduino: Control commands/coordinates to adjust hardware (servos/tracking).
- Web Client: Sends user commands or mode switches back to the Backend (if applicable).

##### Architectural Patterns and Design Principles
- The project follows a typical client-server model augmented with hardware interfaces.
- Use real-time communication (WebSockets) as a key design pattern for asynchronous updates between the server and the client.
- Separation of concerns: The backend handles hardware and business logic while the frontend is solely focused on the presentation and user interaction.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Hardware Layer"
        A["Arduino Radar & Servo Controller"]:::hardware
    end

    subgraph "Backend Layer"
        B["FastAPI Backend Server"]:::backend
        B1["app.py"]:::file
        B2["radar.py"]:::file
        B --> B1
        B --> B2
    end

    subgraph "Frontend Layer"
        C["Web Client (Browser Interface)"]:::client
        C1["templates/index.html"]:::file
        C2["static/css/styles.css"]:::file
        C3["static/js"]:::file
        C --> C1
        C --> C2
        C --> C3
    end

    A -->|"SensorData(Angle,Distance)"| B
    B -->|"CoordinateCommands(x,y)"| A
    B <-->|"WebSocketConnection-forRealTimeUpdates"| C

    click A "https://github.com/peaniz/military-defense-simulation/blob/master/RadarAndFace.ino"
    click B1 "https://github.com/peaniz/military-defense-simulation/blob/master/app.py"
    click B2 "https://github.com/peaniz/military-defense-simulation/blob/master/radar.py"
    click C1 "https://github.com/peaniz/military-defense-simulation/blob/master/templates/index.html"
    click C2 "https://github.com/peaniz/military-defense-simulation/blob/master/static/css/styles.css"
    click C3 "https://github.com/peaniz/military-defense-simulation/tree/master/static/js"

    classDef hardware fill:#FFA500,stroke:#333,stroke-width:2px;
    classDef backend fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef client fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef file fill:#FFFFFF,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
```
