Repository navigation
fix(steps): arm the live IMU stream during step calibration (gen5) #1059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,12 @@ class _StepCalibrationScreenState extends State<StepCalibrationScreen> { | |
|
|
||
| bool _busy = false; | ||
|
|
||
| /// The [AppState] this screen asked to own the live IMU stream while a | ||
| /// routine runs. Held as a captured reference (not re-read from `context`) | ||
| /// so [dispose] can release it without touching an unmounted context (§4.5), | ||
| /// the same pattern the live-HR screens use for `retainLiveHrView`. | ||
| AppState? _imuOwner; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
|
|
@@ -81,9 +87,27 @@ class _StepCalibrationScreenState extends State<StepCalibrationScreen> { | |
| @override | ||
| void dispose() { | ||
| _ticker?.cancel(); | ||
| // Backstop for every exit path: leaving the window held would keep the | ||
| // 100 Hz IMU stream (and its battery cost) armed after the screen is gone. | ||
| _releaseImuWindow(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| /// The band's live step count this screen shows is the band's own 100 Hz IMU | ||
| /// stream. Since #287 a plain gen5 foreground owns no live stream, so the | ||
| /// routine must request the IMU stream explicitly through the bounded | ||
| /// movement-sampling owner, or the feedback count never leaves 0 (#1047). | ||
| void _retainImuWindow(AppState app) { | ||
| _imuOwner = app..setMovementSamplingWindow(true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Fallback still blocks calibration IMU When the radio fallback latch is active when a calibration routine starts, Clear the fallback before starting calibration, or detect the suppressed IMU intent and tell the wearer the routine cannot collect live counts. |
||
| } | ||
|
|
||
| /// Release the IMU window if one is held. Safe to call when nothing was | ||
| /// held, and idempotent, so every routine-ending path can call it (§4.3). | ||
| void _releaseImuWindow() { | ||
| _imuOwner?.setMovementSamplingWindow(false); | ||
| _imuOwner = null; | ||
| } | ||
|
|
||
| Future<void> _loadProfiles() async { | ||
| for (final c in StepCategory.values) { | ||
| _profiles[c] = await LocalDb.liveStepGainProfile(c); | ||
|
|
@@ -132,6 +156,7 @@ class _StepCalibrationScreenState extends State<StepCalibrationScreen> { | |
| }; | ||
| }); | ||
| if (begun.session != null) { | ||
| _retainImuWindow(app); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Startup steps are excluded When the wearer starts walking before the IMU stream has finished arming, Arm the IMU before starting the calibration session and take the baseline only once the stream is active.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A second calibration routine can lose steps from its band count. The band baseline for a routine includes the partial minute left by the previous routine. Stopping the IMU stream does not clear that partial state. Only connect and disconnect call Stream stops must not leave a partial chunk whose clock spans the pause. The band baseline and the readout it subtracts from must use the same sample history. Choose one of these approaches:
This change newly exposes calibration routines to the path, because the routine now acquires and releases the IMU owner. The same discard also affects any mid-session stall.
|
||
| _ticker = Timer.periodic(Motion.tick, (_) => _tick()); | ||
| } | ||
| } | ||
|
|
@@ -206,6 +231,7 @@ class _StepCalibrationScreenState extends State<StepCalibrationScreen> { | |
|
|
||
| void _cancel() { | ||
| _ticker?.cancel(); | ||
| _releaseImuWindow(); | ||
| setState(() { | ||
| _session = null; | ||
| _routineStart = null; | ||
|
|
@@ -282,6 +308,10 @@ class _StepCalibrationScreenState extends State<StepCalibrationScreen> { | |
| // §4.5: `context` may not be used after an await without this. | ||
| if (!mounted) return; | ||
| final out = s.finish(countedSteps: counted); | ||
| // The band's counts are now read off the session; the routine is over on | ||
| // every branch below, so stop owning the live stream here. (An early | ||
| // `!mounted` return above leaves `dispose` to release it.) | ||
| _releaseImuWindow(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Failed read leaves calibration stuck When an Android household routine's final pedometer flush or total read throws, Release the IMU owner and restore the routine's busy state in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Finished routine release untested The added tests cover cancellation and disposal but never complete a routine, so a regression that leaves the IMU stream armed after successful completion would pass the new lifecycle suite unnoticed. Add a widget test that completes a calibration routine and asserts From `push: tags: ['v*']` — it doesn't touch PRs. `test/` is large and not flat
(it has `adapters/`, `support/`, and other subdirectories alongside the
top-level test files). Several regression tests are named for the bug they pin
(`readiness_flash_test`, `readiness_freeze_test`, `readiness_saturation_test`,
`readiness_baseline_pollution_test`). A behavior change with no accompanying
test is a real finding — CI passing doesn't mean the right test exists. |
||
| final l = AppLocalizations.of(context); | ||
|
|
||
| if (out.bandSilent) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // The band's live step count on the calibration screen is a direct readout of | ||
| // the band's own 100 Hz IMU stream. Since #287 a plain gen5 foreground owns no | ||
| // live stream, so unless the calibration routine explicitly requests the IMU | ||
| // stream the feedback count sits at 0 forever (#1047: "step count calibration | ||
| // not working" — live HR worked, steps stuck at 0, gen5 firmware 50.41). | ||
| // | ||
| // These tests pin the fix at the owner seam (what the screen asks the engine | ||
| // for), not the wire: starting a routine must make the app an owner of the IMU | ||
| // stream (`movementSampling`), and EVERY routine-ending path must release it — | ||
| // a sticky owner left armed is the §4.3 latch bug the repo keeps reintroducing. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:provider/provider.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:sqflite_common_ffi/sqflite_ffi.dart'; | ||
|
|
||
| import 'package:openstrap_edge/ble/ble_engine.dart'; | ||
| import 'package:openstrap_edge/data/db.dart'; | ||
| import 'package:openstrap_edge/state/app_state.dart'; | ||
| import 'package:openstrap_edge/ui2/profile/step_calibration.dart'; | ||
| import 'package:openstrap_edge/ui2/ui2.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| const name = 'step_calibration_imu_stream_test.db'; | ||
|
|
||
| setUpAll(() async { | ||
| sqfliteFfiInit(); | ||
| databaseFactory = databaseFactoryFfi; | ||
| await LocalDb.close(); | ||
| LocalDb.dbName = name; | ||
| await databaseFactory.deleteDatabase( | ||
| p.join(await databaseFactory.getDatabasesPath(), name), | ||
| ); | ||
| }); | ||
|
|
||
| setUp(() { | ||
| SharedPreferences.setMockInitialValues({}); | ||
| BleEngine.resetBandClaimForTest(); | ||
| }); | ||
| tearDown(BleEngine.resetBandClaimForTest); | ||
|
|
||
| tearDownAll(() async { | ||
| await LocalDb.close(); | ||
| await databaseFactory.deleteDatabase( | ||
| p.join(await databaseFactory.getDatabasesPath(), name), | ||
| ); | ||
| }); | ||
|
|
||
| // initState kicks off `_loadProfiles`, four real sqflite reads on a | ||
| // background isolate. pumpAndSettle only drives the fake-async frame clock | ||
| // and would let those reads land after the test (against a closed DB), so | ||
| // flush the real async with runAsync — the pattern the MetricDetail test | ||
| // uses — before each test finishes. | ||
| Future<void> settle(WidgetTester t) async { | ||
| for (var i = 0; i < 8; i++) { | ||
| await t.runAsync( | ||
| () => Future<void>.delayed(const Duration(milliseconds: 20))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Test teardown closes database early When database reads take longer than the fixed 160 ms wait, Await |
||
| await t.pump(const Duration(milliseconds: 16)); | ||
| } | ||
| } | ||
|
|
||
| Future<AppState> pump(WidgetTester t) async { | ||
| t.view.physicalSize = const Size(390 * 3, 2400 * 3); | ||
| t.view.devicePixelRatio = 3; | ||
| addTearDown(t.view.reset); | ||
| final app = AppState.forTesting(); | ||
| addTearDown(app.dispose); | ||
| await t.pumpWidget(MaterialApp( | ||
| theme: buildTheme(Brightness.light), | ||
| home: ChangeNotifierProvider<AppState>.value( | ||
| value: app, | ||
| child: const StepCalibrationScreen(), | ||
| ), | ||
| )); | ||
| await settle(t); | ||
| return app; | ||
| } | ||
|
|
||
| // Start a gait routine ("Slow walk"): its reference is the wearer, not the | ||
| // phone, so it touches no platform channel and starts cleanly in a test. | ||
| Future<void> startSlowWalk(WidgetTester t) async { | ||
| await t.tap(find.text('Slow walk')); | ||
| // _start awaits beginCalibrationRoutine (no real async for gait) then arms | ||
| // the window and installs a 1 Hz ticker. Flush microtasks; do NOT | ||
| // pumpAndSettle — the periodic ticker never settles. | ||
| await t.pump(); | ||
| await t.pump(const Duration(milliseconds: 20)); | ||
| await settle(t); | ||
| } | ||
|
|
||
| testWidgets('starting a routine makes the app own the live IMU stream', | ||
| (t) async { | ||
| final app = await pump(t); | ||
| expect(app.debugLiveOwners.movementSampling, isFalse, | ||
| reason: 'nothing owns the IMU stream before a routine starts'); | ||
|
|
||
| await startSlowWalk(t); | ||
| expect(app.debugLiveOwners.movementSampling, isTrue, | ||
| reason: 'the running routine must request the IMU stream, or its ' | ||
| 'live band-step feedback never leaves 0 (#1047)'); | ||
| }); | ||
|
|
||
| testWidgets('cancelling a routine releases the IMU stream', (t) async { | ||
| final app = await pump(t); | ||
| await startSlowWalk(t); | ||
| expect(app.debugLiveOwners.movementSampling, isTrue); | ||
|
|
||
| await t.tap(find.text('Cancel')); | ||
| await t.pump(); | ||
| expect(app.debugLiveOwners.movementSampling, isFalse, | ||
| reason: 'the IMU owner must not stick past a cancelled routine (§4.3)'); | ||
| }); | ||
|
|
||
| testWidgets('disposing the screen mid-routine releases the IMU stream', | ||
| (t) async { | ||
| final app = await pump(t); | ||
| await startSlowWalk(t); | ||
| expect(app.debugLiveOwners.movementSampling, isTrue); | ||
|
|
||
| // Replace the screen with an empty route → the state is disposed. | ||
| await t.pumpWidget(const MaterialApp(home: SizedBox.shrink())); | ||
| await t.pump(); | ||
| expect(app.debugLiveOwners.movementSampling, isFalse, | ||
| reason: 'dispose is the backstop that frees the stream when the screen ' | ||
| 'is torn down mid-routine (§4.3)'); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Hidden calibration drains battery
When the app backgrounds during an active calibration,
_retainImuWindowleaves movement sampling enabled, andLiveStreamController.ownersanddesiredLiveStreamscontinue requesting the 100 Hz IMU stream while the screen is hidden, draining battery.Gate the IMU owner on foreground state and resume sampling when the app returns to the foreground.
Also at
lib/ui2/profile/step_calibration.dart:159.