# rishab-bansal/backend architecture

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

- Interactive diagram: https://gitdiagram.com/rishab-bansal/backend
- Repository: https://github.com/rishab-bansal/backend
- Video tour (about a minute): https://gitdiagram.com/rishab-bansal/backend/video

## Overview

This is a FastAPI web application for interactive cart-pendulum simulations, with LQR, sliding-mode, and uncontrolled strategies and a browser canvas display. The main workflow is to open a strategy page, start a simulation, compute the system evolution, stream state updates over a WebSocket, and render them. The README is empty; route and simulation wiring are supported by sampled source, while some template-to-script connections are inferred only from the file tree and are left out.

Main entry points: [`main.py`](https://github.com/rishab-bansal/backend/blob/main/main.py) (FastAPI app), [`projects/pend_on_cart/loop.py`](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/loop.py) (Simulation loop), [`static/script/main.js`](https://github.com/rishab-bansal/backend/blob/main/static/script/main.js) (WebSocket client), [`projects/pend_on_cart/controller.py`](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/controller.py) (Control strategies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web interface"]
  node_app["FastAPI app<br/>[main.py]"]
  node_home["Home page<br/>[home.html]"]
  node_strategy_pages["Strategy pages"]
  node_client["WebSocket client<br/>[main.js]"]
  node_state["Display state<br/>[sb_state.js]"]
  node_canvas["Canvas renderer<br/>[sb.js]"]
end

subgraph group_runtime["Simulation runtime"]
  node_simulator["Simulation loop<br/>[loop.py]"]
  node_chart["Simulation plot<br/>[loop.py]"]
end

subgraph group_physics["Control and physics"]
  node_controller["Control strategies<br/>[controller.py]"]
  node_robot["Pendulum model<br/>[system.py]"]
end

node_visitor(("Visitor"))

node_visitor -->|"opens pages"| node_app
node_app -->|"renders"| node_home
node_app -->|"renders"| node_strategy_pages
node_visitor -->|"starts simulation"| node_client
node_client -->|"sends context"| node_app
node_app -->|"dispatches simulation"| node_simulator
node_simulator -->|"selects strategy"| node_controller
node_controller -->|"queries dynamics"| node_robot
node_simulator -->|"creates model"| node_robot
node_simulator -->|"sends state updates"| node_app
node_app -->|"delivers updates"| node_client
node_client -->|"updates state"| node_state
node_canvas -->|"reads state"| node_state
node_simulator -->|"plots results"| node_chart

click node_app "https://github.com/rishab-bansal/backend/blob/main/main.py"
click node_home "https://github.com/rishab-bansal/backend/blob/main/templates/home.html"
click node_strategy_pages "https://github.com/rishab-bansal/backend/tree/main/templates/projects/self_balance"
click node_client "https://github.com/rishab-bansal/backend/blob/main/static/script/main.js"
click node_state "https://github.com/rishab-bansal/backend/blob/main/static/script/sb_state.js"
click node_canvas "https://github.com/rishab-bansal/backend/blob/main/static/script/sb.js"
click node_simulator "https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/loop.py"
click node_controller "https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/controller.py"
click node_robot "https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/system.py"
click node_chart "https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/loop.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_app,node_home,node_strategy_pages,node_client,node_state,node_canvas toneBlue
class node_simulator,node_chart toneAmber
class node_controller,node_robot toneMint
class node_visitor toneIndigo
```

## Components

### Web interface

- [FastAPI app](https://github.com/rishab-bansal/backend/blob/main/main.py): `main.py`
- [Home page](https://github.com/rishab-bansal/backend/blob/main/templates/home.html): `templates/home.html`
- [Strategy pages](https://github.com/rishab-bansal/backend/tree/main/templates/projects/self_balance): `templates/projects/self_balance`
- [WebSocket client](https://github.com/rishab-bansal/backend/blob/main/static/script/main.js): `static/script/main.js`
- [Display state](https://github.com/rishab-bansal/backend/blob/main/static/script/sb_state.js): `static/script/sb_state.js`
- [Canvas renderer](https://github.com/rishab-bansal/backend/blob/main/static/script/sb.js): `static/script/sb.js`

### Simulation runtime

- [Simulation loop](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/loop.py): `projects/pend_on_cart/loop.py`
- [Simulation plot](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/loop.py): `projects/pend_on_cart/loop.py`

### Control and physics

- [Control strategies](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/controller.py): `projects/pend_on_cart/controller.py`
- [Pendulum model](https://github.com/rishab-bansal/backend/blob/main/projects/pend_on_cart/system.py): `projects/pend_on_cart/system.py`

### Other components

- **Visitor**

## Connections

- Visitor → FastAPI app: opens pages
- FastAPI app → Home page: renders
- FastAPI app → Strategy pages: renders
- Visitor → WebSocket client: starts simulation
- WebSocket client → FastAPI app: sends context
- FastAPI app → Simulation loop: dispatches simulation
- Simulation loop → Control strategies: selects strategy
- Control strategies → Pendulum model: queries dynamics
- Simulation loop → Pendulum model: creates model
- Simulation loop → FastAPI app: sends state updates
- FastAPI app → WebSocket client: delivers updates
- WebSocket client → Display state: updates state
- Canvas renderer → Display state: reads state
- Simulation loop → Simulation plot: plots results
