From 15fb7449e6db486f32200ee3470cb2db2654e4a3 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Fri, 13 Nov 2026 00:40:56 -0700 Subject: [PATCH] improved sim timings --- app.js | 41 +++++++++++++++++++++++------------------ index.html | 2 +- src/lib.rs | 18 ++++++++++++------ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/app.js b/app.js index 5843528..80ab5f5 100644 --- a/app.js +++ b/app.js @@ -1,24 +1,27 @@ import init, { Controller, Interval } from './pkg/traffic_lights.js'; +// min/max/pass mirror Controller::demo()'s defaults in src/lib.rs, modeled +// after a Siemens M60 timing chart: main street > side street > left turns. const phases = [ - { id: 1, label: 'West left', path: 'M200 342H264C289 342 342 289 342 264V200', x: 196, y: 342, labelX: 166, labelY: 348 }, - { id: 2, label: 'East through', path: 'M440 254H200', x: 444, y: 254, labelX: 474, labelY: 260 }, - { id: 3, label: 'North left', path: 'M298 200V264C298 289 351 342 376 342H440', x: 298, y: 196, labelX: 298, labelY: 168 }, - { id: 4, label: 'South through', path: 'M386 440V200', x: 386, y: 444, labelX: 386, labelY: 483 }, - { id: 5, label: 'East left', path: 'M440 298H376C351 298 298 351 298 376V440', x: 444, y: 298, labelX: 474, labelY: 304 }, - { id: 6, label: 'West through', path: 'M200 386H440', x: 196, y: 386, labelX: 166, labelY: 392 }, - { id: 7, label: 'South left', path: 'M342 440V376C342 351 289 298 264 298H200', x: 342, y: 444, labelX: 342, labelY: 483 }, - { id: 8, label: 'North through', path: 'M254 200V440', x: 254, y: 196, labelX: 254, labelY: 168 }, + { id: 1, label: 'West left', path: 'M200 342H264C289 342 342 289 342 264V200', x: 196, y: 342, labelX: 166, labelY: 348, min: 5, max: 20, pass: 3 }, + { id: 2, label: 'East through', path: 'M440 254H200', x: 444, y: 254, labelX: 474, labelY: 260, min: 15, max: 50, pass: 4 }, + { id: 3, label: 'North left', path: 'M298 200V264C298 289 351 342 376 342H440', x: 298, y: 196, labelX: 298, labelY: 168, min: 5, max: 15, pass: 3 }, + { id: 4, label: 'South through', path: 'M386 440V200', x: 386, y: 444, labelX: 386, labelY: 483, min: 10, max: 35, pass: 3 }, + { id: 5, label: 'East left', path: 'M440 298H376C351 298 298 351 298 376V440', x: 444, y: 298, labelX: 474, labelY: 304, min: 5, max: 20, pass: 3 }, + { id: 6, label: 'West through', path: 'M200 386H440', x: 196, y: 386, labelX: 166, labelY: 392, min: 15, max: 50, pass: 4 }, + { id: 7, label: 'South left', path: 'M342 440V376C342 351 289 298 264 298H200', x: 342, y: 444, labelX: 342, labelY: 483, min: 5, max: 15, pass: 3 }, + { id: 8, label: 'North through', path: 'M254 200V440', x: 254, y: 196, labelX: 254, labelY: 168, min: 10, max: 35, pass: 3 }, ]; const byId = (id) => document.getElementById(id); let controller; let timer = null; -// Relative arrival likelihood per simulated second: main street busier than -// the side street, through movements busier than their paired left turn. -const ARRIVAL_CHANCE = { 1: .12, 2: .35, 3: .1, 4: .22, 5: .12, 6: .35, 7: .1, 8: .22 }; -const QUEUE_CAP = 4; +// Relative arrival likelihood per simulated second: through movements busier +// than their paired left turn, and the main street (1/2/5/6) only slightly +// busier than the side street (3/4/7/8) within each movement type. +const ARRIVAL_CHANCE = { 1: .13, 2: .27, 3: .11, 4: .22, 5: .13, 6: .27, 7: .11, 8: .22 }; +const QUEUE_CAP = 6; const TRANSIT_MS = 850; // Each car is { id, phase, state: 'queued' | 'moving', moveStart? }. @@ -30,10 +33,12 @@ const cars = []; function spawnRandomTraffic() { if (!byId('random-traffic').checked) return; for (const phase of phases) { - const queued = cars.filter((car) => car.phase === phase.id && car.state === 'queued').length; - if (queued < QUEUE_CAP && Math.random() < ARRIVAL_CHANCE[phase.id]) { - cars.push({ id: nextCarId++, phase: phase.id, state: 'queued' }); + // The detector fires on the roll alone, same as a real vehicle arriving; + // QUEUE_CAP only limits how many waiting cars get drawn, never demand. + if (Math.random() < ARRIVAL_CHANCE[phase.id]) { controller.call(phase.id); + const queued = cars.filter((car) => car.phase === phase.id && car.state === 'queued').length; + if (queued < QUEUE_CAP) cars.push({ id: nextCarId++, phase: phase.id, state: 'queued' }); } } } @@ -131,9 +136,9 @@ function buildControls() { byId('phase-timing').innerHTML = phases.map((phase) => ` ${phase.id}${phase.label} - - - + + + `).join(''); diff --git a/index.html b/index.html index 7ae5ce5..cfb3e1b 100644 --- a/index.html +++ b/index.html @@ -131,7 +131,7 @@
-
+
Changes restart with 5 seconds of all red. diff --git a/src/lib.rs b/src/lib.rs index dcb5dbe..2582bcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,7 +90,7 @@ impl Phase { min_seconds, max_seconds, pass_seconds, - yellow_seconds: 3, + yellow_seconds: 4, red_seconds: 1, } } @@ -392,15 +392,21 @@ impl Controller { #[wasm_bindgen] impl Controller { pub fn demo() -> Self { + // Modeled after a Siemens M60 default timing chart: the main street + // (2/6) carries the most green, the side street (4/8) less, and both + // left-turn pairs (1/5, 3/7) get short, actuated-only allocations. let phases = (1..=8) .map(|number| { let id = PhaseId::new(number).unwrap(); + let (min, max, pass) = match number { + 2 | 6 => (15, 50, 4), + 4 | 8 => (10, 35, 3), + 1 | 5 => (5, 20, 3), + 3 | 7 => (5, 15, 3), + _ => unreachable!(), + }; - if number % 2 == 0 { - Phase::new(id, 8, 25, 3) - } else { - Phase::new(id, 5, 15, 3) - } + Phase::new(id, min, max, pass) }) .collect();