From c118bc066b2f55673811182068a558436d6a2da9 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 8 Nov 2026 02:38:32 +0000 Subject: [PATCH] Drive left and right Grove buzzers from image side bits. Packet version 2 puts left and right in an explicit bitmask, rejects version 1, and keeps the not-ready double blip separate from objects on both sides. Co-authored-by: pschmeln --- docs/PROTOTYPE.md | 18 +- docs/WEB_DEMO.md | 65 ++-- prototype/firmware/uno_r4_wifi/cane_logic.c | 89 ++--- prototype/firmware/uno_r4_wifi/cane_logic.h | 56 ++- .../firmware/uno_r4_wifi/uno_r4_wifi.ino | 40 +- .../ios/CaneDemo/CaneDemo/DebugRootView.swift | 20 +- .../CaneDemo/CaneDemo/DemoController.swift | 33 +- prototype/tests/print_fixture.c | 22 +- prototype/tests/test_cane.c | 366 +++++------------- web/app.js | 74 ++-- web/index.html | 26 +- web/logic.test.mjs | 171 ++++---- web/protocol.js | 45 +-- web/state.js | 28 +- web/styles.css | 1 + web/tracking.js | 44 +++ 16 files changed, 448 insertions(+), 650 deletions(-) diff --git a/docs/PROTOTYPE.md b/docs/PROTOTYPE.md index aa0ca6c..68b4279 100644 --- a/docs/PROTOTYPE.md +++ b/docs/PROTOTYPE.md @@ -38,22 +38,22 @@ The camera path can report tracking, an ARKit camera step, and an optional plane The simulation path is a separate bench control on the phone. It can request the D2 hazard actuator, the D3 unavailable actuator, and a D4 left or right label. The screen says the input is simulated. Those controls do not read the camera and do not mean an obstacle was detected. The shared encoder rejects a camera-sourced hazard or a camera-sourced route cue, so a buggy packet of that shape does not move the actuators. -There is no random left or right choice. Nothing in the camera path picks a direction. +The Bluefy page can sound the left buzzer, the right buzzer, or both from boxes in the center half of the picture. That is image left and image right. It is not a turn command. The native camera packet still keeps both side bits off. -## Four channels +## Two buzzers | Output | Pin | Meaning while the link is alive | | --- | --- | --- | -| Hazard | D2 | Pulses only for an elevated claim. Off is not an all-clear. | -| System unavailable | D3 | Pulses when the selected source says the pipeline cannot support a claim. | -| Route demo | D4 | Pulses for a manual left or right label. The label is on the phone and in the claim. The pulse does not mean that side is safe. | -| Link | D5 | Board-owned. A short blip every 2 seconds means packets are arriving. A fast, mostly-on pattern means the watchdog expired. | +| Left buzzer | D2 Grove socket | Steady pulse when the claim has the left bit. | +| Status logic | D3 | On during the not-ready pattern. Do not put a buzzer here. | +| Right buzzer | D4 Grove socket | Steady pulse when the claim has the right bit. | +| Link indicator | D5 | Board-owned. A short blip every 2 seconds means packets are arriving. A fast pattern means the watchdog expired. Not a buzzer. | -The programmed watchdog is 500 ms after the last valid packet, using the board's `millis()`. At 500 ms the link is still a heartbeat. At 501 ms it is an outage. During an outage the board forces D2, D3, and D4 off and keeps the outage pattern on D5. A rejected packet does not refresh the watchdog. Before the first valid packet, the board is already in outage. +Packet version is 2. Byte 2 is a side bitmask: 1 left, 2 right, 3 both. Byte 4 is always 0. A version 1 packet is rejected and does not sound a side. -Silence on all four outputs means the board is unpowered, the firmware is not running, or the drivers are disconnected. That is a fault, not a clear path. +The not-ready pattern is a double blip on both buzzers once a second, with D3 held on. That pattern is not "objects on both sides." Objects on both sides use the steady pulse on both buzzers and leave D3 off. -The patterns are different periods in code so a supervisor can tell the channels apart. Whether a particular actuator feels distinct is a bench check, not a result recorded here. +The programmed watchdog is 500 ms after the last valid packet, using the board's `millis()`. At 500 ms the link is still a heartbeat. At 501 ms it is an outage. During an outage the board forces D2 and D4 off and keeps the fast pattern on D5. Quiet buzzers are not an all-clear. If D5 is also quiet, the board is unpowered or not running. ## Actuator wiring diff --git a/docs/WEB_DEMO.md b/docs/WEB_DEMO.md index 0204e6b..13517fd 100644 --- a/docs/WEB_DEMO.md +++ b/docs/WEB_DEMO.md @@ -1,43 +1,48 @@ -# Bluefy page +# Two blue buzzers -The page is [web/index.html](../web/index.html). It is the phone runtime for this increment. A standard iPhone 16, with Bluefy installed by you, opens the page. The page uses the rear camera, runs COCO-SSD in the browser, and writes the existing 18-byte Bluetooth packet to the UNO R4 WiFi. A laptop is not part of that runtime. +Use the Bluefy page at [web/index.html](../web/index.html) on a standard iPhone 16. Reflash the board with [prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino](../prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino) before opening the new page. An old version 1 sketch will reject the new packets, and an old page will be rejected by this sketch. Neither mismatch sounds a side buzzer. -This repository has not run that combination on a phone. A green test log is not a Bluefy result. +The page has not been run on a phone here, and the buzzers have not been heard here. -## What the packet means +## Pins -The native camera path still cannot send an elevated hazard. The web page uses a separate source, image heuristic, value 3. +| Cane side | Grove socket | What it does | +| --- | --- | --- | +| Left | D2 | Left buzzer. Image-left boxes, or the manual left switch. | +| None | D3 | Status output only. On during the not-ready double blip. No buzzer. | +| Right | D4 | Right buzzer. Image-right boxes, or the manual right switch. | +| None | D5 | Link indicator. Slow blip while packets arrive. Fast pattern when the 500 ms watchdog expires. No buzzer. | -- D2 pulses only for an elevated claim. From the web page that means a supported box is in the center of the picture, or you turned on the manual D2 switch. It is not a measured collision. -- D3 pulses when the camera, model, or page is not ready. Quiet D3 is not an all-clear. -- D4 pulses only for a manual left or right label. That label is not a clear direction. -- D5 is driven by the board. A short blip means packets are arriving. A fast pattern means the 500 ms watchdog expired. The page tries to send every 200 ms while it is connected. +Use the Grove sockets on D2 and D4. Do not use D3 as the right side. A Grove cable is SIG, VCC, and GND. The module in the photo is marked GND, VCC, NC, SIG. NC is unused. SIG goes to the digital pin through the Grove socket. -The image-heuristic packet is rejected if it contains a distance or a route. The model does not report meters, speed, or time to contact. If the model fails, the page shows the error and does not invent a warning from brightness. +Seeed's older Grove Buzzer v1.1 page describes an onboard transistor. That description is not proof for a module marked v1.2. Before you apply power, match your module to its own Seeed page. Connect it only if SIG is a logic input to the module's own driver. Do not connect a bare motor, or a piezo that expects the GPIO to supply its current, to D2 or D4. This repository does not add a new current rating for the module. -## Software already in the repo +## How a side is chosen -- Sketch: [prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino](../prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino) -- The compile job that passed on the previous pull request used Arduino CLI 1.5.1, board package `arduino:renesas_uno` 1.6.0, and ArduinoBLE 2.1.0. This page keeps those versions. -- Pick the board Arduino UNO R4 WiFi. The sketch calls `Serial.begin(115200)` and prints `Cane demo controller`, `Advertising CaneDemo as a BLE peripheral.`, `accepted source=`, and `rejected packet`. -- Pinned browser downloads, only when you tap Load object model: TensorFlow.js 4.22.0 and COCO-SSD 2.2.3 from jsDelivr, both Apache-2.0. The weights are the library's `lite_mobilenet_v2` files at `https://storage.googleapis.com/tfjs-models/savedmodel/ssdlite_mobilenet_v2/model.json`. The camera picture is not sent anywhere. +Section 5 still accepts only person, bicycle, car, motorcycle, bus, and truck, with a score of at least 0.5, and only when the box center is inside the middle half of the picture (from 25% to 75% of both width and height). Boxes farther out are ignored. -## What only you can do +For those boxes, image left and image right are the two sides of the unmirrored rear-camera frame. The page does not flip the video. A center x less than half the picture width sets the left bit. Greater than half sets the right bit. Exactly half sets both bits for that one box. Several boxes are combined, so a left box and a right box can sound together. That both-sides object pattern is a steady pulse. It is not the not-ready double blip. -1. Merge the pull request. -2. In the repository Settings, open Pages, and set the source to GitHub Actions. The workflow [pages.yml](../.github/workflows/pages.yml) does not run for a pull request, and it cannot switch that setting. Until that source is selected, there is no published page. `https://pschmeln.github.io/agent-workflow/` returned 404 when this branch was prepared. -3. After the Pages job on `main` succeeds, open the URL printed by that job in Bluefy on the iPhone 16. Do not type a guessed address. -4. Install Bluefy yourself. Flash the sketch. Wire four external drivers to D2, D3, D4, and D5. Do not power actuators from the header pins. -5. With a sighted spotter, use the page sections one at a time, then the combined switch. Background the app and confirm D5 changes to the fast pattern. Do not cover anyone's eyes. +The not-ready double blip plays on both buzzers when the camera, model, or page is not ready, and D3 stays on. It means the pipeline is down. It does not mean objects are on both sides. Quiet buzzers are not an all-clear: watch D5. A slow blip means the link is alive. A fast pattern means the link timed out. -## Actuator check +## Update steps -| What you do | What should move | +1. In Arduino IDE, select Arduino UNO R4 WiFi. Use the UNO R4 board package 1.6.0 and ArduinoBLE 2.1.0, the same versions the compile job uses. +2. Open the sketch above and upload it. Serial Monitor is 115200. The sketch prints `Cane demo controller`, `D2 left buzzer, D4 right buzzer, D3 status logic, D5 link logic.`, and `Advertising CaneDemo as a BLE peripheral.` +3. Plug the left buzzer into Grove D2 and the right buzzer into Grove D4. +4. Merge this pull request, set GitHub Pages to GitHub Actions, and open the Pages URL in Bluefy. Reload the page so Bluefy does not keep the previous file. The old page and the new sketch do not belong together. +5. On the page, try camera alone, model alone, then Bluetooth. Use the manual left and manual right switches one at a time. Then turn on section 5. + +## What a spotter can check + +| What you do | What should happen | | --- | --- | -| Phone not connected | D5 fast, D2 D3 D4 off | -| Connected, no manual switch and no combined warning | D3 unavailable pattern, D5 slow blip | -| Manual D2 | D2 pulses, serial `source=simulation` | -| Manual D4 left or right label | D4 pulses. The label is not a safe turn | -| Combined warning on, supported object in the center box | D2 pulses, serial `source=image-heuristic` | -| Combined warning on, nothing in the center | D2 off. The page says this is not an all-clear | -| Leave Bluefy or disconnect | D5 fast within about a second | +| Power the board before the phone connects | D2 and D4 quiet, D5 fast | +| Connect, leave section 5 off and manual off | Both buzzers double-blip, D3 on, D5 slow. This is not-ready, not two objects | +| Manual left only | D2 steady pulse, D4 quiet, serial `source=simulation` | +| Manual right only | D4 steady pulse, D2 quiet | +| Section 5 on, one supported box left of center | D2 steady pulse, serial `source=image-heuristic` | +| Section 5 on, boxes on both image sides | D2 and D4 steady pulse, D3 off | +| Stop the camera or leave Bluefy | Double-blip or, if the link drops, D5 fast and both buzzers quiet | + +Do this with eyes uncovered. The cane you already use stays the cane. diff --git a/prototype/firmware/uno_r4_wifi/cane_logic.c b/prototype/firmware/uno_r4_wifi/cane_logic.c index 2ab6760..6984cad 100644 --- a/prototype/firmware/uno_r4_wifi/cane_logic.c +++ b/prototype/firmware/uno_r4_wifi/cane_logic.c @@ -71,43 +71,42 @@ static void record_plane(const cane_input_t *in, cane_assessment_t *out) { } } +static int side_bits(int sides) { + return sides & (int)(CANE_SIDE_LEFT | CANE_SIDE_RIGHT); +} + void cane_assess(const cane_input_t *in, cane_assessment_t *out) { out->source = in->source; out->tracking = in->tracking; out->scene_depth_supported = in->scene_depth_supported ? 1 : 0; - out->hazard = CANE_HAZARD_NO_CLAIM; + out->sides = 0; out->system_unavailable = 1; - out->route_cue = CANE_ROUTE_NONE; record_plane(in, out); if (in->source == CANE_SOURCE_CAMERA) { int pipeline_ready = in->camera_authorized && in->world_tracking_supported && in->tracking == CANE_TRACKING_NORMAL; out->system_unavailable = pipeline_ready ? 0 : 1; - out->hazard = CANE_HAZARD_NO_CLAIM; - out->route_cue = CANE_ROUTE_NONE; + out->sides = 0; return; } if (in->source == CANE_SOURCE_IMAGE_HEURISTIC) { clear_plane(out); - out->route_cue = CANE_ROUTE_NONE; if (!in->image_pipeline_ready) { out->system_unavailable = 1; - out->hazard = CANE_HAZARD_NO_CLAIM; + out->sides = 0; return; } out->system_unavailable = 0; - out->hazard = in->image_heuristic_elevated ? CANE_HAZARD_ELEVATED : CANE_HAZARD_NO_CLAIM; + out->sides = side_bits(in->image_sides); return; } if (in->source == CANE_SOURCE_SIMULATION) { - out->hazard = in->simulation_elevated_hazard ? CANE_HAZARD_ELEVATED : CANE_HAZARD_NO_CLAIM; + clear_plane(out); out->system_unavailable = in->simulation_unavailable ? 1 : 0; - if (in->route_cue == CANE_ROUTE_DEMO_LEFT || in->route_cue == CANE_ROUTE_DEMO_RIGHT) { - out->route_cue = in->route_cue; - } + out->sides = in->simulation_unavailable ? 0 : side_bits(in->simulation_sides); } } @@ -150,9 +149,8 @@ void cane_command_from_assessment( uint16_t time_ms_low, cane_command_t *command) { command->source = assessment->source; - command->hazard = assessment->hazard; + command->sides = side_bits(assessment->sides); command->system_unavailable = assessment->system_unavailable ? 1 : 0; - command->route_cue = assessment->route_cue; command->tracking = assessment->tracking; command->scene_depth_supported = assessment->scene_depth_supported ? 1 : 0; command->plane_hit_cm = assessment->plane_hit_valid ? assessment->plane_hit_cm : CANE_PLANE_CM_NONE; @@ -165,26 +163,20 @@ int cane_command_admissible(const cane_command_t *command) { command->source != CANE_SOURCE_IMAGE_HEURISTIC) { return 0; } - if (command->hazard != CANE_HAZARD_NO_CLAIM && command->hazard != CANE_HAZARD_ELEVATED) { + if ((command->sides & ~(int)(CANE_SIDE_LEFT | CANE_SIDE_RIGHT)) != 0) { return 0; } if (command->system_unavailable != 0 && command->system_unavailable != 1) { return 0; } - if (command->route_cue != CANE_ROUTE_NONE && command->route_cue != CANE_ROUTE_DEMO_LEFT && - command->route_cue != CANE_ROUTE_DEMO_RIGHT) { - return 0; - } if (command->tracking < CANE_TRACKING_UNSUPPORTED || command->tracking > CANE_TRACKING_NORMAL) { return 0; } - if (command->source == CANE_SOURCE_CAMERA) { - if (command->hazard != CANE_HAZARD_NO_CLAIM || command->route_cue != CANE_ROUTE_NONE) { - return 0; - } + if (command->source == CANE_SOURCE_CAMERA && command->sides != 0) { + return 0; } - if (command->source == CANE_SOURCE_IMAGE_HEURISTIC) { - if (command->route_cue != CANE_ROUTE_NONE || command->plane_hit_cm != CANE_PLANE_CM_NONE) { + if (command->source == CANE_SOURCE_IMAGE_HEURISTIC || command->source == CANE_SOURCE_SIMULATION) { + if (command->plane_hit_cm != CANE_PLANE_CM_NONE) { return 0; } } @@ -198,9 +190,9 @@ int cane_command_encode(const cane_command_t *command, uint8_t out[CANE_PACKET_B } out[0] = CANE_PACKET_VERSION; out[1] = (uint8_t)command->source; - out[2] = (uint8_t)command->hazard; + out[2] = (uint8_t)command->sides; out[3] = (uint8_t)command->system_unavailable; - out[4] = (uint8_t)command->route_cue; + out[4] = 0; out[5] = (uint8_t)command->tracking; out[6] = command->scene_depth_supported ? 1u : 0u; out[7] = 0; @@ -215,7 +207,7 @@ int cane_command_encode(const cane_command_t *command, uint8_t out[CANE_PACKET_B int cane_command_decode(const uint8_t *bytes, size_t len, cane_command_t *command) { uint16_t expected; uint16_t actual; - if (len != CANE_PACKET_BYTES || bytes[0] != CANE_PACKET_VERSION || bytes[7] != 0) { + if (len != CANE_PACKET_BYTES || bytes[0] != CANE_PACKET_VERSION || bytes[4] != 0 || bytes[7] != 0) { return 0; } expected = cane_crc16(bytes, 16); @@ -224,9 +216,8 @@ int cane_command_decode(const uint8_t *bytes, size_t len, cane_command_t *comman return 0; } command->source = bytes[1]; - command->hazard = bytes[2]; + command->sides = bytes[2]; command->system_unavailable = bytes[3]; - command->route_cue = bytes[4]; command->tracking = bytes[5]; command->scene_depth_supported = bytes[6] ? 1 : 0; command->sequence = read_le32(&bytes[8]); @@ -239,9 +230,8 @@ void cane_link_init(cane_link_t *link) { link->last_valid_ms = 0; link->have_valid = 0; link->command.source = CANE_SOURCE_CAMERA; - link->command.hazard = CANE_HAZARD_NO_CLAIM; + link->command.sides = 0; link->command.system_unavailable = 1; - link->command.route_cue = CANE_ROUTE_NONE; link->command.tracking = CANE_TRACKING_UNAVAILABLE; link->command.scene_depth_supported = 0; link->command.plane_hit_cm = CANE_PLANE_CM_NONE; @@ -260,6 +250,11 @@ int cane_link_accept(cane_link_t *link, const uint8_t *bytes, size_t len, uint32 return 1; } +int cane_fault_blip(uint32_t now_ms) { + uint32_t phase = now_ms % CANE_FAULT_PERIOD_MS; + return phase < 80u || (phase >= 160u && phase < 240u); +} + int cane_pulse(uint32_t now_ms, uint32_t period_ms, uint32_t on_ms) { if (period_ms == 0 || on_ms == 0) { return 0; @@ -282,17 +277,23 @@ void cane_link_outputs(const cane_link_t *link, uint32_t now_ms, cane_outputs_t outage ? CANE_LINK_OUT_PERIOD_MS : CANE_LINK_OK_PERIOD_MS, outage ? CANE_LINK_OUT_ON_MS : CANE_LINK_OK_ON_MS); if (outage) { - out->hazard = 0; - out->unavailable = 0; - out->route = 0; + out->left = 0; + out->right = 0; + out->status = 0; + return; + } + if (link->command.system_unavailable) { + int blip = cane_fault_blip(now_ms); + out->left = blip; + out->right = blip; + out->status = 1; return; } - out->hazard = link->command.hazard == CANE_HAZARD_ELEVATED && - cane_pulse(now_ms, CANE_HAZARD_PERIOD_MS, CANE_HAZARD_ON_MS); - out->unavailable = link->command.system_unavailable && - cane_pulse(now_ms, CANE_UNAVAILABLE_PERIOD_MS, CANE_UNAVAILABLE_ON_MS); - out->route = link->command.route_cue != CANE_ROUTE_NONE && - cane_pulse(now_ms, CANE_ROUTE_PERIOD_MS, CANE_ROUTE_ON_MS); + out->status = 0; + out->left = (link->command.sides & CANE_SIDE_LEFT) && + cane_pulse(now_ms, CANE_OBJECT_PERIOD_MS, CANE_OBJECT_ON_MS); + out->right = (link->command.sides & CANE_SIDE_RIGHT) && + cane_pulse(now_ms, CANE_OBJECT_PERIOD_MS, CANE_OBJECT_ON_MS); } int cane_status_encode( @@ -301,7 +302,7 @@ int cane_status_encode( uint8_t bytes[CANE_STATUS_BYTES]) { bytes[0] = CANE_PACKET_VERSION; bytes[1] = (uint8_t)out->link_state; - bytes[2] = (uint8_t)((out->hazard ? 1u : 0u) | (out->unavailable ? 2u : 0u) | (out->route ? 4u : 0u) | + bytes[2] = (uint8_t)((out->left ? 1u : 0u) | (out->right ? 2u : 0u) | (out->status ? 4u : 0u) | (out->link ? 8u : 0u)); bytes[3] = 0; write_le32(&bytes[4], last_sequence); @@ -316,9 +317,9 @@ int cane_status_decode(const uint8_t *bytes, size_t len, cane_status_t *status) return 0; } status->link_state = bytes[1]; - status->hazard_on = (bytes[2] & 1u) != 0; - status->unavailable_on = (bytes[2] & 2u) != 0; - status->route_on = (bytes[2] & 4u) != 0; + status->left_on = (bytes[2] & 1u) != 0; + status->right_on = (bytes[2] & 2u) != 0; + status->status_on = (bytes[2] & 4u) != 0; status->link_on = (bytes[2] & 8u) != 0; status->last_sequence = read_le32(&bytes[4]); return 1; diff --git a/prototype/firmware/uno_r4_wifi/cane_logic.h b/prototype/firmware/uno_r4_wifi/cane_logic.h index 0e94211..58ce2e9 100644 --- a/prototype/firmware/uno_r4_wifi/cane_logic.h +++ b/prototype/firmware/uno_r4_wifi/cane_logic.h @@ -14,24 +14,24 @@ extern "C" { * A plane-hit field is optional context and never authorizes a path. */ -#define CANE_PACKET_VERSION 1u +#define CANE_PACKET_VERSION 2u #define CANE_PACKET_BYTES 18u #define CANE_STATUS_BYTES 8u #define CANE_WATCHDOG_MS 500u #define CANE_PLANE_CM_NONE 0xFFFFu #define CANE_PLANE_CM_MAX 2000u -#define CANE_PIN_HAZARD 2 -#define CANE_PIN_UNAVAILABLE 3 -#define CANE_PIN_ROUTE 4 +#define CANE_PIN_LEFT 2 +#define CANE_PIN_STATUS 3 +#define CANE_PIN_RIGHT 4 #define CANE_PIN_LINK 5 -#define CANE_HAZARD_PERIOD_MS 200u -#define CANE_HAZARD_ON_MS 100u -#define CANE_UNAVAILABLE_PERIOD_MS 500u -#define CANE_UNAVAILABLE_ON_MS 250u -#define CANE_ROUTE_PERIOD_MS 1000u -#define CANE_ROUTE_ON_MS 200u +#define CANE_SIDE_LEFT 0x01u +#define CANE_SIDE_RIGHT 0x02u + +#define CANE_OBJECT_PERIOD_MS 200u +#define CANE_OBJECT_ON_MS 100u +#define CANE_FAULT_PERIOD_MS 1000u #define CANE_LINK_OK_PERIOD_MS 2000u #define CANE_LINK_OK_ON_MS 80u #define CANE_LINK_OUT_PERIOD_MS 200u @@ -43,17 +43,6 @@ enum { CANE_SOURCE_IMAGE_HEURISTIC = 3 }; -enum { - CANE_HAZARD_NO_CLAIM = 0, - CANE_HAZARD_ELEVATED = 1 -}; - -enum { - CANE_ROUTE_NONE = 0, - CANE_ROUTE_DEMO_LEFT = 1, - CANE_ROUTE_DEMO_RIGHT = 2 -}; - enum { CANE_TRACKING_UNSUPPORTED = 0, CANE_TRACKING_UNAVAILABLE = 1, @@ -82,18 +71,16 @@ typedef struct { int scene_depth_supported; int has_plane_hit; float plane_hit_meters; - int simulation_elevated_hazard; + int simulation_sides; int simulation_unavailable; - int route_cue; - int image_heuristic_elevated; + int image_sides; int image_pipeline_ready; } cane_input_t; typedef struct { int source; - int hazard; + int sides; int system_unavailable; - int route_cue; int scene_depth_supported; int plane_hit_valid; uint16_t plane_hit_cm; @@ -102,9 +89,8 @@ typedef struct { typedef struct { int source; - int hazard; + int sides; int system_unavailable; - int route_cue; int tracking; int scene_depth_supported; uint16_t plane_hit_cm; @@ -119,22 +105,24 @@ typedef struct { } cane_link_t; typedef struct { - int hazard; - int unavailable; - int route; + int left; + int right; + int status; int link; int link_state; } cane_outputs_t; typedef struct { int link_state; - int hazard_on; - int unavailable_on; - int route_on; + int left_on; + int right_on; + int status_on; int link_on; uint32_t last_sequence; } cane_status_t; +int cane_fault_blip(uint32_t now_ms); + uint16_t cane_crc16(const uint8_t *data, size_t len); void cane_assess(const cane_input_t *in, cane_assessment_t *out); diff --git a/prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino b/prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino index 638495a..cb96f67 100644 --- a/prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino +++ b/prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino @@ -4,10 +4,12 @@ * Wi-Fi is not started. Arduino documents that the ESP32-S3 antenna * is shared, so this sketch uses Bluetooth LE only. * - * D2-D5 are 5 V logic enables for external actuator drivers. - * The UNO R4 WiFi datasheet says each RA4M1 GPIO can safely handle - * up to 8 mA. Do not connect a motor, solenoid, or vibration puck - * directly to a GPIO. See docs/PROTOTYPE.md. + * D2 is the left Grove socket and D4 is the right Grove socket. + * D3 is an internal status output. D5 is the link indicator. + * Do not put a third buzzer on D3 or D5. + * Confirm the Grove Buzzer module's SIG pin is a logic input before + * connecting it. Do not power a bare motor from a GPIO. + * See docs/WEB_DEMO.md. */ #include @@ -55,12 +57,12 @@ static void print_outputs(uint32_t now_ms, const cane_outputs_t *out) { Serial.print(source_name(link.command.source)); Serial.print(" link="); Serial.print(out->link_state == CANE_LINK_OUTAGE ? "outage" : "heartbeat"); - Serial.print(" pins hazard="); - Serial.print(out->hazard); - Serial.print(" unavailable="); - Serial.print(out->unavailable); - Serial.print(" route="); - Serial.print(out->route); + Serial.print(" pins left="); + Serial.print(out->left); + Serial.print(" right="); + Serial.print(out->right); + Serial.print(" status="); + Serial.print(out->status); Serial.print(" linkpin="); Serial.println(out->link); } @@ -68,12 +70,12 @@ static void print_outputs(uint32_t now_ms, const cane_outputs_t *out) { static void apply_outputs(uint32_t now_ms) { cane_outputs_t out; cane_link_outputs(&link, now_ms, &out); - digitalWrite(CANE_PIN_HAZARD, out.hazard ? HIGH : LOW); - digitalWrite(CANE_PIN_UNAVAILABLE, out.unavailable ? HIGH : LOW); - digitalWrite(CANE_PIN_ROUTE, out.route ? HIGH : LOW); + digitalWrite(CANE_PIN_LEFT, out.left ? HIGH : LOW); + digitalWrite(CANE_PIN_STATUS, out.status ? HIGH : LOW); + digitalWrite(CANE_PIN_RIGHT, out.right ? HIGH : LOW); digitalWrite(CANE_PIN_LINK, out.link ? HIGH : LOW); - if (!have_previous || out.hazard != previous.hazard || out.unavailable != previous.unavailable || - out.route != previous.route || out.link_state != previous.link_state) { + if (!have_previous || out.left != previous.left || out.right != previous.right || + out.status != previous.status || out.link_state != previous.link_state) { print_outputs(now_ms, &out); previous = out; have_previous = 1; @@ -88,14 +90,14 @@ static void apply_outputs(uint32_t now_ms) { } void setup() { - set_pin_low(CANE_PIN_HAZARD); - set_pin_low(CANE_PIN_UNAVAILABLE); - set_pin_low(CANE_PIN_ROUTE); + set_pin_low(CANE_PIN_LEFT); + set_pin_low(CANE_PIN_STATUS); + set_pin_low(CANE_PIN_RIGHT); set_pin_low(CANE_PIN_LINK); cane_link_init(&link); Serial.begin(115200); Serial.println("Cane demo controller"); - Serial.println("Logic outputs only: D2 hazard, D3 unavailable, D4 route demo, D5 link."); + Serial.println("D2 left buzzer, D4 right buzzer, D3 status logic, D5 link logic."); Serial.println("No packet yet, so the link output uses the outage pattern."); if (!BLE.begin()) { diff --git a/prototype/ios/CaneDemo/CaneDemo/DebugRootView.swift b/prototype/ios/CaneDemo/CaneDemo/DebugRootView.swift index ff1c555..17637c7 100644 --- a/prototype/ios/CaneDemo/CaneDemo/DebugRootView.swift +++ b/prototype/ios/CaneDemo/CaneDemo/DebugRootView.swift @@ -11,7 +11,7 @@ struct DebugRootView: View { Text("Supervised cane demo") .font(.largeTitle) .accessibilityAddTraits(.isHeader) - Text("Not a mobility aid. The existing cane stays in use. Video stays on this iPhone. No claim here means the way is clear. A quiet hazard channel is not an all-clear.") + Text("Not a mobility aid. The existing cane stays in use. This native build does not command the left or right buzzer. Quiet is not an all-clear.") .font(.body) CameraPreview(session: demo.session) .frame(height: 220) @@ -80,24 +80,10 @@ struct DebugRootView: View { private var simulationSection: some View { VStack(alignment: .leading, spacing: 8) { - Text("Actuator bench test") + Text("Left and right buzzers") .font(.title2) - Text("These switches send a simulated packet. They do not read the camera and they do not detect an obstacle.") - Toggle("D2 hazard actuator", isOn: $demo.simulatedHazard) - .accessibilityLabel("Simulated D2 hazard actuator") - Toggle("D3 unavailable actuator", isOn: $demo.simulatedUnavailable) - .accessibilityLabel("Simulated D3 unavailable actuator") - Picker("D4 route actuator label", selection: $demo.simulatedRoute) { - Text("Off").tag(CANE_ROUTE_NONE) - Text("Left label").tag(CANE_ROUTE_DEMO_LEFT) - Text("Right label").tag(CANE_ROUTE_DEMO_RIGHT) - } - Text("D4 only means a manual left or right label is active. Neither label is a clear direction. D5 is the link actuator. The board pulses it. Disconnect Bluetooth to test the outage pattern.") - .font(.footnote) + Text("This native screen does not sound the cane buzzers. Use the Bluefy page for left and right image feedback. A camera packet from this app keeps both side bits off.") } - .onChange(of: demo.simulatedHazard) { _, _ in demo.refreshClaim() } - .onChange(of: demo.simulatedUnavailable) { _, _ in demo.refreshClaim() } - .onChange(of: demo.simulatedRoute) { _, _ in demo.refreshClaim() } } private var linkSection: some View { diff --git a/prototype/ios/CaneDemo/CaneDemo/DemoController.swift b/prototype/ios/CaneDemo/CaneDemo/DemoController.swift index 2a3bfbd..a5b7e21 100644 --- a/prototype/ios/CaneDemo/CaneDemo/DemoController.swift +++ b/prototype/ios/CaneDemo/CaneDemo/DemoController.swift @@ -10,9 +10,6 @@ final class DemoController: NSObject, ObservableObject, ARSessionDelegate, CBCen let session = ARSession() @Published var sourceIsSimulation = false - @Published var simulatedHazard = false - @Published var simulatedUnavailable = false - @Published var simulatedRoute = CANE_ROUTE_NONE @Published var cameraAuthorized = false @Published var worldTrackingSupported = false @@ -95,11 +92,6 @@ final class DemoController: NSObject, ObservableObject, ARSessionDelegate, CBCen func setSource(simulation: Bool) { sourceIsSimulation = simulation - if !simulation { - simulatedHazard = false - simulatedUnavailable = false - simulatedRoute = CANE_ROUTE_NONE - } refreshClaim() } @@ -133,17 +125,12 @@ final class DemoController: NSObject, ObservableObject, ARSessionDelegate, CBCen scene_depth_supported: Int32(sceneDepthSupported ? 1 : 0), has_plane_hit: 0, plane_hit_meters: 0, - simulation_elevated_hazard: 0, + simulation_sides: 0, simulation_unavailable: 0, - route_cue: Int32(CANE_ROUTE_NONE), - image_heuristic_elevated: 0, + image_sides: 0, image_pipeline_ready: 0 ) - if sourceIsSimulation { - input.simulation_elevated_hazard = Int32(simulatedHazard ? 1 : 0) - input.simulation_unavailable = Int32(simulatedUnavailable ? 1 : 0) - input.route_cue = Int32(simulatedRoute) - } else { + if !sourceIsSimulation { input.has_plane_hit = Int32(hasPlaneHit ? 1 : 0) input.plane_hit_meters = planeHitMeters } @@ -152,18 +139,8 @@ final class DemoController: NSObject, ObservableObject, ARSessionDelegate, CBCen private func describe(_ assessment: cane_assessment_t) -> String { let source = assessment.source == Int32(CANE_SOURCE_SIMULATION) ? "Simulation" : "Camera pipeline" - let hazard = assessment.hazard == Int32(CANE_HAZARD_ELEVATED) ? "Elevated hazard claim" : "No hazard claim" let availability = assessment.system_unavailable == 1 ? "System unavailable" : "Pipeline running" - let route: String - switch assessment.route_cue { - case Int32(CANE_ROUTE_DEMO_LEFT): - route = "Demo route cue: left label" - case Int32(CANE_ROUTE_DEMO_RIGHT): - route = "Demo route cue: right label" - default: - route = "No route cue" - } - return "\(source). \(hazard). \(availability). \(route). This is not an all-clear." + return "\(source). Sides \(assessment.sides). \(availability). This native build does not command the left or right buzzer. This is not an all-clear." } private func sendLatest() { @@ -342,7 +319,7 @@ final class DemoController: NSObject, ObservableObject, ARSessionDelegate, CBCen return } let link = status.link_state == Int32(CANE_LINK_OUTAGE) ? "link outage" : "link heartbeat" - boardText = "\(link). sequence \(status.last_sequence). pins hazard \(status.hazard_on) unavailable \(status.unavailable_on) route \(status.route_on) link \(status.link_on)" + boardText = "\(link). sequence \(status.last_sequence). pins left \(status.left_on) right \(status.right_on) status \(status.status_on) link \(status.link_on)" } private func reasonText(_ reason: ARCamera.TrackingState.Reason) -> String { diff --git a/prototype/tests/print_fixture.c b/prototype/tests/print_fixture.c index dd64388..4016bd4 100644 --- a/prototype/tests/print_fixture.c +++ b/prototype/tests/print_fixture.c @@ -16,31 +16,27 @@ static void emit(const char *name, const cane_command_t *command, int last) { printf("]}%s\n", last ? "" : ","); } -static cane_command_t command_for(int source, int hazard, int unavailable, int route, uint16_t plane_cm) { +static cane_command_t command_for(int source, int sides, int unavailable) { cane_command_t command; memset(&command, 0, sizeof(command)); command.source = source; - command.hazard = hazard; + command.sides = sides; command.system_unavailable = unavailable; - command.route_cue = route; command.tracking = CANE_TRACKING_NORMAL; - command.plane_hit_cm = plane_cm; + command.plane_hit_cm = CANE_PLANE_CM_NONE; command.sequence = 9; command.time_ms_low = 200; return command; } int main(void) { - cane_command_t simulation = command_for( - CANE_SOURCE_SIMULATION, CANE_HAZARD_ELEVATED, 0, CANE_ROUTE_DEMO_LEFT, CANE_PLANE_CM_NONE); - cane_command_t image = command_for( - CANE_SOURCE_IMAGE_HEURISTIC, CANE_HAZARD_ELEVATED, 0, CANE_ROUTE_NONE, CANE_PLANE_CM_NONE); - cane_command_t camera = command_for( - CANE_SOURCE_CAMERA, CANE_HAZARD_NO_CLAIM, 1, CANE_ROUTE_NONE, CANE_PLANE_CM_NONE); + cane_command_t left = command_for(CANE_SOURCE_SIMULATION, CANE_SIDE_LEFT, 0); + cane_command_t both = command_for(CANE_SOURCE_IMAGE_HEURISTIC, CANE_SIDE_LEFT | CANE_SIDE_RIGHT, 0); + cane_command_t none = command_for(CANE_SOURCE_CAMERA, 0, 1); printf("[\n"); - emit("simulation", &simulation, 0); - emit("image", &image, 0); - emit("camera", &camera, 1); + emit("left", &left, 0); + emit("both", &both, 0); + emit("none", &none, 1); printf("]\n"); return 0; } diff --git a/prototype/tests/test_cane.c b/prototype/tests/test_cane.c index 9e320e4..c590c0c 100644 --- a/prototype/tests/test_cane.c +++ b/prototype/tests/test_cane.c @@ -23,21 +23,12 @@ static cane_input_t camera_ready(void) { return input; } -static cane_input_t simulation_input(void) { - cane_input_t input; - memset(&input, 0, sizeof(input)); - input.source = CANE_SOURCE_SIMULATION; - input.tracking = CANE_TRACKING_NORMAL; - input.camera_authorized = 1; - input.world_tracking_supported = 1; - return input; -} - -static void test_actuator_pins(void) { - expect(CANE_PIN_HAZARD == 2, "hazard actuator logic output is D2"); - expect(CANE_PIN_UNAVAILABLE == 3, "unavailable actuator logic output is D3"); - expect(CANE_PIN_ROUTE == 4, "route actuator logic output is D4"); - expect(CANE_PIN_LINK == 5, "link actuator logic output is D5"); +static void test_pins(void) { + expect(CANE_PIN_LEFT == 2, "left buzzer is D2"); + expect(CANE_PIN_STATUS == 3, "status logic is D3"); + expect(CANE_PIN_RIGHT == 4, "right buzzer is D4"); + expect(CANE_PIN_LINK == 5, "link indicator is D5"); + expect(CANE_PACKET_VERSION == 2, "packet version is 2"); } static void test_crc_vector(void) { @@ -45,299 +36,140 @@ static void test_crc_vector(void) { expect(cane_crc16(digits, 9) == 0x29B1, "CRC-16/CCITT-FALSE vector"); } -static void test_camera_makes_no_hazard_or_route(void) { +static void test_camera_never_sets_sides(void) { cane_input_t input = camera_ready(); cane_assessment_t assessment; - input.simulation_elevated_hazard = 1; - input.route_cue = CANE_ROUTE_DEMO_LEFT; + input.simulation_sides = CANE_SIDE_LEFT; + input.image_sides = CANE_SIDE_RIGHT; input.has_plane_hit = 1; input.plane_hit_meters = 0.2f; - input.scene_depth_supported = 0; cane_assess(&input, &assessment); - expect(assessment.source == CANE_SOURCE_CAMERA, "camera source preserved"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "camera path makes no hazard claim"); - expect(assessment.route_cue == CANE_ROUTE_NONE, "camera path clears route cues"); - expect(assessment.system_unavailable == 0, "normal authorized tracking is available"); - expect(assessment.plane_hit_valid == 1, "close plane hit is recorded as context"); + expect(assessment.sides == 0, "camera path sets no side"); + expect(assessment.system_unavailable == 0, "normal camera pipeline is available"); + expect(assessment.plane_hit_valid == 1, "camera plane context can still be recorded"); expect(assessment.plane_hit_cm == 20, "0.2 m plane hit is 20 cm of context"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "plane hit does not raise a hazard"); } -static void test_camera_unavailable_conditions(void) { - cane_assessment_t assessment; +static void test_image_sides_and_faults(void) { cane_input_t input = camera_ready(); - input.tracking = CANE_TRACKING_LIMITED; - cane_assess(&input, &assessment); - expect(assessment.system_unavailable == 1, "limited tracking is unavailable"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "limited tracking is not a hazard claim"); - - input = camera_ready(); - input.tracking = CANE_TRACKING_UNAVAILABLE; - cane_assess(&input, &assessment); - expect(assessment.system_unavailable == 1, "unavailable tracking stays unavailable"); - - input = camera_ready(); - input.camera_authorized = 0; - cane_assess(&input, &assessment); - expect(assessment.system_unavailable == 1, "denied camera is unavailable"); - - input = camera_ready(); - input.world_tracking_supported = 0; - input.tracking = CANE_TRACKING_UNSUPPORTED; - cane_assess(&input, &assessment); - expect(assessment.system_unavailable == 1, "unsupported world tracking is unavailable"); - - input = camera_ready(); - input.scene_depth_supported = 0; - cane_assess(&input, &assessment); - expect(assessment.system_unavailable == 0, "missing scene depth alone is not unavailable"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "missing scene depth is not a hazard"); -} - -static void test_plane_hit_bounds(void) { cane_assessment_t assessment; - cane_input_t input = camera_ready(); + cane_command_t command; + uint8_t packet[CANE_PACKET_BYTES]; + input.source = CANE_SOURCE_IMAGE_HEURISTIC; + input.image_pipeline_ready = 1; + input.image_sides = CANE_SIDE_LEFT | CANE_SIDE_RIGHT | 0x80; input.has_plane_hit = 1; - input.plane_hit_meters = 0.0f; - cane_assess(&input, &assessment); - expect(assessment.plane_hit_valid == 0, "zero plane hit is not valid"); - - input.plane_hit_meters = -1.0f; - cane_assess(&input, &assessment); - expect(assessment.plane_hit_valid == 0, "negative plane hit is not valid"); - - input.plane_hit_meters = nanf(""); - cane_assess(&input, &assessment); - expect(assessment.plane_hit_valid == 0, "NaN plane hit is not valid"); - - input.plane_hit_meters = 25.0f; - cane_assess(&input, &assessment); - expect(assessment.plane_hit_valid == 0, "plane hit beyond 20 m is not kept"); - - input.tracking = CANE_TRACKING_LIMITED; - input.plane_hit_meters = 1.5f; + input.plane_hit_meters = 0.4f; cane_assess(&input, &assessment); - expect(assessment.plane_hit_valid == 0, "plane hit requires normal tracking"); -} + expect(assessment.sides == (CANE_SIDE_LEFT | CANE_SIDE_RIGHT), "both image sides are kept"); + expect((assessment.sides & 0x80) == 0, "extra side bits are dropped"); + expect(assessment.plane_hit_valid == 0, "image path drops a distance"); + expect(assessment.system_unavailable == 0, "fresh image path is available"); + cane_command_from_assessment(&assessment, 4, 50, &command); + expect(cane_command_encode(&command, packet) == 1, "image packet encodes"); + expect(packet[0] == 2, "image packet version is 2"); + expect(packet[2] == (CANE_SIDE_LEFT | CANE_SIDE_RIGHT), "side bits are byte 2"); + expect(packet[4] == 0, "byte 4 stays zero"); + expect(packet[14] == 0xFF && packet[15] == 0xFF, "image packet has no distance"); -static void test_simulation_channels_stay_separate(void) { - cane_assessment_t assessment; - cane_input_t input = simulation_input(); - input.simulation_elevated_hazard = 1; - input.simulation_unavailable = 1; - input.route_cue = CANE_ROUTE_DEMO_RIGHT; - input.has_plane_hit = 1; - input.plane_hit_meters = 0.3f; + input.image_pipeline_ready = 0; cane_assess(&input, &assessment); - expect(assessment.source == CANE_SOURCE_SIMULATION, "simulation source preserved"); - expect(assessment.hazard == CANE_HAZARD_ELEVATED, "simulation can request a hazard"); - expect(assessment.system_unavailable == 1, "simulation unavailable is independent"); - expect(assessment.route_cue == CANE_ROUTE_DEMO_RIGHT, "demo route is kept"); - expect(assessment.hazard == CANE_HAZARD_ELEVATED, "route cue does not clear a hazard"); + expect(assessment.sides == 0, "stale image path clears both sides"); + expect(assessment.system_unavailable == 1, "stale image path is unavailable"); - input.route_cue = 9; - input.simulation_elevated_hazard = 0; - cane_assess(&input, &assessment); - expect(assessment.route_cue == CANE_ROUTE_NONE, "unknown route value is dropped"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "simulation without the flag makes no claim"); + command.plane_hit_cm = 30; + expect(cane_command_encode(&command, packet) == 0, "image distance is rejected"); + command.plane_hit_cm = CANE_PLANE_CM_NONE; + command.sides = 0x10; + expect(cane_command_encode(&command, packet) == 0, "unknown side bit is rejected"); } -static void test_displacement_is_not_velocity(void) { - float meters = 99.0f; - expect(cane_camera_displacement_m(0, 0, 0, 3, 4, 0, 1, &meters) == 1, "3-4-5 displacement is valid"); - expect(meters > 4.999f && meters < 5.001f, "displacement magnitude is 5 m"); - meters = 99.0f; - expect(cane_camera_displacement_m(0, 0, 0, 3, 4, 0, 0, &meters) == 0, "non-normal tracking has no displacement"); - expect(meters == 99.0f, "rejected displacement does not write an output"); - expect(cane_camera_displacement_m(0, 0, 0, 0.0f / 0.0f, 0, 0, 1, &meters) == 0, "NaN pose is rejected"); +static void test_version_mismatch_is_rejected(void) { + uint8_t legacy[CANE_PACKET_BYTES]; + uint16_t crc; + cane_link_t link; + cane_outputs_t out; + memset(legacy, 0, sizeof(legacy)); + legacy[0] = 1; + legacy[1] = CANE_SOURCE_SIMULATION; + legacy[2] = 1; + legacy[4] = 1; + legacy[5] = CANE_TRACKING_NORMAL; + legacy[14] = 0xFF; + legacy[15] = 0xFF; + crc = cane_crc16(legacy, 16); + legacy[16] = (uint8_t)(crc & 0xFF); + legacy[17] = (uint8_t)(crc >> 8); + cane_link_init(&link); + expect(cane_link_accept(&link, legacy, sizeof(legacy), 1000) == 0, "version 1 packet is rejected"); + cane_link_outputs(&link, 1000, &out); + expect(out.link_state == CANE_LINK_OUTAGE, "rejected legacy packet leaves the link in outage"); + expect(out.left == 0 && out.right == 0, "rejected legacy packet does not sound a side"); } -static void fill_command(cane_command_t *command, int source, int hazard, int route) { +static void fill_sides(cane_command_t *command, int source, int sides, int unavailable) { memset(command, 0, sizeof(*command)); command->source = source; - command->hazard = hazard; - command->route_cue = route; + command->sides = sides; + command->system_unavailable = unavailable; command->tracking = CANE_TRACKING_NORMAL; command->plane_hit_cm = CANE_PLANE_CM_NONE; - command->sequence = 7; - command->time_ms_low = 1234; -} - -static void test_packet_roundtrip_and_rejection(void) { - cane_command_t command; - cane_command_t decoded; - uint8_t packet[CANE_PACKET_BYTES]; - uint8_t altered[CANE_PACKET_BYTES]; - fill_command(&command, CANE_SOURCE_SIMULATION, CANE_HAZARD_ELEVATED, CANE_ROUTE_DEMO_LEFT); - command.system_unavailable = 1; - command.plane_hit_cm = 150; - command.scene_depth_supported = 1; - expect(cane_command_encode(&command, packet) == 1, "simulation command encodes"); - expect(cane_command_decode(packet, CANE_PACKET_BYTES, &decoded) == 1, "simulation command decodes"); - expect(decoded.source == CANE_SOURCE_SIMULATION, "decoded source"); - expect(decoded.hazard == CANE_HAZARD_ELEVATED, "decoded hazard"); - expect(decoded.system_unavailable == 1, "decoded unavailable"); - expect(decoded.route_cue == CANE_ROUTE_DEMO_LEFT, "decoded route"); - expect(decoded.sequence == 7, "decoded sequence"); - expect(decoded.time_ms_low == 1234, "decoded sender time"); - expect(decoded.plane_hit_cm == 150, "decoded plane context"); - expect(decoded.scene_depth_supported == 1, "decoded scene-depth flag"); - - memcpy(altered, packet, sizeof(packet)); - altered[2] ^= 0x01; - expect(cane_command_decode(altered, CANE_PACKET_BYTES, &decoded) == 0, "flipped hazard fails CRC"); - expect(cane_command_decode(packet, CANE_PACKET_BYTES - 1, &decoded) == 0, "short packet is rejected"); - - fill_command(&command, CANE_SOURCE_CAMERA, CANE_HAZARD_ELEVATED, CANE_ROUTE_NONE); - expect(cane_command_admissible(&command) == 0, "camera hazard is not admissible"); - expect(cane_command_encode(&command, packet) == 0, "encoder refuses a camera hazard"); - - fill_command(&command, CANE_SOURCE_CAMERA, CANE_HAZARD_NO_CLAIM, CANE_ROUTE_DEMO_RIGHT); - expect(cane_command_encode(&command, packet) == 0, "encoder refuses a camera route cue"); + command->sequence = 9; + command->time_ms_low = 200; } -static void accept_simulation(cane_link_t *link, int hazard, int unavailable, int route, uint16_t plane_cm, uint32_t now_ms) { +static void accept_sides(cane_link_t *link, int sides, int unavailable, uint32_t now_ms) { cane_command_t command; uint8_t packet[CANE_PACKET_BYTES]; - fill_command(&command, CANE_SOURCE_SIMULATION, hazard, route); - command.system_unavailable = unavailable; - command.plane_hit_cm = plane_cm; - expect(cane_command_encode(&command, packet) == 1, "fixture packet encodes"); - expect(cane_link_accept(link, packet, CANE_PACKET_BYTES, now_ms) == 1, "fixture packet accepted"); + fill_sides(&command, CANE_SOURCE_SIMULATION, sides, unavailable); + expect(cane_command_encode(&command, packet) == 1, "side packet encodes"); + expect(cane_link_accept(link, packet, CANE_PACKET_BYTES, now_ms) == 1, "side packet accepted"); } -static void test_watchdog_and_outputs(void) { +static void test_patterns_and_watchdog(void) { cane_link_t link; - cane_outputs_t at_hazard; - cane_outputs_t at_other_plane; - cane_outputs_t later; + cane_outputs_t out; uint8_t bad[CANE_PACKET_BYTES]; cane_link_init(&link); - cane_link_outputs(&link, 0, &at_hazard); - expect(at_hazard.link_state == CANE_LINK_OUTAGE, "no packet starts in link outage"); - expect(at_hazard.hazard == 0 && at_hazard.unavailable == 0 && at_hazard.route == 0, "outage clears commanded pins"); - expect(at_hazard.link == 1, "outage pattern is on at t=0"); - - accept_simulation(&link, CANE_HAZARD_ELEVATED, 1, CANE_ROUTE_DEMO_LEFT, 80, 1000); - cane_link_outputs(&link, 1000, &at_hazard); - expect(at_hazard.link_state == CANE_LINK_HEARTBEAT, "fresh packet is a heartbeat"); - expect(at_hazard.hazard == 1, "hazard pattern is on at the start of its period"); - expect(at_hazard.unavailable == 1, "unavailable pattern is on at the start of its period"); - expect(at_hazard.route == 1, "route pattern is on at the start of its period"); - expect(at_hazard.link == 0, "healthy link blip is off 1000 ms into its 2000 ms period"); - - accept_simulation(&link, CANE_HAZARD_ELEVATED, 1, CANE_ROUTE_DEMO_LEFT, 400, 1000); - cane_link_outputs(&link, 1000, &at_other_plane); - expect(at_other_plane.hazard == at_hazard.hazard, "plane centimeters do not change hazard output"); - expect(at_other_plane.unavailable == at_hazard.unavailable, "plane centimeters do not change unavailable"); - expect(at_other_plane.route == at_hazard.route, "plane centimeters do not change route"); - expect(at_other_plane.link == at_hazard.link, "plane centimeters do not change link"); - - cane_link_outputs(&link, 1000 + CANE_WATCHDOG_MS, &later); - expect(later.link_state == CANE_LINK_HEARTBEAT, "elapsed watchdog limit is still a heartbeat"); - cane_link_outputs(&link, 1000 + CANE_WATCHDOG_MS + 1, &later); - expect(later.link_state == CANE_LINK_OUTAGE, "one millisecond past the watchdog is an outage"); - expect(later.hazard == 0 && later.unavailable == 0 && later.route == 0, "outage drops the last command"); + cane_link_outputs(&link, 0, &out); + expect(out.link_state == CANE_LINK_OUTAGE && out.left == 0 && out.right == 0, "startup is outage with buzzers off"); + expect(out.link == 1, "outage link pattern is on at t=0"); + + accept_sides(&link, CANE_SIDE_LEFT | CANE_SIDE_RIGHT, 0, 1000); + cane_link_outputs(&link, 1400, &out); + expect(out.left == 1 && out.right == 1, "both object sides use the object pulse at t=1400"); + expect(out.status == 0, "object warning does not raise the status pin"); + expect(cane_fault_blip(1400) == 0, "fault blip is off at t=1400"); + + accept_sides(&link, 0, 1, 1000); + cane_link_outputs(&link, 1180, &out); + expect(out.left == 1 && out.right == 1 && out.status == 1, "unavailable uses both buzzers and D3"); + expect(cane_pulse(180, CANE_OBJECT_PERIOD_MS, CANE_OBJECT_ON_MS) == 0, "object pulse is off at phase 180"); + + cane_link_outputs(&link, 1000 + CANE_WATCHDOG_MS, &out); + expect(out.link_state == CANE_LINK_HEARTBEAT, "500 ms is still inside the watchdog"); + cane_link_outputs(&link, 1000 + CANE_WATCHDOG_MS + 1, &out); + expect(out.link_state == CANE_LINK_OUTAGE && out.left == 0 && out.right == 0, "watchdog silences both buzzers"); memset(bad, 0, sizeof(bad)); expect(cane_link_accept(&link, bad, sizeof(bad), 5000) == 0, "corrupt packet is ignored"); - cane_link_outputs(&link, 1000 + CANE_WATCHDOG_MS + 1, &later); - expect(later.link_state == CANE_LINK_OUTAGE, "rejected packet does not refresh the watchdog"); -} - -static void test_patterns(void) { - expect(cane_pulse(0, CANE_LINK_OUT_PERIOD_MS, CANE_LINK_OUT_ON_MS) == 1, "outage pulse starts on"); - expect(cane_pulse(CANE_LINK_OUT_ON_MS, CANE_LINK_OUT_PERIOD_MS, CANE_LINK_OUT_ON_MS) == 0, "outage pulse turns off"); - expect(cane_pulse(0, CANE_LINK_OK_PERIOD_MS, CANE_LINK_OK_ON_MS) == 1, "heartbeat starts with a blip"); - expect(cane_pulse(CANE_LINK_OK_ON_MS, CANE_LINK_OK_PERIOD_MS, CANE_LINK_OK_ON_MS) == 0, "heartbeat blip ends"); - expect(cane_pulse(CANE_HAZARD_ON_MS, CANE_HAZARD_PERIOD_MS, CANE_HAZARD_ON_MS) == 0, "hazard pulse is half period"); -} - -static void test_status_roundtrip(void) { - cane_outputs_t outputs; - cane_status_t status; - uint8_t bytes[CANE_STATUS_BYTES]; - memset(&outputs, 0, sizeof(outputs)); - outputs.link_state = CANE_LINK_OUTAGE; - outputs.link = 1; - expect(cane_status_encode(&outputs, 42, bytes) == 1, "status encodes"); - expect(cane_status_decode(bytes, CANE_STATUS_BYTES, &status) == 1, "status decodes"); - expect(status.link_state == CANE_LINK_OUTAGE, "status link state"); - expect(status.link_on == 1, "status link pin"); - expect(status.hazard_on == 0, "status hazard pin"); - expect(status.last_sequence == 42, "status sequence"); -} - -static void test_assessment_command_is_sendable(void) { - cane_input_t input = camera_ready(); - cane_assessment_t assessment; - cane_command_t command; - uint8_t packet[CANE_PACKET_BYTES]; - input.has_plane_hit = 1; - input.plane_hit_meters = 1.25f; - cane_assess(&input, &assessment); - cane_command_from_assessment(&assessment, 3, 9, &command); - expect(cane_command_encode(&command, packet) == 1, "camera assessment encodes"); - expect(packet[1] == CANE_SOURCE_CAMERA, "packet source is camera"); - expect(packet[2] == CANE_HAZARD_NO_CLAIM, "packet hazard is no claim"); - expect(packet[4] == CANE_ROUTE_NONE, "packet route is none"); } -static void test_image_heuristic_is_not_a_distance(void) { - cane_input_t input = camera_ready(); - cane_assessment_t assessment; - cane_command_t command; - uint8_t packet[CANE_PACKET_BYTES]; - - input.image_heuristic_elevated = 1; - cane_assess(&input, &assessment); - expect(assessment.source == CANE_SOURCE_CAMERA, "camera source ignores the image flag"); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "camera source still cannot raise a hazard"); - - input = camera_ready(); - input.source = CANE_SOURCE_IMAGE_HEURISTIC; - input.image_pipeline_ready = 1; - input.image_heuristic_elevated = 1; - input.has_plane_hit = 1; - input.plane_hit_meters = 0.4f; - input.route_cue = CANE_ROUTE_DEMO_RIGHT; - cane_assess(&input, &assessment); - expect(assessment.hazard == CANE_HAZARD_ELEVATED, "fresh image heuristic can raise a hazard"); - expect(assessment.system_unavailable == 0, "fresh image pipeline is available"); - expect(assessment.route_cue == CANE_ROUTE_NONE, "image heuristic drops a route cue"); - expect(assessment.plane_hit_valid == 0, "image heuristic drops a plane distance"); - cane_command_from_assessment(&assessment, 4, 50, &command); - expect(cane_command_encode(&command, packet) == 1, "image heuristic packet encodes"); - expect(packet[1] == CANE_SOURCE_IMAGE_HEURISTIC, "packet source is image heuristic"); - expect(packet[14] == 0xFF && packet[15] == 0xFF, "image heuristic packet has no distance"); - - input.image_pipeline_ready = 0; - cane_assess(&input, &assessment); - expect(assessment.hazard == CANE_HAZARD_NO_CLAIM, "stale image pipeline clears the hazard"); - expect(assessment.system_unavailable == 1, "stale image pipeline is unavailable"); - - fill_command(&command, CANE_SOURCE_IMAGE_HEURISTIC, CANE_HAZARD_ELEVATED, CANE_ROUTE_DEMO_LEFT); - command.plane_hit_cm = CANE_PLANE_CM_NONE; - expect(cane_command_encode(&command, packet) == 0, "image heuristic route is rejected"); - fill_command(&command, CANE_SOURCE_IMAGE_HEURISTIC, CANE_HAZARD_ELEVATED, CANE_ROUTE_NONE); - command.plane_hit_cm = 30; - expect(cane_command_encode(&command, packet) == 0, "image heuristic distance is rejected"); +static void test_displacement(void) { + float meters = 99.0f; + expect(cane_camera_displacement_m(0, 0, 0, 3, 4, 0, 1, &meters) == 1, "displacement is valid"); + expect(meters > 4.999f && meters < 5.001f, "displacement magnitude is 5 m"); + expect(cane_camera_displacement_m(0, 0, 0, 3, 4, 0, 0, &meters) == 0, "non-normal tracking has no displacement"); } int main(void) { - test_actuator_pins(); + test_pins(); test_crc_vector(); - test_camera_makes_no_hazard_or_route(); - test_camera_unavailable_conditions(); - test_plane_hit_bounds(); - test_simulation_channels_stay_separate(); - test_displacement_is_not_velocity(); - test_packet_roundtrip_and_rejection(); - test_watchdog_and_outputs(); - test_patterns(); - test_status_roundtrip(); - test_assessment_command_is_sendable(); - test_image_heuristic_is_not_a_distance(); + test_camera_never_sets_sides(); + test_image_sides_and_faults(); + test_version_mismatch_is_rejected(); + test_patterns_and_watchdog(); + test_displacement(); if (failures != 0) { fprintf(stderr, "%d failure(s)\n", failures); return 1; diff --git a/web/app.js b/web/app.js index cbc187e..a8a9ba5 100644 --- a/web/app.js +++ b/web/app.js @@ -1,8 +1,9 @@ import { COMMAND_UUID, - HAZARD_ELEVATED, HEARTBEAT_MS, SERVICE_UUID, + SIDE_LEFT, + SIDE_RIGHT, SOURCE_IMAGE_HEURISTIC, SOURCE_SIMULATION, STATUS_UUID, @@ -11,7 +12,7 @@ import { encodeCommand, } from "./protocol.js"; import { claimForMode } from "./state.js"; -import { centerZoneHit, updateTracks } from "./tracking.js"; +import { sideWarnings, updateTracks } from "./tracking.js"; const TFJS_URL = "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.22.0/dist/tf.min.js"; const COCO_URL = "https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd@2.2.3/dist/coco-ssd.min.js"; @@ -29,9 +30,8 @@ const claimStatus = document.querySelector("#claim-status"); const video = document.querySelector("#video"); const overlay = document.querySelector("#overlay"); const manualMode = document.querySelector("#manual-mode"); -const manualHazard = document.querySelector("#manual-hazard"); -const manualUnavailable = document.querySelector("#manual-unavailable"); -const manualRoute = document.querySelector("#manual-route"); +const manualLeft = document.querySelector("#manual-left"); +const manualRight = document.querySelector("#manual-right"); const detectEnable = document.querySelector("#detect-enable"); const state = { @@ -43,6 +43,7 @@ const state = { lastFrameMs: null, tracks: [], zoneHit: null, + sides: 0, device: null, server: null, commandChar: null, @@ -89,26 +90,43 @@ function currentClaim() { const mode = detectEnable.checked ? "detect" : manualMode.checked ? "manual" : "idle"; return claimForMode({ mode, + sides: state.sides, + left: manualLeft.checked, + right: manualRight.checked, tabHidden: document.visibilityState === "hidden", cameraOn: state.cameraOn, modelReady: state.modelReady, modelError: state.modelError, frameAgeMs, detectionEnabled: detectEnable.checked, - zoneHit: Boolean(state.zoneHit), - hazard: manualHazard.checked, - unavailable: manualUnavailable.checked, - route: Number(manualRoute.value), }); } +function sideText(sides) { + const left = (sides & SIDE_LEFT) !== 0; + const right = (sides & SIDE_RIGHT) !== 0; + if (left && right) { + return "left and right buzzers"; + } + if (left) { + return "left buzzer"; + } + if (right) { + return "right buzzer"; + } + return "no side buzzer"; +} + function describeClaim(claim) { - const hazard = claim.hazard === HAZARD_ELEVATED ? "elevated image or manual hazard" : "no hazard claim"; - const availability = claim.unavailable ? "unavailable" : "pipeline running"; - const hit = state.zoneHit - ? `Center box: ${state.zoneHit.className} score ${state.zoneHit.score.toFixed(2)} id ${state.zoneHit.trackId}.` - : "No supported class in the center zone."; - claimStatus.textContent = `${sourceName(claim.source)}. ${hazard}. ${availability}. ${hit} This is not an all-clear.`; + const availability = claim.unavailable + ? "not-ready double blip on both buzzers. That pattern is not objects on both sides." + : "side pattern follows the image or the manual switches."; + const hit = state.zoneHit && state.zoneHit.hits.length + ? state.zoneHit.onDivider + ? "One box center is exactly on the middle line, so both buzzers mark that one spot." + : `Image sides: ${sideText(state.sides)}.` + : "No supported class in the center half of the picture."; + claimStatus.textContent = `${sourceName(claim.source)}. ${sideText(claim.sides)}. ${availability} ${hit} Quiet buzzers are not an all-clear.`; } async function sendClaim() { @@ -120,9 +138,8 @@ async function sendClaim() { state.sequence = (state.sequence + 1) >>> 0; const bytes = encodeCommand({ source: claim.source, - hazard: claim.hazard, + sides: claim.sides, unavailable: claim.unavailable, - route: claim.route, tracking: claim.tracking, sceneDepth: 0, sequence: state.sequence, @@ -184,11 +201,13 @@ async function detectOnce() { })); state.tracks = updateTracks(state.tracks, detections, now); state.lastFrameMs = now; - state.zoneHit = centerZoneHit(state.tracks, video.videoWidth, video.videoHeight, now); + state.zoneHit = sideWarnings(state.tracks, video.videoWidth, video.videoHeight, now); + state.sides = (state.zoneHit.left ? SIDE_LEFT : 0) | (state.zoneHit.right ? SIDE_RIGHT : 0); drawTracks(); - detectStatus.textContent = state.zoneHit - ? `Experimental center warning: ${state.zoneHit.className}, score ${state.zoneHit.score.toFixed(2)}, track ${state.zoneHit.trackId}. Not a distance.` - : "No supported class in the center zone. That is not an all-clear."; + const names = state.zoneHit.hits.map((hit) => `${hit.className} ${hit.side}`).join(", "); + detectStatus.textContent = state.zoneHit.hits.length + ? `Center-half boxes: ${names}. Image left is the left buzzer. Not a distance.` + : "No supported class in the center half. That is not an all-clear."; } catch (error) { state.modelError = true; state.zoneHit = null; @@ -242,6 +261,7 @@ document.querySelector("#camera-stop").addEventListener("click", () => { state.lastFrameMs = null; state.tracks = []; state.zoneHit = null; + state.sides = 0; if (state.stream) { for (const track of state.stream.getTracks()) { track.stop(); @@ -277,9 +297,8 @@ document.querySelector("#model-load").addEventListener("click", async () => { }); function manualEnabled(enabled) { - manualHazard.disabled = !enabled; - manualUnavailable.disabled = !enabled; - manualRoute.disabled = !enabled; + manualLeft.disabled = !enabled; + manualRight.disabled = !enabled; } manualMode.addEventListener("change", () => { @@ -292,9 +311,8 @@ manualMode.addEventListener("change", () => { detectEnable.addEventListener("change", () => { if (detectEnable.checked) { manualMode.checked = false; - manualHazard.checked = false; - manualUnavailable.checked = false; - manualRoute.value = "0"; + manualLeft.checked = false; + manualRight.checked = false; } manualEnabled(false); }); @@ -318,7 +336,7 @@ async function connectGatt(device) { status.addEventListener("characteristicvaluechanged", (event) => { const decoded = decodeStatus(new Uint8Array(event.target.value.buffer)); boardStatus.textContent = decoded - ? `Board ${decoded.linkState === 1 ? "link outage" : "link heartbeat"}, sequence ${decoded.sequence}, pins hazard ${decoded.hazardOn} unavailable ${decoded.unavailableOn} route ${decoded.routeOn} link ${decoded.linkOn}.` + ? `Board ${decoded.linkState === 1 ? "link outage" : "link heartbeat"}, sequence ${decoded.sequence}, pins left ${decoded.leftOn} right ${decoded.rightOn} status ${decoded.statusOn} link ${decoded.linkOn}.` : "Board status did not match the 8-byte status packet."; }); } diff --git a/web/index.html b/web/index.html index e67dc8b..45276c8 100644 --- a/web/index.html +++ b/web/index.html @@ -9,7 +9,7 @@

