improved sim timings

This commit is contained in:
SowinskiBraeden committed 2026-11-13 00:40:56 -07:00
1 parent 76527587da
commit 15fb7449e6
3 files changed
+36 -25

No files matched your search

+23 -18
View File
@@ -1,24 +1,27 @@
import init, { Controller, Interval } from './pkg/traffic_lights.js'; 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 = [ const phases = [
{ id: 1, label: 'West left', path: 'M200 342H264C289 342 342 289 342 264V200', x: 196, y: 342, labelX: 166, labelY: 348 }, { 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 }, { 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 }, { 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 }, { 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 }, { 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 }, { 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 }, { 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 }, { 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); const byId = (id) => document.getElementById(id);
let controller; let controller;
let timer = null; let timer = null;
// Relative arrival likelihood per simulated second: main street busier than // Relative arrival likelihood per simulated second: through movements busier
// the side street, through movements busier than their paired left turn. // than their paired left turn, and the main street (1/2/5/6) only slightly
const ARRIVAL_CHANCE = { 1: .12, 2: .35, 3: .1, 4: .22, 5: .12, 6: .35, 7: .1, 8: .22 }; // busier than the side street (3/4/7/8) within each movement type.
const QUEUE_CAP = 4; 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; const TRANSIT_MS = 850;
// Each car is { id, phase, state: 'queued' | 'moving', moveStart? }. // Each car is { id, phase, state: 'queued' | 'moving', moveStart? }.
@@ -30,10 +33,12 @@ const cars = [];
function spawnRandomTraffic() { function spawnRandomTraffic() {
if (!byId('random-traffic').checked) return; if (!byId('random-traffic').checked) return;
for (const phase of phases) { for (const phase of phases) {
const queued = cars.filter((car) => car.phase === phase.id && car.state === 'queued').length; // The detector fires on the roll alone, same as a real vehicle arriving;
if (queued < QUEUE_CAP && Math.random() < ARRIVAL_CHANCE[phase.id]) { // QUEUE_CAP only limits how many waiting cars get drawn, never demand.
cars.push({ id: nextCarId++, phase: phase.id, state: 'queued' }); if (Math.random() < ARRIVAL_CHANCE[phase.id]) {
controller.call(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) => ` byId('phase-timing').innerHTML = phases.map((phase) => `
<tr> <tr>
<th scope="row">${phase.id}</th><td>${phase.label}</td> <th scope="row">${phase.id}</th><td>${phase.label}</td>
<td><input class="form-control form-control-sm" id="min-${phase.id}" aria-label="Phase ${phase.id} minimum green seconds" type="number" min="1" max="255" step="1" value="${phase.id % 2 ? 5 : 8}" required></td> <td><input class="form-control form-control-sm" id="min-${phase.id}" aria-label="Phase ${phase.id} minimum green seconds" type="number" min="1" max="255" step="1" value="${phase.min}" required></td>
<td><input class="form-control form-control-sm" id="max-${phase.id}" aria-label="Phase ${phase.id} maximum green seconds" type="number" min="1" max="255" step="1" value="${phase.id % 2 ? 15 : 25}" required></td> <td><input class="form-control form-control-sm" id="max-${phase.id}" aria-label="Phase ${phase.id} maximum green seconds" type="number" min="1" max="255" step="1" value="${phase.max}" required></td>
<td><input class="form-control form-control-sm" id="pass-${phase.id}" aria-label="Phase ${phase.id} passage seconds" type="number" min="1" max="255" step="1" value="3" required></td> <td><input class="form-control form-control-sm" id="pass-${phase.id}" aria-label="Phase ${phase.id} passage seconds" type="number" min="1" max="255" step="1" value="${phase.pass}" required></td>
</tr> </tr>
`).join(''); `).join('');
+1 -1
View File
@@ -131,7 +131,7 @@
</table> </table>
</div> </div>
<div class="d-flex flex-wrap align-items-end gap-3"> <div class="d-flex flex-wrap align-items-end gap-3">
<div><label class="form-label small" for="yellow">Yellow (s)</label><input id="yellow" class="form-control form-control-sm clearance-input" type="number" min="1" max="255" step="1" value="3" required></div> <div><label class="form-label small" for="yellow">Yellow (s)</label><input id="yellow" class="form-control form-control-sm clearance-input" type="number" min="1" max="255" step="1" value="4" required></div>
<div><label class="form-label small" for="red-clearance">Red clearance (s)</label><input id="red-clearance" class="form-control form-control-sm clearance-input" type="number" min="1" max="255" step="1" value="1" required></div> <div><label class="form-label small" for="red-clearance">Red clearance (s)</label><input id="red-clearance" class="form-control form-control-sm clearance-input" type="number" min="1" max="255" step="1" value="1" required></div>
<button id="apply" type="submit" class="btn btn-primary btn-sm" disabled>Apply &amp; reset</button> <button id="apply" type="submit" class="btn btn-primary btn-sm" disabled>Apply &amp; reset</button>
<span class="small text-secondary">Changes restart with 5 seconds of all red.</span> <span class="small text-secondary">Changes restart with 5 seconds of all red.</span>
+12 -6
View File
@@ -90,7 +90,7 @@ impl Phase {
min_seconds, min_seconds,
max_seconds, max_seconds,
pass_seconds, pass_seconds,
yellow_seconds: 3, yellow_seconds: 4,
red_seconds: 1, red_seconds: 1,
} }
} }
@@ -392,15 +392,21 @@ impl Controller {
#[wasm_bindgen] #[wasm_bindgen]
impl Controller { impl Controller {
pub fn demo() -> Self { 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) let phases = (1..=8)
.map(|number| { .map(|number| {
let id = PhaseId::new(number).unwrap(); 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, min, max, pass)
Phase::new(id, 8, 25, 3)
} else {
Phase::new(id, 5, 15, 3)
}
}) })
.collect(); .collect();
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.