# Tactiverge Academy — companion code for "Software layers, loops and timing"
# Lesson: https://tactiverge.win/academy/robotics-foundations/robotic-system-architecture/software-layers-and-loops/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
# One 1 ms timer tick drives three loops at different rates.
PERIODS_MS = {"control": 1, "estimate": 10, "plan": 100}
runs = {name: 0 for name in PERIODS_MS}

for tick in range(1000):                 # one simulated second
    due = [name for name, period in PERIODS_MS.items() if tick % period == 0]
    for name in due:
        runs[name] += 1
    if tick in (0, 1, 10, 100):
        print(f"t = {tick:3d} ms: {', '.join(due)}")

for name, count in runs.items():
    print(f"{name:8s} ran {count:4d} times -> {count} Hz")