Cane demo

-

This page is a bench demo for a standard iPhone 16 with the Bluefy app, plus an Arduino UNO R4 WiFi. Video stays on the phone. Nothing here is a measured distance, a collision alarm, or a safe direction. A quiet hazard actuator is not an all-clear. We have not run this page on a phone.

+

This page is a bench demo for a standard iPhone 16 with the Bluefy app, plus an Arduino UNO R4 WiFi and two blue Grove buzzers. Video stays on the phone. A left box is not a command to turn left. Quiet buzzers are not an all-clear. We have not run this page on a phone or heard the buzzers.

1. What this phone can see

Checking this page.
@@ -27,37 +27,30 @@

2. Camera only

3. Model only

-

Loads a COCO-SSD model in this browser and draws boxes. Boxes are 2D image positions. They are not meters. This does not send a hazard until section 5 is on.

+

Loads a COCO-SSD model in this browser and draws boxes. Boxes are 2D image positions. They are not meters. This does not sound a buzzer until section 5 is on.

Model is not loaded.

No detection yet. No detection is not an all-clear.

4. Bluetooth only

-

Connects to the board named CaneDemo. The switches below are a manual simulation. They are not the camera.

+

Connects to the board named CaneDemo. The switches below sound one buzzer at a time. They are not the camera and they are not turn commands.

