Skip to content

About

A distributed V2X (Vehicle-to-Everything) simulation engine. AIM replaces traditional time-based traffic lights with a real-time, reservation-based consensus protocol. Built with Go and gRPC, this project demonstrates how autonomous vehicles can dynamically negotiate intersection access to maximize throughput and ensure zero collisions.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

40 Commits

Folders and files

Repository files navigation

Autonomous Intersection Manager (AIM)

A distributed V2X (Vehicle-to-Everything) simulation engine. AIM replaces traditional time-based traffic lights with a real-time, reservation-based protocol: autonomous vehicles negotiate access to a four-way intersection with a central manager over a bidirectional gRPC stream.

Built with Go and gRPC, the project is meant to demonstrate how vehicles can dynamically reserve time slots in order to maximise throughput and avoid collisions.


How it works

┌─────────────────────┐        gRPC bidirectional stream        ┌──────────────────────────┐
│  cmd/spawner        │ ──────────────────────────────────────> │  cmd/manager             │
│  random traffic     │                                         │  IntersectionService     │
└─────────┬───────────┘                                         │  VehicleRegistry         │
          │ uses                                                │  ReserveOrSuggest        │
          ▼                                                     └──────────────────────────┘
┌─────────────────────┐                                                     ▲
│ internal/vehicle    │ ────────────────────────────────────────────────────┘
│ Simulate()          │
└─────────────────────┘
          ▲
          │
┌─────────┴───────────┐
│  cmd/agent          │  single manually configured vehicle
└─────────────────────┘
  1. Every vehicle opens a Negotiate stream with the manager.
  2. At each simulation tick (about one second) the vehicle sends its telemetry: vehicle_id, speed, eta, origin_lane, direction.
  3. The manager checks the request against the vehicles already registered. Conflicting trajectories are separated by a safety margin of 2 seconds.
  4. The manager answers with one of:
    • STATUS_ACK_LOCK — the slot is free and has been reserved;
    • STATUS_REJECT — the slot is busy; target_eta contains the next free time;
    • STATUS_ADJUST_SPEED — reserved for future use, currently unused.
  5. On STATUS_REJECT the vehicle recomputes its speed to arrive at target_eta.
  6. When the vehicle reaches the intersection it closes the stream and the manager removes it from the registry.

Conflict rules

arePathsConflicting currently implements a simplified geometry:

Situation Conflict
Same origin lane Yes
Opposite lanes, both going straight No
Any other combination Yes

Extending this matrix to all real turn movements is tracked in issue #22.


Repository layout

Path Description
cmd/manager/main.go gRPC server, vehicle registry, conflict logic
cmd/manager/main_test.go End-to-end and concurrency tests
cmd/agent/main.go Manually configured single vehicle
cmd/spawner/main.go Random traffic generator for stress tests
internal/vehicle/vehicle.go Shared gRPC client / simulation loop
proto/intersection.proto gRPC contract
proto/intersection.pb.go Generated protobuf messages
proto/intersection_grpc.pb.go Generated gRPC stubs
proto.bat Windows helper to regenerate the stubs
docker-compose.yml Manager + spawner stack
.github/workflows/ci.yml Build, vet and race tests on GitHub Actions

Quick start

Docker

docker compose up --build

This starts:

  • manager on port 50051;
  • spawner, which creates a new random vehicle every 1.5 seconds.

Follow the manager logs with:

docker compose logs -f manager

Local (Go 1.26+)

Terminal 1 — manager:

go run ./cmd/manager

Terminal 2 — traffic:

go run ./cmd/spawner

Or run a single manually configured vehicle:

MANAGER_ADDR=localhost:50051 \
VEHICLE_ID=CAR_TEST \
START_SPEED=40 \
START_DISTANCE=400 \
ORIGIN_LANE=NORTH \
DIRECTION=STRAIGHT \
go run ./cmd/agent

On Windows PowerShell use $env:NAME = "value" before go run.


Configuration

cmd/agent

Variable Default Description
MANAGER_ADDR localhost:50051 Manager address
VEHICLE_ID CAR_MANUAL Vehicle identifier
START_SPEED 40.0 Initial speed in m/s
START_DISTANCE 400.0 Distance from the intersection in metres
ORIGIN_LANE UNKNOWN NORTH, SOUTH, EAST, WEST
DIRECTION UNKNOWN STRAIGHT, LEFT, RIGHT

cmd/spawner

Variable Default Description
MANAGER_ADDR localhost:50051 Manager address

Tests

go test ./...
go test -race ./...

The test suite currently covers:

  • a full end-to-end negotiation over an in-memory gRPC connection;
  • concurrent reservations of the same slot by 10 goroutines;
  • two simultaneous gRPC clients competing for the same ETA.

