update arch for ring-barrier cycles

This commit is contained in:
SowinskiBraeden committed 2026-11-13 09:28:12 -07:00
1 parent 15fb7449e6
commit 24fc9e649e
2 files changed
+263 -180

No files matched your search

+1 -1
View File
@@ -141,7 +141,7 @@
</details> </details>
<footer class="d-flex align-items-center justify-content-between gap-3 small text-secondary pb-2"> <footer class="d-flex align-items-center justify-content-between gap-3 small text-secondary pb-2">
<span>Fixed pairs: 1/5 → 2/6 → 3/7 → 4/8</span> <span>Ring 1: 1 → 2 | 3 → 4 &middot; Ring 2: 5 → 6 | 7 → 8</span>
<div class="d-flex align-items-center gap-2"> <div class="d-flex align-items-center gap-2">
<svg class="icon" aria-hidden="true"><use href="./icons.svg#chip" /></svg> <svg class="icon" aria-hidden="true"><use href="./icons.svg#chip" /></svg>
<span>Rust/WebAssembly demo</span> <span>Rust/WebAssembly demo</span>
+262 -179
View File
@@ -5,11 +5,12 @@ use wasm_bindgen::prelude::*;
* Descriptive comments/inspuration sourced from here: * Descriptive comments/inspuration sourced from here:
* https://www.cedengineering.com/userfiles/Introduction%20to%20Traffic%20Signal%20Phasing-R1.pdf * https://www.cedengineering.com/userfiles/Introduction%20to%20Traffic%20Signal%20Phasing-R1.pdf
* *
* This first version uses four fixed pairs: * Dual-ring, single-barrier operation:
* ring 1: 1 -> 2 | 3 -> 4 * ring 1: 1 -> 2 | 3 -> 4
* ring 2: 5 -> 6 | 7 -> 8 * ring 2: 5 -> 6 | 7 -> 8
* The vertical line marks the main/side street barrier. * The vertical line marks the main/side street barrier. Within a barrier group
* Each pair completes green, yellow and red clearance together. * each ring advances on its own (e.g. 1/5 -> 2/5 -> 2/6 is normal), but both
* rings must park at the barrier before either can cross into the next group.
*/ */
/* /*
@@ -96,34 +97,6 @@ impl Phase {
} }
} }
/*
* A phase set is defined as any unique combination of
* vehicle or pedestrian phases that occur at the same time.
*
* Two phases are said to be simultaneous phases when they operate together for the
* same period of time. When one of the phases turns green, so does the other phase, and
* when one of the phases turns yellow, so does the other phase.
* Two phases are said to be concurrent phases when they operate together for some
* period of time, but not necessarily the same period of time
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PhaseSetId(u8);
#[derive(Clone)]
pub struct PhaseSet {
id: PhaseSetId,
phases: Vec<PhaseId>,
}
impl PhaseSet {
pub fn new(id: u8, first: PhaseId, second: PhaseId) -> Self {
Self {
id: PhaseSetId(id),
phases: vec![first, second],
}
}
}
/* /*
* u8 bits represents phases 1-8 * u8 bits represents phases 1-8
* each bit indicates whether recall is active * each bit indicates whether recall is active
@@ -191,14 +164,66 @@ impl TimingChart {
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Interval { pub enum Interval {
// Also doubles as "parked at the barrier, waiting on the other ring" and
// the pre-startup state, since both show the same red lamp to a viewer.
Red, Red,
Green, Green,
Yellow, Yellow,
RedClearance, RedClearance,
} }
// Which side of the barrier a ring is currently serving.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Group {
A,
B,
}
impl Group {
fn flip(self) -> Self {
match self {
Group::A => Group::B,
Group::B => Group::A,
}
}
}
// Which of the two phases within the current barrier group a ring is on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Slot {
First,
Second,
}
// Ring 1 carries phases 1-4, ring 2 carries phases 5-8; group A is the first
// phase pair in each ring (before the barrier), group B is the second.
fn ring_phase_number(ring_index: usize, group: Group, slot: Slot) -> u8 {
match (ring_index, group, slot) {
(0, Group::A, Slot::First) => 1,
(0, Group::A, Slot::Second) => 2,
(0, Group::B, Slot::First) => 3,
(0, Group::B, Slot::Second) => 4,
(1, Group::A, Slot::First) => 5,
(1, Group::A, Slot::Second) => 6,
(1, Group::B, Slot::First) => 7,
(1, Group::B, Slot::Second) => 8,
_ => unreachable!("only two rings, two groups and two slots exist"),
}
}
// Bitmask of the four phases that belong to one barrier group (both rings).
fn group_mask(group: Group) -> u8 {
let numbers: [u8; 4] = match group {
Group::A => [1, 2, 5, 6],
Group::B => [3, 4, 7, 8],
};
numbers
.iter()
.fold(0, |mask, &number| mask | PhaseId::new(number).unwrap().bit())
}
struct RingState { struct RingState {
phase: PhaseId, slot: Slot,
interval: Interval, interval: Interval,
elapsed_seconds: u32, elapsed_seconds: u32,
gap_remaining: u32, gap_remaining: u32,
@@ -207,9 +232,9 @@ struct RingState {
} }
impl RingState { impl RingState {
fn new(phase: PhaseId) -> Self { fn parked() -> Self {
Self { Self {
phase, slot: Slot::First,
interval: Interval::Red, interval: Interval::Red,
elapsed_seconds: 0, elapsed_seconds: 0,
gap_remaining: 0, gap_remaining: 0,
@@ -217,6 +242,17 @@ impl RingState {
ready: false, ready: false,
} }
} }
fn green(slot: Slot) -> Self {
Self {
slot,
interval: Interval::Green,
elapsed_seconds: 0,
gap_remaining: 0,
maximum_elapsed: None,
ready: false,
}
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -226,9 +262,6 @@ pub struct IntersectionId(usize);
pub struct Controller { pub struct Controller {
id: IntersectionId, id: IntersectionId,
phases: Vec<Phase>, phases: Vec<Phase>,
// A traffic signal cycle is one complete set of signal phase sets.
// also can represent, phase rotation (phasing sequence): order of phase sets on conflicting calls
rotation: Vec<PhaseSet>,
timing_chart: TimingChart, timing_chart: TimingChart,
// external monitor checks controller for bad output (conflicts) // external monitor checks controller for bad output (conflicts)
@@ -247,8 +280,8 @@ pub struct Controller {
startup_remaining: u32, startup_remaining: u32,
// manual control of the phase set timing by input // manual control of the phase set timing by input
// manual: bool, // manual: bool,
group: Group,
rings: [RingState; 2], rings: [RingState; 2],
current_set: usize,
/* /*
* For a phase to be serviced it must first receive a call for service. A vehicle call is a * For a phase to be serviced it must first receive a call for service. A vehicle call is a
* demand for the green interval of a vehicle phase while a pedestrian call is a demand * demand for the green interval of a vehicle phase while a pedestrian call is a demand
@@ -259,7 +292,7 @@ pub struct Controller {
} }
impl Controller { impl Controller {
pub fn new(id: usize, phases: Vec<Phase>, rotation: Vec<PhaseSet>) -> Self { pub fn new(id: usize, phases: Vec<Phase>) -> Self {
assert_eq!(phases.len(), 8, "Configure phases 1 through 8"); assert_eq!(phases.len(), 8, "Configure phases 1 through 8");
for number in 1..=8 { for number in 1..=8 {
@@ -267,30 +300,16 @@ impl Controller {
assert_eq!(count, 1, "Each phase must be configured exactly once"); assert_eq!(count, 1, "Each phase must be configured exactly once");
} }
let pairs = [[1, 5], [2, 6], [3, 7], [4, 8]];
assert_eq!(rotation.len(), pairs.len());
for (set, pair) in rotation.iter().zip(pairs) {
assert_eq!(set.phases.len(), 2);
assert_eq!(set.phases[0].0, pair[0]);
assert_eq!(set.phases[1].0, pair[1]);
}
let rings = [
RingState::new(rotation[0].phases[0]),
RingState::new(rotation[0].phases[1]),
];
let startup_seconds = 5; let startup_seconds = 5;
Self { Self {
id: IntersectionId(id), id: IntersectionId(id),
phases, phases,
rotation,
timing_chart: TimingChart::new(), timing_chart: TimingChart::new(),
startup_seconds, startup_seconds,
startup_remaining: u32::from(startup_seconds), startup_remaining: u32::from(startup_seconds),
rings, group: Group::A,
current_set: 0, rings: [RingState::parked(), RingState::parked()],
pending_calls: 0, pending_calls: 0,
elapsed_seconds: 0, elapsed_seconds: 0,
} }
@@ -300,64 +319,76 @@ impl Controller {
self.phases.iter().find(|phase| phase.id == id).unwrap() self.phases.iter().find(|phase| phase.id == id).unwrap()
} }
fn set_mask(&self, index: usize) -> u8 {
self.rotation[index]
.phases
.iter()
.fold(0, |mask, phase| mask | phase.bit())
}
fn demand(&self) -> u8 { fn demand(&self) -> u8 {
self.pending_calls | self.timing_chart.min_recall self.pending_calls | self.timing_chart.min_recall
} }
fn has_conflicting_call(&self) -> bool { // A ring should stop resting and start timing toward ending its current
self.demand() & !self.set_mask(self.current_set) != 0 // phase only when there's demand it can actually act on: its own next
// phase within this barrier group, or anything waiting in the other
// group (since parking to help cross the barrier is the only way to
// eventually reach it). Demand for the OTHER ring's own succession phase
// (e.g. phase 2 while this ring sits on phase 5) is not this ring's
// concern — it can keep resting while the other ring handles it alone.
fn ring_wants_to_advance(&self, index: usize) -> bool {
let demand = self.demand();
let ring = &self.rings[index];
let own_succession = ring.slot == Slot::First && {
let second = ring_phase_number(index, self.group, Slot::Second);
demand & PhaseId::new(second).unwrap().bit() != 0
};
own_succession || demand & group_mask(self.group.flip()) != 0
} }
fn start_phase_set(&mut self, index: usize) { fn enter_group(&mut self, group: Group) {
self.current_set = index; self.group = group;
self.pending_calls &= !self.set_mask(index); self.rings = [RingState::parked(), RingState::parked()];
let conflicting_call = self.has_conflicting_call();
for (ring, &phase) in self.rings.iter_mut().zip(&self.rotation[index].phases) { for index in 0..2 {
*ring = RingState::new(phase); self.try_resume_ring(index);
ring.interval = Interval::Green; }
}
if conflicting_call { // A parked ring (waiting at the barrier) picks back up as soon as either
ring.maximum_elapsed = Some(0); // of its two phases in the current group has demand, skipping straight
// to the second phase if the first has none (matches real phase-skip
// behavior rather than always serving the first phase regardless).
fn try_resume_ring(&mut self, index: usize) {
if self.rings[index].interval != Interval::Red {
return;
}
let demand = self.demand();
let first = PhaseId::new(ring_phase_number(index, self.group, Slot::First)).unwrap();
let second = PhaseId::new(ring_phase_number(index, self.group, Slot::Second)).unwrap();
if demand & first.bit() != 0 {
self.rings[index] = RingState::green(Slot::First);
self.pending_calls &= !first.bit();
} else if demand & second.bit() != 0 {
self.rings[index] = RingState::green(Slot::Second);
self.pending_calls &= !second.bit();
}
}
// Runs the per-ring min/gap/max countdown for every ring still resting
// in green, independent of whatever the other ring is doing.
fn update_green_readiness(&mut self) {
for index in 0..2 {
if self.rings[index].interval != Interval::Green || self.rings[index].ready {
continue;
} }
}
}
fn start_interval(&mut self, interval: Interval) { let wants_to_advance = self.ring_wants_to_advance(index);
for ring in &mut self.rings { let number = ring_phase_number(index, self.group, self.rings[index].slot);
ring.interval = interval; let phase = self.phase(PhaseId::new(number).unwrap()).clone();
ring.elapsed_seconds = 0;
}
}
fn next_phase_set(&self) -> usize {
for offset in 1..=self.rotation.len() {
let index = (self.current_set + offset) % self.rotation.len();
if self.demand() & self.set_mask(index) != 0 {
return index;
}
}
self.current_set
}
fn step_green(&mut self) {
let conflicting_call = self.has_conflicting_call();
for index in 0..self.rings.len() {
let phase = self.phase(self.rings[index].phase).clone();
let ring = &mut self.rings[index]; let ring = &mut self.rings[index];
ring.gap_remaining = ring.gap_remaining.saturating_sub(1); ring.gap_remaining = ring.gap_remaining.saturating_sub(1);
if !conflicting_call || ring.ready { if !wants_to_advance {
continue; continue;
} }
@@ -368,24 +399,68 @@ impl Controller {
ring.ready = ring.gap_remaining == 0 || *maximum >= u32::from(phase.max_seconds); ring.ready = ring.gap_remaining == 0 || *maximum >= u32::from(phase.max_seconds);
} }
} }
}
if self.rings.iter().all(|ring| ring.ready) { // Advances one ring's own Green -> Yellow -> RedClearance -> (next slot
self.start_interval(Interval::Yellow); // or parked at the barrier) sequence, entirely independent of the other ring.
fn step_ring(&mut self, index: usize) {
let number = ring_phase_number(index, self.group, self.rings[index].slot);
let phase = self.phase(PhaseId::new(number).unwrap()).clone();
let ring = &mut self.rings[index];
match ring.interval {
Interval::Green => {
if ring.ready {
ring.interval = Interval::Yellow;
ring.elapsed_seconds = 0;
}
}
Interval::Yellow => {
if ring.elapsed_seconds >= u32::from(phase.yellow_seconds) {
ring.interval = Interval::RedClearance;
ring.elapsed_seconds = 0;
}
}
Interval::RedClearance => {
if ring.elapsed_seconds >= u32::from(phase.red_seconds) {
self.advance_ring(index);
}
}
Interval::Red => {}
} }
} }
fn clearance_seconds(&self, interval: Interval) -> u32 { // Called once a ring finishes clearing its current phase: either moves on
self.rings // to the group's second phase (if it has demand) or parks at the barrier.
.iter() fn advance_ring(&mut self, index: usize) {
.map(|ring| { let slot = self.rings[index].slot;
let phase = self.phase(ring.phase);
match interval { if slot == Slot::First {
Interval::Yellow => u32::from(phase.yellow_seconds), let second = ring_phase_number(index, self.group, Slot::Second);
_ => u32::from(phase.red_seconds), let id = PhaseId::new(second).unwrap();
}
}) if self.demand() & id.bit() != 0 {
.max() self.pending_calls &= !id.bit();
.unwrap() self.rings[index] = RingState::green(Slot::Second);
return;
}
}
self.rings[index] = RingState::parked();
}
// Both rings cross the barrier together only once both have parked, and
// only if the other side actually has something to serve - otherwise an
// idle intersection would flip barriers every single tick for no reason.
fn try_cross_barrier(&mut self) {
if !self.rings.iter().all(|ring| ring.interval == Interval::Red) {
return;
}
let next = self.group.flip();
if self.demand() & group_mask(next) != 0 {
self.enter_group(next);
}
} }
} }
@@ -410,14 +485,7 @@ impl Controller {
}) })
.collect(); .collect();
let rotation = vec![ let mut controller = Controller::new(0, phases);
PhaseSet::new(1, PhaseId(1), PhaseId(5)),
PhaseSet::new(2, PhaseId(2), PhaseId(6)),
PhaseSet::new(3, PhaseId(3), PhaseId(7)),
PhaseSet::new(4, PhaseId(4), PhaseId(8)),
];
let mut controller = Controller::new(0, phases, rotation);
// Matches the paper's own example: minimum recall on the main-street // Matches the paper's own example: minimum recall on the main-street
// through phases only, so they rest in green absent conflicting demand. // through phases only, so they rest in green absent conflicting demand.
// Every other phase is purely actuated and is skipped when uncalled. // Every other phase is purely actuated and is skipped when uncalled.
@@ -432,7 +500,7 @@ impl Controller {
if self.startup_remaining > 0 { if self.startup_remaining > 0 {
self.startup_remaining -= 1; self.startup_remaining -= 1;
if self.startup_remaining == 0 { if self.startup_remaining == 0 {
self.start_phase_set(0); self.enter_group(Group::A);
} }
return; return;
} }
@@ -441,20 +509,13 @@ impl Controller {
ring.elapsed_seconds += 1; ring.elapsed_seconds += 1;
} }
match self.rings[0].interval { self.update_green_readiness();
Interval::Green => self.step_green(),
Interval::Yellow => { for index in 0..2 {
if self.rings[0].elapsed_seconds >= self.clearance_seconds(Interval::Yellow) { self.step_ring(index);
self.start_interval(Interval::RedClearance);
}
}
Interval::RedClearance => {
if self.rings[0].elapsed_seconds >= self.clearance_seconds(Interval::RedClearance) {
self.start_phase_set(self.next_phase_set());
}
}
Interval::Red => {}
} }
self.try_cross_barrier();
} }
// A red/yellow call latches. A green hit restarts passage time. // A red/yellow call latches. A green hit restarts passage time.
@@ -463,31 +524,30 @@ impl Controller {
return false; return false;
}; };
// Both rings in the current set are serviced simultaneously, so a hit on for index in 0..2 {
// either phase of the active pair restarts passage time for both of them. let ring = &self.rings[index];
if self if ring.interval != Interval::Green {
.rings continue;
.iter() }
.any(|ring| ring.phase == id && ring.interval == Interval::Green) if ring_phase_number(index, self.group, ring.slot) != number {
{ continue;
let passages: Vec<u32> = self }
.rings
.iter() let passage = u32::from(self.phase(id).pass_seconds);
.map(|ring| u32::from(self.phase(ring.phase).pass_seconds)) let ring = &mut self.rings[index];
.collect(); if !ring.ready {
for (ring, passage) in self.rings.iter_mut().zip(passages) { ring.gap_remaining = ring.gap_remaining.max(passage);
if !ring.ready {
ring.gap_remaining = ring.gap_remaining.max(passage);
}
} }
return true; return true;
} }
self.pending_calls |= id.bit(); self.pending_calls |= id.bit();
if self.startup_remaining == 0 && self.rings[0].interval == Interval::Green { if self.startup_remaining == 0 {
for ring in &mut self.rings { for index in 0..2 {
ring.maximum_elapsed.get_or_insert(0); if self.rings[index].interval == Interval::Green && self.ring_wants_to_advance(index) {
self.rings[index].maximum_elapsed.get_or_insert(0);
}
} }
} }
@@ -497,22 +557,30 @@ impl Controller {
pub fn set_min_recall(&mut self, mask: u8) { pub fn set_min_recall(&mut self, mask: u8) {
self.timing_chart.min_recall = mask; self.timing_chart.min_recall = mask;
if self.startup_remaining == 0 && self.rings[0].interval == Interval::Green { if self.startup_remaining > 0 {
let conflicting_call = self.has_conflicting_call(); return;
for ring in &mut self.rings { }
if conflicting_call {
ring.maximum_elapsed.get_or_insert(0); for index in 0..2 {
} else { if self.rings[index].interval != Interval::Green {
ring.maximum_elapsed = None; continue;
ring.ready = false; }
} if self.ring_wants_to_advance(index) {
self.rings[index].maximum_elapsed.get_or_insert(0);
} else {
self.rings[index].maximum_elapsed = None;
self.rings[index].ready = false;
} }
} }
} }
pub fn signal(&self, number: u8) -> Interval { pub fn signal(&self, number: u8) -> Interval {
for ring in &self.rings { for index in 0..2 {
if ring.phase.0 == number { let ring = &self.rings[index];
if ring.interval == Interval::Red {
continue;
}
if ring_phase_number(index, self.group, ring.slot) == number {
return ring.interval; return ring.interval;
} }
} }
@@ -526,20 +594,30 @@ impl Controller {
// Seconds the phase has been green, if it is one of the two active rings. // Seconds the phase has been green, if it is one of the two active rings.
pub fn ring_elapsed(&self, number: u8) -> Option<u32> { pub fn ring_elapsed(&self, number: u8) -> Option<u32> {
let id = PhaseId::new(number)?; for index in 0..2 {
self.rings let ring = &self.rings[index];
.iter() if ring.interval == Interval::Green
.find(|ring| ring.phase == id && ring.interval == Interval::Green) && ring_phase_number(index, self.group, ring.slot) == number
.map(|ring| ring.elapsed_seconds) {
return Some(ring.elapsed_seconds);
}
}
None
} }
// Passage time left before this phase's green could gap out, if active. // Passage time left before this phase's green could gap out, if active.
pub fn ring_gap(&self, number: u8) -> Option<u32> { pub fn ring_gap(&self, number: u8) -> Option<u32> {
let id = PhaseId::new(number)?; for index in 0..2 {
self.rings let ring = &self.rings[index];
.iter() if ring.interval == Interval::Green
.find(|ring| ring.phase == id && ring.interval == Interval::Green) && ring_phase_number(index, self.group, ring.slot) == number
.map(|ring| ring.gap_remaining) {
return Some(ring.gap_remaining);
}
}
None
} }
pub fn time(&self) -> u32 { pub fn time(&self) -> u32 {
@@ -561,7 +639,7 @@ impl Controller {
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
let mut controller = Controller::new(self.id.0, self.phases.clone(), self.rotation.clone()); let mut controller = Controller::new(self.id.0, self.phases.clone());
controller.timing_chart.min_recall = self.timing_chart.min_recall; controller.timing_chart.min_recall = self.timing_chart.min_recall;
*self = controller; *self = controller;
} }
@@ -609,7 +687,11 @@ impl fmt::Display for Controller {
if self.startup_remaining > 0 { if self.startup_remaining > 0 {
writeln!(f, "STARTUP ALL RED {}s", self.startup_remaining)?; writeln!(f, "STARTUP ALL RED {}s", self.startup_remaining)?;
} else { } else {
writeln!(f, "PHASE SET {}", self.rotation[self.current_set].id.0)?; let group = match self.group {
Group::A => "A",
Group::B => "B",
};
writeln!(f, "BARRIER GROUP {group}")?;
} }
write!(f, "PHASE ")?; write!(f, "PHASE ")?;
@@ -654,7 +736,8 @@ impl fmt::Display for Controller {
"RING", "PHASE", "INTERVAL", "ELAPSED", "MIN", "GAP", "MAX", "RING", "PHASE", "INTERVAL", "ELAPSED", "MIN", "GAP", "MAX",
)?; )?;
for (index, ring) in self.rings.iter().enumerate() { for (index, ring) in self.rings.iter().enumerate() {
let phase = self.phase(ring.phase); let phase_number = ring_phase_number(index, self.group, ring.slot);
let phase = self.phase(PhaseId::new(phase_number).unwrap());
let interval = match ring.interval { let interval = match ring.interval {
Interval::Red => "Red", Interval::Red => "Red",
Interval::Green => "Green", Interval::Green => "Green",
@@ -688,7 +771,7 @@ impl fmt::Display for Controller {
f, f,
"{:>4} {:>6} {:<14} {:>7} {:>3} {:>3} {:>3} {}", "{:>4} {:>6} {:<14} {:>7} {:>3} {:>3} {:>3} {}",
index + 1, index + 1,
ring.phase.0, phase_number,
interval, interval,
ring.elapsed_seconds, ring.elapsed_seconds,
min, min,
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.