Bluetooth is not connected.

No board status yet.

- - - -

D4's left and right labels are not clear directions. D5 is the board's own link light: a slow blip while packets arrive, and a fast pattern when packets stop.

+ + +

D5 is a link light on the board, not a third buzzer. A slow blip means packets are arriving. A fast pattern means the link timed out. Quiet buzzers are not an all-clear.

5. Camera, model, and Bluetooth together

Combined warning is off.

-

The warning can turn on only for a person, bicycle, car, motorcycle, bus, or truck whose box center is in the middle of the picture and whose score is at least 0.5. That is an image guess. It is not proof of an obstacle and it does not choose a direction.

+

The same classes and the same center half of the picture still decide whether anything counts. A counted box whose center is left of the picture's middle sounds the left buzzer. Right of the middle sounds the right buzzer. A center exactly on the middle line sounds both, for that one box. Boxes outside the center half are ignored. This is left and right in the camera picture. It is not a turn instruction and not a distance.

What you do, in order

  1. Flash prototype/firmware/uno_r4_wifi/uno_r4_wifi.ino to an Arduino UNO R4 WiFi. In Arduino IDE pick that board, install ArduinoBLE 2.1.0, and use the UNO R4 board package 1.6.0. The sketch opens serial at 115200 and prints Cane demo controller and Advertising CaneDemo as a BLE peripheral.
  2. -
  3. Wire four external drivers. D2 is hazard, D3 is unavailable, D4 is the route demo, D5 is the link. Do not power an actuator from a header pin.
  4. +
  5. Plug one Grove cable from the left blue buzzer into the D2 socket, and one from the right blue buzzer into the D4 socket. Leave D3 and D5 without buzzers. Do not power a bare motor from a header pin.
  6. Install Bluefy yourself from the App Store. Open this HTTPS page in Bluefy on the iPhone 16. A laptop is not the runtime.
  7. Read section 1. Then try camera alone, model alone, and Bluetooth alone. Then turn on section 5.
  8. With a sighted spotter, check the physical outputs listed at the bottom. Do not cover anyone's eyes. The cane you already use stays the cane.
  9. @@ -67,7 +60,8 @@

    What a spotter can check

    • Before the phone connects, D5 uses the fast outage pattern and D2, D3, and D4 stay off. Serial prints link=outage.
    • After Connect, D5 changes to a short blip about every 2 seconds. Serial prints accepted source= and an increasing seq.
    • -
    • Manual D2 pulses only the hazard driver. Manual D3 pulses unavailable. Manual D4 pulses the route driver. None of those means the path is clear.
    • +
    • Manual left sounds only D2. Manual right sounds only D4. Both switches together sound the steady object pattern on both buzzers.
    • +
    • Stop the camera while connected. Both buzzers change to a double blip once a second, and D3 stays on. That double blip means not ready. It does not mean objects are on both sides.
    • Switch the phone away from Bluefy, or turn Bluetooth off. Within about a second D2, D3, and D4 go off and D5 returns to the fast pattern.
    • Come back and tap Connect if it does not reconnect. Section 5 stays off until you turn it on again.
    diff --git a/web/logic.test.mjs b/web/logic.test.mjs index 19d1a4d..e57e322 100644 --- a/web/logic.test.mjs +++ b/web/logic.test.mjs @@ -3,11 +3,9 @@ import { readFileSync } from "node:fs"; import test from "node:test"; import { - HAZARD_ELEVATED, - HAZARD_NO_CLAIM, PLANE_CM_NONE, - ROUTE_DEMO_LEFT, - ROUTE_NONE, + SIDE_LEFT, + SIDE_RIGHT, SOURCE_CAMERA, SOURCE_IMAGE_HEURISTIC, SOURCE_SIMULATION, @@ -17,16 +15,19 @@ import { createWriteQueue, encodeCommand, } from "./protocol.js"; -import { claimForMode, imageHeuristicClaim, pipelineReady } from "./state.js"; -import { centerZoneHit, iou, updateTracks } from "./tracking.js"; +import { claimForMode, imageHeuristicClaim } from "./state.js"; +import { iou, sideWarnings, updateTracks } from "./tracking.js"; function box(x, y, w, h) { return { x, y, w, h }; } +function track(id, className, score, bbox) { + return { id, className, score, bbox, lastSeen: 0 }; +} + test("crc16 matches the CCITT-FALSE vector", () => { - const digits = new TextEncoder().encode("123456789"); - assert.equal(crc16(digits), 0x29b1); + assert.equal(crc16(new TextEncoder().encode("123456789")), 0x29b1); }); test("javascript packets match the C encoder", () => { @@ -42,40 +43,25 @@ test("javascript packets match the C encoder", () => { planeCm: PLANE_CM_NONE, }; const expected = { - simulation: { - ...shared, - source: SOURCE_SIMULATION, - hazard: HAZARD_ELEVATED, - route: ROUTE_DEMO_LEFT, - }, - image: { - ...shared, - source: SOURCE_IMAGE_HEURISTIC, - hazard: HAZARD_ELEVATED, - route: ROUTE_NONE, - }, - camera: { - ...shared, - source: SOURCE_CAMERA, - hazard: HAZARD_NO_CLAIM, - unavailable: 1, - route: ROUTE_NONE, - }, + left: { ...shared, source: SOURCE_SIMULATION, sides: SIDE_LEFT }, + both: { ...shared, source: SOURCE_IMAGE_HEURISTIC, sides: SIDE_LEFT | SIDE_RIGHT }, + none: { ...shared, source: SOURCE_CAMERA, sides: 0, unavailable: 1 }, }; for (const row of fixture) { const encoded = encodeCommand(expected[row.name]); assert.ok(encoded, row.name); assert.deepEqual(Array.from(encoded), row.bytes, row.name); + assert.equal(encoded[0], 2); + assert.equal(encoded[4], 0); } }); -test("image heuristic cannot carry a distance or a route", () => { +test("version 2 rejects a camera side and a fake distance", () => { assert.equal( commandAdmissible({ - source: SOURCE_IMAGE_HEURISTIC, - hazard: HAZARD_ELEVATED, + source: SOURCE_CAMERA, + sides: SIDE_LEFT, unavailable: 0, - route: ROUTE_DEMO_LEFT, tracking: TRACKING_NORMAL, planeCm: PLANE_CM_NONE, }), @@ -84,9 +70,8 @@ test("image heuristic cannot carry a distance or a route", () => { assert.equal( encodeCommand({ source: SOURCE_IMAGE_HEURISTIC, - hazard: HAZARD_ELEVATED, + sides: SIDE_RIGHT, unavailable: 0, - route: ROUTE_NONE, tracking: TRACKING_NORMAL, sceneDepth: 0, sequence: 1, @@ -95,57 +80,44 @@ test("image heuristic cannot carry a distance or a route", () => { }), null ); - assert.equal( - commandAdmissible({ - source: SOURCE_CAMERA, - hazard: HAZARD_ELEVATED, - unavailable: 0, - route: ROUTE_NONE, - tracking: TRACKING_NORMAL, - planeCm: PLANE_CM_NONE, - }), - false - ); +}); + +test("center gate chooses left, right, both, or none", () => { + const tracks = [ + track(1, "person", 0.9, box(20, 40, 20, 20)), + track(2, "car", 0.8, box(60, 40, 20, 20)), + track(3, "person", 0.9, box(0, 0, 10, 10)), + track(4, "kite", 0.99, box(40, 40, 20, 20)), + ]; + const both = sideWarnings(tracks, 100, 100, 0); + assert.equal(both.left, true); + assert.equal(both.right, true); + assert.equal(both.onDivider, false); + + const leftOnly = sideWarnings([track(1, "person", 0.9, box(20, 40, 20, 20))], 100, 100, 0); + assert.equal(leftOnly.left, true); + assert.equal(leftOnly.right, false); + + const divider = sideWarnings([track(1, "bus", 0.9, box(40, 40, 20, 20))], 100, 100, 0); + assert.equal(divider.left, true); + assert.equal(divider.right, true); + assert.equal(divider.onDivider, true); + assert.equal(divider.hits[0].side, "divider"); + + const none = sideWarnings([track(3, "person", 0.9, box(0, 0, 10, 10))], 100, 100, 0); + assert.equal(none.left, false); + assert.equal(none.right, false); }); test("iou keeps an id and expiry drops it", () => { - const first = updateTracks( - [], - [{ className: "person", score: 0.9, bbox: box(0, 0, 10, 10) }], - 1000 - ); - const second = updateTracks( - first, - [{ className: "person", score: 0.8, bbox: box(1, 1, 10, 10) }], - 1100 - ); - assert.equal(second.length, 1); + const first = updateTracks([], [{ className: "person", score: 0.9, bbox: box(0, 0, 10, 10) }], 1000); + const second = updateTracks(first, [{ className: "person", score: 0.8, bbox: box(1, 1, 10, 10) }], 1100); assert.equal(second[0].id, first[0].id); assert.ok(iou(box(0, 0, 10, 10), box(1, 1, 10, 10)) > 0.3); - const moved = updateTracks( - second, - [{ className: "person", score: 0.8, bbox: box(100, 100, 10, 10) }], - 1200 - ); - assert.notEqual(moved[0].id, second[0].id); - const expired = updateTracks(second, [], 2000); - assert.equal(expired.length, 0); -}); - -test("center warning is 2D and ignores unsupported classes", () => { - const tracks = [ - { id: 1, className: "kite", score: 0.99, bbox: box(40, 40, 20, 20), lastSeen: 0 }, - { id: 2, className: "person", score: 0.2, bbox: box(40, 40, 20, 20), lastSeen: 0 }, - { id: 3, className: "person", score: 0.8, bbox: box(0, 0, 10, 10), lastSeen: 0 }, - { id: 4, className: "car", score: 0.7, bbox: box(40, 40, 20, 20), lastSeen: 0 }, - ]; - const hit = centerZoneHit(tracks, 100, 100, 0); - assert.equal(hit.className, "car"); - assert.equal(hit.zone, "center"); - assert.equal(centerZoneHit([], 100, 100, 0), null); + assert.equal(updateTracks(second, [], 2000).length, 0); }); -test("faults clear an image hazard and stay distinct from manual input", () => { +test("faults clear sides and stay distinct from a manual side", () => { const fresh = imageHeuristicClaim({ cameraOn: true, modelReady: true, @@ -153,33 +125,25 @@ test("faults clear an image hazard and stay distinct from manual input", () => { frameAgeMs: 100, tabHidden: false, detectionEnabled: true, - zoneHit: true, + sides: SIDE_LEFT, }); - assert.equal(fresh.source, SOURCE_IMAGE_HEURISTIC); - assert.equal(fresh.hazard, HAZARD_ELEVATED); + assert.equal(fresh.sides, SIDE_LEFT); + assert.equal(fresh.unavailable, 0); assert.equal(fresh.planeCm, PLANE_CM_NONE); - assert.equal(fresh.route, ROUTE_NONE); const stale = imageHeuristicClaim({ - cameraOn: true, + cameraOn: false, modelReady: true, modelError: false, - frameAgeMs: 1000, + frameAgeMs: 10, tabHidden: false, detectionEnabled: true, - zoneHit: true, + sides: SIDE_RIGHT, }); - assert.equal(stale.hazard, HAZARD_NO_CLAIM); + assert.equal(stale.sides, 0); assert.equal(stale.unavailable, 1); - assert.equal(pipelineReady({ - cameraOn: false, - modelReady: true, - modelError: false, - frameAgeMs: 10, - tabHidden: false, - }), false); - const hiddenManual = claimForMode({ + const hidden = claimForMode({ mode: "manual", tabHidden: true, cameraOn: false, @@ -187,25 +151,22 @@ test("faults clear an image hazard and stay distinct from manual input", () => { modelError: false, frameAgeMs: null, detectionEnabled: false, - zoneHit: false, - hazard: true, - unavailable: false, - route: ROUTE_DEMO_LEFT, + sides: 0, + left: true, + right: false, }); - assert.equal(hiddenManual.source, SOURCE_IMAGE_HEURISTIC); - assert.equal(hiddenManual.hazard, HAZARD_NO_CLAIM); - assert.notEqual(hiddenManual.source, SOURCE_SIMULATION); + assert.equal(hidden.source, SOURCE_IMAGE_HEURISTIC); + assert.equal(hidden.sides, 0); const manual = claimForMode({ mode: "manual", tabHidden: false, - hazard: true, - unavailable: false, - route: ROUTE_DEMO_LEFT, + left: true, + right: false, }); assert.equal(manual.source, SOURCE_SIMULATION); - assert.equal(manual.hazard, HAZARD_ELEVATED); - assert.equal(manual.route, ROUTE_DEMO_LEFT); + assert.equal(manual.sides, SIDE_LEFT); + assert.equal(manual.unavailable, 0); }); test("writes stay in order", async () => { diff --git a/web/protocol.js b/web/protocol.js index 2c3483c..c1849aa 100644 --- a/web/protocol.js +++ b/web/protocol.js @@ -2,7 +2,7 @@ export const SERVICE_UUID = "8f3a1c20-7b6e-4d5a-9c11-6e2f0a9b1001"; export const COMMAND_UUID = "8f3a1c20-7b6e-4d5a-9c11-6e2f0a9b1002"; export const STATUS_UUID = "8f3a1c20-7b6e-4d5a-9c11-6e2f0a9b1003"; -export const PACKET_VERSION = 1; +export const PACKET_VERSION = 2; export const PACKET_BYTES = 18; export const PLANE_CM_NONE = 0xffff; export const WATCHDOG_MS = 500; @@ -13,12 +13,8 @@ export const SOURCE_CAMERA = 1; export const SOURCE_SIMULATION = 2; export const SOURCE_IMAGE_HEURISTIC = 3; -export const HAZARD_NO_CLAIM = 0; -export const HAZARD_ELEVATED = 1; - -export const ROUTE_NONE = 0; -export const ROUTE_DEMO_LEFT = 1; -export const ROUTE_DEMO_RIGHT = 2; +export const SIDE_LEFT = 0x01; +export const SIDE_RIGHT = 0x02; export const TRACKING_UNSUPPORTED = 0; export const TRACKING_UNAVAILABLE = 1; @@ -47,31 +43,24 @@ export function commandAdmissible(command) { ) { return false; } - if (command.hazard !== HAZARD_NO_CLAIM && command.hazard !== HAZARD_ELEVATED) { + const sides = command.sides & 0xff; + if ((sides & ~0x03) !== 0) { return false; } if (command.unavailable !== 0 && command.unavailable !== 1) { return false; } - if ( - command.route !== ROUTE_NONE && - command.route !== ROUTE_DEMO_LEFT && - command.route !== ROUTE_DEMO_RIGHT - ) { - return false; - } if (command.tracking < TRACKING_UNSUPPORTED || command.tracking > TRACKING_NORMAL) { return false; } - if (command.source === SOURCE_CAMERA) { - if (command.hazard !== HAZARD_NO_CLAIM || command.route !== ROUTE_NONE) { - return false; - } + if (command.source === SOURCE_CAMERA && sides !== 0) { + return false; } - if (command.source === SOURCE_IMAGE_HEURISTIC) { - if (command.route !== ROUTE_NONE || command.planeCm !== PLANE_CM_NONE) { - return false; - } + if ( + (command.source === SOURCE_IMAGE_HEURISTIC || command.source === SOURCE_SIMULATION) && + command.planeCm !== PLANE_CM_NONE + ) { + return false; } return true; } @@ -95,9 +84,9 @@ export function encodeCommand(command) { const bytes = new Uint8Array(PACKET_BYTES); bytes[0] = PACKET_VERSION; bytes[1] = command.source; - bytes[2] = command.hazard; + bytes[2] = command.sides & 0x03; bytes[3] = command.unavailable; - bytes[4] = command.route; + bytes[4] = 0; bytes[5] = command.tracking; bytes[6] = command.sceneDepth ? 1 : 0; bytes[7] = 0; @@ -118,9 +107,9 @@ export function decodeStatus(bytes) { } return { linkState: bytes[1], - hazardOn: (bytes[2] & 1) !== 0, - unavailableOn: (bytes[2] & 2) !== 0, - routeOn: (bytes[2] & 4) !== 0, + leftOn: (bytes[2] & 1) !== 0, + rightOn: (bytes[2] & 2) !== 0, + statusOn: (bytes[2] & 4) !== 0, linkOn: (bytes[2] & 8) !== 0, sequence: (bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24)) >>> 0, }; diff --git a/web/state.js b/web/state.js index b1d35f4..3686fb2 100644 --- a/web/state.js +++ b/web/state.js @@ -1,8 +1,7 @@ import { - HAZARD_ELEVATED, - HAZARD_NO_CLAIM, PLANE_CM_NONE, - ROUTE_NONE, + SIDE_LEFT, + SIDE_RIGHT, SOURCE_IMAGE_HEURISTIC, SOURCE_SIMULATION, STALE_MS, @@ -20,13 +19,16 @@ export function pipelineReady(input) { return true; } +function cleanSides(sides) { + return sides & (SIDE_LEFT | SIDE_RIGHT); +} + export function imageHeuristicClaim(input) { const ready = pipelineReady(input); return { source: SOURCE_IMAGE_HEURISTIC, - hazard: ready && input.detectionEnabled && input.zoneHit ? HAZARD_ELEVATED : HAZARD_NO_CLAIM, + sides: ready && input.detectionEnabled ? cleanSides(input.sides) : 0, unavailable: ready ? 0 : 1, - route: ROUTE_NONE, planeCm: PLANE_CM_NONE, sceneDepth: 0, tracking: ready ? TRACKING_NORMAL : TRACKING_UNAVAILABLE, @@ -35,18 +37,20 @@ export function imageHeuristicClaim(input) { } export function manualClaim(input) { - let route = input.route; - if (route !== 0 && route !== 1 && route !== 2) { - route = ROUTE_NONE; + let sides = 0; + if (input.left) { + sides |= SIDE_LEFT; + } + if (input.right) { + sides |= SIDE_RIGHT; } return { source: SOURCE_SIMULATION, - hazard: input.hazard ? HAZARD_ELEVATED : HAZARD_NO_CLAIM, - unavailable: input.unavailable ? 1 : 0, - route, + sides, + unavailable: 0, planeCm: PLANE_CM_NONE, sceneDepth: 0, - tracking: input.tabHidden ? TRACKING_UNAVAILABLE : TRACKING_NORMAL, + tracking: TRACKING_NORMAL, experimental: false, }; } diff --git a/web/styles.css b/web/styles.css index f2c601b..dcac6e4 100644 --- a/web/styles.css +++ b/web/styles.css @@ -50,6 +50,7 @@ video, canvas { width: 100%; height: auto; display: block; + transform: none; } canvas { diff --git a/web/tracking.js b/web/tracking.js index b551c4a..ddd0f25 100644 --- a/web/tracking.js +++ b/web/tracking.js @@ -92,3 +92,47 @@ export function centerZoneHit(tracks, width, height, nowMs) { } return best; } + +export function sideWarnings(tracks, width, height, nowMs) { + const result = { left: false, right: false, onDivider: false, hits: [] }; + if (width <= 0 || height <= 0) { + return result; + } + const x0 = width * CENTER_MARGIN; + const x1 = width * (1 - CENTER_MARGIN); + const y0 = height * CENTER_MARGIN; + const y1 = height * (1 - CENTER_MARGIN); + const midpoint = width / 2; + for (const track of tracks) { + if (nowMs - track.lastSeen > TRACK_MAX_AGE_MS) { + continue; + } + if (!SUPPORTED_CLASSES.includes(track.className) || track.score < MIN_SCORE) { + continue; + } + const cx = track.bbox.x + track.bbox.w / 2; + const cy = track.bbox.y + track.bbox.h / 2; + if (cx < x0 || cx > x1 || cy < y0 || cy > y1) { + continue; + } + let side = "left"; + if (cx > midpoint) { + side = "right"; + result.right = true; + } else if (cx < midpoint) { + result.left = true; + } else { + side = "divider"; + result.left = true; + result.right = true; + result.onDivider = true; + } + result.hits.push({ + trackId: track.id, + className: track.className, + score: track.score, + side, + }); + } + return result; +}