The manager test uses a real TCP port (50052) for the concurrent gRPC test, so that port must be free when the tests run.


Regenerating the protobuf stubs

Requirements: protoc, protoc-gen-go, protoc-gen-go-grpc.

protoc \
  --go_out=. --go_opt=paths=source_relative \
  --go-grpc_out=. --go-grpc_opt=paths=source_relative \
  proto/intersection.proto

On Windows you can run proto.bat instead.


Open issues / roadmap

  • #22 — Extend the trajectory conflict matrix to all turn movements.
  • #23 — Realistic acceleration/deceleration for the vehicle agent.
  • #24 — Preemption and priority for emergency vehicles.
  • #25 — Structured logging with log/slog and negotiation metrics.
  • #26 — Configurable traffic scenarios for the spawner (bursts, manoeuvre mix, deterministic seed).

Known limitations

  • ReserveOrSuggest does not currently exclude the requesting vehicle from the conflict scan. A vehicle that renegotiates on the same stream can therefore conflict with its own previous reservation. A multi-tick regression test is still missing.
  • Reservations are kept for the whole trip (from the initial distance to the intersection), not only while the vehicle occupies the intersection.
  • Rejected requests are not queued, so the protocol is not strictly FIFO.
  • The simulation tick is not compensated for gRPC latency or scheduling delays.
  • gRPC uses insecure credentials: the project is intended for local simulation only.

Calcolo ETA

Se il veicolo sta ancora rallentando (o accelerando) per raggiungere $v_{\text{target}}$, inviare al Manager ETA = d / v sarebbe impreciso perché $v$ cambierà nei prossimi secondi. Calcoliamo invece il tempo reale diviso in due fasi:Tempo per raggiungere $v_{\text{target}}$:$$t_{\text{ramp}} = \frac{\vert{}v_{\text{target}} - v\vert{}}{a}$$(dove $a = \text{maxAccel}$ se $v_{\text{target}} > v$, oppure $a = \text{maxDecel}$ se $v_{\text{target}} < v$).Distanza percorsa durante la rampa:$$d_{\text{ramp}} = \frac{v + v_{\text{target}}}{2} \cdot t_{\text{ramp}}$$Due casi possibili:Caso 1 ($d_{\text{ramp}} \ge d$): Il veicolo raggiunge l'incrocio mentre sta ancora accelerando o frenando. Risolviamo l'equazione del moto $d = v \cdot t + \frac{1}{2} a_{\text{signed}} t^2$:$$\text{ETA} = \frac{-v + \sqrt{v^2 + 2 \cdot a_{\text{signed}} \cdot d}}{a_{\text{signed}}}$$Caso 2 ($d_{\text{ramp}} < d$): Il veicolo completa la rampa in $t_{\text{ramp}}$ e percorre la distanza rimanente $(d - d_{\text{ramp}})$ a velocità costante $v_{\text{target}}$:$$\text{ETA} = t_{\text{ramp}} + \frac{d - d_{\text{ramp}}}{v_{\text{target}}}$$

Modello Fisico della Reservation Grid

Per superare il limite del margine fisso da 2.0s ed evitare sovrapposizioni reali nella simulazione, definiamo:Lunghezza geometrica della manovra dentro l'incrocio (assumendo un incrocio standard largo $W = 14\text{ m}$):Svolta a destra (DIR_RIGHT): arco corto $\approx 9.0\text{ m}$ (impegna 1 quadrante).Attraversamento dritto (DIR_STRAIGHT): tratto rettilineo $= 14.0\text{ m}$ (impegna 2 quadranti).Svolta a sinistra (DIR_LEFT): arco ampio $\approx 18.5\text{ m}$ (impegna 3 quadranti e dura più a lungo a parità di velocità).Tempo di attraversamento + Safety Buffer dinamico:Durata fisica: $t_{\text{cross}} = \frac{L_{\text{manovra}} + L_{\text{veicolo}}}{v_{\text{ingresso}}}$ (con velocità minima di sicurezza $v_{\min} = 5\text{ m/s}$ per evitare divisioni per zero o stalli infiniti).Margine di sicurezza dinamico (tempo di reazione + frenata residua): $\text{buffer} = 0.5\text{ s} + \frac{L_{\text{veicolo}}}{v_{\text{ingresso}}}$.L'intervallo prenotato sui quadranti impegnati dalla traiettoria diventa esattamente:$$[\text{entryTimeMs},\ \text{exitTimeMs} + \text{bufferMs}]$$Risoluzione dei conflitti (Sliding Window):Due prenotazioni vanno in conflitto se e solo se condividono almeno un quadrante (calcolato dalla bitmask di conflict.go) e i loro intervalli assoluti si sovrappongono (startA < endB && startB < endA).Se c'è conflitto, l'algoritmo sposta entryTimeMs subito dopo endB e ripete la scansione fino a trovare il primo corridoio temporale completamente libero.

