firmament: pin down the code, pattern, and session format
Gray code helps neighboring projector columns, not LEDs decoded one at a time, so the code is a 12-bit weight-6 code that catches any single bit flip and merged blobs.
Assisted-by: Claude Opus 5.5 via Claude Code
diff --git a/firmament/DESIGN.md b/firmament/DESIGN.md
index 644fee4..773ebf8 100644
--- a/firmament/DESIGN.md
+++ b/firmament/DESIGN.md
@@ -22,8 +22,8 @@ as test data.
| Parameter | Value | Notes |
| --- | --- | --- |
-| LED count | 500 | ten strands of 50 |
-| ID code length | 9 bits | 2^9 = 512 >= 500 |
+| LED count | up to 500 | a few hundred expected; the full tree is ten strands of 50 |
+| ID code | 12 bits, weight 6 | 924 codes >= 500; see [`SPEC.md`](SPEC.md#id-code) |
| Spacing along a strand | ~0.10 m | 50 LEDs per 5 m; max consecutive distance |
| Strand boundaries | every 50th LED | the spacing prior does not hold across these |
| WLED outputs | 2 x 250 | to confirm: indices continue across both outputs |
@@ -33,8 +33,9 @@ as test data.
- **Solver language: Python** (NumPy, OpenCV, SciPy). The problem is small
(500 points, a few dozen stations); revisit only if it's slow.
-- **Stations, not continuous capture,** for v1: hold still for a cycle or two,
- then move. Continuous capture can come later on the same session format.
+- **Stations, not continuous capture,** for v1: hold still for at least two
+ cycles (4.2 s), then move. Continuous capture can come later on the same
+ session format.
- **Capture app: native Swift** (a Mac with Xcode is on hand). An
off-the-shelf LiDAR recorder that exports per-frame poses could stand in for
a first spike; not checked which ones do.
@@ -47,10 +48,9 @@ as test data.
- One cycle, looped forever:
1. all on (sync, and each LED's "on" brightness)
2. all off (sync, and the background)
- 3. 9 gray-code bits, plain white on/off
- 4. a parity bit
-- Each slot is held for 3 or more camera frames. At 30 fps and 100 ms per
- slot, a 12-slot cycle is about 1.2 s.
+ 3. 12 bits of a constant-weight code, plain white on/off
+- Each slot lasts 150 ms, about 4.5 camera frames at 30 fps, so a 14-slot
+ cycle is 2.1 s. [`SPEC.md`](SPEC.md#pattern) has the exact pattern.
- Low brightness, to limit blooming and stay well inside the 10A supply.
- Debug modes: light a single index, or a single strand. Use these to answer
whether WLED indices continue across the two 250-pixel outputs (light LED
@@ -76,9 +76,9 @@ as test data.
threshold, connected components.
2. **Sync**: find the all-on/all-off frames, then slice each station into bit
slots.
-3. **Decode** IDs per blob: threshold each LED halfway between its own on and
- off brightness, read the gray code, check parity, and use repeated cycles to
- reject bad decodes.
+3. **Decode** IDs per blob: call each LED's 6 brightest data slots ones,
+ reject codes that aren't in the table, and use repeated cycles to reject
+ bad decodes.
4. **Triangulate** each LED from all views with known poses, robustly (reject
by reprojection error, take the median).
5. **Refine** with bundle adjustment (see below).
@@ -124,20 +124,11 @@ A small service that computes a color per LED from (x, y, z) each frame and
sends DDP frames to WLED. Effects: vertical sweeps, rotating colors, rising
flames. It can share DDP code with the pattern player.
-## Capture session layout (sketch)
+## Capture session layout
-```
-session-YYYYMMDD-HHMM/
- session.json # LED count, code scheme and slot timing, device model, notes
- stations/
- 001/
- frames/ # image per frame (or a video)
- poses.jsonl # {t, transform[16], fx, fy, cx, cy, w, h, tracking}
- depth/ # optional
- player.jsonl # optional: pattern player's own timeline, debug only
-```
-
-The synthetic generator writes this same format.
+[`SPEC.md`](SPEC.md#capture-session) defines it: per-station directories of
+JPEG frames and a `poses.jsonl`, with the tree's LED count and strand length
+in a separate `tree.json`. The synthetic generator writes this same format.
## Testing
@@ -169,7 +160,8 @@ The synthetic generator writes this same format.
In order. Steps 1, 2 and 4 need no phone or tree; 3 and 5 do.
1. **Spec.** Pin down the code scheme, slot timing, and session format. These
- are the contract between capture, synthetic data, and solver.
+ are the contract between capture, synthetic data, and solver. Drafted in
+ [`SPEC.md`](SPEC.md).
2. **Pattern player.** DDP loop with debug modes; check it on the tree.
3. **Tracking spike.** Minimal Swift recorder (or an existing app) on the
phone, with the pattern player running, in a dim room. Does tracking hold?
diff --git a/firmament/SPEC.md b/firmament/SPEC.md
new file mode 100644
index 0000000..c82597c
--- /dev/null
+++ b/firmament/SPEC.md
@@ -0,0 +1,170 @@
+# firmament spec
+
+Format 1, drafted 2026-11-22. This is the contract between the pattern
+player, the recorder, the synthetic generator, and the solver. A change
+here that breaks old sessions bumps the format number.
+
+[`DESIGN.md`](DESIGN.md) covers why the pieces are shaped this way. This
+document covers only what each piece must produce or accept.
+
+## Tree description
+
+The solver reads the tree's properties from `tree.json`, kept separately
+from any recording. The same tree is recorded many times, and the
+recorder never needs these values.
+
+```json
+{
+ "led_count": 500,
+ "strand_length": 50
+}
+```
+
+| Field | Meaning |
+| --- | --- |
+| `led_count` | Number of LEDs, indexed `0` to `led_count - 1`. Between 2 and 500. |
+| `strand_length` | LEDs per physical strand. Strands start at every multiple of this index, and the spacing prior doesn't hold across a strand boundary. |
+
+## ID code
+
+Each LED blinks a 12-bit **constant-weight code**: every codeword has
+exactly 6 ones. Two properties follow from the fixed weight:
+
+- Any single flipped bit changes the count of ones, so the decoder
+ detects it.
+- Two overlapping blobs with different codes read as more than 6 ones, so
+ a merged detection is rejected rather than decoded as the wrong LED.
+
+### Codeword table
+
+LED index `i` gets codeword `code(i)`, built as follows:
+
+1. List every 12-bit value below 2048 (bit 11 clear) that has exactly 6
+ ones, in increasing order. The list has 462 entries, `c[0]` to
+ `c[461]`.
+2. Set `code(2k) = c[k]` and `code(2k + 1) = c[k] XOR 0xFFF`.
+
+Each pair is a codeword and its complement, so every bit is on for
+exactly one LED of each pair. For any `led_count`, each data slot lights
+half the LEDs, give or take one. No data slot can be mistaken for a sync
+slot, and the per-slot power draw stays flat.
+
+The first six entries are `63, 4032, 95, 4000, 111, 3984`. The table
+supports 924 LEDs, and this format caps them at 500.
+
+## Pattern
+
+The pattern player loops one 14-slot cycle forever:
+
+| Slot | Content |
+| --- | --- |
+| 0 | All LEDs on |
+| 1 | All LEDs off |
+| 2 to 13 | Bit `s - 2` of each LED's codeword, least significant bit first |
+
+The following rules apply to every slot:
+
+- A slot lasts 150 ms, so a cycle lasts 2.1 s.
+- "On" is white (R = G = B) at a single brightness for the whole run.
+ "Off" is black.
+- The player sends one DDP frame per slot, and may resend it within the
+ slot to keep WLED in realtime mode. It never sends a frame that mixes
+ two slots.
+
+Slot timing is nominal. The solver finds slot boundaries from the sync
+slots in the images, never from wall-clock time.
+
+### Decoding rule
+
+The solver decodes an LED from its brightness in each of the 12 data
+slots. It calls the 6 brightest slots ones and the rest zeros. It rejects
+the decode if the gap between the 6th and 7th brightest readings is too
+small relative to that LED's on/off range, or if the result isn't a code
+in the table for `led_count`. The threshold is a solver setting, not part
+of this contract.
+
+## Capture session
+
+A session is one walk around the tree. The recorder writes it, the
+synthetic generator writes the same layout, and the solver reads it.
+
+```
+session-YYYYMMDD-HHMMSS/
+ session.json
+ stations/
+ 001/
+ poses.jsonl
+ frames/
+ 000000.jpg
+ 000001.jpg
+ player.jsonl # optional, debug only
+```
+
+### Stations
+
+A **station** is a stretch of capture where the camera is meant to be
+still. The recorder saves frames only between a station's start and end,
+so each station is its own directory, numbered from `001`.
+
+A station must last at least two cycles (4.2 s). Any 14 consecutive slots
+contain every slot once, so the solver doesn't need the station to start
+on a cycle boundary.
+
+### `session.json`
+
+```json
+{
+ "format": 1,
+ "pattern": "cw12-6/150ms",
+ "started_at": "2026-11-22T19:04:11-08:00",
+ "device": "iPhone16,1",
+ "notes": ""
+}
+```
+
+| Field | Meaning |
+| --- | --- |
+| `format` | This spec's format number. |
+| `pattern` | The pattern the player was running. `cw12-6/150ms` is the pattern in this document. |
+| `started_at` | Local time the session started, ISO 8601 with offset. |
+| `device` | The device's model identifier. The synthetic generator writes `synthetic`. |
+| `notes` | Free text. |
+
+### `poses.jsonl`
+
+`poses.jsonl` holds one JSON object per saved frame, in capture order:
+
+```json
+{"frame": 0, "t": 8123.4567, "transform": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0.12, 1.43, 2.05, 1], "intrinsics": [1450.2, 1450.2, 959.5, 719.5], "width": 1920, "height": 1440, "tracking": "normal"}
+```
+
+| Field | Meaning |
+| --- | --- |
+| `frame` | Frame number within the station, from 0. The image is `frames/<frame, 6 digits>.jpg`. |
+| `t` | ARKit frame timestamp in seconds. Only differences between values are meaningful. |
+| `transform` | Camera-to-world transform, 16 numbers, column-major (ARKit's `camera.transform`). |
+| `intrinsics` | `[fx, fy, cx, cy]` in pixels of the saved image. |
+| `width`, `height` | Saved image size in pixels. |
+| `tracking` | `normal`, `limited:<reason>`, or `notAvailable`, from ARKit's tracking state. |
+
+The world frame is ARKit's gravity-aligned frame: meters, y up. The
+camera frame follows ARKit: x right, y up, the camera looking down −z.
+
+### Frames
+
+Frames are JPEG, saved in the sensor's native orientation without
+rotation, so `intrinsics` apply to them directly.
+
+### `player.jsonl`
+
+`player.jsonl` is an optional log from the pattern player, one object per
+slot sent: `{"t": <laptop Unix time>, "slot": <0 to 13>}`. The solver must
+not depend on it.
+
+## Not in format 1
+
+- Depth. LiDAR's value is still an open question in
+ [`DESIGN.md`](DESIGN.md#open-questions), so the format leaves it out
+ until that's answered.
+- Video. Frames are always individual images.
+- Continuous capture outside stations.