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.
┌─────────────────────┐ gRPC bidirectional stream ┌──────────────────────────┐
│ cmd/spawner │ ──────────────────────────────────────> │ cmd/manager │
│ random traffic │ │ IntersectionService │
└─────────┬───────────┘ │ VehicleRegistry │
│ uses │ ReserveOrSuggest │
▼ └──────────────────────────┘
┌─────────────────────┐ ▲
│ internal/vehicle │ ────────────────────────────────────────────────────┘
│ Simulate() │
└─────────────────────┘
▲
│
┌─────────┴───────────┐
│ cmd/agent │ single manually configured vehicle
└─────────────────────┘
- Every vehicle opens a
Negotiatestream with the manager. - At each simulation tick (about one second) the vehicle sends its telemetry:
vehicle_id,speed,eta,origin_lane,direction. - The manager checks the request against the vehicles already registered. Conflicting trajectories are separated by a safety margin of 2 seconds.
- The manager answers with one of:
STATUS_ACK_LOCK— the slot is free and has been reserved;STATUS_REJECT— the slot is busy;target_etacontains the next free time;STATUS_ADJUST_SPEED— reserved for future use, currently unused.
- On
STATUS_REJECTthe vehicle recomputes its speed to arrive attarget_eta. - When the vehicle reaches the intersection it closes the stream and the manager removes it from the registry.
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.
| 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 |
docker compose up --buildThis starts:
manageron port50051;spawner, which creates a new random vehicle every 1.5 seconds.
Follow the manager logs with:
docker compose logs -f managerTerminal 1 — manager:
go run ./cmd/managerTerminal 2 — traffic:
go run ./cmd/spawnerOr 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/agentOn Windows PowerShell use $env:NAME = "value" before go run.
| 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 |
| Variable | Default | Description |
|---|---|---|
MANAGER_ADDR |
localhost:50051 |
Manager address |
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.
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.protoOn Windows you can run proto.bat instead.
- #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/slogand negotiation metrics. - #26 — Configurable traffic scenarios for the spawner (bursts, manoeuvre mix, deterministic seed).
ReserveOrSuggestdoes 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.
Se il veicolo sta ancora rallentando (o accelerando) per raggiungere
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
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
Tempo ideale (