Parametri cmd/spawner/main.go

Parametri configurabili sia via Flag CLI che via Variabili d'Ambiente (con precedenza ai flag CLI):

-profile / SPAWNER_PROFILE: profili predefiniti normal (default), burst (ondate simultanee su più corsie per forzare conflitti sullo stesso ETA) e high_congestion (intervalli stretti, alta densità e molte svolte a sinistra).

-seed / SPAWNER_SEED: seed deterministico (default 42).

-interval / SPAWNER_INTERVAL: intervallo base tra arrivi/ondate (es. 1500ms).

-turn-ratio / SPAWNER_TURN_RATIO: probabilità complessiva di svolta (0.0 = solo dritti, 1.0 = solo svolte, bilanciate tra sinistra e destra).

-emergency-ratio / SPAWNER_EMERGENCY_RATIO: probabilità di mezzo di emergenza (default 0.10).

-burst-size / SPAWNER_BURST_SIZE: quanti veicoli generare simultaneamente ad ogni tick (es. 1 in normal, 3 in burst).

-max-vehicles / SPAWNER_MAX_VEHICLES: numero massimo di veicoli da generare prima di attendere il completamento e terminare (0 = infinito, utile nei benchmark).

Profilo high_congestion + Script rapido:

Impostando -profile=high_congestion (o SPAWNER_PROFILE=high_congestion), lo spawner configura automaticamente un intervallo di 350ms, burst-size=2, turn-ratio=0.55 e distanze ravvicinate (180m–320m) per portare la Reservati

Metriche

Tempo ideale ($t_{\text{ideal}}$) vs Tempo effettivo ($t_{\text{actual}}$) e Delay:Al primo pacchetto ricevuto per un vehicle_id (firstSeenAt), registriamo la distanza iniziale $d_0 = \text{DistanceM} + 14.0 + \text{VehicleLengthM}$ (fino all'uscita fisica dall'incrocio) e la velocità iniziale $v_0 = \text{Speed}$.Tempo ideale in flusso libero: $t_{\text{ideal}} = \frac{d_0}{v_0}$ (se $d_0 &gt; 0, v_0 &gt; 0$).Quando HasExited == true (o al completamento pulito dello stream), calcoliamo $t_{\text{actual}} = (t_{\text{exit}} - t_{\text{first}})$ in secondi e il ritardo $\text{Delay} = \max(0, t_{\text{actual}} - t_{\text{ideal}})$.Fermate complete (total_stops):Un veicolo subisce una fermata completa quando la sua velocità scende sotto 0.5 m/s (Speed < 0.5) mentre non è ancora uscito (DistanceM > 0), usando un flag booleano wasStopped per contare ogni episodio di arresto completo (se riparte e si ferma di nuovo in coda, conta 2).Indice di penalità energetica / consumo (energy_penalty_index):Nel traffico urbano i picchi di consumo e usura derivano dalle variazioni di energia cinetica ($\Delta v^2$) durante frenate e riaccelerazioni. Per ogni coppia di tick consecutivi $(v_{t-1}, v_t)$ sommiamo il lavoro specifico di variazione velocità $\sum \vert{}v_t^2 - v_{t-1}^2\vert{} / 100$ (normalizzato per veicolo completato). In flusso costante (AIM senza conflitti) vale 0.0; al semaforo con frenate da $35\text{ m/s} \to 0 \to 35\text{ m/s}$ cresce drasticamente.Separazione Veicoli Ordinari vs Emergenze (is_emergency):Calcoliamo separatamente emergency_avg_time_sec (tempo medio di attraversamento $t_{\text{actual}}$ delle ambulanze) e emergency_avg_delay_sec, oltre a ordinary_avg_delay_sec e avg_delay_sec globale.Esportazione automatica JSON (results_.json):All'arresto del Manager (PrintSummary() / ExportJSON()), scriviamo automaticamente results_aim.json oppure results_traffic_light.json (sovrascrivibile via flag -metrics-out o env METRICS_OUT) nella directory corrente.

About

A distributed V2X (Vehicle-to-Everything) simulation engine. AIM replaces traditional time-based traffic lights with a real-time, reservation-based consensus protocol. Built with Go and gRPC, this project demonstrates how autonomous vehicles can dynamically negotiate intersection access to maximize throughput and ensure zero collisions.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages