Repository navigation
8.1: Bluetooth layout notice, update-start fix, ESC, BMS and cruise fixes - #141
Draft
zjwhitehead wants to merge 8 commits into
Draft
zjwhitehead wants to merge 8 commits into
zjwhitehead wants to merge 8 commits into
Conversation
…sted From an audit against defects found while testing the Rust port: - Never suppress the throttle command unless DISARMED. A relay request reports active as soon as it is queued, before the session rejects it for being armed, so each relay write from a bonded client in flight dropped a throttle frame. - Clear the TWAI transmit queue once frames have been waiting for two 20 ms cycles. With the ESC not acknowledging, about 36 old commands were kept and replayed oldest-first on reconnect while current ones were dropped. - Recover from bus-off. Previously the controller stayed off the bus until the next power cycle. - Guard setESCThrottle when the TWAI driver failed to install. - TWAI receive queue 5 -> 32: one telemetry reply is exactly five frames. - Alert event queue 20 -> 64: an ESC disconnect emits 25 events at once and dropped events leave stale alerts on screen. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
8.1 adds the ESC relay characteristics ahead of telemetry and the update service, which moves every later attribute. A bonded phone caches where attributes were, and nothing told it they had moved, so after updating from 8.0 a phone can subscribe to the wrong attribute and never receive telemetry. Android works around this in the app; iOS has no way to. On the first boot of a new build with a bond present, raise the standard Service Changed indication once. The stack records it against each stored bond and delivers it when that phone reconnects. The build is identified by the image's ELF hash, kept in its own NVS namespace. Bench: fires once on the first boot of a new build and not again; boots cleanly with an existing bond. Not yet verified with a phone, which is the part that matters. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
8.0 requested the faster connection parameters after esp_ota_begin(). The update hardening moved the request ahead of it so the longer supervision timeout would cover the erase. In practice the phone schedules the change while the erase has the radio stalled, the two sides end up on different timing, and the link dies exactly one supervision timeout (6 s) later with the start command unanswered. Bench, Pixel 9 Pro, target slot holding a full image so the erase takes about 2 s: before, 1 of 4 starts got through (and 3 of 4 failed in an earlier mixed set); after, 8 of 8. Starts with a short erase (slot already blank) were not affected. Restores the 8.0 order and keeps the 6 s timeout value. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Ten host tests that replay each loss case mid-transfer (acknowledgement lost, final packet lost, first or middle packet lost, stale packets, re-send after the idle abort) and check the ACK sequence, that the flash image still matches, and that each sector is written exactly once. They pin the behaviour the phone apps' retry rule relies on. No firmware change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The stored build record misses one case: this build ran on the controller before, 8.0 (which keeps no record) was installed and a phone paired with it, and the same build was then installed again. Nothing looked new, no notice went out, and an iPhone was left on the 8.0 layout until re-paired. Seen on the bench with an iPhone 11. The first boot after any over-the-air update is still unconfirmed when Bluetooth starts, so treat that as a new build too. Bench: the same build installed three times from the iPhone with 8.0 and a fresh pairing in between; the notice printed each time and telemetry was back in about 2.9 s each time. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
When the ESC stopped acknowledging, 6cfb50e cleared the TWAI transmit queue, but twai_clear_transmit_queue() in the pinned ESP-IDF 4.4.7 driver leaves the frame already loaded in the controller's transmit buffer. The controller retries that frame until something acknowledges it, so the throttle command from the moment the ESC went silent was the first thing the ESC acted on when it came back. On a stall the driver is now stopped and restarted instead: twai_stop() puts the controller in reset mode, which halts that transmission (twai_hal_stop), and twai_start() puts it straight back on the bus before the current command is queued. The same reconnect check found a second path. While the controller is off the bus (bus-off, recovering, stopped) twai_transmit() refuses every frame and libcanard keeps them, so after recovery 32 old commands went out first and the current one had been refused because the 1 KiB pool was full. The transport queue is now emptied on a stall or whenever the driver is not running. The pinned CanardAdapter has no call for that, so a node that accepts nothing is registered on it to reach the shared libcanard queue. New native-emu-can environment (added to CI) runs the real esc.cpp with the pinned SINE-ESC-CAN CanardAdapter, SineEsc and libcanard against a model of the IDF 4.4.7 TWAI driver. Before this change 4 of 6 tests failed: on reconnect after 5 s silent at full power the ESC received {19000, 10350} instead of {10350}, and after bus-off recovery it received 32 copies of the bus-off era command and not the current one. Disabling either half of the fix fails its two tests. On reconnect the ESC can still receive the command from the previous 20 ms cycle, the same age as on a healthy bus. Not run against a real ESC. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
BMS_CAN::begin() (Adafruit MCP2515 0.2.1) sets RXB0 to receive any message with rollover off. Per the MCP2515 data sheet (4.2.1 and the receive flowchart), a frame that arrives while RXB0 is still full is then dropped with EFLG.RX0OVR set, and RXB1 is never used. The INT pin is not used and bmsTask drains the chip every 100 ms, so the controller kept only the first BMS frame of each 100 ms window and silently lost the rest, newer values included. The link still reads connected because any frame refreshes it, so the pilot sees pack voltage, current, SOC, power, cell voltages or temperatures that may be older than they look, with no indication. After BMS_CAN::begin(), RXB0CTRL is now written with rollover enabled (the same value Linux's mcp251x driver uses), so a second frame waits in RXB1. On every drain EFLG.RX0OVR/RX1OVR are read, counted and cleared, and the total is logged on USB serial at most every 10 s. BMS_CAN keeps its MCP2515 private, so this uses a second Adafruit_SPIDevice with the library's chip select and SPI settings, inside the same SPI bus mutex. Loss is reduced and now visible, not prevented: a third frame inside one 100 ms window, or frames arriving while the display holds the shared SPI bus, are still dropped by the chip. Preventing it would need the MCP2515 interrupt or a faster drain; the counter is there to show from a real pack whether that is needed. The native-emu-can environment now also runs the pinned Adafruit MCP2515 driver and BMS_CAN against a model of the MCP2515 receive buffers. With the rollover write removed (today's configuration), 2 of 4 tests fail: the second of two frames between drains is lost (highest cell 0 instead of 4.120 V; pack voltage left at the older 50.3 V instead of 50.2 V). With it, all 4 pass. Not run against a real BMS. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
handleThrottle() copied a setpoint from the BLE throttle characteristic straight into the cruise output, so a phone moving cruise from 1100 to 1600 us stepped the motor by 500 us in one 20 ms tick, where the lever is limited to 8 us (chill) or 27 us (sport) per tick up and twice that down. The range check (idle to CRUISE_MAX_PWM) also lived only in the BLE handler, not where the value is used. The cruise branch now calls cruiseOutputTick() in throttle.cpp: the setpoint latches the output being flown on entry as before, a BLE setpoint replaces it only if isCruiseSetpointAllowed() (the same rule the BLE handler now shares), and the output reaches it through applyModeRampClamp(), the ramp and clamp armed throttle uses. Arming state is checked at both ends as before. The performance-mode write is untouched, so changing mode while armed with the throttle off still applies on the next tick. Before the fix, 2 of 4 new test_throttle tests failed: the first tick after a 1600 us setpoint output 1600 instead of 1108 (chill) / 1127 (sport), the first tick back down output 1035 instead of 1584 / 1546, and setpoints 1681, 1950, 1000 and 65535 reached the output. native-test now passes 132 of 132. Not run on hardware. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Six fixes for the 8.1 line. The first is needed for anyone updating from 8.0 with an iPhone; the second is the ESC command-path fix already proposed for master in #140; the third stops updates started from 8.1 on Android failing on the first attempt. Fixes 4 to 6 close three defects found while testing the others: an old throttle command sent when the ESC reconnects, BMS frames lost between reads, and a cruise setpoint from the app that stepped the motor.
1. Tell bonded phones when a new build changes the Bluetooth layout
8.1 adds the ESC relay and QC record characteristics ahead of telemetry and the update service, which moves every later attribute. A bonded phone caches where attributes were, and nothing told it they had moved.
On the first boot of a new build with a bond present, the controller now raises the standard Service Changed indication once. The stack records it against each stored bond and delivers it when that phone reconnects. The build is identified by the image's ELF hash, kept in its own NVS namespace, so this also covers every later update.
Tested on an iPhone 11 (iOS 27.0.1) against a bench controller, updating over the air from 8.0:
The console prints
[BLE] New firmware build: told bonded phones the GATT layout changedonce, on the first boot of the new build, and not again.The notice also goes out on the first boot after any over-the-air update, even when the build record matches. Without that, installing a build that had run on the controller before, with 8.0 and a fresh pairing in between, sent nothing and left the iPhone on the 8.0 layout (seen on the bench). With it, the same build installed three times that way printed the notice each time and telemetry was back in about 2.9 s each time.
2. Keep ESC commands current when the CAN bus stalls or a relay is requested
Same change as #140, cherry-picked onto this line:
setESCThrottlewhen the TWAI driver failed to install.3. Ask for update connection timing after the flash erase, not before
8.0 requested the faster connection parameters after
esp_ota_begin(). The update hardening on this line moved the request ahead of it. On Android the phone then applies the change while the erase has the radio stalled, the two sides end up on different timing, and the link dies exactly one supervision timeout (6 s) later with the start command unanswered. It only shows when the target slot holds a full image, which is every real update, so a pilot on 8.1 would see the first attempt fail and the second succeed.This restores the 8.0 order and keeps the 6 s timeout value. The host test that pinned the old order is updated.
Bench, target slot holding a full image each time:
The iPhone was not affected by the bug, and the change costs it nothing.
Also added: ten host tests that replay each packet-loss case mid-transfer (acknowledgement lost, final packet lost, first or middle packet lost, stale packets, re-send after the idle abort) and check the ACK sequence, that the image still matches, and that each sector is written once. No firmware change; they pin the behaviour the apps' new retry rule depends on.
4. Abort the stuck ESC frame and drop queued commands after a CAN stall
Fix 2 clears the driver's transmit queue after a stall, but two paths still delivered old commands:
twai_clear_transmit_queue()leaves the frame already loaded in the controller's transmit buffer, and the controller retries it until something acknowledges it. The ESC acted on the command from the moment it went silent as soon as it came back.On a detected stall the controller now stops and restarts the TWAI driver, which is the only way this ESP-IDF version halts a frame in transmission, and empties the transport's queue. It also empties that queue whenever the driver is not running. It only acts after two consecutive 20 ms cycles with frames still waiting, so it never fires on a healthy bus.
esc.cppagainst a model of the TWAI driver)Still possible: on reconnect the ESC can receive the command from the previous 20 ms cycle, the same age as on a healthy bus.
5. Use both MCP2515 receive buffers for the BMS and count dropped frames
The BMS library leaves the CAN chip's first receive buffer accepting everything with rollover off, so the second buffer is never used. The BMS task reads the chip every 100 ms with no interrupt line, so only the first frame in each window survived and newer ones were dropped silently. Any frame keeps the link showing as connected, so pack voltage, current, cell voltages and temperatures could be stale with no indication.
Rollover is now enabled, so a second frame waits in the second buffer. On every read the chip's two overflow flags are read, counted and cleared, and a running total is printed on USB serial at most every 10 s. If the register write does not read back, the console prints
BMS CAN: receive rollover not enabledat start-up.This reduces loss and makes it visible; it does not prevent it. A third frame inside one 100 ms window, or frames arriving while the display holds the shared SPI bus, are still dropped. Preventing that needs the chip's interrupt line or a faster read; the counter is there to show whether that is needed.
6. Ramp a cruise setpoint written over Bluetooth like the throttle lever
A cruise setpoint written to the BLE characteristic was copied straight to the output, so a change from 1100 to 1600 µs reached the motor in one 20 ms tick. The range check existed only in the BLE handler.
The setpoint is now checked again in the throttle task (not below idle, not above the cruise maximum) and reached through the same ramp and clamp the lever uses. A change from the app takes up to about 1.3 s in chill or 0.5 s in sport to arrive. Entering cruise and the held output are unchanged. Changing performance mode while armed at idle is untouched and has a test.
Checks for 4 to 6: native tests 132 of 132, a new CAN/BMS emulation suite 10 of 10 (added to CI), update and settings emulation suites unchanged, firmware build, cpplint, and 38 of 38 screenshot tests with no reference changed.
Not verified
Known and not addressed here
About one update in thirty from the Pixel loses its link mid-transfer (2 of 34 full updates on this build, at sectors 187 and 181 of 277). The controller console for the second shows it ending the connection itself with reason 573 (HCI 0x3D, MIC failure) straight after acknowledging a sector, with no reset; the phone hears nothing and times out 6 s later at −47 dBm. The controller stays on its current firmware and the next attempt succeeds. Not seen in about 20 iPhone updates. The cause inside the Bluetooth controller is not known, and whether 8.0 does the same has not been measured.
One fresh pairing on 8.0 needed three pairing prompts before it held (two later ones needed one). The handler involved is unchanged in 8.1. Cause unknown; the phone recovered by itself.
🤖 Generated with Claude Code