From bcc47741f826aaf65726923cbec9d65416a1b4db Mon Sep 17 00:00:00 2001 From: JP Date: Fri, 6 Nov 2026 16:35:35 +0100 Subject: [PATCH 1/7] build(android): upgrade Gradle toolchain for Java 25 Gradle 8.14 cannot run on Java 25. Move to the versions of the Flutter 3.47 template: Gradle 9.3.1, AGP 9.1.0 and Kotlin 2.4.0. Required by the upgrade: - Remove the unused flutter_headset_detector, which calls jcenter(). That method no longer exists in Gradle 9 and no newer release is available. - Set android.newDsl and android.builtInKotlin to false, as in the template. - Replace proguard-android.txt, which AGP 9 rejects. -dontoptimize keeps the previous release behavior. Co-Authored-By: Claude Opus 5.5 --- open_wearable/android/app/build.gradle | 2 +- open_wearable/android/app/proguard-rules.pro | 3 +++ open_wearable/android/gradle.properties | 4 ++-- .../android/gradle/wrapper/gradle-wrapper.properties | 2 +- open_wearable/android/settings.gradle | 4 ++-- open_wearable/ios/Podfile.lock | 6 ------ open_wearable/pubspec.lock | 8 -------- open_wearable/pubspec.yaml | 1 - 8 files changed, 9 insertions(+), 21 deletions(-) diff --git a/open_wearable/android/app/build.gradle b/open_wearable/android/app/build.gradle index 30ee3d7cb..fb0161f69 100644 --- a/open_wearable/android/app/build.gradle +++ b/open_wearable/android/app/build.gradle @@ -38,7 +38,7 @@ android { signingConfig = signingConfigs.debug minifyEnabled = true shrinkResources = true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } diff --git a/open_wearable/android/app/proguard-rules.pro b/open_wearable/android/app/proguard-rules.pro index 232a0d400..ba0ea4dac 100644 --- a/open_wearable/android/app/proguard-rules.pro +++ b/open_wearable/android/app/proguard-rules.pro @@ -1 +1,4 @@ -dontwarn org.slf4j.impl.StaticLoggerBinder +# Same behavior as the proguard-android.txt that AGP 9 no longer accepts. +# Remove once a release build has been tested on a device with R8 optimization. +-dontoptimize diff --git a/open_wearable/android/gradle.properties b/open_wearable/android/gradle.properties index c0356b5da..2f0b4cadd 100644 --- a/open_wearable/android/gradle.properties +++ b/open_wearable/android/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true android.enableJetifier=true -android.builtInKotlin=true -android.newDsl=true +android.builtInKotlin=false +android.newDsl=false diff --git a/open_wearable/android/gradle/wrapper/gradle-wrapper.properties b/open_wearable/android/gradle/wrapper/gradle-wrapper.properties index ca025c83a..37f78a6af 100644 --- a/open_wearable/android/gradle/wrapper/gradle-wrapper.properties +++ b/open_wearable/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/open_wearable/android/settings.gradle b/open_wearable/android/settings.gradle index 4c7c25c54..cb8b00a2a 100644 --- a/open_wearable/android/settings.gradle +++ b/open_wearable/android/settings.gradle @@ -18,8 +18,8 @@ pluginManagement { plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" - id "com.android.application" version "8.12.1" apply false - id("org.jetbrains.kotlin.android") version "2.2.21" apply false + id "com.android.application" version "9.1.0" apply false + id("org.jetbrains.kotlin.android") version "2.4.0" apply false } include ":app" diff --git a/open_wearable/ios/Podfile.lock b/open_wearable/ios/Podfile.lock index 6b286ff3b..1495fc6f4 100644 --- a/open_wearable/ios/Podfile.lock +++ b/open_wearable/ios/Podfile.lock @@ -1,7 +1,5 @@ PODS: - Flutter (1.0.0) - - flutter_headset_detector (3.1.0): - - Flutter - iOSMcuManagerLibrary (1.12): - SwiftCBOR (= 0.5.0) - ZIPFoundation (= 0.9.19) @@ -16,7 +14,6 @@ PODS: DEPENDENCIES: - Flutter (from `Flutter`) - - flutter_headset_detector (from `.symlinks/plugins/flutter_headset_detector/ios`) - mcumgr_flutter (from `.symlinks/plugins/mcumgr_flutter/darwin`) - SwiftProtobuf (= 1.37.0) @@ -30,14 +27,11 @@ SPEC REPOS: EXTERNAL SOURCES: Flutter: :path: Flutter - flutter_headset_detector: - :path: ".symlinks/plugins/flutter_headset_detector/ios" mcumgr_flutter: :path: ".symlinks/plugins/mcumgr_flutter/darwin" SPEC CHECKSUMS: Flutter: 71a624a5bc0c04062bf19101d501e466baf2fb47 - flutter_headset_detector: 37d2407c6c59aa6e8a9daecf732854862ff6dd4a iOSMcuManagerLibrary: f72db849f6dd66b1f26cd7eea776050a22c8591c mcumgr_flutter: b9864b2ae8334b6ba2b33f9ab4fc0a67bbe75c7a SwiftCBOR: aa87349b4ccddd19f861aa544f7c27d43dee5772 diff --git a/open_wearable/pubspec.lock b/open_wearable/pubspec.lock index c42d3920e..60e8b8643 100644 --- a/open_wearable/pubspec.lock +++ b/open_wearable/pubspec.lock @@ -342,14 +342,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" - flutter_headset_detector: - dependency: "direct main" - description: - name: flutter_headset_detector - sha256: fe061eceb106a61b837ae58eda1575604db24299a4ebe13e34839dd3d30085df - url: "https://pub.dev" - source: hosted - version: "3.1.0" flutter_lints: dependency: "direct dev" description: diff --git a/open_wearable/pubspec.yaml b/open_wearable/pubspec.yaml index bf476f350..922a95baa 100644 --- a/open_wearable/pubspec.yaml +++ b/open_wearable/pubspec.yaml @@ -60,7 +60,6 @@ dependencies: sensors_plus: ^7.0.0 device_info_plus: ^13.2.0 pub_semver: ^2.2.0 - flutter_headset_detector: ^3.1.0 record: ^7.0.0 path: ^1.9.1 web: ^1.1.1 From e265bc5f15e85e24d235eea08bc7531ade1d2f26 Mon Sep 17 00:00:00 2001 From: JP Date: Fri, 6 Nov 2026 16:35:35 +0100 Subject: [PATCH 2/7] docs: add seminar context and documentation structure Add a CLAUDE.md with the project goals, grading, dates and workflow of the Wearable Computing lab, and a docs folder that collects material for the presentations and the paper. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ docs/README.md | 29 +++++++++++++++ docs/journal.md | 24 +++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 CLAUDE.md create mode 100644 docs/README.md create mode 100644 docs/journal.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 000000000..5775b8138 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,95 @@ +# CLAUDE.md + +Dieses Repo ist unser Team-Fork der OpenWearables-App für das **Praktikum Wearable Computing** (KIT, TECO, WS 2026/27). Wir sind Projektgruppe 4 und bauen **Earable Fitness Tracking**. Quelle für den Seminarteil: Intro-Folien vom 04.11.2026. + +## Unser Projekt: Earable Fitness Tracking + +**Ziel:** Ein Echtzeit-System für OpenEarable 2.0, das aus den Sensordaten am Ohr Schritte zählt, Übungen erkennt, Wiederholungen zählt und übungsbezogene Werte schätzt. Die Ergebnisse zeigt eine App dem Nutzer an. + +**Aufgaben:** + +1. **Schrittzähler:** robust und dauerhaft laufend auf IMU-Basis. Echte Schritte von Kopfbewegungen trennen. Vorverarbeitung, Peak-Detection und adaptive Schwellwerte untersuchen, mit verschiedenen Gehgeschwindigkeiten und Alltagsbewegungen testen. +2. **Übungen und Wiederholungen:** zuerst Kniebeugen und Liegestütze, später erweiterbar. Herausfinden, welche IMU-Features die Übungen gut beschreiben, und Algorithmen mit leichtgewichtigen ML-Klassifikatoren vergleichen. +3. **Sprunghöhe:** Sprünge erkennen und die Höhe aus der vertikalen Kopfbewegung schätzen. Genauigkeit gegen eine passende Referenzmessung prüfen. +4. **App-Integration:** Echtzeit-Feedback und Zusammenfassung von Schritten, Übungen, Wiederholungen und Sprunghöhe. +5. **Evaluation:** unter realistischen Bedingungen, über verschiedene Personen, Tempi und Ausführungsstile. Metriken: Schrittzahl-Fehler, Erkennungsgenauigkeit, Wiederholungsfehler, Sprunghöhen-Fehler. + +**Bonus:** Übungen automatisch erkennen, Gehen, Laufen und Treppensteigen unterscheiden, multimodale Erkennung aus IMU und Mikrofon (z. B. Schrittgeräusche). + +**Ausgangspunkt:** Die App gibt Zugriff auf alle Sensoren, eigene Apps laufen direkt in ihr, Audio ist über LE Audio verfügbar. Daten werden zum Smartphone gestreamt oder bei großen Mengen und Audio auf microSD geschrieben. Gerät, microSD und bei Bedarf ein Smartphone mit BLE 5.3 stellt der Lehrstuhl. + +## Seminar: Rahmen und Bewertung + +Gruppen aus 4 Personen: Problem verstehen, Lösung konzipieren, Prototyp bauen, iterieren, mit Nutzern testen, dokumentieren. Insgesamt sollen etwa 10–20 Personen an Datensammlung und/oder Evaluation teilnehmen. Das Ganze gilt als Vorstudie, auf der andere aufbauen können. + +| Teil | Gewicht | Erwartung | +| --- | --- | --- | +| Zwischenpräsentation | unbenotet | ca. 10 min Vortrag, 10 min Diskussion | +| Abschlusspräsentation | **50 %** | 20 min inkl. Prototyp-Demo, 5 min Diskussion | +| Code | **25 %** | GitHub-Repo oder Zip, sorgfältig dokumentiert, gutes README, Abhängigkeiten festgehalten (`pubspec.yaml`, für Python `requirements.txt` oder `pyproject.toml`) | +| Paper | **25 %** | TECO-Overleaf-Vorlage, ca. 10–15 Seiten ohne Referenzen und Abbildungen, Abbildungen trotzdem einbauen | + +**Aufbau des Papers:** kurze Einleitung, Related Work (ca. 1 Seite), Problem, Dokumentation der Lösung, Diskussion und Fazit. Andere sollen damit weiterarbeiten können. + +### Termine + +Pflichttermine sind fett. Q/A-Termine sonst freiwillig: mindestens 24 h vorher anmelden und etwas Praktisches zum Zeigen mitbringen (Stand, Fortschritt, Probleme). + +| Datum | Termin | +| --- | --- | +| **11.11.2026** | **Q/A-Session (Pflicht), bis dahin zur Prüfung angemeldet sein** | +| 25.11., 09.12.2026 | Q/A | +| **16.12.2026** | **Zwischenpräsentation** | +| 13.01., 27.01., 10.02.2027 | Q/A | +| **17.02.2027** | **Abschlusspräsentation** | +| **31.03.2027, 23:59** | **Finale Abgabe** | + +## Dokumentation ist Teil jeder Aufgabe + +Präsentationen und Paper entstehen aus dieser Doku, deshalb wird sie **laufend** geschrieben und nicht erst am Ende: + +- Jede nennenswerte Änderung, jedes Experiment und jede Entscheidung kommt im selben PR nach `docs/`. Aufbau siehe [docs/README.md](docs/README.md). +- Immer festhalten, **warum** etwas so gemacht wurde und welche Alternativen verworfen wurden. +- Messwerte nur mit Kontext: Personenzahl, Ablauf, Sensorrate, Referenzmessung, Datum. +- Abbildungen und Diagramme, die in Präsentation oder Paper taugen, unter `docs/figures/` ablegen und das Skript dazu mit einchecken. +- Claude denkt von sich aus daran und nennt am Ende einer Aufgabe, was dokumentiert wurde. +- Praktikumsdoku auf Deutsch, Code und Kommentare auf Englisch. + +**Das Repo ist öffentlich:** keine Rohdaten von Probanden und keine personenbezogenen Daten einchecken, nur anonymisierte Ergebnisse. + +## Repo-Überblick + +- `open_wearable/`: die Flutter-App, hier läuft alles ab. + - `lib/apps/`: In-App-Anwendungen. Neue App in `lib/apps//` anlegen und in `_apps` in `lib/apps/widgets/apps_page.dart` eintragen. `posture_tracker/` ist die Vorlage (`model/`, `view/`, `view_model/`). + - `lib/view_models/`: übergreifender Zustand (Provider), `lib/router.dart`: Routen, `lib/main.dart`: Start. + - `test/`: Tests, `docs/`: App-Doku von Upstream. Bei Architekturänderungen mitpflegen. +- `docs/`: unsere Praktikumsdoku. +- Sensorzugriff läuft über das Paket `open_earable_flutter`. Beispiel: `lib/apps/posture_tracker/model/earable_attitude_tracker.dart`. + +## Entwickeln + +Befehle aus `open_wearable/` heraus. Vor jedem PR müssen die letzten drei durchlaufen: + +```bash +flutter pub get +flutter run +dart format lib test +flutter analyze +flutter test +``` + +- Ohne Hardware testen: `flutter run --dart-define=APP_STORE_PREVIEW=true` +- Flutter-Version steht in `open_wearable/.flutter_version`. +- **Windows:** Für `flutter pub get` muss der Entwicklermodus an sein (Symlinks für Plugins). +- **Android-Toolchain:** Gradle 9.3.1, AGP 9.1.0, Kotlin 2.4.0. Java 25 braucht mindestens Gradle 9.1, Flutter 3.47 testet bis Gradle 9.3.1. Die CI baut mit Java 21. Hintergrund im [Journal](docs/journal.md). + +## Git + +- **`origin` ist unser Fork, `gh` zielt bei Forks aber standardmäßig auf Upstream.** PRs daher immer so erstellen: + + ```bash + gh pr create --repo philgenb/openearable-activity-tracker --base main + ``` + +- Nichts an `OpenEarable/app` schicken, außer das Team will das ausdrücklich. +- Regeln aus [CONTRIBUTING.md](CONTRIBUTING.md): eigener Branch pro Änderung, Conventional Commits, Rebase statt Merge, öffentliche APIs dokumentieren. diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..f4d3e8c9e --- /dev/null +++ b/docs/README.md @@ -0,0 +1,29 @@ +# Praktikumsdoku: Earable Fitness Tracking + +Hier sammeln wir alles, was wir für Zwischenpräsentation, Abschlusspräsentation und Paper brauchen. Die technische Doku der App selbst liegt in [`open_wearable/docs/`](../open_wearable/docs/README.md). + +## Aufbau + +| Pfad | Inhalt | Wird genutzt für | +| --- | --- | --- | +| [`journal.md`](journal.md) | Was wir gemacht und entschieden haben, neueste Einträge oben | Stand für Q/A und Präsentationen, roter Faden fürs Paper | +| `algorithmen/` | Eine Datei pro Algorithmus (Schritte, Übungen, Sprunghöhe): Ansatz, Parameter, Alternativen | Paper: Dokumentation der Lösung | +| `evaluation/` | Studienprotokolle, Metriken, anonymisierte Ergebnisse | Paper und Präsentation: Ergebnisse, Diskussion | +| `related-work.md` | Gelesene Paper mit Kurznotiz und Zitat | Paper: Related Work | +| `praesentationen/` | Gliederung und Kernaussagen je Termin | Zwischen- und Abschlusspräsentation | +| `figures/` | Abbildungen samt Quelle oder Skript | Präsentationen und Paper | + +Bisher gibt es nur das Journal. Die anderen Pfade legen wir an, sobald es dafür Inhalt gibt. + +## Journal-Eintrag + +Kurz halten und diese Fragen beantworten: + +- **Was** wurde gemacht? +- **Warum** so, und was wurde verworfen? +- **Ergebnis:** Zahlen mit Kontext (Personenzahl, Ablauf, Sensorrate, Referenzmessung). +- **Offen:** Was fehlt noch, was ist ungeprüft? + +## Datenschutz + +Das Repo ist öffentlich. Rohdaten von Probanden und personenbezogene Daten gehören nicht hierher, nur anonymisierte Ergebnisse. diff --git a/docs/journal.md b/docs/journal.md new file mode 100644 index 000000000..64a1707eb --- /dev/null +++ b/docs/journal.md @@ -0,0 +1,24 @@ +# Journal + +Neueste Einträge oben. Format siehe [README](README.md#journal-eintrag). + +## 2026-11-06: Android-Build auf Java 25 umgestellt + +**Was:** Gradle von 8.14 auf 9.3.1 aktualisiert, AGP von 8.12.1 auf 9.1.0, Kotlin von 2.2.21 auf 2.4.0. + +**Warum:** Der Build brach unter Java 25 ab, weil Gradle das erst ab Version 9.1 unterstützt. Wir nehmen 9.3.1 statt einer neueren Version, weil das die höchste ist, die Flutter 3.47 kennt. AGP und Kotlin entsprechen der Vorlage von Flutter 3.47. Die Alternative, lokal ein älteres JDK zu installieren und per `flutter config --jdk-dir` einzustellen, hätte jeder im Team einzeln machen müssen. + +**Folgeänderungen, die dafür nötig waren:** + +- `flutter_headset_detector` entfernt. Es nutzt `jcenter()`, das in Gradle 9 nicht mehr existiert, und es gibt keine neuere Version. Die App verwendete das Paket nirgends. +- `android.newDsl` und `android.builtInKotlin` auf `false` gesetzt, wie in der Flutter-Vorlage. Mit AGP 9 gehen sonst Flutter-Plugins kaputt. +- `proguard-android.txt` durch `proguard-android-optimize.txt` ersetzt, weil AGP 9 die alte Datei ablehnt. Mit `-dontoptimize` in `proguard-rules.pro` bleibt das Verhalten der Release-App wie bisher. + +**Ergebnis:** Mit Java 25.0.3 und Flutter 3.47.6 laufen Debug- und Release-APK durch, `flutter analyze` ist sauber und alle 142 Tests bestehen. + +**Offen:** + +- Noch nicht auf einem echten Gerät ausprobiert. +- `ios/Podfile.lock` wurde von Hand angepasst, weil unter Windows kein `pod install` läuft. +- `-dontoptimize` kann raus, sobald ein Release-Build mit R8-Optimierung auf einem Gerät getestet wurde. +- Flutter warnt, dass künftige Versionen Plugins mit eigenem Kotlin-Plugin nicht mehr bauen (`flutter_archive`, `mcumgr_flutter`). Das betrifft uns erst beim nächsten Flutter-Update. From cc660757f65f8c4e59fcc65896e303e708ede500 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 6 Nov 2026 14:58:26 +0100 Subject: [PATCH 3/7] feat: initial activity tracker prototype --- open_wearable/.gitignore | 2 + open_wearable/docs/activity_tracker.md | 124 +++ open_wearable/lib/app_store_preview.dart | 9 + .../apps/activity_tracker/assets/activity.svg | 5 + .../lib/apps/activity_tracker/assets/jump.svg | 5 + .../lib/apps/activity_tracker/assets/logo.svg | 5 + .../apps/activity_tracker/assets/pushup.svg | 5 + .../apps/activity_tracker/assets/squat.svg | 5 + .../models/activity_engine.dart | 355 +++++++ .../models/activity_models.dart | 195 ++++ .../services/activity_sensor_source.dart | 303 ++++++ .../services/activity_storage.dart | 118 +++ .../activity_tracker_controller.dart | 363 +++++++ .../widgets/activity_session_pages.dart | 624 ++++++++++++ .../widgets/activity_tracker_page.dart | 913 ++++++++++++++++++ .../widgets/activity_widgets.dart | 311 ++++++ open_wearable/lib/apps/widgets/apps_page.dart | 26 +- open_wearable/lib/main.dart | 15 +- open_wearable/pubspec.yaml | 1 + .../activity_controller_test.dart | 118 +++ .../activity_engine_test.dart | 192 ++++ .../activity_sensor_source_test.dart | 180 ++++ .../activity_storage_test.dart | 60 ++ .../activity_test_support.dart | 102 ++ .../activity_tracker/activity_ui_test.dart | 215 +++++ open_wearable/tool/replay_activity_trace.dart | 113 +++ 26 files changed, 4357 insertions(+), 7 deletions(-) create mode 100644 open_wearable/docs/activity_tracker.md create mode 100644 open_wearable/lib/apps/activity_tracker/assets/activity.svg create mode 100644 open_wearable/lib/apps/activity_tracker/assets/jump.svg create mode 100644 open_wearable/lib/apps/activity_tracker/assets/logo.svg create mode 100644 open_wearable/lib/apps/activity_tracker/assets/pushup.svg create mode 100644 open_wearable/lib/apps/activity_tracker/assets/squat.svg create mode 100644 open_wearable/lib/apps/activity_tracker/models/activity_engine.dart create mode 100644 open_wearable/lib/apps/activity_tracker/models/activity_models.dart create mode 100644 open_wearable/lib/apps/activity_tracker/services/activity_sensor_source.dart create mode 100644 open_wearable/lib/apps/activity_tracker/services/activity_storage.dart create mode 100644 open_wearable/lib/apps/activity_tracker/view_models/activity_tracker_controller.dart create mode 100644 open_wearable/lib/apps/activity_tracker/widgets/activity_session_pages.dart create mode 100644 open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart create mode 100644 open_wearable/lib/apps/activity_tracker/widgets/activity_widgets.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_controller_test.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_engine_test.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_sensor_source_test.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_storage_test.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_test_support.dart create mode 100644 open_wearable/test/apps/activity_tracker/activity_ui_test.dart create mode 100644 open_wearable/tool/replay_activity_trace.dart diff --git a/open_wearable/.gitignore b/open_wearable/.gitignore index b223fb72a..25a6dcab9 100644 --- a/open_wearable/.gitignore +++ b/open_wearable/.gitignore @@ -53,3 +53,5 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release +# Widget Preview related +.widget_preview/ diff --git a/open_wearable/docs/activity_tracker.md b/open_wearable/docs/activity_tracker.md new file mode 100644 index 000000000..2b149f4ea --- /dev/null +++ b/open_wearable/docs/activity_tracker.md @@ -0,0 +1,124 @@ +# Activity Tracker — core implementation + +Activity Tracker is a built-in mini-app under **Apps → Activity Tracker**. It +uses the host wearable picker, sensor configuration provider, shared sensor +streams and Material theme. UI copy is English. The reference is the individual +phone screens on the core page of the [approved Figma file](https://www.figma.com/design/8jNxHQ1Xf9OE5VedoepAjY). + +## Flow and scope + +1. Connect an OpenEarable with three-axis accelerometer and gyroscope streams. +2. Check the fit and stand still for 1.5 seconds. Start tracking once calibrated. +3. Steps continue across mini-app navigation. Choose Squats or Push-ups, hold the + starting position briefly, then perform complete down/up cycles. Finish each + set before starting another exercise. Exercise sets suppress step counting. +4. Jump test detects take-off/free flight/landing and displays an estimated + height only when the timing and signal pass the quality checks. Jumps can + also be detected between exercises after a still preparation period. +5. Pause/resume preserves confirmed counts. Disconnects and data gaps discard + incomplete movements and require still calibration before counting resumes. +6. End the session from the overview menu. History, reference trials, JSON and + CSV exports are available in Sessions. Leaving the mini-app does not end an + active session; leaving readiness without starting releases its sensors. + The app tile also opens without a connected wearable, so history and an + interrupted session remain accessible offline. + +There is no ML, automatic exercise selection, walking/running/stairs classifier +or microphone processing in this implementation. The user selects the exercise. + +**Execution boundary:** tracking is continuous while the application is in the +foreground. Background/lock-screen operation is not implemented as a platform +service. Counting explicitly pauses on backgrounding; returning requires fresh +calibration. The host does not shut down IMU sensors for an active session. + +## Architecture + +- `models/activity_engine.dart`: pure Dart, causal algorithms, independent of + Flutter/BLE/UI. Inputs use m/s², rad/s and sensor seconds. +- `services/activity_sensor_source.dart`: verifies units, converts timestamps + using each sensor's exponent (OE2 uses microseconds), joins batched IMU streams + by timestamp, selects a streaming rate near 100 Hz and reconnects by device ID. + Shared configuration handles are applied once. Existing recording flags and + higher rates are preserved; stopping restores known original settings only + if another consumer has not changed them. +- `view_models/activity_tracker_controller.dart`: app-wide owner registered in + the host provider tree; consumes every sample, updates widgets at up to 10 Hz, + owns session state and checkpoints confirmed events. +- `services/activity_storage.dart`: serialized atomic JSON checkpoints under + the app support `activity_tracker` directory and streamed raw CSV per session. + Startup recovers unfinished sessions as interrupted, without resuming partial + repetitions. Snapshots retain the latest 200 sessions and 90 daily totals; + raw traces remain on disk. Export uses the system share sheet after a user tap. +- `widgets/`: separate readiness, overview, live exercise, jump, pause/disconnect, + history, summary and validation screens. Surfaces, buttons and typography use + the existing theme; exercise icons are local SVG exports from Figma. + +## Algorithms and known limits + +Still calibration estimates gravity direction and magnitude. A complementary +filter propagates direction with the gyroscope and slowly corrects near 1 g. +Vertical acceleration is smoothed causally. Missing/invalid data, gaps above +120 ms and clock restarts reset partial states without changing totals. + +Steps use peak prominence, an adaptive amplitude threshold, a 280 ms refractory +period, plausible 0.28–1.5 second intervals and a regular three-peak confirmation +window. The first three steps are credited together. One/two-step bouts can be +missed. Isolated peaks and strong head rotations are suppressed. This is a +heuristic starting point: head motions resembling gait can still cause errors. + +Repetitions use bounded integration of projected acceleration as a motion proxy, +hysteretic down/return phases, minimum excursion and duration checks. It is not +a measured squat depth or a form-quality assessment. Parameters differ for +squats and push-ups. Slow, shallow, asymmetric or head-independent movements +need real-data tuning; a full body cycle cannot always be inferred from the ear. + +Jump height uses `h = g * flight_time² / 8`, with still preparation, propulsion, +a low-acceleration flight interval and landing confirmation. Similar take-off +and landing posture is assumed. It estimates ballistic flight height, not +absolute head displacement. Samples slower than 40 Hz, ambiguous landings and +clipped impacts produce an unavailable height. The ±2 g clipping heuristic is +conservative for the OE2 compact format. Walking/running is not classified. + +Synthetic replay tests verify implementation behavior, **not real-world +accuracy**. No earable hardware or participant dataset was available during +implementation. User-independent performance and jump-height validity remain +to be measured. + +## Evaluation workflow + +Record separate sessions/trials with synchronized reference video, independent +step/repetition annotations and a jump reference such as a contact mat or +calibrated high-frame-rate video. Use multiple participants, movement speeds, +execution styles and negative controls (talking, head turns, sitting, handling +the earable). Tune on development participants and evaluate on held-out users. + +Study mode freezes detected results when opened. Choose session steps, a +completed exercise set or an individual jump attempt. Enter a participant ID, +movement pattern/trial label, reference source, reference count and optionally +reference jump height. Save the trial. Reference intervals must match the +chosen result; use separate sessions for independent step trials. + +The UI computes signed count error, absolute percentage error (undefined for +zero reference), per-activity count MAE and paired jump-height MAE. Missing +height estimates stay missing; report valid-height coverage alongside height +error. Export includes trial identities, result labels and capture times. +Automatic exercise-classification accuracy is outside this manual-mode scope. + +Each session export contains metadata JSON, reference-trials CSV and raw IMU +CSV with sensor timestamps, SI values, mode, pause/calibration status and event +deltas. Metadata includes the starting gravity calibration, and the raw trace +includes a bounded readiness pre-roll. Keep the exported JSON next to its CSV +to restore the initial calibration during replay. +Replay the same live algorithms with: + +```sh +dart run tool/replay_activity_trace.dart /path/to/session.csv +``` + +Validation commands: + +```sh +dart format lib/apps/activity_tracker test/apps/activity_tracker tool/replay_activity_trace.dart +flutter analyze +flutter test +``` diff --git a/open_wearable/lib/app_store_preview.dart b/open_wearable/lib/app_store_preview.dart index 36aa87506..5ea5a94a3 100644 --- a/open_wearable/lib/app_store_preview.dart +++ b/open_wearable/lib/app_store_preview.dart @@ -1,6 +1,9 @@ import 'dart:async'; import 'dart:math' as math; +import 'apps/activity_tracker/services/activity_sensor_source.dart'; +import 'apps/activity_tracker/view_models/activity_tracker_controller.dart'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -105,6 +108,12 @@ class _AppStorePreviewAppState extends State { ChangeNotifierProvider( create: (_) => FirmwareUpdateRequestProvider(), ), + ChangeNotifierProvider( + create: (context) => ActivityTrackerController( + source: + OpenEarableActivitySource(context.read()), + ), + ), ChangeNotifierProxyProvider( create: (_) => SensorRecorderProvider(), update: (_, wearablesProvider, recorderProvider) { diff --git a/open_wearable/lib/apps/activity_tracker/assets/activity.svg b/open_wearable/lib/apps/activity_tracker/assets/activity.svg new file mode 100644 index 000000000..3374586b0 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/assets/activity.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/open_wearable/lib/apps/activity_tracker/assets/jump.svg b/open_wearable/lib/apps/activity_tracker/assets/jump.svg new file mode 100644 index 000000000..b5ffda256 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/assets/jump.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/open_wearable/lib/apps/activity_tracker/assets/logo.svg b/open_wearable/lib/apps/activity_tracker/assets/logo.svg new file mode 100644 index 000000000..8905bebed --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/assets/logo.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/open_wearable/lib/apps/activity_tracker/assets/pushup.svg b/open_wearable/lib/apps/activity_tracker/assets/pushup.svg new file mode 100644 index 000000000..8353e1faf --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/assets/pushup.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/open_wearable/lib/apps/activity_tracker/assets/squat.svg b/open_wearable/lib/apps/activity_tracker/assets/squat.svg new file mode 100644 index 000000000..a4ad3c661 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/assets/squat.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart new file mode 100644 index 000000000..862e532e2 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart @@ -0,0 +1,355 @@ +import 'dart:math' as math; +import 'activity_models.dart'; + +/// Events from one sample. Counts are deltas, so UI throttling loses no events. +class ActivityUpdate { + const ActivityUpdate({this.steps = 0, this.repetitions = 0, this.jump}); + final int steps, repetitions; + final JumpAttempt? jump; +} + +/// Causal, replayable first implementation of the core tracking algorithms. +/// +/// Requires still calibration, SI units and strictly increasing sensor time. +/// Gaps reset partial movements, never confirmed counts. These heuristics need +/// ear-worn, participant-independent evaluation before accuracy claims. +class ActivityEngine { + static const gravity = 9.80665; + ActivityKind kind = ActivityKind.steps; + bool calibrated = false; + double calibrationProgress = 0; + String phase = 'Stand still briefly'; + double movement = 0; + String quality = 'Waiting for motion data'; + double? _lastTime; + MotionVector _up = const MotionVector(0, 0, 1); + MotionVector _calibrationSum = MotionVector.zero; + double _calibrationSeconds = 0; + int _calibrationSamples = 0; + double _gravityMagnitude = gravity; + double _smooth = 0; + final _steps = _StepDetector(); + final _reps = _RepetitionDetector(); + final _jumps = _JumpDetector(); + + /// Validated calibration snapshot for reproducing a session offline. + Map get calibration => + {'x': _up.x, 'y': _up.y, 'z': _up.z, 'gravity': _gravityMagnitude}; + void restoreCalibration(Map value) { + final up = MotionVector(value['x'] ?? 0, value['y'] ?? 0, value['z'] ?? 0); + final g = value['gravity'] ?? 0; + if (!up.isFinite || + (up.length - 1).abs() > 0.05 || + !g.isFinite || + (g - gravity).abs() > 0.6) { + throw ArgumentError('Invalid initial IMU calibration'); + } + recalibrate(); + _up = up.unit; + _gravityMagnitude = g; + calibrated = true; + calibrationProgress = 1; + } + + /// Clears transient state on pause/reconnect, requiring a fresh still window. + void recalibrate() { + calibrated = false; + calibrationProgress = 0; + _lastTime = null; + _calibrationSum = MotionVector.zero; + _calibrationSeconds = 0; + _calibrationSamples = 0; + _smooth = 0; + movement = 0; + _steps.reset(); + _reps.reset(); + _jumps.reset(); + phase = 'Stand still briefly'; + } + + /// Switching modes discards incomplete cycles and provisional steps. + void select(ActivityKind next) { + kind = next; + _steps.reset(); + _reps.reset(); + _jumps.reset(); + _smooth = 0; + phase = next == ActivityKind.steps + ? 'Tracking steps' + : 'Hold the start position'; + } + + ActivityUpdate add(MotionSample s) { + if (!s.seconds.isFinite || + !s.acceleration.isFinite || + (s.gyroscope != null && !s.gyroscope!.isFinite)) { + recalibrate(); + quality = 'Invalid sample · stand still'; + return const ActivityUpdate(); + } + final previousTime = _lastTime; + final dt = previousTime == null ? 0.01 : s.seconds - previousTime; + // Duplicate/out-of-order packets must not advance any detector. + if (dt <= 0) { + if (dt < -0.5) { + recalibrate(); + quality = 'Sensor clock restarted · stand still'; + } + return const ActivityUpdate(); + } + if (previousTime != null && dt > 0.12) { + recalibrate(); + quality = 'Data gap · stand still to resume'; + } + _lastTime = s.seconds; + final magnitude = s.acceleration.length; + final angularSpeed = s.gyroscope?.length ?? 0; + final clipped = [s.acceleration.x, s.acceleration.y, s.acceleration.z] + .any((v) => v.abs() >= 19.4); // OE2 compact IMU format is ±2 g. + if (!calibrated) { + final still = (magnitude - gravity).abs() < 0.55 && angularSpeed < 0.18; + if (!still || dt > 0.12) { + _calibrationSum = MotionVector.zero; + _calibrationSamples = 0; + _calibrationSeconds = 0; + } else { + _calibrationSeconds += dt; + _calibrationSum = _calibrationSum + s.acceleration; + _calibrationSamples++; + } + calibrationProgress = (_calibrationSeconds / 1.5).clamp(0, 1); + if (_calibrationSeconds >= 1.5 && _calibrationSamples >= 50) { + final mean = _calibrationSum * (1 / _calibrationSamples); + _up = mean.unit; + _gravityMagnitude = mean.length; + calibrated = true; + } + quality = calibrated ? 'Signal ready' : 'Stand still briefly'; + return const ActivityUpdate(); + } + // Complementary gravity direction: gyro propagation plus slow correction + // only near 1 g. This avoids interpreting a head turn as vertical motion. + if (s.gyroscope != null) { + _up = (_up - s.gyroscope!.cross(_up) * dt).unit; + } + if ((magnitude - _gravityMagnitude).abs() < 1.2) { + final a = dt / (2.5 + dt); + _up = (_up * (1 - a) + s.acceleration.unit * a).unit; + } + final vertical = s.acceleration.dot(_up) - _gravityMagnitude; + _smooth += dt / (0.055 + dt) * (vertical - _smooth); + movement = _smooth; + quality = clipped + ? 'Impact clipped' + : dt > 0.035 + ? 'Low sample rate' + : 'Steady signal'; + final jump = (kind == ActivityKind.steps || kind == ActivityKind.jumps) + ? _jumps.add(s.seconds, magnitude, angularSpeed, dt, clipped) + : null; + if (kind == ActivityKind.steps) { + phase = 'Tracking steps'; + var count = 0; + if (_jumps.inMotion || clipped) { + _steps.reset(); + } else { + count = _steps.add(s.seconds, _smooth, angularSpeed); + } + return ActivityUpdate(steps: count, jump: jump); + } + if (kind == ActivityKind.jumps) { + phase = _jumps.inMotion ? 'Jump in progress' : 'Ready for next jump'; + return ActivityUpdate(jump: jump); + } + final count = clipped + ? 0 + : _reps.add( + s.seconds, + _smooth, + dt, + kind == ActivityKind.squats ? 0.065 : 0.035, + ); + if (clipped) _reps.reset(); + phase = _reps.phase; + return ActivityUpdate(repetitions: count); + } +} + +class _StepDetector { + double _previous = 0, _slope = 0, _trough = 0, _amplitude = 2; + double _lastPeak = -100, _previousInterval = 0; + int _pending = 0; + bool _confirmedBout = false; + void reset() { + _previous = _slope = _trough = 0; + _amplitude = 2; + _lastPeak = -100; + _previousInterval = 0; + _pending = 0; + _confirmedBout = false; + } + + int add(double time, double value, double angularSpeed) { + _trough = math.min(_trough, value); + final slope = value - _previous; + var count = 0; + if (_slope > 0 && + slope <= 0 && + _previous > math.max(0.65, _amplitude * 0.28) && + _previous - _trough > 1.1 && + angularSpeed < 1.6) { + final interval = time - _lastPeak; + if (interval >= 0.28) { + final regular = interval <= 1.5 && + (_previousInterval == 0 || + (interval - _previousInterval).abs() < _previousInterval * 0.4); + if (!regular) { + _pending = 1; + _confirmedBout = false; + _previousInterval = 0; + } else { + _pending++; + if (_confirmedBout) { + count = 1; + } else if (_pending >= 3) { + count = _pending; // Confirm the first steps retroactively. + _confirmedBout = true; + } + _previousInterval = interval; + } + _lastPeak = time; + _amplitude = 0.8 * _amplitude + 0.2 * (_previous - _trough); + _trough = 0; + } + } + _slope = slope; + _previous = value; + return count; + } +} + +class _RepetitionDetector { + double _velocity = 0, _position = 0, _minimum = 0, _began = 0, _settle = 0; + int _state = 0; + String phase = 'Hold the start position'; + void reset() { + _velocity = _position = _minimum = _began = _settle = 0; + _state = 0; + phase = 'Hold the start position'; + } + + int add(double t, double a, double dt, double minDepth) { + // Bounded integration is a movement proxy, not a measurement of depth. + // Rebase each complete cycle to prevent unbounded displacement drift. + if (_settle < 0.7) { + _settle = a.abs() < 0.5 ? _settle + dt : 0; + return 0; + } + _velocity = _velocity * math.exp(-dt / 1.5) + a * dt; + _position = _position * math.exp(-dt / 4) + _velocity * dt; + _minimum = math.min(_minimum, _position); + if (_state == 0) { + phase = 'Ready · lower your body'; + if (_position < -minDepth * 0.4 && _velocity < -0.035) { + _state = 1; + _began = t; + phase = 'Lowering'; + } else if (a.abs() < 0.15 && _velocity.abs() < 0.025) { + _position = _minimum = 0; + } + } else if (t - _began > 8) { + reset(); + } else if (_state == 1 && _minimum <= -minDepth && _velocity > 0.025) { + _state = 2; + phase = 'Returning to start'; + } else if (_state == 2 && + _velocity <= 0 && + _position >= _minimum * 0.2 && + _position - _minimum >= minDepth * 0.8 && + t - _began >= 0.65) { + _state = 0; + _velocity = _position = _minimum = 0; + phase = 'Repetition complete'; + return 1; + } + return 0; + } +} + +class _JumpDetector { + double _still = 0, + _armedAt = -100, + _takeoff = -100, + _endFlight = -100, + _cooldown = -100; + bool _airborne = false, _clipped = false, _slow = false; + bool get inMotion => _airborne || _endFlight > 0; + void reset() { + _still = 0; + _armedAt = _takeoff = _endFlight = _cooldown = -100; + _airborne = _clipped = _slow = false; + } + + JumpAttempt? add( + double t, + double magnitude, + double gyro, + double dt, + bool clipped, + ) { + if (t < _cooldown) return null; + _still = (magnitude - ActivityEngine.gravity).abs() < 0.8 && gyro < 0.3 + ? _still + dt + : 0; + if (!_airborne && _endFlight < 0) { + if (_still >= 0.5) _armedAt = t; + if (t - _armedAt < 0.7 && magnitude > 12.3) { + _takeoff = t; + _clipped |= clipped; + } + if (t - _takeoff < 0.5 && magnitude < 3.5) { + _airborne = true; + _takeoff = t; + _clipped |= clipped; + _slow = dt > 0.025; + } + return null; + } + _clipped |= clipped; + _slow |= dt > 0.025; + if (_airborne && magnitude >= 3.5) { + _airborne = false; + _endFlight = t; + } + if (_airborne && t - _takeoff > 0.85) { + reset(); + return null; + } + if (_endFlight > 0 && (magnitude > 12.3 || t - _endFlight > 0.12)) { + final flight = _endFlight - _takeoff; + final landed = magnitude > 12.3; + final valid = + flight >= 0.12 && flight <= 0.85 && landed && !_clipped && !_slow; + final result = flight >= 0.08 + ? JumpAttempt( + at: t, + flightSeconds: flight, + heightCm: valid + ? ActivityEngine.gravity * flight * flight / 8 * 100 + : null, + reason: valid + ? null + : _clipped + ? 'Impact clipped' + : _slow + ? 'Sample rate too low' + : 'Unclear take-off or landing', + ) + : null; + reset(); + _cooldown = t + 0.8; + return result; + } + return null; + } +} diff --git a/open_wearable/lib/apps/activity_tracker/models/activity_models.dart b/open_wearable/lib/apps/activity_tracker/models/activity_models.dart new file mode 100644 index 000000000..b20ce5b18 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/models/activity_models.dart @@ -0,0 +1,195 @@ +import 'dart:math' as math; + +/// Core activities selected by the user. No automatic activity classifier. +enum ActivityKind { + steps('Steps'), + squats('Squats'), + pushUps('Push-ups'), + jumps('Jump test'); + + const ActivityKind(this.label); + final String label; +} + +/// IMU vector in sensor coordinates, using SI units. +class MotionVector { + const MotionVector(this.x, this.y, this.z); + static const zero = MotionVector(0, 0, 0); + final double x, y, z; + MotionVector operator +(MotionVector b) => + MotionVector(x + b.x, y + b.y, z + b.z); + MotionVector operator -(MotionVector b) => + MotionVector(x - b.x, y - b.y, z - b.z); + MotionVector operator *(double s) => MotionVector(x * s, y * s, z * s); + double dot(MotionVector b) => x * b.x + y * b.y + z * b.z; + MotionVector cross(MotionVector b) => + MotionVector(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x); + double get length => math.sqrt(dot(this)); + MotionVector get unit => length > 0 ? this * (1 / length) : zero; + bool get isFinite => x.isFinite && y.isFinite && z.isFinite; +} + +/// One accelerometer sample plus a recent gyroscope sample (rad/s). +/// [seconds] uses the sensor clock, converted using its timestamp exponent. +class MotionSample { + const MotionSample(this.seconds, this.acceleration, {this.gyroscope}); + final double seconds; + final MotionVector acceleration; + final MotionVector? gyroscope; +} + +/// Flight-time estimate. A null height explicitly marks an unusable attempt. +class JumpAttempt { + const JumpAttempt({ + required this.at, + required this.flightSeconds, + this.heightCm, + this.reason, + }); + final double at, flightSeconds; + final double? heightCm; + final String? reason; + Map toJson() => { + 'at': at, + 'flightSeconds': flightSeconds, + 'heightCm': heightCm, + 'reason': reason, + }; + factory JumpAttempt.fromJson(Map j) => JumpAttempt( + at: (j['at'] as num).toDouble(), + flightSeconds: (j['flightSeconds'] as num).toDouble(), + heightCm: (j['heightCm'] as num?)?.toDouble(), + reason: j['reason'] as String?, + ); +} + +/// A completed manually selected exercise set. +class ExerciseSet { + const ExerciseSet({ + required this.kind, + required this.repetitions, + required this.seconds, + }); + final ActivityKind kind; + final int repetitions, seconds; + Map toJson() => + {'kind': kind.name, 'repetitions': repetitions, 'seconds': seconds}; + factory ExerciseSet.fromJson(Map j) => ExerciseSet( + kind: ActivityKind.values.byName(j['kind']), + repetitions: j['repetitions'], + seconds: j['seconds'], + ); +} + +/// Independent ground truth for a single trial; no inferred labels are used. +class ValidationTrial { + const ValidationTrial({ + required this.participant, + required this.pattern, + required this.referenceSource, + required this.kind, + required this.detected, + required this.reference, + this.estimatedHeightCm, + this.referenceHeightCm, + this.resultLabel = '', + this.capturedAt, + }); + final String participant, pattern, referenceSource; + final String resultLabel; + final DateTime? capturedAt; + final ActivityKind kind; + final int detected, reference; + final double? estimatedHeightCm, referenceHeightCm; + int get countError => detected - reference; + double? get absolutePercentageError => + reference > 0 ? countError.abs() / reference * 100 : null; + double? get heightErrorCm => + estimatedHeightCm != null && referenceHeightCm != null + ? estimatedHeightCm! - referenceHeightCm! + : null; + Map toJson() => { + 'participant': participant, + 'pattern': pattern, + 'referenceSource': referenceSource, + 'resultLabel': resultLabel, + 'capturedAt': capturedAt?.toIso8601String(), + 'kind': kind.name, + 'detected': detected, + 'reference': reference, + 'estimatedHeightCm': estimatedHeightCm, + 'referenceHeightCm': referenceHeightCm, + }; + factory ValidationTrial.fromJson(Map j) => ValidationTrial( + participant: j['participant'], + pattern: j['pattern'], + referenceSource: j['referenceSource'], + resultLabel: j['resultLabel'] ?? '', + capturedAt: DateTime.tryParse(j['capturedAt'] ?? ''), + kind: ActivityKind.values.byName(j['kind']), + detected: j['detected'], + reference: j['reference'], + estimatedHeightCm: (j['estimatedHeightCm'] as num?)?.toDouble(), + referenceHeightCm: (j['referenceHeightCm'] as num?)?.toDouble(), + ); +} + +/// Persisted session, including interrupted sessions recovered after restart. +class ActivitySession { + ActivitySession({ + required this.id, + required this.startedAt, + required this.deviceName, + this.endedAt, + this.steps = 0, + this.interrupted = false, + this.calibration = const {}, + List? sets, + List? jumps, + List? trials, + }) : sets = sets ?? [], + jumps = jumps ?? [], + trials = trials ?? []; + final String id, deviceName; + final Map calibration; + final DateTime startedAt; + DateTime? endedAt; + int steps; + bool interrupted; + final List sets; + final List jumps; + final List trials; + int get repetitions => sets.fold(0, (n, s) => n + s.repetitions); + double? get bestHeightCm => jumps + .map((j) => j.heightCm) + .whereType() + .fold(null, (best, h) => best == null || h > best ? h : best); + Map toJson() => { + 'id': id, + 'startedAt': startedAt.toIso8601String(), + 'endedAt': endedAt?.toIso8601String(), + 'deviceName': deviceName, + 'steps': steps, + 'interrupted': interrupted, + 'calibration': calibration, + 'sets': sets.map((s) => s.toJson()).toList(), + 'jumps': jumps.map((s) => s.toJson()).toList(), + 'trials': trials.map((s) => s.toJson()).toList(), + }; + factory ActivitySession.fromJson(Map j) => ActivitySession( + id: j['id'], + startedAt: DateTime.parse(j['startedAt']), + endedAt: j['endedAt'] == null ? null : DateTime.parse(j['endedAt']), + deviceName: j['deviceName'], + steps: j['steps'] ?? 0, + interrupted: j['interrupted'] ?? false, + calibration: (j['calibration'] as Map? ?? {}) + .map((key, value) => MapEntry(key, (value as num).toDouble())), + sets: (j['sets'] as List).map((e) => ExerciseSet.fromJson(e)).toList(), + jumps: + (j['jumps'] as List).map((e) => JumpAttempt.fromJson(e)).toList(), + trials: (j['trials'] as List? ?? []) + .map((e) => ValidationTrial.fromJson(e)) + .toList(), + ); +} diff --git a/open_wearable/lib/apps/activity_tracker/services/activity_sensor_source.dart b/open_wearable/lib/apps/activity_tracker/services/activity_sensor_source.dart new file mode 100644 index 000000000..031f344ec --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/services/activity_sensor_source.dart @@ -0,0 +1,303 @@ +import 'dart:async'; +import 'dart:math' as math; +import 'package:open_earable_flutter/open_earable_flutter.dart'; +import 'package:open_wearable/models/sensor_streams.dart'; +import 'package:open_wearable/view_models/sensor_configuration_provider.dart'; +import 'package:open_wearable/view_models/wearables_provider.dart'; +import '../models/activity_models.dart'; + +/// Replaceable input for live OpenEarable streams or deterministic replay. +abstract class ActivitySensorSource { + Stream get samples; + Stream get interruptions; + String get deviceName; + Future open(String deviceId); + Future close(); + void dispose(); +} + +/// Owns only the IMU configuration changes made by Activity Tracker. +/// Rebinds streams by device ID when the SDK creates a new wearable on reconnect. +class OpenEarableActivitySource implements ActivitySensorSource { + OpenEarableActivitySource(this.wearables) { + wearables.addListener(_devicesChanged); + } + final WearablesProvider wearables; + final _samples = StreamController.broadcast(); + final _interruptions = StreamController.broadcast(); + final List _subscriptions = []; + final Map _owned = {}; + String? _deviceId; + Wearable? _wearable; + SensorConfigurationProvider? _configurationProvider; + final List<(double, MotionVector)> _gyros = []; + final List<(double, MotionVector)> _accelerations = []; + bool _binding = false, _disposed = false; + @override + Stream get samples => _samples.stream; + @override + Stream get interruptions => _interruptions.stream; + @override + String get deviceName => _wearable?.name ?? 'OpenEarable'; + + @override + Future open(String deviceId) async { + if (_deviceId != deviceId) await close(); + _deviceId = deviceId; + await _bind(force: true); + } + + void _devicesChanged() { + if (_deviceId == null || _disposed) return; + final current = + wearables.wearables.where((w) => w.deviceId == _deviceId).firstOrNull; + if (!identical(current, _wearable)) { + unawaited( + _bind().catchError((Object e) { + if (!_disposed) _interruptions.add('Unable to reconnect: $e'); + }), + ); + } + } + + Future _bind({bool force = false}) async { + if (_binding || _disposed || _deviceId == null) return; + _binding = true; + final attemptedWearable = + wearables.wearables.where((w) => w.deviceId == _deviceId).firstOrNull; + try { + final next = attemptedWearable; + if (!force && identical(next, _wearable) && _subscriptions.isNotEmpty) { + return; + } + await _unbind(restore: force); + if (_disposed || _deviceId == null) return; + _wearable = next; + if (next == null) { + _interruptions.add('Connection lost'); + return; + } + if (!next.hasCapability()) { + throw StateError('No sensor streaming capability'); + } + final sensors = next.requireCapability().sensors; + Sensor? find(String token) => sensors + .where( + (s) => + '${s.sensorName} ${s.chartTitle}' + .toLowerCase() + .contains(token) && + s.axisCount == 3, + ) + .firstOrNull; + final acc = find('accelerometer'); + final gyro = find('gyroscope'); + if (acc == null || gyro == null) { + throw StateError('Accelerometer and gyroscope are required'); + } + if (!acc.axisUnits.every((u) => ['m/s²', 'm/s^2', 'm/s2'].contains(u))) { + throw StateError( + 'Unsupported acceleration units: ${acc.axisUnits.join(', ')}', + ); + } + final gyroDegrees = + gyro.axisUnits.every((u) => ['dps', 'deg/s', '°/s'].contains(u)); + if (!gyroDegrees && !gyro.axisUnits.every((u) => u == 'rad/s')) { + throw StateError('Unsupported angular velocity units'); + } + _configurationProvider = wearables.getSensorConfigurationProvider(next); + // Shared accelerometer/gyroscope configurations are applied once. + for (final c in { + ...acc.relatedConfigurations, + ...gyro.relatedConfigurations, + }) { + _configure(c); + } + _subscriptions.add( + SensorStreams.shared(wearable: next, sensor: gyro).listen( + (s) { + final v = _vector(s); + if (v == null) return; + _gyros.add( + (sensorSeconds(gyro, s), v * (gyroDegrees ? math.pi / 180 : 1)), + ); + if (_gyros.length > 256) _gyros.removeAt(0); + _drainSamples(); + }, + onError: _streamError, + onDone: () => _streamError('Gyroscope stream ended'), + ), + ); + _subscriptions.add( + SensorStreams.shared(wearable: next, sensor: acc).listen( + (s) { + final v = _vector(s); + if (v == null) return; + final t = sensorSeconds(acc, s); + _accelerations.add((t, v)); + if (_accelerations.length > 128) { + _accelerations.removeAt(0); + _interruptions.add('Waiting for synchronized IMU data'); + } + _drainSamples(); + }, + onError: _streamError, + onDone: () => _streamError('Accelerometer stream ended'), + ), + ); + _interruptions.add(null); + } catch (_) { + await _unbind(restore: true); + rethrow; + } finally { + _binding = false; + // Device changes can arrive while subscriptions are being cancelled. + if (!_disposed && + _deviceId != null && + !identical( + attemptedWearable, + wearables.wearables + .where((w) => w.deviceId == _deviceId) + .firstOrNull, + )) { + scheduleMicrotask(_devicesChanged); + } + } + } + + void _streamError(Object e) { + if (!_disposed) _interruptions.add('Motion stream interrupted: $e'); + } + + void _drainSamples() { + // Buffer differently batched BLE streams. Never integrate stale gyro data; + // hold acceleration until a timestamp-matched gyro sample has arrived. + while (_accelerations.isNotEmpty && _gyros.isNotEmpty) { + final acc = _accelerations.first; + final candidates = + _gyros.where((g) => (g.$1 - acc.$1).abs() <= 0.02).toList() + ..sort( + (a, b) => (a.$1 - acc.$1).abs().compareTo((b.$1 - acc.$1).abs()), + ); + if (candidates.isEmpty) { + if (_gyros.last.$1 > acc.$1 + 0.08) { + _accelerations.removeAt(0); + _interruptions.add('IMU timestamps do not match'); + continue; + } + break; + } + _accelerations.removeAt(0); + _samples + .add(MotionSample(acc.$1, acc.$2, gyroscope: candidates.first.$2)); + } + } + + /// Timestamp conversion is deliberately metadata-driven (OE2 uses µs). + static double sensorSeconds(Sensor sensor, SensorValue value) => + value.timestamp * math.pow(10, sensor.timestampExponent).toDouble(); + static MotionVector? _vector(SensorValue s) { + final values = s is SensorDoubleValue + ? s.values + : s is SensorIntValue + ? s.values.map((v) => v.toDouble()).toList() + : []; + return values.length == 3 + ? MotionVector(values[0], values[1], values[2]) + : null; + } + + void _configure(SensorConfiguration c) { + final provider = _configurationProvider!; + final previous = provider.getSelectedConfigurationValue(c); + final previousOptions = previous is ConfigurableSensorConfigurationValue + ? previous.options + : {}; + final candidates = c.values + .whereType() + .where((v) { + if (v is ConfigurableSensorConfigurationValue) { + final options = (v as ConfigurableSensorConfigurationValue).options; + return options.contains(StreamSensorConfigOption()) && + previousOptions.every(options.contains); + } + return true; + }) + .where((v) => v.frequencyHz > 0) + .toList() + ..sort((a, b) { + final frequency = a.frequencyHz.compareTo(b.frequencyHz); + if (frequency != 0) return frequency; + final aFlags = a is ConfigurableSensorConfigurationValue + ? (a as ConfigurableSensorConfigurationValue).options.length + : 0; + final bFlags = b is ConfigurableSensorConfigurationValue + ? (b as ConfigurableSensorConfigurationValue).options.length + : 0; + return aFlags.compareTo(bFlags); + }); + if (candidates.isEmpty) { + throw StateError('No streaming frequency available for ${c.name}'); + } + // Keep a higher existing rate; never remove an active recording option. + final target = previous is SensorFrequencyConfigurationValue + ? math.max(100, previous.frequencyHz) + : 100; + final chosen = + candidates.where((v) => v.frequencyHz >= target).firstOrNull ?? + candidates.last; + _owned[c] = (previous, chosen); + provider.addSensorConfiguration(c, chosen, markPending: false); + provider.applyConfiguration(c, chosen); + } + + Future _unbind({required bool restore}) async { + for (final s in _subscriptions) { + await s.cancel(); + } + _subscriptions.clear(); + if (restore && + _wearable != null && + wearables.wearables.contains(_wearable)) { + for (final e in _owned.entries) { + final current = + _configurationProvider?.getSelectedConfigurationValue(e.key); + final original = e.value.$1; + // Respect changes another app/recorder made after us. An unknown + // original configuration is deliberately not guessed as "off". + if (original != null && current == e.value.$2) { + _configurationProvider?.addSensorConfiguration( + e.key, + original, + markPending: false, + ); + _configurationProvider?.applyConfiguration(e.key, original); + } + } + } + _owned.clear(); + _gyros.clear(); + _accelerations.clear(); + _configurationProvider = null; + _wearable = null; + } + + @override + Future close() async { + _deviceId = null; + await _unbind(restore: true); + } + + @override + void dispose() { + _disposed = true; + wearables.removeListener(_devicesChanged); + unawaited( + close().whenComplete(() async { + await _samples.close(); + await _interruptions.close(); + }), + ); + } +} diff --git a/open_wearable/lib/apps/activity_tracker/services/activity_storage.dart b/open_wearable/lib/apps/activity_tracker/services/activity_storage.dart new file mode 100644 index 000000000..8ad058653 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/services/activity_storage.dart @@ -0,0 +1,118 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:path_provider/path_provider.dart'; +import '../models/activity_models.dart'; + +/// Disk-backed snapshots and streaming raw traces, kept out of widget state. +/// A serial write queue prevents older checkpoints replacing newer snapshots. +class ActivityStorage { + Directory? _directory; + IOSink? _trace; + Object? _traceError; + Future _writes = Future.value(); + Future get directory async => _directory ??= await Directory( + '${(await getApplicationSupportDirectory()).path}/activity_tracker', + ).create(recursive: true); + Future?> load() async { + final f = File('${(await directory).path}/sessions.json'); + if (!await f.exists()) return null; + return jsonDecode(await f.readAsString()) as Map; + } + + Future save(Map snapshot) { + final encoded = jsonEncode(snapshot); + final next = _writes.then((_) async { + final dir = await directory; + final temp = File('${dir.path}/sessions.tmp'); + await temp.writeAsString(encoded, flush: true); + await temp.rename('${dir.path}/sessions.json'); + }); + // Keep subsequent saves usable after a single I/O failure. + _writes = next.catchError((Object _) {}); + return next; + } + + Future startTrace(String id) async { + await closeTrace(); + _trace = File('${(await directory).path}/$id.csv') + .openWrite(mode: FileMode.append); + _traceError = null; + _trace!.done.catchError((Object e) { + _traceError = e; + }); + _trace!.writeln( + 'sensor_seconds,ax_m_s2,ay_m_s2,az_m_s2,gx_rad_s,gy_rad_s,gz_rad_s,mode,paused,calibrated,step_delta,rep_delta,jump_height_cm', + ); + } + + void append( + MotionSample s, + ActivityKind kind, + bool paused, + bool calibrated, + int steps, + int reps, + double? height, + ) { + _trace?.writeln( + '${s.seconds},${s.acceleration.x},${s.acceleration.y},${s.acceleration.z},${s.gyroscope?.x ?? ''},${s.gyroscope?.y ?? ''},${s.gyroscope?.z ?? ''},${kind.name},$paused,$calibrated,$steps,$reps,${height ?? ''}', + ); + } + + Future flushTrace() async { + if (_traceError != null) { + throw FileSystemException('Raw trace write failed: $_traceError'); + } + await _trace?.flush(); + } + + Future closeTrace() async { + final sink = _trace; + _trace = null; + await sink?.flush(); + await sink?.close(); + } + + /// Explicit export includes session metadata, reference trials and raw IMU. + Future> export(ActivitySession session) async { + await flushTrace(); + final root = (await directory).path; + final json = File('$root/${session.id}.json'); + await json.writeAsString( + const JsonEncoder.withIndent(' ').convert({ + 'schemaVersion': 1, + 'algorithmVersion': 'heuristic-1', + 'heightModel': + 'g * flight_seconds^2 / 8; equal take-off and landing posture assumed', + 'session': session.toJson(), + }), + ); + final trials = File('$root/${session.id}_trials.csv'); + String cell(Object? value) => + '"${(value ?? '').toString().replaceAll('"', '""')}"'; + await trials.writeAsString( + [ + 'participant,movement_pattern,reference_source,result,captured_at,activity,detected,reference,count_error,absolute_percentage_error,estimated_height_cm,reference_height_cm,height_error_cm', + ...session.trials.map( + (t) => [ + t.participant, + t.pattern, + t.referenceSource, + t.resultLabel, + t.capturedAt?.toIso8601String(), + t.kind.name, + t.detected, + t.reference, + t.countError, + t.absolutePercentageError, + t.estimatedHeightCm, + t.referenceHeightCm, + t.heightErrorCm, + ].map(cell).join(','), + ), + ].join('\n'), + ); + final raw = File('$root/${session.id}.csv'); + return [json.path, if (await raw.exists()) raw.path, trials.path]; + } +} diff --git a/open_wearable/lib/apps/activity_tracker/view_models/activity_tracker_controller.dart b/open_wearable/lib/apps/activity_tracker/view_models/activity_tracker_controller.dart new file mode 100644 index 000000000..884d10c94 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/view_models/activity_tracker_controller.dart @@ -0,0 +1,363 @@ +import 'dart:async'; +import 'dart:collection'; +import 'dart:math' as math; +import 'package:flutter/widgets.dart'; +import '../models/activity_engine.dart'; +import '../models/activity_models.dart'; +import '../services/activity_sensor_source.dart'; +import '../services/activity_storage.dart'; + +/// App-wide session owner: navigation never owns the live sensor subscription. +/// Confirmed results are checkpointed; incomplete cycles are never restored. +class ActivityTrackerController extends ChangeNotifier + with WidgetsBindingObserver { + ActivityTrackerController({ + required this.source, + ActivityStorage? storage, + bool observeLifecycle = true, + }) : storage = storage ?? ActivityStorage(), + _observeLifecycle = observeLifecycle { + if (observeLifecycle) WidgetsBinding.instance.addObserver(this); + ready = _load(); + _sampleSubscription = source.samples.listen( + _onSample, + onError: (Object e) => _interrupt('Motion stream interrupted: $e'), + ); + _statusSubscription = source.interruptions.listen((message) { + if (message != null) _interrupt(message); + }); + _timer = Timer.periodic(const Duration(seconds: 1), (_) { + if (deviceId == null) return; + if (_lastArrival != null && + DateTime.now().difference(_lastArrival!).inSeconds >= 2 && + interruption == null) { + _interrupt('Motion data interrupted'); + } + if (isTracking && ++_ticks % 5 == 0) unawaited(checkpoint()); + _notify(); + }); + } + final ActivitySensorSource source; + final ActivityStorage storage; + final bool _observeLifecycle; + final engine = ActivityEngine(); + late final Future ready; + late final StreamSubscription _sampleSubscription; + late final StreamSubscription _statusSubscription; + late final Timer _timer; + final List sessions = []; + final Map _dailySteps = {}; + final Queue _signal = Queue(); + final Queue _preRoll = Queue(); + List get signal => List.unmodifiable(_signal); + ActivitySession? current; + String? deviceId, interruption, storageError; + bool preparing = false, paused = false, backgrounded = false; + bool _disposed = false; + int _ticks = 0, repetitions = 0; + DateTime? _lastArrival, _lastUiUpdate; + final Stopwatch _setClock = Stopwatch(); + bool get isTracking => current != null; + bool get connected => + deviceId != null && + interruption == null && + _lastArrival != null && + DateTime.now().difference(_lastArrival!) < const Duration(seconds: 2); + bool get canCount => + isTracking && connected && !paused && !backgrounded && engine.calibrated; + String get deviceName => isTracking ? current!.deviceName : source.deviceName; + ActivityKind get mode => engine.kind; + int get setSeconds => _setClock.elapsed.inSeconds; + String get _day => DateTime.now().toIso8601String().substring(0, 10); + int get todaySteps => _dailySteps[_day] ?? 0; + int get currentSetNumber => + 1 + (current?.sets.where((s) => s.kind == mode).length ?? 0); + + Future _load() async { + try { + final data = await storage.load(); + if (data == null) return; + _dailySteps.addAll(Map.from(data['dailySteps'] ?? {})); + sessions.addAll( + (data['sessions'] as List? ?? []) + .map((s) => ActivitySession.fromJson(s)), + ); + for (final session in sessions.where((s) => s.endedAt == null)) { + session.interrupted = true; + session.endedAt = + DateTime.tryParse(data['checkpointAt'] ?? '') ?? session.startedAt; + } + } catch (e) { + storageError = 'Saved sessions could not be read. $e'; + } + _notify(); + } + + /// Opens a selected wearable; an existing session keeps its current device. + Future prepare(String id) async { + await ready; + if (isTracking) return; + preparing = true; + deviceId = id; + interruption = 'Waiting for motion data'; + _lastArrival = null; + _preRoll.clear(); + engine.recalibrate(); + _notify(); + try { + await source.open(id); + } catch (e) { + interruption = 'Unable to start IMU streaming: $e'; + } + preparing = false; + _notify(); + } + + Future reconnect() async { + if (deviceId == null) return; + engine.recalibrate(); + try { + await source.open(deviceId!); + } catch (e) { + _interrupt('Unable to reconnect: $e'); + } + } + + /// Cancels readiness streaming if the user leaves without starting a session. + Future releasePreparedDevice() async { + if (isTracking) return; + deviceId = null; + await source.close(); + } + + Future start() async { + await ready; + if (isTracking || !connected || !engine.calibrated) return; + final now = DateTime.now(); + final session = ActivitySession( + id: now.microsecondsSinceEpoch.toString(), + startedAt: now, + deviceName: source.deviceName, + calibration: engine.calibration, + ); + try { + await storage.startTrace(session.id); + for (final sample in _preRoll) { + storage.append(sample, ActivityKind.steps, false, false, 0, 0, null); + } + _preRoll.clear(); + } catch (e) { + storageError = 'Raw motion data could not be recorded. $e'; + _notify(); + return; + } + current = session; + sessions.insert(0, session); + paused = false; + engine.select(ActivityKind.steps); + await checkpoint(); + _notify(); + } + + void startExercise(ActivityKind kind) { + if (!canCount || mode != ActivityKind.steps || kind == ActivityKind.steps) { + return; + } + repetitions = 0; + _setClock + ..reset() + ..start(); + engine.select(kind); + _signal.clear(); + _notify(); + } + + void pause() { + if (!isTracking) return; + paused = true; + _setClock.stop(); + engine.recalibrate(); + unawaited(checkpoint()); + _notify(); + } + + void resume() { + if (!isTracking) return; + paused = false; + engine.recalibrate(); + if (mode != ActivityKind.steps) _setClock.start(); + _notify(); + } + + void finishSet() { + if (current == null || mode == ActivityKind.steps) return; + if (mode != ActivityKind.jumps) { + current!.sets.add( + ExerciseSet( + kind: mode, + repetitions: repetitions, + seconds: setSeconds, + ), + ); + } + repetitions = 0; + _setClock + ..stop() + ..reset(); + paused = false; + engine.select(ActivityKind.steps); + unawaited(checkpoint()); + _notify(); + } + + Future finish() async { + if (current == null) return null; + finishSet(); + final result = current!; + result.endedAt = DateTime.now(); + current = null; + deviceId = null; + await checkpoint(); + try { + await storage.closeTrace(); + } catch (e) { + storageError = 'Motion trace could not be saved. $e'; + } + await source.close(); + _notify(); + return result; + } + + void addTrial(ActivitySession session, ValidationTrial trial) { + session.trials.add(trial); + unawaited(checkpoint()); + _notify(); + } + + void _onSample(MotionSample sample) { + if (!isTracking) { + _preRoll.add(sample); + if (_preRoll.length > 250) _preRoll.removeFirst(); + } + final wasInterrupted = interruption != null; + interruption = null; + _lastArrival = DateTime.now(); + if (wasInterrupted) engine.recalibrate(); + final update = + paused || backgrounded ? const ActivityUpdate() : engine.add(sample); + if (mode != ActivityKind.steps) { + if (canCount) { + _setClock.start(); + } else { + _setClock.stop(); + } + } + if (isTracking) { + if (update.steps > 0) { + current!.steps += update.steps; + _dailySteps[_day] = todaySteps + update.steps; + } + repetitions += update.repetitions; + if (update.jump != null) current!.jumps.add(update.jump!); + storage.append( + sample, + mode, + paused || backgrounded, + engine.calibrated, + update.steps, + update.repetitions, + update.jump?.heightCm, + ); + if (update.steps > 0 || update.repetitions > 0 || update.jump != null) { + unawaited(checkpoint()); + } + } + _signal.add(engine.movement); + if (_signal.length > 160) _signal.removeFirst(); + // Algorithms consume every sample; widgets redraw at most 10 times/s. + if (_lastUiUpdate == null || + _lastArrival!.difference(_lastUiUpdate!).inMilliseconds >= 100) { + _lastUiUpdate = _lastArrival; + _notify(); + } + } + + void _interrupt(String message) { + if (interruption == message) return; + interruption = message; + _setClock.stop(); + engine.recalibrate(); + _notify(); + } + + /// Checkpoints counts and current partial set without resuming it on restart. + Future checkpoint() async { + if (storageError?.startsWith('Saved sessions could not be read') ?? false) { + return; + } + final recentDays = _dailySteps.keys.toList()..sort(); + for (final day in recentDays.take(math.max(0, recentDays.length - 90))) { + _dailySteps.remove(day); + } + final snapshot = { + 'schemaVersion': 1, + 'checkpointAt': DateTime.now().toIso8601String(), + 'dailySteps': _dailySteps, + 'sessions': sessions.take(200).map((s) { + final json = s.toJson(); + if (identical(s, current) && + mode != ActivityKind.steps && + mode != ActivityKind.jumps) { + json['sets'] = [ + ...s.sets.map((e) => e.toJson()), + ExerciseSet( + kind: mode, + repetitions: repetitions, + seconds: setSeconds, + ).toJson(), + ]; + } + return json; + }).toList(), + }; + try { + await storage.save(snapshot); + await storage.flushTrace(); + } catch (e) { + storageError = 'Session could not be saved. $e'; + _notify(); + } + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.paused || + state == AppLifecycleState.detached) { + backgrounded = true; + _setClock.stop(); + engine.recalibrate(); + unawaited(checkpoint()); + } else if (state == AppLifecycleState.resumed && backgrounded) { + backgrounded = false; + engine.recalibrate(); + if (!paused && mode != ActivityKind.steps) _setClock.start(); + _notify(); + } + } + + void _notify() { + if (!_disposed) notifyListeners(); + } + + @override + void dispose() { + _disposed = true; + if (_observeLifecycle) WidgetsBinding.instance.removeObserver(this); + _timer.cancel(); + unawaited(_sampleSubscription.cancel()); + unawaited(_statusSubscription.cancel()); + unawaited(checkpoint().whenComplete(storage.closeTrace)); + source.dispose(); + super.dispose(); + } +} diff --git a/open_wearable/lib/apps/activity_tracker/widgets/activity_session_pages.dart b/open_wearable/lib/apps/activity_tracker/widgets/activity_session_pages.dart new file mode 100644 index 000000000..9a124b475 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/widgets/activity_session_pages.dart @@ -0,0 +1,624 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:share_plus/share_plus.dart'; +import 'package:open_wearable/widgets/common/app_section_card.dart'; +import '../models/activity_models.dart'; +import '../view_models/activity_tracker_controller.dart'; +import 'activity_widgets.dart'; + +/// Stored workouts and the currently running session. +class ActivitySessionsPage extends StatelessWidget { + const ActivitySessionsPage({super.key}); + @override + Widget build(BuildContext context) => Consumer( + builder: (context, c, _) => ActivityLayout( + title: 'Sessions', + children: [ + if (c.sessions.isEmpty) + const ActivityNotice( + 'No sessions yet', + 'Start tracking to save your steps, exercise sets and jump attempts.', + ), + for (final s in c.sessions) + ActivityCard( + onTap: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ActivitySummaryPage(session: s), + ), + ), + child: Row( + children: [ + const ActivityIcon('activity', box: true), + const SizedBox(width: 12), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '${activityDate(s.startedAt)} · ${activityTime(s.startedAt)}', + style: const TextStyle(fontWeight: FontWeight.w700), + ), + const SizedBox(height: 5), + ActivityCaption( + '${s.steps} steps · ${s.repetitions} reps · ${s.jumps.length} jumps', + ), + if (s.endedAt == null || s.interrupted) + ActivityCaption( + s.interrupted + ? 'Recovered after interruption' + : 'Session in progress', + ), + ], + ), + ), + const Icon(Icons.chevron_right), + ], + ), + ), + ], + ), + ); +} + +/// Saved metrics and exports, using only confirmed algorithm events. +class ActivitySummaryPage extends StatelessWidget { + const ActivitySummaryPage({super.key, required this.session}); + final ActivitySession session; + @override + Widget build(BuildContext context) => Consumer( + builder: (context, c, _) { + final live = identical(c.current, session); + final duration = (session.endedAt ?? DateTime.now()) + .difference(session.startedAt) + .inSeconds; + final validJumps = + session.jumps.where((j) => j.heightCm != null).length; + return ActivityLayout( + title: 'Session summary', + actions: [ + OutlinedButton( + onPressed: () => exportActivitySession(context, session), + child: const Text('Export'), + ), + ElevatedButton( + onPressed: () => Navigator.pop(context), + child: Text(live ? 'Back to tracking' : 'Done'), + ), + ], + children: [ + Row( + children: [ + ActivityBadge( + live + ? 'Session in progress' + : session.interrupted + ? 'Session recovered' + : 'Session saved', + warning: session.interrupted, + ), + ], + ), + Text( + '${activityDate(session.startedAt)}, ${activityTime(session.startedAt)} · ${activityDuration(duration)}', + style: + const TextStyle(fontWeight: FontWeight.w700, fontSize: 18), + ), + ActivityCaption(session.deviceName), + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ActivityMetric( + 'Steps', + '${session.steps}', + detail: 'During this session', + ), + ), + const SizedBox(width: 10), + Expanded( + child: ActivityMetric( + 'Repetitions', + '${session.repetitions}', + detail: '${session.sets.length} completed sets', + ), + ), + ], + ), + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ActivityMetric( + 'Jumps', + '${session.jumps.length}', + detail: '$validJumps valid height estimates', + ), + ), + const SizedBox(width: 10), + Expanded( + child: ActivityMetric( + 'Best estimate', + session.bestHeightCm == null + ? '—' + : '${session.bestHeightCm!.toStringAsFixed(1)} cm', + detail: 'Flight-time estimate', + ), + ), + ], + ), + AppSectionCard( + title: 'Your workout', + child: session.sets.isEmpty + ? const ActivityCaption('No completed exercise sets.') + : Column( + children: [ + for (var i = 0; i < session.sets.length; i++) + ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + session.sets[i].kind.label, + style: const TextStyle( + fontSize: 14, + fontWeight: FontWeight.w700, + ), + ), + subtitle: ActivityCaption( + 'Set ${i + 1} · ${activityDuration(session.sets[i].seconds)}', + ), + trailing: Text( + '${session.sets[i].repetitions} reps', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.w700, + ), + ), + ), + ], + ), + ), + if (session.jumps.isNotEmpty) + AppSectionCard( + title: 'Jump attempts', + child: Column( + children: [ + for (var i = 0; i < session.jumps.length; i++) + ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + 'Jump ${i + 1}', + style: const TextStyle( + fontSize: 14, + fontWeight: FontWeight.w700, + ), + ), + subtitle: ActivityCaption( + session.jumps[i].reason ?? 'Good signal', + ), + trailing: Text( + session.jumps[i].heightCm == null + ? '—' + : '${session.jumps[i].heightCm!.toStringAsFixed(1)} cm', + ), + ), + ], + ), + ), + if (session.interrupted) + const ActivityNotice( + 'Confirmed results recovered', + 'The previous session ended unexpectedly. Incomplete movements were discarded.', + ), + TextButton.icon( + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ActivityStudyPage(session: session), + ), + ), + icon: const Icon(Icons.science_outlined), + label: Text( + 'Study mode · ${session.trials.length} reference trials', + ), + ), + if (live) + OutlinedButton( + onPressed: () async { + await c.finish(); + }, + child: const Text('End and save session'), + ), + if (c.storageError != null) + ActivityNotice( + 'Storage needs attention', + c.storageError!, + warning: true, + ), + ], + ); + }, + ); +} + +/// Reference-based evaluation for participants, speeds and execution styles. +/// Sources refer to recorded sets, individual jump attempts or session steps. +class ActivityStudyPage extends StatefulWidget { + const ActivityStudyPage({super.key, required this.session}); + final ActivitySession session; + @override + State createState() => _ActivityStudyPageState(); +} + +class _ActivityStudyPageState extends State { + final _participant = TextEditingController(); + final _pattern = TextEditingController(); + final _source = TextEditingController(text: 'Annotated video'); + final _reference = TextEditingController(); + final _height = TextEditingController(); + ActivityKind _kind = ActivityKind.steps; + int _record = 0; + bool _triedSaving = false; + late final ActivitySession _captured = + ActivitySession.fromJson(widget.session.toJson()); + final DateTime _capturedAt = DateTime.now(); + @override + void dispose() { + for (final c in [_participant, _pattern, _source, _reference, _height]) { + c.dispose(); + } + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final session = widget.session; + final sets = _captured.sets.where((s) => s.kind == _kind).toList(); + final recordCount = _kind == ActivityKind.steps + ? 1 + : _kind == ActivityKind.jumps + ? _captured.jumps.length + : sets.length; + final hasRecord = _record < recordCount; + final detected = !hasRecord + ? null + : _kind == ActivityKind.steps + ? _captured.steps + : _kind == ActivityKind.jumps + ? 1 + : sets[_record].repetitions; + final estimate = _kind == ActivityKind.jumps && hasRecord + ? _captured.jumps[_record].heightCm + : null; + final reference = int.tryParse(_reference.text); + final referenceHeight = double.tryParse(_height.text.replaceAll(',', '.')); + final valid = hasRecord && + _participant.text.trim().isNotEmpty && + _pattern.text.trim().isNotEmpty && + _source.text.trim().isNotEmpty && + reference != null && + reference >= 0 && + (_height.text.isEmpty || + (referenceHeight != null && + referenceHeight.isFinite && + referenceHeight > 0)); + final error = + reference == null || detected == null ? null : detected - reference; + final heightTrials = + session.trials.where((t) => t.heightErrorCm != null).toList(); + return ActivityLayout( + title: 'Validation session', + actions: [ + OutlinedButton( + onPressed: () => Navigator.pop(context), + child: const Text('Back'), + ), + ElevatedButton( + onPressed: () => exportActivitySession(context, session), + child: const Text('Export trials'), + ), + ], + children: [ + const Row(children: [ActivityBadge('Study mode')]), + const ActivityCaption( + 'Results are captured when this screen opens. Each reference must cover the same interval.', + ), + const Text( + 'Compare against a reference', + style: TextStyle(fontSize: 24, fontWeight: FontWeight.w700), + ), + TextField( + controller: _participant, + decoration: const InputDecoration( + labelText: 'Participant ID', + hintText: 'e.g. P01', + ), + onChanged: (_) => setState(() {}), + ), + TextField( + controller: _pattern, + decoration: const InputDecoration( + labelText: 'Movement pattern / trial', + hintText: 'e.g. Slow pace · trial 1', + ), + onChanged: (_) => setState(() {}), + ), + ActivityCard( + child: Column( + children: [ + DropdownButtonFormField( + initialValue: _kind, + decoration: const InputDecoration(labelText: 'Activity'), + items: ActivityKind.values + .map( + (k) => DropdownMenuItem(value: k, child: Text(k.label)), + ) + .toList(), + onChanged: (k) => setState(() { + _kind = k!; + _record = 0; + _reference.clear(); + _height.clear(); + }), + ), + const SizedBox(height: 12), + if (hasRecord) + DropdownButtonFormField( + key: ValueKey('${_kind.name}$_record'), + initialValue: _record, + isExpanded: true, + decoration: + const InputDecoration(labelText: 'Detected result'), + items: List.generate( + recordCount, + (i) => DropdownMenuItem( + value: i, + child: Text( + _kind == ActivityKind.steps + ? 'Entire session · ${session.steps} steps' + : _kind == ActivityKind.jumps + ? 'Jump ${i + 1}' + : 'Set ${i + 1} · ${sets[i].repetitions} reps', + ), + ), + ), + onChanged: (i) => setState(() => _record = i!), + ) + else + const ActivityCaption('Complete a set or a jump test first.'), + const SizedBox(height: 12), + TextField( + controller: _source, + decoration: + const InputDecoration(labelText: 'Reference source'), + onChanged: (_) => setState(() {}), + ), + ], + ), + ), + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ActivityMetric( + 'Detected', + detected?.toString() ?? '—', + detail: _kind == ActivityKind.jumps + ? 'Jump attempt' + : 'Confirmed count', + ), + ), + const SizedBox(width: 10), + Expanded( + child: ActivityCard( + child: TextField( + controller: _reference, + keyboardType: TextInputType.number, + decoration: InputDecoration( + labelText: 'Reference count', + errorText: + _triedSaving && (reference == null || reference < 0) + ? 'Enter a count ≥ 0' + : null, + ), + onChanged: (_) => setState(() {}), + ), + ), + ), + ], + ), + if (_kind == ActivityKind.jumps) + TextField( + controller: _height, + keyboardType: const TextInputType.numberWithOptions(decimal: true), + decoration: const InputDecoration( + labelText: 'Reference height (cm)', + hintText: 'Measured independently', + ), + onChanged: (_) => setState(() {}), + ), + AppSectionCard( + title: 'Trial comparison', + child: Column( + children: [ + _ComparisonRow( + 'Count error', + 'Detected − reference', + error == null ? '—' : '${error > 0 ? '+' : ''}$error', + ), + if (reference != null && reference > 0 && error != null) + _ComparisonRow( + 'Absolute percentage error', + 'Relative to the reference', + '${(error.abs() / reference * 100).toStringAsFixed(1)}%', + ), + if (_kind == ActivityKind.jumps) ...[ + _ComparisonRow( + 'Jump estimate', + 'Flight-time estimate', + estimate == null + ? 'Unavailable' + : '${estimate.toStringAsFixed(1)} cm', + ), + _ComparisonRow( + 'Height difference', + 'Estimate − reference', + estimate != null && referenceHeight != null + ? '${(estimate - referenceHeight).toStringAsFixed(1)} cm' + : '—', + ), + ], + ], + ), + ), + const ActivityNotice( + 'Reference needed', + 'Use independently annotated counts and measurements. Compare separate trials across users, speeds and execution styles. Choose Steps for negative controls such as head movements.', + ), + if (_triedSaving && !valid) + const ActivityCaption( + 'Enter a participant, movement pattern, reference source and valid reference values.', + ), + ElevatedButton( + onPressed: () { + setState(() => _triedSaving = true); + if (!valid) return; + context.read().addTrial( + session, + ValidationTrial( + participant: _participant.text.trim(), + pattern: _pattern.text.trim(), + referenceSource: _source.text.trim(), + resultLabel: _kind == ActivityKind.steps + ? 'Session steps' + : '${_kind == ActivityKind.jumps ? 'Jump' : 'Set'} ${_record + 1}', + capturedAt: _capturedAt, + kind: _kind, + detected: detected!, + reference: reference, + estimatedHeightCm: estimate, + referenceHeightCm: referenceHeight, + ), + ); + setState(() { + _reference.clear(); + _height.clear(); + _triedSaving = false; + }); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Reference trial saved')), + ); + }, + child: const Text('Save reference trial'), + ), + if (session.trials.isNotEmpty) + AppSectionCard( + title: '${session.trials.length} saved reference trials', + subtitle: '${heightTrials.length} paired height estimates', + child: Column( + children: [ + for (final kind in ActivityKind.values) + if (session.trials.any((t) => t.kind == kind)) + _ComparisonRow( + '${kind.label} · count MAE', + 'Mean absolute error across saved trials', + (session.trials.where((t) => t.kind == kind).fold( + 0, + (n, t) => n + t.countError.abs(), + ) / + session.trials + .where((t) => t.kind == kind) + .length) + .toStringAsFixed(2), + ), + if (heightTrials.isNotEmpty) + _ComparisonRow( + 'Height MAE', + 'Paired estimates only', + '${(heightTrials.fold(0, (n, t) => n + t.heightErrorCm!.abs()) / heightTrials.length).toStringAsFixed(1)} cm', + ), + for (final t in session.trials.reversed) + ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + '${t.participant} · ${t.kind.label}', + style: const TextStyle( + fontSize: 14, + fontWeight: FontWeight.w700, + ), + ), + subtitle: ActivityCaption( + '${t.pattern} · ${t.detected}/${t.reference} detected/reference', + ), + trailing: + Text('${t.countError > 0 ? '+' : ''}${t.countError}'), + ), + ], + ), + ), + ], + ); + } +} + +class _ComparisonRow extends StatelessWidget { + const _ComparisonRow(this.title, this.detail, this.value); + final String title, detail, value; + @override + Widget build(BuildContext context) => Padding( + padding: const EdgeInsets.symmetric(vertical: 10), + child: Row( + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + title, + style: const TextStyle( + fontWeight: FontWeight.w700, + fontSize: 14, + ), + ), + const SizedBox(height: 4), + ActivityCaption(detail), + ], + ), + ), + const SizedBox(width: 10), + Text( + value, + style: TextStyle( + fontWeight: FontWeight.w700, + color: Theme.of(context).colorScheme.primary, + ), + ), + ], + ), + ); +} + +/// Shares local exports only after an explicit Export action. +Future exportActivitySession( + BuildContext context, + ActivitySession session, +) async { + final controller = context.read(); + final box = context.findRenderObject() as RenderBox?; + final origin = box == null ? null : box.localToGlobal(Offset.zero) & box.size; + try { + final paths = await controller.storage.export(session); + await SharePlus.instance.share( + ShareParams( + title: 'Activity Tracker session', + files: paths.map(XFile.new).toList(), + sharePositionOrigin: origin, + ), + ); + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Export could not be created: $e')), + ); + } + } +} diff --git a/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart b/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart new file mode 100644 index 000000000..e304c66c5 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart @@ -0,0 +1,913 @@ +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; +import 'package:open_earable_flutter/open_earable_flutter.dart'; +import 'package:open_wearable/widgets/common/app_section_card.dart'; +import 'package:open_wearable/apps/widgets/select_earable_view.dart'; +import 'package:open_wearable/apps/widgets/app_compatibility.dart'; +import 'package:provider/provider.dart'; +import '../models/activity_models.dart'; +import '../view_models/activity_tracker_controller.dart'; +import 'activity_session_pages.dart'; +import 'activity_widgets.dart'; + +/// Built-in mini-app entry; history and interrupted sessions work offline. +class ActivityTrackerPage extends StatefulWidget { + const ActivityTrackerPage({super.key, this.wearable}); + final Wearable? wearable; + @override + State createState() => _ActivityTrackerPageState(); +} + +class _ActivityTrackerPageState extends State { + bool _fitChecked = false, _starting = false; + late ActivityTrackerController _controller; + @override + void initState() { + super.initState(); + _controller = context.read(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted && widget.wearable != null) { + context + .read() + .prepare(widget.wearable!.deviceId); + } + }); + } + + @override + void dispose() { + _controller.releasePreparedDevice(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => Consumer( + builder: (context, c, _) { + if (!c.isTracking && c.deviceId == null) { + return ActivityLayout( + title: 'Activity Tracker', + actions: [ + OutlinedButton( + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => const ActivitySessionsPage(), + ), + ), + child: const Text('View sessions'), + ), + ElevatedButton( + onPressed: () => _chooseWearable(context), + child: const Text('Choose wearable'), + ), + ], + children: const [ + SizedBox(height: 12), + Text( + 'Ready to move?', + style: TextStyle(fontSize: 24, fontWeight: FontWeight.w700), + ), + ActivityNotice( + 'Connect your earable', + 'Choose a connected OpenEarable to start tracking. Your saved sessions are available at any time.', + ), + ], + ); + } + if (!c.isTracking) return _ready(c); + if (!c.connected || c.paused) { + return ActivityInterruptionView(controller: c); + } + final latest = c.sessions.where((s) => s.endedAt != null).firstOrNull; + final lastJump = c.current!.jumps.lastOrNull; + return ActivityLayout( + title: 'Activity Tracker', + appBarActions: [ + PopupMenuButton( + onSelected: (value) async { + if (value == 'pause') c.pause(); + if (value == 'finish') { + final session = await c.finish(); + if (context.mounted && session != null) { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ActivitySummaryPage(session: session), + ), + ); + } + } + }, + itemBuilder: (_) => [ + const PopupMenuItem( + value: 'pause', + child: Text('Pause tracking'), + ), + const PopupMenuItem( + value: 'finish', + child: Text('End session'), + ), + ], + ), + ], + actions: [ + OutlinedButton( + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => ActivityStudyPage(session: c.current!), + ), + ), + child: const Text('Study mode'), + ), + ElevatedButton( + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => const ActivitySessionsPage(), + ), + ), + child: const Text('View sessions'), + ), + ], + children: [ + _DeviceStrip(c), + Container( + padding: const EdgeInsets.all(18), + decoration: BoxDecoration( + color: Theme.of(context).brightness == Brightness.dark + ? Theme.of(context).colorScheme.primaryContainer + : const Color(0xFF835B58), + borderRadius: BorderRadius.circular(18), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'TODAY’S STEPS', + style: TextStyle( + color: Colors.white, + fontSize: 12, + fontWeight: FontWeight.w700, + ), + ), + ActivityIcon('activity', color: Colors.white), + ], + ), + const SizedBox(height: 8), + Text( + _number(c.todaySteps), + style: const TextStyle( + color: Colors.white, + fontSize: 52, + height: 1.12, + fontWeight: FontWeight.w700, + letterSpacing: -0.2, + ), + ), + const SizedBox(height: 8), + ActivityCaption( + c.engine.calibrated + ? 'Tracking since ${activityTime(c.current!.startedAt)}' + : 'Stand still briefly to resume counting', + color: Colors.white, + ), + const SizedBox(height: 8), + ActivitySignal(c.signal, color: Colors.white, height: 34), + ], + ), + ), + if (c.storageError != null) + ActivityNotice( + 'Storage needs attention', + c.storageError!, + warning: true, + ), + if (!c.engine.calibrated) + ActivityNotice( + 'Stand still briefly', + 'Keep your head relaxed while the motion signal settles.', + ), + if (c.mode != ActivityKind.steps) + ActivityCard( + onTap: () => _openExercise(context, c), + child: Row( + children: [ + ActivityIcon(_icon(c.mode), box: true), + const SizedBox(width: 12), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Continue ${c.mode.label.toLowerCase()}', + style: + const TextStyle(fontWeight: FontWeight.w700), + ), + ActivityCaption( + '${c.repetitions} confirmed repetitions', + ), + ], + ), + ), + const Icon(Icons.chevron_right), + ], + ), + ), + const Padding( + padding: EdgeInsets.symmetric(vertical: 3), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + child: Text( + 'Start an exercise', + style: TextStyle( + fontWeight: FontWeight.w700, + fontSize: 16, + ), + ), + ), + SizedBox(width: 8), + Flexible(child: ActivityCaption('Choose manually')), + ], + ), + ), + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: _exerciseCard( + context, + c, + ActivityKind.squats, + 'Count complete repetitions', + ), + ), + const SizedBox(width: 10), + Expanded( + child: _exerciseCard( + context, + c, + ActivityKind.pushUps, + 'Track your next set', + ), + ), + ], + ), + ActivityCard( + onTap: c.canCount && c.mode == ActivityKind.steps + ? () { + c.startExercise(ActivityKind.jumps); + _openExercise(context, c); + } + : null, + child: Row( + children: [ + const ActivityIcon('jump', box: true), + const SizedBox(width: 12), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + 'Jump test', + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 4), + ActivityCaption( + lastJump?.heightCm != null + ? 'Last estimate ${lastJump!.heightCm!.toStringAsFixed(1)} cm' + : 'Detect jumps and estimate height', + ), + ], + ), + ), + const Icon(Icons.chevron_right), + ], + ), + ), + ActivityCard( + onTap: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => + ActivitySummaryPage(session: latest ?? c.current!), + ), + ), + child: Row( + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + latest == null ? 'Current session' : 'Last workout', + style: const TextStyle(fontWeight: FontWeight.w700), + ), + const SizedBox(height: 6), + ActivityCaption( + latest == null + ? 'Your results will appear here' + : '${activityDate(latest.startedAt)} · ${activityTime(latest.startedAt)}', + ), + ], + ), + ), + Text( + '${(latest ?? c.current!).repetitions} reps', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.w700, + ), + ), + ], + ), + ), + const ActivityCaption( + 'Tracking continues between screens while the app is in the foreground. Keep the app open during measurement.', + ), + ], + ); + }, + ); + + Widget _ready(ActivityTrackerController c) => ActivityLayout( + title: 'Activity Tracker', + appBarActions: [ + IconButton( + tooltip: 'View sessions', + onPressed: () => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => const ActivitySessionsPage(), + ), + ), + icon: const Icon(Icons.history), + ), + ], + actions: [ + ElevatedButton( + onPressed: + !_starting && _fitChecked && c.connected && c.engine.calibrated + ? () async { + setState(() => _starting = true); + await c.start(); + if (mounted) setState(() => _starting = false); + } + : null, + child: Text(_starting ? 'Starting…' : 'Start tracking'), + ), + ], + children: [ + const SizedBox(height: 12), + const Text( + 'Ready to move?', + style: TextStyle( + fontSize: 24, + fontWeight: FontWeight.w700, + letterSpacing: -0.3, + ), + ), + Text( + 'Wear your earable securely and take a moment to get comfortable.', + style: TextStyle( + color: Theme.of(context).colorScheme.onSurfaceVariant, + ), + ), + ActivityCard( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Container( + width: 52, + height: 52, + decoration: BoxDecoration( + color: Theme.of(context) + .colorScheme + .primary + .withValues(alpha: 0.12), + borderRadius: BorderRadius.circular(14), + ), + child: Icon( + Icons.hearing, + color: Theme.of(context).colorScheme.primary, + ), + ), + const SizedBox(width: 14), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + c.deviceName, + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 4), + ActivityCaption( + c.connected + ? 'Connected · motion data available' + : 'Connecting to motion sensors', + ), + ], + ), + ), + ], + ), + const SizedBox(height: 8), + const ActivityBadge('Selected'), + ], + ), + ), + AppSectionCard( + title: 'A quick check', + child: Column( + children: [ + CheckboxListTile( + contentPadding: EdgeInsets.zero, + title: const Text( + '1. Check the fit', + style: TextStyle(fontSize: 14, fontWeight: FontWeight.w700), + ), + subtitle: + const ActivityCaption('Wear your earable securely.'), + value: _fitChecked, + onChanged: (v) => setState(() => _fitChecked = v ?? false), + ), + const Divider(), + ListTile( + contentPadding: EdgeInsets.zero, + title: const Text( + '2. Stand still briefly', + style: TextStyle(fontSize: 14, fontWeight: FontWeight.w700), + ), + subtitle: const ActivityCaption( + 'Keep your head relaxed for 1.5 seconds.', + ), + trailing: c.engine.calibrated + ? const Icon(Icons.check) + : SizedBox( + width: 28, + height: 28, + child: CircularProgressIndicator( + value: c.connected + ? c.engine.calibrationProgress + : null, + strokeWidth: 2, + ), + ), + ), + const Divider(), + const ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + '3. Start tracking', + style: TextStyle(fontSize: 14, fontWeight: FontWeight.w700), + ), + subtitle: + ActivityCaption('Steps continue between exercises.'), + ), + ], + ), + ), + ActivityNotice( + c.engine.calibrated + ? 'You’re ready' + : 'Waiting for a steady signal', + c.engine.calibrated + ? 'Motion data is available. You can begin.' + : c.interruption ?? + 'Stand still while the motion sensors calibrate.', + ), + if (c.interruption != null && !c.preparing) + TextButton.icon( + onPressed: c.reconnect, + icon: const Icon(Icons.refresh), + label: const Text('Retry connection'), + ), + if (c.storageError != null) + ActivityNotice( + 'Storage needs attention', + c.storageError!, + warning: true, + ), + const ActivityCaption( + 'This first implementation uses motion sensors. Jump height is an estimate. Tracking pauses when the app moves to the background.', + ), + ], + ); + + Widget _exerciseCard( + BuildContext context, + ActivityTrackerController c, + ActivityKind kind, + String detail, + ) => + ActivityCard( + onTap: c.canCount && c.mode == ActivityKind.steps + ? () { + c.startExercise(kind); + _openExercise(context, c); + } + : null, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ActivityIcon(_icon(kind), box: true), + const SizedBox(height: 8), + Text( + kind.label, + style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w700), + ), + const SizedBox(height: 4), + SizedBox(height: 34, child: ActivityCaption(detail)), + const SizedBox(height: 8), + Row( + children: [ + Expanded( + child: Text( + 'Start set', + style: TextStyle( + fontSize: 12, + color: Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.w700, + ), + ), + ), + Icon( + Icons.chevron_right, + size: 16, + color: Theme.of(context).colorScheme.primary, + ), + ], + ), + ], + ), + ); +} + +void _openExercise(BuildContext context, ActivityTrackerController c) => + Navigator.push( + context, + MaterialPageRoute(builder: (_) => const _ExercisePage()), + ); + +void _chooseWearable(BuildContext context) => Navigator.push( + context, + MaterialPageRoute( + builder: (_) => SelectEarableView( + supportedDevicePrefixes: const ['OpenEarable'], + requiredCapabilities: [ + WearableCapabilityRequirement.capability( + label: 'sensor streaming', + ), + WearableCapabilityRequirement.capability< + SensorConfigurationManager>(label: 'sensor configuration'), + ], + startApp: (wearable, _) async => + ActivityTrackerPage(wearable: wearable), + ), + ), + ); +String _icon(ActivityKind kind) => switch (kind) { + ActivityKind.squats => 'squat', + ActivityKind.pushUps => 'pushup', + ActivityKind.jumps => 'jump', + ActivityKind.steps => 'activity' + }; +String _number(int value) => value + .toString() + .replaceAllMapped(RegExp(r'(\d)(?=(\d{3})+(?!\d))'), (m) => '${m[1]},'); + +class _DeviceStrip extends StatelessWidget { + const _DeviceStrip(this.controller); + final ActivityTrackerController controller; + @override + Widget build(BuildContext context) => Padding( + padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 7), + child: Row( + children: [ + const Icon(Icons.bluetooth, size: 18), + const SizedBox(width: 6), + Expanded( + child: ActivityCaption( + '${controller.deviceName} · ${controller.connected ? 'Connected' : 'Disconnected'}', + ), + ), + ], + ), + ); +} + +class _ExercisePage extends StatelessWidget { + const _ExercisePage(); + @override + Widget build(BuildContext context) => Consumer( + builder: (context, c, _) { + if (!c.isTracking || c.mode == ActivityKind.steps) { + return ActivityLayout( + title: 'Workout complete', + actions: [ + ElevatedButton( + onPressed: () => Navigator.pop(context), + child: const Text('Back to tracking'), + ), + ], + children: const [ + ActivityNotice( + 'Set saved', + 'Your confirmed results are in the session.', + ), + ], + ); + } + if (!c.connected || c.paused) { + return ActivityInterruptionView(controller: c, exercise: true); + } + final jumpMode = c.mode == ActivityKind.jumps; + final last = c.current!.jumps.lastOrNull; + return ActivityLayout( + title: c.mode.label, + actions: [ + OutlinedButton(onPressed: c.pause, child: const Text('Pause')), + ElevatedButton( + onPressed: () { + c.finishSet(); + Navigator.pop(context); + }, + child: Text(jumpMode ? 'Finish test' : 'Finish set'), + ), + ], + children: [ + _DeviceStrip(c), + ActivityCard( + padding: 18, + child: SizedBox( + width: double.infinity, + child: Column( + children: [ + ActivityBadge( + !c.engine.calibrated + ? 'Stand still to calibrate' + : jumpMode + ? c.engine.phase + : 'Counting repetitions', + ), + const SizedBox(height: 12), + ActivityIcon(_icon(c.mode), size: 30), + const SizedBox(height: 8), + Text( + jumpMode + ? last?.heightCm?.toStringAsFixed(1) ?? '—' + : '${c.repetitions}', + style: const TextStyle( + fontSize: 80, + height: 1.15, + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 8), + ActivityCaption( + jumpMode ? 'cm · estimated height' : 'repetitions', + ), + const SizedBox(height: 12), + if (jumpMode) + Text( + c.current!.bestHeightCm == null + ? 'Your next jump will appear here' + : 'Session best ${c.current!.bestHeightCm!.toStringAsFixed(1)} cm', + style: TextStyle( + fontSize: 12, + color: Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.w700, + ), + ) + else ...[ + Row( + mainAxisSize: MainAxisSize.min, + children: List.generate( + 4, + (i) => Container( + width: 38, + height: 4, + margin: const EdgeInsets.symmetric(horizontal: 3), + decoration: BoxDecoration( + color: Theme.of(context) + .colorScheme + .primary + .withValues( + alpha: i < + (c.engine.phase == + 'Returning to start' + ? 3 + : c.engine.phase == 'Lowering' + ? 2 + : 1) + ? 1 + : 0.16, + ), + borderRadius: BorderRadius.circular(4), + ), + ), + ), + ), + const SizedBox(height: 8), + ActivityCaption( + c.engine.phase, + color: Theme.of(context).colorScheme.primary, + ), + ], + ], + ), + ), + ), + if (!jumpMode) ...[ + Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ActivityMetric( + 'Current set', + '${c.currentSetNumber}', + detail: 'Complete cycles only', + ), + ), + const SizedBox(width: 10), + Expanded( + child: ActivityMetric( + 'Elapsed', + activityDuration(c.setSeconds), + detail: 'Tracking active', + ), + ), + ], + ), + AppSectionCard( + title: 'Movement', + subtitle: c.engine.quality, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ActivitySignal(c.signal), + const SizedBox(height: 10), + const ActivityCaption( + 'Hold the start position briefly. Lower and return fully; keep your head aligned with your body.', + ), + ], + ), + ), + ] else ...[ + const ActivityNotice( + 'Jump straight up', + 'Begin from a still position. Keep take-off and landing posture similar. Height uses flight time and is an estimate.', + ), + AppSectionCard( + title: 'Recent attempts', + child: c.current!.jumps.isEmpty + ? const ActivityCaption( + 'Stand still briefly, then jump and land in place.', + ) + : Column( + children: c.current!.jumps.reversed + .take(5) + .map( + (j) => ListTile( + contentPadding: EdgeInsets.zero, + title: Text( + 'Jump ${c.current!.jumps.indexOf(j) + 1}', + style: const TextStyle( + fontSize: 14, + fontWeight: FontWeight.w700, + ), + ), + subtitle: ActivityCaption( + j.reason ?? + 'Good signal · flight ${j.flightSeconds.toStringAsFixed(2)} s', + ), + trailing: Text( + j.heightCm == null + ? '—' + : '${j.heightCm!.toStringAsFixed(1)} cm', + style: TextStyle( + color: + Theme.of(context).colorScheme.primary, + fontWeight: FontWeight.w700, + ), + ), + ), + ) + .toList(), + ), + ), + ], + ], + ); + }, + ); +} + +/// Pause and disconnect states preserve counters and offer explicit recovery. +class ActivityInterruptionView extends StatelessWidget { + const ActivityInterruptionView({ + super.key, + required this.controller, + this.exercise = false, + }); + final ActivityTrackerController controller; + final bool exercise; + @override + Widget build(BuildContext context) { + final c = controller; + final lost = !c.connected; + return ActivityLayout( + title: c.mode == ActivityKind.steps ? 'Activity Tracker' : c.mode.label, + actions: [ + OutlinedButton( + onPressed: () async { + final session = await c.finish(); + if (context.mounted && session != null) { + Navigator.pushReplacement( + context, + MaterialPageRoute( + builder: (_) => ActivitySummaryPage(session: session), + ), + ); + } + }, + child: const Text('End session'), + ), + ElevatedButton( + onPressed: lost + ? () async { + await c.reconnect(); + if (context.mounted && !c.connected) { + GoRouter.maybeOf(context)?.push('/connect-devices'); + } + } + : c.resume, + child: Text(lost ? 'Reconnect' : 'Resume'), + ), + ], + children: [ + _DeviceStrip(c), + ActivityNotice( + lost ? 'Connection lost' : 'Take your time', + lost + ? 'Your confirmed results are saved. Reconnect your earable, then stand still briefly.' + : 'Workout counting is paused. Your completed repetitions are saved.', + warning: lost, + ), + ActivityCard( + padding: 24, + child: Column( + children: [ + Icon( + lost ? Icons.bluetooth_disabled : Icons.pause_circle_outline, + size: 38, + color: Theme.of(context).colorScheme.primary, + ), + const SizedBox(height: 16), + Text( + c.mode == ActivityKind.steps + ? '${c.current?.steps ?? 0}' + : '${c.repetitions}', + style: const TextStyle( + fontSize: 64, + fontWeight: FontWeight.w700, + ), + ), + ActivityCaption( + c.mode == ActivityKind.steps + ? 'confirmed session steps' + : 'confirmed repetitions', + ), + const SizedBox(height: 18), + ActivityBadge( + lost ? 'Tracking interrupted' : 'Paused', + warning: lost, + ), + ], + ), + ), + ActivityNotice( + lost ? 'Bring your earable closer' : 'Ready when you are', + lost + ? c.interruption ?? + 'Check that your earable is switched on and seated securely.' + : 'Resume to continue your current set, or finish the workout.', + ), + ], + ); + } +} diff --git a/open_wearable/lib/apps/activity_tracker/widgets/activity_widgets.dart b/open_wearable/lib/apps/activity_tracker/widgets/activity_widgets.dart new file mode 100644 index 000000000..5505ed10b --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/widgets/activity_widgets.dart @@ -0,0 +1,311 @@ +import 'dart:math' as math; +import 'package:flutter/material.dart'; +import 'package:flutter_svg/flutter_svg.dart'; + +/// Icon assets exported from the approved Figma design. +class ActivityIcon extends StatelessWidget { + const ActivityIcon( + this.name, { + super.key, + this.size = 24, + this.color, + this.box = false, + }); + final String name; + final double size; + final Color? color; + final bool box; + @override + Widget build(BuildContext context) { + final scheme = Theme.of(context).colorScheme; + final icon = SvgPicture.asset( + 'lib/apps/activity_tracker/assets/$name.svg', + width: size, + height: size, + colorFilter: ColorFilter.mode(color ?? scheme.primary, BlendMode.srcIn), + ); + return box + ? Container( + width: 44, + height: 44, + decoration: BoxDecoration( + color: scheme.primary.withValues(alpha: 0.12), + borderRadius: BorderRadius.circular(14), + ), + alignment: Alignment.center, + child: icon, + ) + : icon; + } +} + +/// Figma's 18 px card, inheriting light/dark surfaces from the host theme. +class ActivityCard extends StatelessWidget { + const ActivityCard({ + super.key, + required this.child, + this.onTap, + this.padding = 14, + }); + final Widget child; + final VoidCallback? onTap; + final double padding; + @override + Widget build(BuildContext context) => Card( + margin: EdgeInsets.zero, + clipBehavior: Clip.antiAlias, + child: InkWell( + onTap: onTap, + child: Padding(padding: EdgeInsets.all(padding), child: child), + ), + ); +} + +class ActivityMetric extends StatelessWidget { + const ActivityMetric(this.label, this.value, {super.key, this.detail}); + final String label, value; + final String? detail; + @override + Widget build(BuildContext context) => ActivityCard( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ActivityCaption(label), + const SizedBox(height: 6), + Text( + value, + style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w700), + ), + if (detail != null) ...[ + const SizedBox(height: 6), + ActivityCaption(detail!), + ], + ], + ), + ); +} + +class ActivityCaption extends StatelessWidget { + const ActivityCaption(this.text, {super.key, this.color}); + final String text; + final Color? color; + @override + Widget build(BuildContext context) => Text( + text, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: color ?? Theme.of(context).colorScheme.onSurfaceVariant, + fontSize: 12, + height: 1.4, + ), + ); +} + +class ActivityBadge extends StatelessWidget { + const ActivityBadge(this.label, {super.key, this.warning = false}); + final String label; + final bool warning; + @override + Widget build(BuildContext context) { + final scheme = Theme.of(context).colorScheme; + final color = warning + ? scheme.error + : Theme.of(context).brightness == Brightness.dark + ? const Color(0xFF81C784) + : const Color(0xFF2E7D32); + return Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7), + decoration: BoxDecoration( + color: color.withValues(alpha: 0.09), + borderRadius: BorderRadius.circular(99), + ), + child: Text( + '• $label', + textAlign: TextAlign.center, + style: TextStyle( + color: color, + fontSize: 12, + fontWeight: FontWeight.w700, + ), + ), + ); + } +} + +class ActivityNotice extends StatelessWidget { + const ActivityNotice( + this.title, + this.message, { + super.key, + this.warning = false, + }); + final String title, message; + final bool warning; + @override + Widget build(BuildContext context) { + final scheme = Theme.of(context).colorScheme; + final color = warning ? scheme.error : scheme.primary; + return Container( + width: double.infinity, + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + color: color.withValues(alpha: 0.07), + borderRadius: BorderRadius.circular(18), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Icon( + warning ? Icons.info_outline : Icons.check, + size: 18, + color: color, + ), + const SizedBox(width: 10), + Expanded( + child: Text( + title, + style: TextStyle( + fontSize: 14, + fontWeight: FontWeight.w700, + color: color, + ), + ), + ), + ], + ), + const SizedBox(height: 8), + ActivityCaption(message, color: color), + ], + ), + ); + } +} + +/// Live signal, deliberately empty until real samples have arrived. +class ActivitySignal extends StatelessWidget { + const ActivitySignal(this.values, {super.key, this.color, this.height = 44}); + final List values; + final Color? color; + final double height; + @override + Widget build(BuildContext context) => SizedBox( + height: height, + width: double.infinity, + child: CustomPaint( + painter: _SignalPainter( + values, + color ?? Theme.of(context).colorScheme.primary, + ), + ), + ); +} + +class _SignalPainter extends CustomPainter { + _SignalPainter(this.values, this.color); + final List values; + final Color color; + @override + void paint(Canvas canvas, Size size) { + final paint = Paint() + ..color = color.withValues(alpha: 0.3) + ..strokeWidth = 1; + canvas.drawLine( + Offset(0, size.height / 2), + Offset(size.width, size.height / 2), + paint, + ); + if (values.length < 2) return; + final scale = + math.max(2, values.fold(0, (p, v) => math.max(p, v.abs()))); + final path = Path(); + for (var i = 0; i < values.length; i++) { + final x = i / (values.length - 1) * size.width; + final y = size.height / 2 - values[i] / scale * size.height * 0.4; + if (i == 0) { + path.moveTo(x, y); + } else { + path.lineTo(x, y); + } + } + canvas.drawPath( + path, + paint + ..color = color + ..strokeWidth = 2 + ..style = PaintingStyle.stroke + ..strokeCap = StrokeCap.round, + ); + } + + @override + bool shouldRepaint(_SignalPainter oldDelegate) => + oldDelegate.values != values || oldDelegate.color != color; +} + +/// Scrollable phone layout with actions anchored above the system safe area. +class ActivityLayout extends StatelessWidget { + const ActivityLayout({ + super.key, + required this.title, + required this.children, + this.actions = const [], + this.appBarActions, + }); + final String title; + final List children, actions; + final List? appBarActions; + @override + Widget build(BuildContext context) => Scaffold( + appBar: AppBar( + title: Text( + title, + style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w700), + ), + actions: appBarActions, + ), + body: SafeArea( + top: false, + child: Column( + children: [ + Expanded( + child: ListView( + key: PageStorageKey('activity-$title'), + keyboardDismissBehavior: + ScrollViewKeyboardDismissBehavior.onDrag, + padding: const EdgeInsets.all(10), + children: [ + for (var i = 0; i < children.length; i++) ...[ + if (i > 0) const SizedBox(height: 8), + children[i], + ], + ], + ), + ), + if (actions.isNotEmpty) + Padding( + padding: const EdgeInsets.fromLTRB(10, 8, 10, 10), + child: Row( + children: [ + for (var i = 0; i < actions.length; i++) ...[ + if (i > 0) const SizedBox(width: 10), + Expanded( + flex: i == actions.length - 1 ? 3 : 2, + child: actions[i], + ), + ], + ], + ), + ), + ], + ), + ), + ); +} + +String activityDuration(int seconds) => + '${(seconds ~/ 60).toString().padLeft(2, '0')}:${(seconds % 60).toString().padLeft(2, '0')}'; +String activityTime(DateTime time) => + '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}'; +String activityDate(DateTime time) => + '${time.day.toString().padLeft(2, '0')}/${time.month.toString().padLeft(2, '0')}/${time.year}'; diff --git a/open_wearable/lib/apps/widgets/apps_page.dart b/open_wearable/lib/apps/widgets/apps_page.dart index 0ec8353a1..cc839233c 100644 --- a/open_wearable/lib/apps/widgets/apps_page.dart +++ b/open_wearable/lib/apps/widgets/apps_page.dart @@ -14,6 +14,7 @@ import 'package:open_wearable/widgets/connector_activity_indicator.dart'; import 'package:open_wearable/widgets/recording_activity_indicator.dart'; import 'package:open_wearable/widgets/sensors/sensor_page_spacing.dart'; import 'package:provider/provider.dart'; +import 'package:open_wearable/apps/activity_tracker/widgets/activity_tracker_page.dart'; /// Metadata and launch configuration for one built-in wearable app. class AppInfo { @@ -27,6 +28,7 @@ class AppInfo { final double? svgIconInset; final double? svgIconScale; final Color? iconBackgroundColor; + final bool availableWithoutDevice; AppInfo({ required this.logoPath, @@ -39,6 +41,7 @@ class AppInfo { this.svgIconInset, this.svgIconScale, this.iconBackgroundColor, + this.availableWithoutDevice = false, }); } @@ -106,6 +109,16 @@ Sensor? _findOpticalTemperatureSensor(List sensors) { } final List _apps = [ + AppInfo( + logoPath: 'lib/apps/activity_tracker/assets/logo.svg', + title: 'Activity Tracker', + description: 'Steps, exercises and jump height', + availableWithoutDevice: true, + supportedDevices: const ['OpenEarable'], + requiredCapabilities: _postureRequiredCapabilities, + accentColor: _appAccentColor, + widget: const ActivityTrackerPage(), + ), AppInfo( logoPath: "lib/apps/posture_tracker/assets/logo.png", title: "Posture Tracker", @@ -254,12 +267,13 @@ class AppsPage extends StatelessWidget { final enabledApps = <_AppListEntry>[]; final disabledApps = <_AppListEntry>[]; for (final app in _apps) { - final isEnabled = connectedWearableNames.any( - (wearableName) => wearableIsCompatibleWithApp( - wearableName: wearableName, - supportedDevicePrefixes: app.supportedDevices, - ), - ); + final isEnabled = app.availableWithoutDevice || + connectedWearableNames.any( + (wearableName) => wearableIsCompatibleWithApp( + wearableName: wearableName, + supportedDevicePrefixes: app.supportedDevices, + ), + ); final entry = _AppListEntry(app: app, isEnabled: isEnabled); if (isEnabled) { enabledApps.add(entry); diff --git a/open_wearable/lib/main.dart b/open_wearable/lib/main.dart index 195fcc45b..21008416d 100644 --- a/open_wearable/lib/main.dart +++ b/open_wearable/lib/main.dart @@ -35,6 +35,8 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:open_wearable/widgets/onboarding/permissions_onboarding_page.dart'; import 'models/bluetooth_auto_connector.dart'; +import 'apps/activity_tracker/services/activity_sensor_source.dart'; +import 'apps/activity_tracker/view_models/activity_tracker_controller.dart'; import 'models/logger.dart'; import 'view_models/app_banner_controller.dart'; import 'view_models/label_provider.dart'; @@ -63,6 +65,12 @@ void main() async { MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => WearablesProvider()), + ChangeNotifierProvider( + create: (context) => ActivityTrackerController( + source: + OpenEarableActivitySource(context.read()), + ), + ), ChangeNotifierProvider( create: (context) => FirmwareUpdateRequestProvider(), ), @@ -136,6 +144,7 @@ class _MyAppState extends State with WidgetsBindingObserver { late final StreamSubscription _wearableProvEventSub; late final WearablesProvider _wearablesProvider; late final SensorRecorderProvider _sensorRecorderProvider; + late final ActivityTrackerController _activityTracker; final AppUpgradeCoordinator _appUpgradeCoordinator = AppUpgradeCoordinator(); bool _closingSensorShutdownInProgress = false; bool _shouldCloseOpenScreensOnResume = false; @@ -165,6 +174,7 @@ class _MyAppState extends State with WidgetsBindingObserver { // Read provider without listening, allowed in initState with Provider _wearablesProvider = context.read(); _sensorRecorderProvider = context.read(); + _activityTracker = context.read(); _unsupportedFirmwareSub = _wearablesProvider.unsupportedFirmwareStream.listen((evt) { @@ -648,7 +658,8 @@ class _MyAppState extends State with WidgetsBindingObserver { } if (!AppShutdownSettings.shutOffAllSensorsOnAppClose || - _sensorRecorderProvider.isRecording) { + _sensorRecorderProvider.isRecording || + _activityTracker.isTracking) { _setBackgroundExecutionForShutdown(false); return; } @@ -721,6 +732,7 @@ class _MyAppState extends State with WidgetsBindingObserver { _pendingCloseShutdownTimer = null; if (_sensorRecorderProvider.isRecording || + _activityTracker.isTracking || !AppShutdownSettings.shutOffAllSensorsOnAppClose) { _setBackgroundExecutionForShutdown(false); return; @@ -750,6 +762,7 @@ class _MyAppState extends State with WidgetsBindingObserver { !_shouldCloseOpenScreensOnResume && !_closingSensorShutdownInProgress && !_sensorRecorderProvider.isRecording && + !_activityTracker.isTracking && backgroundEnteredAt != null && DateTime.now().difference(backgroundEnteredAt) >= _closeShutdownGracePeriod; diff --git a/open_wearable/pubspec.yaml b/open_wearable/pubspec.yaml index 922a95baa..ec61e7b13 100644 --- a/open_wearable/pubspec.yaml +++ b/open_wearable/pubspec.yaml @@ -106,6 +106,7 @@ flutter: # To add assets to your application, add an assets section, like this: assets: + - lib/apps/activity_tracker/assets/ - lib/apps/posture_tracker/assets/ - lib/apps/heart_tracker/assets/ - lib/apps/seal_check/assets/ diff --git a/open_wearable/test/apps/activity_tracker/activity_controller_test.dart b/open_wearable/test/apps/activity_tracker/activity_controller_test.dart new file mode 100644 index 000000000..7b913e9c9 --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_controller_test.dart @@ -0,0 +1,118 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'package:open_wearable/apps/activity_tracker/view_models/activity_tracker_controller.dart'; +import 'activity_test_support.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + test('session survives mode changes, disconnect, pause and restart recovery', + () async { + final source = ReplayActivitySource(); + final storage = MemoryActivityStorage(); + final c = ActivityTrackerController( + source: source, + storage: storage, + observeLifecycle: false, + ); + await c.prepare('ear'); + source.still(); + expect(c.engine.calibrated, isTrue); + await c.start(); + source.walk(10); + expect(c.current!.steps, 10); + expect(c.todaySteps, 10); + c.startExercise(ActivityKind.squats); + source.walk(6); + expect(c.current!.steps, 10); + c.finishSet(); + c.pause(); + source.walk(10); + expect(c.current!.steps, 10); + c.resume(); + source.still(); + source.walk(5); + expect(c.current!.steps, 15); + source.status.add('Connection lost'); + expect(c.connected, isFalse); + expect(c.current!.steps, 15); + source.still(); + source.walk(5); + expect(c.current!.steps, 20); + await c.checkpoint(); + expect(storage.rawSamples, greaterThan(1000)); + c.dispose(); + await Future.delayed(Duration.zero); + final restored = ActivityTrackerController( + source: ReplayActivitySource(), + storage: storage, + observeLifecycle: false, + ); + await restored.ready; + expect(restored.isTracking, isFalse); + expect(restored.sessions.single.steps, 20); + expect(restored.sessions.single.interrupted, isTrue); + expect(restored.sessions.single.endedAt, isNotNull); + expect(restored.todaySteps, 20); + restored.dispose(); + }); + test('foreground resumes with calibration; background never adds counts', + () async { + final source = ReplayActivitySource(); + final c = ActivityTrackerController( + source: source, + storage: MemoryActivityStorage(), + observeLifecycle: false, + ); + await c.prepare('ear'); + source.still(); + await c.start(); + c.didChangeAppLifecycleState(AppLifecycleState.paused); + source.walk(10); + expect(c.current!.steps, 0); + c.didChangeAppLifecycleState(AppLifecycleState.resumed); + source.still(); + source.walk(5); + expect(c.current!.steps, 5); + final session = await c.finish(); + expect(session!.endedAt, isNotNull); + expect(c.isTracking, isFalse); + expect(source.closed, isTrue); + c.dispose(); + }); + test('reference errors and unavailable height survive serialization', () { + final session = ActivitySession( + id: 'test', + startedAt: DateTime(2026), + deviceName: 'ear', + ); + session.trials.add( + const ValidationTrial( + participant: 'P01', + pattern: 'Slow', + referenceSource: 'Video', + kind: ActivityKind.squats, + detected: 19, + reference: 20, + ), + ); + session.trials.add( + const ValidationTrial( + participant: 'P02', + pattern: 'Head turn', + referenceSource: 'Video', + kind: ActivityKind.steps, + detected: 1, + reference: 0, + ), + ); + session.jumps.add( + const JumpAttempt(at: 2, flightSeconds: 0.3, reason: 'Impact clipped'), + ); + final copy = ActivitySession.fromJson(session.toJson()); + expect(copy.trials.first.countError, -1); + expect(copy.trials.first.absolutePercentageError, 5); + expect(copy.trials.last.absolutePercentageError, isNull); + expect(copy.jumps.single.heightCm, isNull); + }); +} diff --git a/open_wearable/test/apps/activity_tracker/activity_engine_test.dart b/open_wearable/test/apps/activity_tracker/activity_engine_test.dart new file mode 100644 index 000000000..346453c8b --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_engine_test.dart @@ -0,0 +1,192 @@ +import 'dart:math' as math; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_engine.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; + +class _Replay { + final engine = ActivityEngine(); + double time = 0; + int steps = 0, reps = 0; + final jumps = []; + void sample( + double z, { + double dt = 0.01, + MotionVector? acceleration, + MotionVector gyro = MotionVector.zero, + }) { + time += dt; + final e = engine.add( + MotionSample( + time, + acceleration ?? MotionVector(0, 0, z), + gyroscope: gyro, + ), + ); + steps += e.steps; + reps += e.repetitions; + if (e.jump != null) jumps.add(e.jump!); + } + + void still([double seconds = 2]) { + for (var i = 0; i < seconds * 100; i++) { + sample(ActivityEngine.gravity); + } + } + + void walk(int count, double hz) { + for (var i = 0; i < count / hz * 100; i++) { + sample( + ActivityEngine.gravity + 2.8 * math.sin(2 * math.pi * hz * i / 100), + ); + } + still(0.4); + } + + void repeat(ActivityKind kind, int count) { + engine.select(kind); + still(1); + final depth = kind == ActivityKind.squats ? 0.35 : 0.16; + final period = kind == ActivityKind.squats ? 2.4 : 1.8; + final omega = 2 * math.pi / period; + for (var i = 0; i < count * period * 100; i++) { + sample( + ActivityEngine.gravity - + depth / 2 * omega * omega * math.cos(omega * i / 100), + ); + } + still(0.8); + } + + void jump({double flight = 0.4, double landing = 17, double dt = 0.01}) { + still(1); + for (var i = 0; i < 10; i++) { + sample(15); + } + for (var i = 0; i < flight / dt; i++) { + sample(0.2, dt: dt); + } + for (var i = 0; i < 6; i++) { + sample(landing, dt: dt); + } + still(1); + } +} + +void main() { + test('requires a still window and rejects invalid/duplicate timestamps', () { + final r = _Replay()..still(1); + expect(r.engine.calibrated, isFalse); + r.still(); + expect(r.engine.calibrated, isTrue); + expect( + r.engine.add(MotionSample(r.time, const MotionVector(0, 0, 100))).steps, + 0, + ); + r.sample(double.nan); + expect(r.engine.calibrated, isFalse); + }); + for (final frequency in [0.8, 1.5, 2.2, 3.0]) { + test('counts periodic steps at $frequency Hz including the initial bout', + () { + final r = _Replay() + ..still() + ..walk(20, frequency); + expect(r.steps, 20); + expect(r.jumps, isEmpty); + }); + } + test('isolated peaks and slow unrelated head movements are not steps', () { + final r = _Replay()..still(); + for (var i = 0; i < 1000; i++) { + r.sample( + ActivityEngine.gravity + 2 * math.sin(2 * math.pi * 0.25 * i / 100), + ); + } + expect(r.steps, 0); + }); + test('gyro propagation rejects pure changes in head orientation', () { + final r = _Replay()..still(); + for (var i = 0; i < 1000; i++) { + final t = i / 100; + final angle = 0.35 * math.sin(2 * math.pi * t); + final omega = 0.35 * 2 * math.pi * math.cos(2 * math.pi * t); + r.sample( + 0, + acceleration: MotionVector( + ActivityEngine.gravity * math.sin(angle), + 0, + ActivityEngine.gravity * math.cos(angle), + ), + gyro: MotionVector(0, -omega, 0), + ); + } + expect(r.steps, 0); + }); + for (final kind in [ActivityKind.squats, ActivityKind.pushUps]) { + test('${kind.label} counts full cycles without counting steps', () { + final r = _Replay() + ..still() + ..repeat(kind, 6); + expect(r.reps, 6); + expect(r.steps, 0); + }); + } + test('incomplete lowering does not count a repetition', () { + final r = _Replay()..still(); + r.engine.select(ActivityKind.squats); + r.still(1); + for (var i = 0; i < 40; i++) { + r.sample(8.8); + } + expect(r.reps, 0); + r.engine.recalibrate(); + r.still(2); + expect(r.reps, 0); + }); + test('estimates jump height from flight time and excludes steps', () { + final r = _Replay()..still(); + r.engine.select(ActivityKind.jumps); + r.jump(); + expect(r.jumps, hasLength(1)); + expect(r.jumps.single.heightCm, closeTo(19.6, 1.1)); + expect(r.steps, 0); + }); + test('clipping and slow samples make height unavailable', () { + final clipped = _Replay()..still(); + clipped.engine.select(ActivityKind.jumps); + clipped.jump(landing: 19.6); + expect(clipped.jumps.single.heightCm, isNull); + final slow = _Replay()..still(); + slow.engine.select(ActivityKind.jumps); + slow.jump(dt: 0.04); + expect(slow.jumps.single.heightCm, isNull); + }); + test('data gaps discard partial flight and do not invent counts', () { + final r = _Replay() + ..still() + ..walk(10, 2); + final previous = r.steps; + r.sample(9.8, dt: 3); + expect(r.engine.calibrated, isFalse); + expect(r.steps, previous); + r.still(); + r.walk(5, 2); + expect(r.steps, previous + 5); + }); + test('clock restarts recover and initial calibration can be replayed', () { + final r = _Replay()..still(); + final saved = r.engine.calibration; + r.time = 0; + r.sample(ActivityEngine.gravity); + expect(r.engine.calibrated, isFalse); + r.still(); + expect(r.engine.calibrated, isTrue); + r.engine.restoreCalibration(saved); + r.walk(5, 2); + expect(r.steps, 5); + expect( + () => r.engine.restoreCalibration({'x': double.nan}), + throwsArgumentError, + ); + }); +} diff --git a/open_wearable/test/apps/activity_tracker/activity_sensor_source_test.dart b/open_wearable/test/apps/activity_tracker/activity_sensor_source_test.dart new file mode 100644 index 000000000..ea60f6c26 --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_sensor_source_test.dart @@ -0,0 +1,180 @@ +import 'dart:async'; +import 'dart:math' as math; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_earable_flutter/open_earable_flutter.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'package:open_wearable/apps/activity_tracker/services/activity_sensor_source.dart'; +import 'package:open_wearable/view_models/sensor_configuration_provider.dart'; +import 'package:open_wearable/view_models/wearables_provider.dart'; + +class _Config + extends SensorFrequencyConfiguration { + _Config() + : super( + name: 'IMU', + values: [ + for (final hz in [0, 50, 100, 200]) + SensorFrequencyConfigurationValue(frequencyHz: hz.toDouble()), + ], + ); + final requests = []; + @override + void setConfiguration(SensorFrequencyConfigurationValue value) { + requests.add(value.frequencyHz); + } +} + +class _Sensor extends Sensor { + _Sensor(_Config config, {required bool gyro}) + : isGyro = gyro, + super( + sensorName: gyro ? 'Gyroscope' : 'Accelerometer', + chartTitle: 'IMU', + shortChartTitle: 'IMU', + timestampExponent: gyro ? -3 : -6, + relatedConfigurations: [config], + ); + final bool isGyro; + final data = StreamController.broadcast(sync: true); + @override + List get axisNames => ['x', 'y', 'z']; + @override + List get axisUnits => List.filled(3, isGyro ? 'dps' : 'm/s²'); + @override + Stream get sensorStream => data.stream; + void emit(double seconds) => data.add( + SensorDoubleValue( + values: isGyro ? [0, 180, 0] : [0, 0, 9.80665], + timestamp: (seconds * (isGyro ? 1000 : 1000000)).round(), + ), + ); +} + +class _Ear extends Wearable + implements SensorManager, SensorConfigurationManager { + _Ear({this.includeGyro = true}) + : super( + name: 'OpenEarable 2.0', + disconnectNotifier: WearableDisconnectNotifier(), + ); + final config = _Config(); + final bool includeGyro; + late final acc = _Sensor(config, gyro: false); + late final gyro = _Sensor(config, gyro: true); + @override + String get deviceId => 'ear'; + @override + List get sensors => [acc, if (includeGyro) gyro]; + @override + List get sensorConfigurations => [config]; + @override + Stream> + get sensorConfigurationStream => const Stream.empty(); + @override + Future disconnect() async {} +} + +class _Devices extends WearablesProvider { + @override + final wearables = []; + final configs = {}; + void connect(_Ear ear) { + wearables.add(ear); + configs[ear] = SensorConfigurationProvider(sensorConfigurationManager: ear) + ..addSensorConfiguration( + ear.config, + ear.config.values.first, + markPending: false, + ); + notifyListeners(); + } + + void drop(_Ear ear) { + wearables.remove(ear); + notifyListeners(); + } + + @override + SensorConfigurationProvider getSensorConfigurationProvider( + Wearable wearable, + ) => + configs[wearable]!; + @override + void dispose() { + for (final c in configs.values) { + c.dispose(); + } + super.dispose(); + } +} + +void main() { + test('missing IMU capability fails once without a retry loop', () async { + final devices = _Devices(); + final ear = _Ear(includeGyro: false); + devices.connect(ear); + final source = OpenEarableActivitySource(devices); + await expectLater(source.open('ear'), throwsStateError); + await Future.delayed(Duration.zero); + expect(ear.config.requests, isEmpty); + source.dispose(); + await Future.delayed(Duration.zero); + devices.dispose(); + }); + test( + 'shared IMU configuration, units, batched timestamps, reconnect and restore', + () async { + final devices = _Devices(); + final ear = _Ear(); + devices.connect(ear); + final source = OpenEarableActivitySource(devices); + final samples = []; + final sub = source.samples.listen(samples.add); + await source.open('ear'); + expect(ear.config.requests, [100]); + for (var i = 0; i < 10; i++) { + ear.acc.emit(1 + i / 100); + } + for (var i = 0; i < 10; i++) { + ear.gyro.emit(1 + i / 100); + } + await Future.delayed(Duration.zero); + expect(samples, hasLength(10)); + expect(samples.first.seconds, 1); + expect(samples.first.gyroscope!.y, closeTo(math.pi, 0.00001)); + devices.drop(ear); + await Future.delayed(Duration.zero); + final reconnected = _Ear(); + devices.connect(reconnected); + await Future.delayed(Duration.zero); + await Future.delayed(Duration.zero); + reconnected.gyro.emit(2); + reconnected.acc.emit(2); + await Future.delayed(Duration.zero); + expect(samples.last.seconds, 2); + expect(reconnected.config.requests, [100]); + await source.close(); + expect(reconnected.config.requests, [100, 0]); + await sub.cancel(); + source.dispose(); + await Future.delayed(Duration.zero); + devices.dispose(); + }); + test('does not overwrite IMU settings changed by another consumer', () async { + final devices = _Devices(); + final ear = _Ear(); + devices.connect(ear); + final source = OpenEarableActivitySource(devices); + await source.open('ear'); + devices.configs[ear]!.addSensorConfiguration( + ear.config, + ear.config.values.last, + markPending: false, + ); + await source.close(); + expect(ear.config.requests, [100]); + source.dispose(); + await Future.delayed(Duration.zero); + devices.dispose(); + }); +} diff --git a/open_wearable/test/apps/activity_tracker/activity_storage_test.dart b/open_wearable/test/apps/activity_tracker/activity_storage_test.dart new file mode 100644 index 000000000..4330ee59b --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_storage_test.dart @@ -0,0 +1,60 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'package:open_wearable/apps/activity_tracker/services/activity_storage.dart'; + +class _DiskStorage extends ActivityStorage { + _DiskStorage(this.root); + final Directory root; + @override + Future get directory async => root; +} + +void main() { + test('checkpoints in order and exports raw data and quoted reference trials', + () async { + final root = await Directory.systemTemp.createTemp('activity-storage-test'); + addTearDown(() => root.delete(recursive: true)); + final store = _DiskStorage(root); + await Future.wait([ + store.save({'steps': 1}), + store.save({'steps': 2}), + ]); + expect((await store.load())!['steps'], 2); + final session = ActivitySession( + id: 'trial', + startedAt: DateTime(2026), + deviceName: 'ear', + ); + session.trials.add( + const ValidationTrial( + participant: 'P01', + pattern: 'Slow, careful', + referenceSource: 'Video "A"', + kind: ActivityKind.steps, + detected: 99, + reference: 100, + ), + ); + await store.startTrace(session.id); + store.append( + const MotionSample(1, MotionVector(0, 0, 9.8)), + ActivityKind.steps, + false, + true, + 0, + 0, + null, + ); + final files = await store.export(session); + expect(files, hasLength(3)); + final metadata = jsonDecode(await File(files.first).readAsString()); + expect(metadata['session']['trials'].single['detected'], 99); + final raw = await File(files[1]).readAsLines(); + expect(raw, hasLength(2)); + expect(raw.first, contains('sensor_seconds')); + expect(await File(files.last).readAsString(), contains('"Video ""A"""')); + await store.closeTrace(); + }); +} diff --git a/open_wearable/test/apps/activity_tracker/activity_test_support.dart b/open_wearable/test/apps/activity_tracker/activity_test_support.dart new file mode 100644 index 000000000..0b1510f75 --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_test_support.dart @@ -0,0 +1,102 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:math' as math; +import 'package:open_earable_flutter/open_earable_flutter.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'package:open_wearable/apps/activity_tracker/services/activity_sensor_source.dart'; +import 'package:open_wearable/apps/activity_tracker/services/activity_storage.dart'; + +class MemoryActivityStorage extends ActivityStorage { + Map? snapshot; + int rawSamples = 0; + @override + Future?> load() async => snapshot; + @override + Future save(Map value) async { + snapshot = jsonDecode(jsonEncode(value)); + } + + @override + Future startTrace(String id) async {} + @override + void append( + MotionSample s, + ActivityKind kind, + bool paused, + bool calibrated, + int steps, + int reps, + double? height, + ) { + rawSamples++; + } + + @override + Future flushTrace() async {} + @override + Future closeTrace() async {} +} + +class ReplayActivitySource implements ActivitySensorSource { + final data = StreamController.broadcast(sync: true); + final status = StreamController.broadcast(sync: true); + double time = 0; + bool closed = false; + @override + String get deviceName => 'OpenEarable 2.0 (L)'; + @override + Stream get samples => data.stream; + @override + Stream get interruptions => status.stream; + @override + Future open(String deviceId) async { + closed = false; + } + + @override + Future close() async { + closed = true; + } + + void emit(double z) { + time += 0.01; + data.add( + MotionSample( + time, + MotionVector(0, 0, z), + gyroscope: MotionVector.zero, + ), + ); + } + + void still([int samples = 200]) { + for (var i = 0; i < samples; i++) { + emit(9.80665); + } + } + + void walk(int steps) { + for (var i = 0; i < steps * 50; i++) { + emit(9.80665 + 2.8 * math.sin(2 * math.pi * 2 * i / 100)); + } + still(40); + } + + @override + void dispose() { + data.close(); + status.close(); + } +} + +class TestActivityWearable extends Wearable { + TestActivityWearable() + : super( + name: 'OpenEarable 2.0 (L)', + disconnectNotifier: WearableDisconnectNotifier(), + ); + @override + String get deviceId => 'activity-test-ear'; + @override + Future disconnect() async {} +} diff --git a/open_wearable/test/apps/activity_tracker/activity_ui_test.dart b/open_wearable/test/apps/activity_tracker/activity_ui_test.dart new file mode 100644 index 000000000..5fab79b4a --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/activity_ui_test.dart @@ -0,0 +1,215 @@ +import 'dart:io'; +import 'dart:ui' as ui; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:provider/provider.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'package:open_wearable/apps/activity_tracker/view_models/activity_tracker_controller.dart'; +import 'package:open_wearable/apps/activity_tracker/widgets/activity_session_pages.dart'; +import 'package:open_wearable/apps/activity_tracker/widgets/activity_tracker_page.dart'; +import 'package:open_wearable/theme/app_theme.dart'; +import 'activity_test_support.dart'; + +void main() { + testWidgets('saved sessions remain accessible without a connected device', + (tester) async { + final c = ActivityTrackerController( + source: ReplayActivitySource(), + storage: MemoryActivityStorage(), + observeLifecycle: false, + ); + await c.ready; + c.sessions.add( + ActivitySession( + id: 'offline', + startedAt: DateTime(2026), + endedAt: DateTime(2026, 1, 1, 0, 1), + deviceName: 'ear', + steps: 42, + ), + ); + await tester.pumpWidget( + ChangeNotifierProvider.value( + value: c, + child: MaterialApp( + theme: AppTheme.lightTheme(), + home: const ActivityTrackerPage(), + ), + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.text('View sessions')); + await tester.pumpAndSettle(); + expect(find.textContaining('42 steps'), findsOneWidget); + expect(tester.takeException(), isNull); + await tester.pumpWidget(const SizedBox.shrink()); + c.dispose(); + await tester.pump(); + }); + Future preview(WidgetTester tester, String name, GlobalKey key) async { + final output = Platform.environment['ACTIVITY_PREVIEW_DIRECTORY']; + if (output == null) return; + await tester.runAsync(() async { + final image = await (key.currentContext!.findRenderObject() + as RenderRepaintBoundary) + .toImage(pixelRatio: 2); + final bytes = await image.toByteData(format: ui.ImageByteFormat.png); + await File('$output/$name.png').writeAsBytes(bytes!.buffer.asUint8List()); + image.dispose(); + }); + } + + for (final width in [320.0, 390.0]) { + testWidgets('readiness, workout and session navigation at $width px', + (tester) async { + tester.view.physicalSize = Size(width, width == 320 ? 568 : 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final font = Platform.environment['ACTIVITY_PREVIEW_FONT']; + if (font != null) { + await tester.runAsync(() async { + final loader = FontLoader('Roboto') + ..addFont(File(font).readAsBytes().then(ByteData.sublistView)); + await loader.load(); + final icons = Platform.environment['ACTIVITY_PREVIEW_MATERIAL_ICONS']; + if (icons != null) { + await (FontLoader('MaterialIcons') + ..addFont( + File(icons).readAsBytes().then(ByteData.sublistView), + )) + .load(); + } + }); + } + final hostTheme = AppTheme.lightTheme(); + final theme = font == null + ? hostTheme + : hostTheme.copyWith( + textTheme: hostTheme.textTheme.apply(fontFamily: 'Roboto'), + elevatedButtonTheme: ElevatedButtonThemeData( + style: hostTheme.elevatedButtonTheme.style!.copyWith( + textStyle: WidgetStatePropertyAll( + hostTheme.elevatedButtonTheme.style!.textStyle! + .resolve({})!.copyWith(fontFamily: 'Roboto'), + ), + ), + ), + outlinedButtonTheme: OutlinedButtonThemeData( + style: hostTheme.outlinedButtonTheme.style!.copyWith( + textStyle: WidgetStatePropertyAll( + hostTheme.outlinedButtonTheme.style!.textStyle! + .resolve({})!.copyWith(fontFamily: 'Roboto'), + ), + ), + ), + ); + final source = ReplayActivitySource(); + final c = ActivityTrackerController( + source: source, + storage: MemoryActivityStorage(), + observeLifecycle: false, + ); + final key = GlobalKey(); + await tester.pumpWidget( + ChangeNotifierProvider.value( + value: c, + child: RepaintBoundary( + key: key, + child: MaterialApp( + debugShowCheckedModeBanner: false, + theme: theme, + home: ActivityTrackerPage(wearable: TestActivityWearable()), + ), + ), + ), + ); + await tester.pump(); + source.still(); + await tester.tap(find.byType(Checkbox).first); + await tester.pumpAndSettle(); + expect(find.text('Ready to move?'), findsOneWidget); + await preview(tester, 'ready-$width', key); + await tester.tap(find.text('Start tracking')); + await tester.pumpAndSettle(); + expect(find.text('TODAY’S STEPS'), findsOneWidget); + expect(find.text('Study mode'), findsOneWidget); + await preview(tester, 'overview-$width', key); + await tester.tap(find.text('Squats').first); + await tester.pumpAndSettle(); + expect(find.textContaining('Counting repetitions'), findsOneWidget); + await preview(tester, 'squats-$width', key); + await tester.tap(find.text('Pause')); + await tester.pumpAndSettle(); + expect(find.text('Take your time'), findsOneWidget); + await tester.tap(find.text('Resume')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Finish set')); + await tester.pumpAndSettle(); + await tester.tap(find.text('View sessions')); + await tester.pumpAndSettle(); + expect(find.text('Sessions'), findsOneWidget); + expect(tester.takeException(), isNull); + await tester.pumpWidget(const SizedBox.shrink()); + c.dispose(); + await tester.pump(); + }); + } + testWidgets( + 'English study inputs save an actual comparison and render in dark mode', + (tester) async { + tester.view.physicalSize = const Size(390, 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final c = ActivityTrackerController( + source: ReplayActivitySource(), + storage: MemoryActivityStorage(), + observeLifecycle: false, + ); + await c.ready; + final session = ActivitySession( + id: 'test', + startedAt: DateTime(2026), + deviceName: 'ear', + steps: 99, + ); + c.sessions.add(session); + await tester.pumpWidget( + ChangeNotifierProvider.value( + value: c, + child: MaterialApp( + theme: AppTheme.darkTheme(), + home: ActivityStudyPage(session: session), + ), + ), + ); + await tester.enterText( + find.widgetWithText(TextField, 'Participant ID'), + 'P01', + ); + await tester.enterText( + find.widgetWithText(TextField, 'Movement pattern / trial'), + 'Slow walking', + ); + await tester.enterText( + find.widgetWithText(TextField, 'Reference count'), + '100', + ); + await tester.scrollUntilVisible( + find.text('Save reference trial'), + 200, + scrollable: find.byType(Scrollable).first, + ); + await tester.tap(find.text('Save reference trial')); + await tester.pumpAndSettle(); + expect(session.trials.single.countError, -1); + expect(session.trials.single.participant, 'P01'); + expect(tester.takeException(), isNull); + await tester.pumpWidget(const SizedBox.shrink()); + c.dispose(); + await tester.pump(); + }); +} diff --git a/open_wearable/tool/replay_activity_trace.dart b/open_wearable/tool/replay_activity_trace.dart new file mode 100644 index 000000000..79075563a --- /dev/null +++ b/open_wearable/tool/replay_activity_trace.dart @@ -0,0 +1,113 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:open_wearable/apps/activity_tracker/models/activity_engine.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; + +/// Replays exported numeric IMU CSV files through the exact live algorithms. +/// Run: dart run tool/replay_activity_trace.dart path/to/session.csv [...] +Future main(List arguments) async { + if (arguments.isEmpty) { + stderr.writeln( + 'Usage: dart run tool/replay_activity_trace.dart [...]', + ); + exitCode = 64; + return; + } + final results = >[]; + for (final path in arguments) { + final engine = ActivityEngine(); + Map? initialCalibration; + final metadata = File(path.replaceFirst(RegExp(r'\.csv$'), '.json')); + if (await metadata.exists()) { + final json = + jsonDecode(await metadata.readAsString()) as Map; + final calibration = (json['session'] + as Map?)?['calibration'] as Map?; + if (calibration != null && calibration.isNotEmpty) { + initialCalibration = + calibration.map((k, v) => MapEntry(k, (v as num).toDouble())); + } + } + final counts = {}; + final jumps = >[]; + var steps = 0, samples = 0; + var wasPaused = false; + var sessionStarted = false; + try { + final rows = File(path) + .openRead() + .transform(utf8.decoder) + .transform(const LineSplitter()); + List? header; + await for (final row in rows) { + if (header == null) { + header = row.split(','); + if (!header.contains('sensor_seconds')) { + throw const FormatException('Not an Activity Tracker IMU trace'); + } + continue; + } + final cells = row.split(','); + final values = Map.fromIterables(header, cells); + double number(String key) => double.parse(values[key]!); + final paused = values['paused'] == 'true'; + if (paused) { + if (!wasPaused) engine.recalibrate(); + wasPaused = true; + continue; + } + wasPaused = false; + final kind = ActivityKind.values.byName(values['mode']!); + if (!sessionStarted && values['calibrated'] == 'true') { + if (initialCalibration != null) { + engine.restoreCalibration(initialCalibration); + } + engine.select(kind); + sessionStarted = true; + } + if (engine.kind != kind) engine.select(kind); + final update = engine.add( + MotionSample( + number('sensor_seconds'), + MotionVector( + number('ax_m_s2'), + number('ay_m_s2'), + number('az_m_s2'), + ), + gyroscope: values['gx_rad_s'] == '' + ? null + : MotionVector( + number('gx_rad_s'), + number('gy_rad_s'), + number('gz_rad_s'), + ), + ), + ); + samples++; + // Pre-roll calibrates the replay but is outside the recorded session. + if (values['calibrated'] != 'true') continue; + steps += update.steps; + counts.update( + kind.name, + (n) => n + update.repetitions, + ifAbsent: () => update.repetitions, + ); + if (update.jump != null) jumps.add(update.jump!.toJson()); + } + results.add({ + 'file': path, + 'algorithmVersion': 'heuristic-1', + 'initialCalibrationRestored': initialCalibration != null, + 'calibratedAtEnd': engine.calibrated, + 'samples': samples, + 'steps': steps, + 'repetitionsByMode': counts, + 'jumps': jumps, + }); + } catch (e) { + stderr.writeln('$path: $e'); + exitCode = 1; + } + } + stdout.writeln(const JsonEncoder.withIndent(' ').convert(results)); +} From 2ee615f519c9cffc3db659a1d50091bced3690db Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 10 Nov 2026 17:52:02 +0100 Subject: [PATCH 4/7] fix(activity-tracker): preserve push-up descent through holds --- docs/README.md | 3 +- docs/algorithmen/liegestuetze.md | 66 +++++ docs/journal.md | 23 ++ open_wearable/docs/activity_tracker.md | 20 +- .../models/activity_engine.dart | 20 +- .../models/push_up_detector.dart | 112 +++++++++ .../push_up_detector_test.dart | 234 ++++++++++++++++++ 7 files changed, 464 insertions(+), 14 deletions(-) create mode 100644 docs/algorithmen/liegestuetze.md create mode 100644 open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart create mode 100644 open_wearable/test/apps/activity_tracker/push_up_detector_test.dart diff --git a/docs/README.md b/docs/README.md index f4d3e8c9e..3e6940e78 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,7 +13,8 @@ Hier sammeln wir alles, was wir für Zwischenpräsentation, Abschlusspräsentati | `praesentationen/` | Gliederung und Kernaussagen je Termin | Zwischen- und Abschlusspräsentation | | `figures/` | Abbildungen samt Quelle oder Skript | Präsentationen und Paper | -Bisher gibt es nur das Journal. Die anderen Pfade legen wir an, sobald es dafür Inhalt gibt. +Erster Algorithmusbericht: [Liegestütz-Zählung](algorithmen/liegestuetze.md). +Weitere Pfade legen wir an, sobald es dafür Inhalt gibt. ## Journal-Eintrag diff --git a/docs/algorithmen/liegestuetze.md b/docs/algorithmen/liegestuetze.md new file mode 100644 index 000000000..8e87389ac --- /dev/null +++ b/docs/algorithmen/liegestuetze.md @@ -0,0 +1,66 @@ +# Liegestütz-Zählung: Algorithmusprüfung + +Stand: 10.11.2026. Manuell gewählter Push-up-Modus, ohne ML. + +## Befund und Entscheidung + +Der Prototyp verwendete denselben Detektor wie Squats, lediglich mit anderer +Mindestamplitude. Geschwindigkeit und Position wurden fortlaufend gedämpft. +Dadurch verschwand ein abgesenkter Positionswert während einer Pause, obwohl +keine vollständige Rückbewegung stattfand. + +Reproduzierbarer synthetischer Gegenfall: 16 cm Absenken in 0,9 s, 3 s halten, +2 cm zurück in 0,9 s, anschließend 5 s halten. Der alte Code zählte 1, der +korrigierte Code 0 Wiederholungen. Das sind simulierte Kopfbewegungen, keine +Messungen von Liegestütztiefe oder Versuchspersonen. + +Ein eigener Push-up-Detektor wurde gewählt, damit die Korrektur den Squat-Zähler +nicht verändert. Die alte Positionsdämpfung wurde verworfen: Sie erzeugt eine +scheinbare Rückkehr. Kontinuierliches Zurücksetzen der Geschwindigkeit wurde +verworfen, weil es bei nahtlosen Wiederholungen neue Integrationsfehler erzeugt. + +## Funktionsweise + +1. Die gemeinsame IMU-Vorverarbeitung schätzt die Schwerkraftrichtung und glättet + die projizierte vertikale Beschleunigung. +2. Der Push-up-Detektor verlangt 0,7 s mit ruhigem Beschleunigungs- und Gyrosignal. + Der Restoffset wird erst nach 0,45 s Filter-Einschwingzeit gemittelt. +3. Die offsetbereinigte Beschleunigung wird kurzfristig integriert. Der + Positionswert bleibt während einer Pause erhalten. +4. Auf Absenken folgt eine Rückbewegung von mindestens 80 % der geschätzten + Absenkung. Erst nahe geschätzter Geschwindigkeit null wird gezählt. Eine + Mindestbewegung und Mindestdauer verwerfen kleine beziehungsweise kurze Zyklen. +5. Nahe Stillstand darf die Geschwindigkeit korrigiert werden, nicht die aktive + Absenkung. Restgeschwindigkeit bleibt beim Übergang zur nächsten Wiederholung + erhalten. Große Kopfrotationen, Übersteuerung, Datenlücken und Moduswechsel + verwerfen unvollständige Zyklen; ein Zeitlimit von 8 s begrenzt Drift. + +Die Schwellwerte sind Heuristiken. Insbesondere der Positionsproxy mit 0,035 m +Mindestamplitude ist keine gemessene anatomische Tiefe. Bei nur geringer +Beschleunigung kann ein IMU-Detektor Stillstand nicht sicher von konstanter +Geschwindigkeit unterscheiden. Ein kurzer Halt in der oberen Position hilft +bei der Bestätigung der letzten Wiederholung. + +## Evaluation und Grenzen + +- 0 Versuchspersonen, keine Hardware-Aufnahmen; ausschließlich synthetische Signale. +- 38 Regressionstests in `open_wearable/test/apps/activity_tracker/push_up_detector_test.dart`. +- Unabhängige quintische Ruhe-zu-Ruhe-Trajektorien liefern die Referenz: jeweils + 6 Zyklen bei 8/16/25 cm simuliertem Hub und 1/1,8/3/5/7 s Zyklusdauer, 100 Hz. + In diesen 15 Signalen wurden vorher 82 von 90, jetzt 90 von 90 Zyklen gezählt. +- Weitere Fälle: 50/100/200 Hz, Halten oben/unten, Teilrückkehr, flache Bewegungen, + Zeitüberschreitung, Kopfrotation, Clipping, Datenlücke, Moduswechsel, andere + feste Sensororientierung, konstante Restoffsets und reproduzierbares Rauschen. +- Die drei Rauschtests verwenden gleichverteilte, unabhängige Störungen bis + ±0,02/0,05/0,1 m/s² bei 100 Hz und festem Zufallsseed; kein Modell realer Störsignale. + +Das ist ein Implementierungsvergleich, kein Nachweis nutzerübergreifender +Genauigkeit. Nächster Schritt: synchronisierte Earable-/Video-Aufnahmen mit +mehreren Personen, Tempi, Kopfhaltungen und Negativkontrollen; Parameter nur auf +Entwicklungspersonen abstimmen, anschließend getrennte Testpersonen auswerten. + +Ausführen aus `open_wearable/`: + +```sh +flutter test test/apps/activity_tracker/push_up_detector_test.dart +``` diff --git a/docs/journal.md b/docs/journal.md index 64a1707eb..ed8be1d96 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -2,6 +2,29 @@ Neueste Einträge oben. Format siehe [README](README.md#journal-eintrag). +## 2026-11-10: Push-up-Zähler nach Rebase geprüft und korrigiert + +**Was:** `feat/activity-tracker` auf `origin/main` rebased; neuere Android-Toolchain +bei einem Konflikt beibehalten. Weiterentwicklung auf `fix/pushup-tracker-review`. +Eigener Push-up-Detektor mit Startpositions-Offsetkorrektur, erhaltener Absenkung +bei Pausen und Abbruch bei starken Kopfrotationen. Squat-Algorithmus unverändert. + +**Warum:** Die bisherige Integrationsdämpfung konnte eine kleine Rückbewegung nach +einer Pause als vollständige Wiederholung zählen. Stattdessen wird der Weg über +den kurzen Zyklus erhalten; Restgeschwindigkeit wird zwischen Zyklen nicht +künstlich zurückgesetzt. Details und verworfene Alternativen unter +[Liegestütz-Algorithmus](algorithmen/liegestuetze.md). + +**Ergebnis:** 38 gezielte Regressionstests. In 15 idealen Signalen mit je 6 Zyklen, +100 Hz, 8/16/25 cm simuliertem Hub und 1–7 s Zyklusdauer: zuvor 82/90, jetzt 90/90 +gezählte Wiederholungen. Eine Teilrückkehr von 2 cm nach 16 cm Absenken und 3 s +Pause zählt jetzt 0 statt 1. Personenzahl: 0, keine realen Messdaten. +`flutter analyze` ohne Befund; vollständige Testsuite: 205 bestanden; +Formatierungsprüfung der geänderten Dart-Dateien ohne Änderungen. + +**Offen:** Hardware-/Nutzerevaluation, belastbare Parameter und Abgrenzung von +Kopfbewegungen gegenüber echten Körperzyklen. Kein Nachweis von Ausführungsqualität. + ## 2026-11-06: Android-Build auf Java 25 umgestellt **Was:** Gradle von 8.14 auf 9.3.1 aktualisiert, AGP von 8.12.1 auf 9.1.0, Kotlin von 2.2.21 auf 2.4.0. diff --git a/open_wearable/docs/activity_tracker.md b/open_wearable/docs/activity_tracker.md index 2b149f4ea..9a71cf5ce 100644 --- a/open_wearable/docs/activity_tracker.md +++ b/open_wearable/docs/activity_tracker.md @@ -66,11 +66,21 @@ window. The first three steps are credited together. One/two-step bouts can be missed. Isolated peaks and strong head rotations are suppressed. This is a heuristic starting point: head motions resembling gait can still cause errors. -Repetitions use bounded integration of projected acceleration as a motion proxy, -hysteretic down/return phases, minimum excursion and duration checks. It is not -a measured squat depth or a form-quality assessment. Parameters differ for -squats and push-ups. Slow, shallow, asymmetric or head-independent movements -need real-data tuning; a full body cycle cannot always be inferred from the ear. +Squats use bounded integration of projected acceleration as a motion proxy. +Push-ups have a separate detector: a quiet 0.7 s starting window estimates a +local acceleration offset after filter settling, then short-window integration +tracks descent and return without decaying displacement during a hold. A return +of at least 80% of the estimated descent and near-zero estimated velocity is +required. Residual velocity is preserved between continuous repetitions to avoid +injecting drift. Near-rest correction, an 8 s cycle timeout and motion bounds +limit integration errors; strong head rotations, clipping and data gaps discard +partial cycles. A brief hold at the top helps the causal filter settle before +the last repetition is confirmed. See `models/push_up_detector.dart`. + +These are heuristic motion proxies, not measured exercise depth or a form-quality +assessment. Fixed thresholds and independent head movement still need real-data +evaluation. The push-up regression suite uses synthetic kinematic signals only; +a full body cycle cannot always be inferred from the ear. Jump height uses `h = g * flight_time² / 8`, with still preparation, propulsion, a low-acceleration flight interval and landing confirmation. Similar take-off diff --git a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart index 862e532e2..78779d98f 100644 --- a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart +++ b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart @@ -1,5 +1,6 @@ import 'dart:math' as math; import 'activity_models.dart'; +import 'push_up_detector.dart'; /// Events from one sample. Counts are deltas, so UI throttling loses no events. class ActivityUpdate { @@ -30,6 +31,7 @@ class ActivityEngine { double _smooth = 0; final _steps = _StepDetector(); final _reps = _RepetitionDetector(); + final _pushUps = PushUpDetector(); final _jumps = _JumpDetector(); /// Validated calibration snapshot for reproducing a session offline. @@ -63,6 +65,7 @@ class ActivityEngine { movement = 0; _steps.reset(); _reps.reset(); + _pushUps.reset(); _jumps.reset(); phase = 'Stand still briefly'; } @@ -72,6 +75,7 @@ class ActivityEngine { kind = next; _steps.reset(); _reps.reset(); + _pushUps.reset(); _jumps.reset(); _smooth = 0; phase = next == ActivityKind.steps @@ -161,14 +165,14 @@ class ActivityEngine { phase = _jumps.inMotion ? 'Jump in progress' : 'Ready for next jump'; return ActivityUpdate(jump: jump); } - final count = clipped - ? 0 - : _reps.add( - s.seconds, - _smooth, - dt, - kind == ActivityKind.squats ? 0.065 : 0.035, - ); + if (kind == ActivityKind.pushUps) { + if (clipped) _pushUps.reset(); + final count = + clipped ? 0 : _pushUps.add(s.seconds, _smooth, dt, angularSpeed); + phase = _pushUps.phase; + return ActivityUpdate(repetitions: count); + } + final count = clipped ? 0 : _reps.add(s.seconds, _smooth, dt, 0.065); if (clipped) _reps.reset(); phase = _reps.phase; return ActivityUpdate(repetitions: count); diff --git a/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart new file mode 100644 index 000000000..7f80416db --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart @@ -0,0 +1,112 @@ +import 'dart:math' as math; + +/// Counts manually selected push-ups from filtered vertical acceleration. +/// +/// Inputs are sensor seconds, m/s², seconds and angular speed in rad/s. +/// Displacement is a short-lived motion proxy, not measured exercise depth. +/// Unlike a leaky position integrator, a stationary hold cannot erase the +/// descent and make a small return look like a complete repetition. +class PushUpDetector { + static const _minimumExcursion = 0.035; + double _velocity = 0, _position = 0, _minimum = 0; + double _began = 0, _settle = 0, _quiet = 0; + double _baseline = 0, _baselineSum = 0; + double? _settleAnchor; + int _baselineSamples = 0; + int _state = 0; + + /// Current phase for live exercise feedback. + String phase = 'Hold the start position'; + + /// Discards the current movement and requires a new quiet starting window. + void reset() { + _velocity = _position = _minimum = _began = _settle = _quiet = 0; + _baseline = _baselineSum = 0; + _baselineSamples = 0; + _settleAnchor = null; + _state = 0; + phase = 'Hold the start position'; + } + + /// Returns one only after a descent and a substantial return to its origin. + int add(double time, double acceleration, double dt, double angularSpeed) { + // The engine handles data gaps and clipping. Reject large head rotations + // locally as well; the settling window must include a quiet gyroscope. + if (angularSpeed > 1.6) { + reset(); + return 0; + } + if (_settle < 0.7) { + final quiet = acceleration.abs() < 0.5 && angularSpeed < 0.18; + if (!quiet || + (_settleAnchor != null && + (acceleration - _settleAnchor!).abs() > 0.05)) { + _settle = _baselineSum = 0; + _baselineSamples = 0; + _settleAnchor = null; + } + if (quiet) { + _settleAnchor ??= acceleration; + _settle += dt; + // Let the causal low-pass filter settle before measuring its offset. + if (_settle >= 0.45) { + _baselineSum += acceleration; + _baselineSamples++; + } + if (_settle >= 0.7) _baseline = _baselineSum / _baselineSamples; + } + return 0; + } + // Estimate a local residual offset in the starting pose. A small gravity + // calibration error must not integrate into velocity throughout the set. + acceleration -= _baseline; + final previousVelocity = _velocity; + _velocity += acceleration * dt; + _position += (previousVelocity + _velocity) * 0.5 * dt; + // Apply a zero-velocity correction only near rest, never decay position. + // Small acceleration alone is insufficient: constant-speed motion also + // has near-zero acceleration, so velocity must already be near zero. + _quiet = + acceleration.abs() < 0.008 && _velocity.abs() < 0.025 ? _quiet + dt : 0; + if (_quiet >= 0.25) _velocity = 0; + if (_velocity.abs() > 2 || _position.abs() > 0.75) { + reset(); + return 0; + } + if (_state != 0 && time - _began > 8) { + reset(); + return 0; + } + if (_state == 0) { + phase = 'Ready · lower your body'; + _minimum = math.min(_minimum, _position); + if (_position < -_minimumExcursion * 0.4 && _velocity < -0.035) { + _state = 1; + _began = time; + phase = 'Lowering'; + } else if (_quiet >= 0.25) { + _position = _minimum = 0; + } + } else if (_state == 1) { + _minimum = math.min(_minimum, _position); + if (_minimum <= -_minimumExcursion && _velocity > 0.025) { + _state = 2; + phase = 'Returning to start'; + } + } else { + final returned = _position >= _minimum * 0.2 && + _position - _minimum >= _minimumExcursion * 0.8; + if (returned && _velocity.abs() <= 0.025 && time - _began >= 0.65) { + _state = 0; + // Keep residual velocity for the start of the next continuous cycle; + // resetting it mid-transition would inject integration drift. + _position = _minimum = 0; + if (_quiet >= 0.25) _velocity = 0; + phase = 'Repetition complete'; + return 1; + } + if (!returned && _velocity < -0.035) reset(); + } + return 0; + } +} diff --git a/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart new file mode 100644 index 000000000..bb092b219 --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart @@ -0,0 +1,234 @@ +import 'dart:math' as math; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_engine.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; + +/// Independent kinematic input: quintic rest-to-rest displacement segments. +/// These ideal signals exercise logic; they are not earable accuracy evidence. +class _PushUpReplay { + _PushUpReplay({ + this.dt = 0.01, + this.up = const MotionVector(0, 0, 1), + this.noise = 0, + }); + final double dt, noise; + final _random = math.Random(42); + final MotionVector up; + final engine = ActivityEngine(); + double time = 0, bias = 0; + int repetitions = 0, steps = 0; + + void sample(double acceleration, {MotionVector gyro = MotionVector.zero}) { + time += dt; + final update = engine.add( + MotionSample( + time, + up * + (ActivityEngine.gravity + + acceleration + + bias + + noise * (2 * _random.nextDouble() - 1)), + gyroscope: gyro, + ), + ); + repetitions += update.repetitions; + steps += update.steps; + } + + void hold(double seconds, {MotionVector gyro = MotionVector.zero}) { + for (var i = 0; i < (seconds / dt).round(); i++) { + sample(0, gyro: gyro); + } + } + + void start() { + hold(2); + engine.select(ActivityKind.pushUps); + hold(1); + } + + void move(double distance, double seconds) { + final count = (seconds / dt).round(); + final duration = count * dt; + for (var i = 0; i < count; i++) { + final u = (i + 0.5) / count; + sample( + distance / + (duration * duration) * + (60 * u - 180 * u * u + 120 * u * u * u), + ); + } + } + + void cycle({ + double depth = 0.16, + double period = 1.8, + double pause = 0, + }) { + move(-depth, period / 2); + hold(pause); + move(depth, period / 2); + hold(pause); + } +} + +void main() { + for (final depth in [0.08, 0.16, 0.25]) { + for (final period in [1.0, 1.8, 3.0, 5.0, 7.0]) { + test('six full push-ups: depth=$depth, period=$period', () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 6; i++) { + r.cycle(depth: depth, period: period); + } + r.hold(1); + expect(r.repetitions, 6); + expect(r.steps, 0); + }); + } + } + for (final dt in [0.02, 0.01, 0.005]) { + test('counts continuous repetitions at dt=$dt', () { + final r = _PushUpReplay(dt: dt)..start(); + for (var i = 0; i < 6; i++) { + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + }); + } + for (final pause in [1.0, 3.0]) { + test('holds of $pause seconds preserve complete repetitions', () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 6; i++) { + r.cycle(pause: pause); + } + expect(r.repetitions, 6); + }); + } + for (final returned in [0.0, 0.02, 0.04, 0.08]) { + test('a bottom hold followed by $returned m return is not a full rep', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.hold(3); + r.move(returned, 0.9); + r.hold(5); + expect(r.repetitions, 0); + }); + } + for (final noise in [0.02, 0.05, 0.1]) { + test('counts six repetitions with bounded synthetic noise $noise', () { + final r = _PushUpReplay(noise: noise)..start(); + for (var i = 0; i < 6; i++) { + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + }); + } + test('repeated shallow movements do not accumulate into a repetition', () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 6; i++) { + r.cycle(depth: 0.01); + } + r.hold(1); + expect(r.repetitions, 0); + }); + test('returning only after the movement timeout does not count', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.hold(9); + r.move(0.16, 0.9); + r.hold(1); + expect(r.repetitions, 0); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('settling also requires a quiet gyroscope', () { + final r = _PushUpReplay()..hold(2); + r.engine.select(ActivityKind.pushUps); + r.hold(1, gyro: const MotionVector(0, 0, 0.5)); + expect(r.engine.phase, 'Hold the start position'); + r.hold(1); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('a strong head rotation discards a partial repetition', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.sample(0, gyro: const MotionVector(0, 0, 2)); + r.move(0.16, 0.9); + r.hold(1); + expect(r.repetitions, 0); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('a clipped impact discards the current repetition', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.sample(20); + r.move(0.16, 0.9); + r.hold(2); + expect(r.repetitions, 0); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('a data gap discards a partial cycle and requires calibration', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.time += 1; + r.sample(0); + expect(r.engine.calibrated, isFalse); + r.move(0.16, 0.9); + r.hold(3); + expect(r.repetitions, 0); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('switching modes discards a partial push-up', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.engine.select(ActivityKind.squats); + r.engine.select(ActivityKind.pushUps); + r.move(0.16, 0.9); + r.hold(1); + expect(r.repetitions, 0); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + }); + test('calibration supports a different fixed earable orientation', () { + final r = _PushUpReplay(up: const MotionVector(0, 1, 0))..start(); + for (var i = 0; i < 6; i++) { + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + }); + for (final bias in [-0.05, 0.05]) { + test('starting-pose calibration removes residual offset $bias', () { + final r = _PushUpReplay()..start(); + r.bias = bias; + r.engine.select(ActivityKind.pushUps); + r.hold(1); + for (var i = 0; i < 6; i++) { + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + }); + } + test('small stationary acceleration residuals do not invent repetitions', () { + for (final bias in [-0.1, -0.05, 0.05, 0.1]) { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 12000; i++) { + r.sample(bias); + } + expect(r.repetitions, 0, reason: 'bias=$bias'); + } + }); +} From 57f4bfcc11fce092bcb9d5d7689faa7603ef6e08 Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 10 Nov 2026 18:10:48 +0100 Subject: [PATCH 5/7] fix(activity-tracker): keep push-up sets ready between repetitions --- docs/algorithmen/liegestuetze.md | 44 ++++++++++++-- docs/journal.md | 21 +++++++ open_wearable/docs/activity_tracker.md | 11 ++-- .../models/activity_engine.dart | 6 +- .../models/push_up_detector.dart | 51 ++++++++++++----- .../widgets/activity_tracker_page.dart | 6 +- .../push_up_detector_test.dart | 57 ++++++++++++++++++- 7 files changed, 167 insertions(+), 29 deletions(-) diff --git a/docs/algorithmen/liegestuetze.md b/docs/algorithmen/liegestuetze.md index 8e87389ac..dd9bd5525 100644 --- a/docs/algorithmen/liegestuetze.md +++ b/docs/algorithmen/liegestuetze.md @@ -27,15 +27,17 @@ verworfen, weil es bei nahtlosen Wiederholungen neue Integrationsfehler erzeugt. Der Restoffset wird erst nach 0,45 s Filter-Einschwingzeit gemittelt. 3. Die offsetbereinigte Beschleunigung wird kurzfristig integriert. Der Positionswert bleibt während einer Pause erhalten. -4. Auf Absenken folgt eine Rückbewegung von mindestens 80 % der geschätzten - Absenkung. Erst nahe geschätzter Geschwindigkeit null wird gezählt. Eine +4. Auf Absenken folgt eine Rückbewegung von mindestens 75 % der geschätzten + Absenkung. Ein vom Rückbewegungstempo abhängiger Umkehrpunkt bestätigt die Zählung. Eine Mindestbewegung und Mindestdauer verwerfen kleine beziehungsweise kurze Zyklen. 5. Nahe Stillstand darf die Geschwindigkeit korrigiert werden, nicht die aktive Absenkung. Restgeschwindigkeit bleibt beim Übergang zur nächsten Wiederholung - erhalten. Große Kopfrotationen, Übersteuerung, Datenlücken und Moduswechsel - verwerfen unvollständige Zyklen; ein Zeitlimit von 8 s begrenzt Drift. + erhalten. Große Kopfrotationen, Übersteuerung, Richtungswechsel und Zeitlimit + verwerfen nur die aktuelle Bewegung, nicht die Satzkalibrierung. Dabei bleibt + der Detektor bereit; ein erneuter 0,7-s-Halt ist nicht nötig. Datenlücken, + Pausen und Moduswechsel setzen weiterhin die Kalibrierung zurück. -Die Schwellwerte sind Heuristiken. Insbesondere der Positionsproxy mit 0,035 m +Die Schwellwerte sind Heuristiken. Insbesondere der Positionsproxy mit 0,025 m Mindestamplitude ist keine gemessene anatomische Tiefe. Bei nur geringer Beschleunigung kann ein IMU-Detektor Stillstand nicht sicher von konstanter Geschwindigkeit unterscheiden. Ein kurzer Halt in der oberen Position hilft @@ -44,7 +46,7 @@ bei der Bestätigung der letzten Wiederholung. ## Evaluation und Grenzen - 0 Versuchspersonen, keine Hardware-Aufnahmen; ausschließlich synthetische Signale. -- 38 Regressionstests in `open_wearable/test/apps/activity_tracker/push_up_detector_test.dart`. +- Ursprünglich 38 Regressionstests in `open_wearable/test/apps/activity_tracker/push_up_detector_test.dart`. - Unabhängige quintische Ruhe-zu-Ruhe-Trajektorien liefern die Referenz: jeweils 6 Zyklen bei 8/16/25 cm simuliertem Hub und 1/1,8/3/5/7 s Zyklusdauer, 100 Hz. In diesen 15 Signalen wurden vorher 82 von 90, jetzt 90 von 90 Zyklen gezählt. @@ -64,3 +66,33 @@ Ausführen aus `open_wearable/`: ```sh flutter test test/apps/activity_tracker/push_up_detector_test.dart ``` + +## Anpassung nach erstem Praxistest am 10.11.2026 + +Rückmeldung: Wiederholungen werden teilweise erkannt, danach erscheint wieder +„Hold the start position“; kleinere Bewegungen gehen verloren. Es liegen dazu +keine Rohdaten oder annotierten Referenzzahlen vor. + +Ursache im Code: Auch lokale Bewegungsabbrüche riefen den vollständigen Reset +mit 0,7-s-Startpositions-Halt auf. Jetzt bleibt eine einmal erreichte +Satzkalibrierung bei solchen Abbrüchen erhalten. Eine übersteuerte Probe wird +verworfen und ihr Filter-Nachlauf entfernt. Echte Datenunterbrechungen bleiben +separat abgesichert. + +Sensitivität: Mindesthub des Bewegungsproxys 0,035 → 0,025 m, +Absenkgeschwindigkeit 0,035 → 0,020 m/s, Rückkehrbeginn 0,025 → 0,015 m/s, +Rückkehranteil 80 → 75 %, Mindestdauer 0,65 → 0,50 s. Der obere Umkehrpunkt +verwendet maximal 0,04 m/s beziehungsweise 30 % des Rückkehr-Spitzenwerts und +erkennt einen Vorzeichenwechsel der Geschwindigkeit. Die aktive +Stillstandskorrektur greift erst unter 0,005 m/s, damit langsame Bewegungen +nicht fälschlich auf null gesetzt werden. Die Schwellwerte sind vorläufig. + +Neue Gegenproben: 8 nahtlose Zyklen mit 4 cm simuliertem Hub bei 2/3/5 s, +Rotations-/Clipping-Störungen ohne erneuten Halt, Teilrückkehr mit Umkehr sowie +ungleiche Absenk-/Aufrichtzeiten. Vor der Änderung fehlten alle 8 langsamen +4-cm-Zyklen sowie die direkt auf die Störung folgenden Wiederholungen in den +entsprechenden Tests. Die ursprünglichen Teilbewegungs- und Negativtests bleiben +erhalten. Es ist weiterhin keine Aussage über reale Zählgenauigkeit möglich. + +Prüfung nach der Anpassung: 46 Push-up-Tests und 213 Tests insgesamt bestanden; +`flutter analyze` ohne Befund. Die bisherigen Negativfälle bleiben grün. diff --git a/docs/journal.md b/docs/journal.md index ed8be1d96..7bbaaac8a 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -2,6 +2,27 @@ Neueste Einträge oben. Format siehe [README](README.md#journal-eintrag). +## 2026-11-10: Kontinuierliche Push-ups nach Praxisrückmeldung + +**Was:** Lokale Bewegungsabbrüche behalten jetzt die Satzkalibrierung. Kein neuer +Startpositions-Halt nach verworfenen Zyklen, Kopfrotationen oder Clipping. +Kleinere Bewegungen werden sensitiver erkannt; Umkehrpunkt und Driftkorrektur +berücksichtigen langsame Bewegungen. Englische UI erklärt den einmaligen Halt. + +**Warum:** Der vollständige Reset blockierte nach einer unsicheren Wiederholung +erneut für die Startposition. Nur die Stillhaltezeit zu verkürzen hätte die +Ursache nicht beseitigt. Echte Datenlücken und Pausen verlangen weiter eine +neue Kalibrierung. Parameter und Gegenproben im +[Algorithmusbericht](algorithmen/liegestuetze.md#anpassung-nach-erstem-praxistest-am-10112026). + +**Evaluation:** Synthetische Regressionen für nahtlose kleinere Zyklen, Störungen, +Teilbewegungen und ungleiches Tempo. Keine neuen Probandenaufnahmen oder +quantitativen Hardware-Messungen; die Praxisrückmeldung ist qualitativ. +46 Push-up-Tests und 213 Tests insgesamt bestanden, `flutter analyze` ohne Befund. + +**Offen:** Erneuter Test am Earable, danach anhand synchronisierter Referenzen +prüfen, ob die höhere Sensitivität zusätzliche Fehlzählungen erzeugt. + ## 2026-11-10: Push-up-Zähler nach Rebase geprüft und korrigiert **Was:** `feat/activity-tracker` auf `origin/main` rebased; neuere Android-Toolchain diff --git a/open_wearable/docs/activity_tracker.md b/open_wearable/docs/activity_tracker.md index 9a71cf5ce..097df68a8 100644 --- a/open_wearable/docs/activity_tracker.md +++ b/open_wearable/docs/activity_tracker.md @@ -70,12 +70,15 @@ Squats use bounded integration of projected acceleration as a motion proxy. Push-ups have a separate detector: a quiet 0.7 s starting window estimates a local acceleration offset after filter settling, then short-window integration tracks descent and return without decaying displacement during a hold. A return -of at least 80% of the estimated descent and near-zero estimated velocity is +of at least 75% of the estimated descent and a speed-adaptive top check is required. Residual velocity is preserved between continuous repetitions to avoid injecting drift. Near-rest correction, an 8 s cycle timeout and motion bounds -limit integration errors; strong head rotations, clipping and data gaps discard -partial cycles. A brief hold at the top helps the causal filter settle before -the last repetition is confirmed. See `models/push_up_detector.dart`. +limit integration errors. Strong head rotations, clipping, timeouts and rejected +cycles discard the movement but keep the set calibration, so no starting-pose +hold is required between repetitions. Clipped filter tails are also cleared. +Data gaps, pauses and mode changes still require fresh calibration. Minimum +motion-proxy excursion is 0.025 m; it is not a physical depth requirement. A brief +natural turnaround at the top lets the causal filter confirm the final repetition. See `models/push_up_detector.dart`. These are heuristic motion proxies, not measured exercise depth or a form-quality assessment. Fixed thresholds and independent head movement still need real-data diff --git a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart index 78779d98f..88291fd8f 100644 --- a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart +++ b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart @@ -166,7 +166,11 @@ class ActivityEngine { return ActivityUpdate(jump: jump); } if (kind == ActivityKind.pushUps) { - if (clipped) _pushUps.reset(); + if (clipped) { + _pushUps.discardMovement(); + // Do not feed the impact's low-pass tail into the next movement. + _smooth = movement = 0; + } final count = clipped ? 0 : _pushUps.add(s.seconds, _smooth, dt, angularSpeed); phase = _pushUps.phase; diff --git a/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart index 7f80416db..3bd1f432f 100644 --- a/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart +++ b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart @@ -7,9 +7,9 @@ import 'dart:math' as math; /// Unlike a leaky position integrator, a stationary hold cannot erase the /// descent and make a small return look like a complete repetition. class PushUpDetector { - static const _minimumExcursion = 0.035; + static const _minimumExcursion = 0.025; double _velocity = 0, _position = 0, _minimum = 0; - double _began = 0, _settle = 0, _quiet = 0; + double _began = 0, _settle = 0, _quiet = 0, _returnPeak = 0; double _baseline = 0, _baselineSum = 0; double? _settleAnchor; int _baselineSamples = 0; @@ -20,7 +20,8 @@ class PushUpDetector { /// Discards the current movement and requires a new quiet starting window. void reset() { - _velocity = _position = _minimum = _began = _settle = _quiet = 0; + _velocity = + _position = _minimum = _began = _settle = _quiet = _returnPeak = 0; _baseline = _baselineSum = 0; _baselineSamples = 0; _settleAnchor = null; @@ -28,12 +29,24 @@ class PushUpDetector { phase = 'Hold the start position'; } + /// Drops one uncertain movement while keeping an established set calibration. + /// Pauses, data gaps and mode changes still use [reset] for a fresh setup. + void discardMovement() { + if (_settle < 0.7) { + reset(); + return; + } + _velocity = _position = _minimum = _began = _quiet = _returnPeak = 0; + _state = 0; + phase = 'Ready · lower your body'; + } + /// Returns one only after a descent and a substantial return to its origin. int add(double time, double acceleration, double dt, double angularSpeed) { // The engine handles data gaps and clipping. Reject large head rotations // locally as well; the settling window must include a quiet gyroscope. if (angularSpeed > 1.6) { - reset(); + discardMovement(); return 0; } if (_settle < 0.7) { @@ -66,21 +79,23 @@ class PushUpDetector { // Apply a zero-velocity correction only near rest, never decay position. // Small acceleration alone is insufficient: constant-speed motion also // has near-zero acceleration, so velocity must already be near zero. - _quiet = - acceleration.abs() < 0.008 && _velocity.abs() < 0.025 ? _quiet + dt : 0; + final restingVelocity = _state == 0 ? 0.025 : 0.005; + _quiet = acceleration.abs() < 0.008 && _velocity.abs() < restingVelocity + ? _quiet + dt + : 0; if (_quiet >= 0.25) _velocity = 0; if (_velocity.abs() > 2 || _position.abs() > 0.75) { - reset(); + discardMovement(); return 0; } if (_state != 0 && time - _began > 8) { - reset(); + discardMovement(); return 0; } if (_state == 0) { phase = 'Ready · lower your body'; _minimum = math.min(_minimum, _position); - if (_position < -_minimumExcursion * 0.4 && _velocity < -0.035) { + if (_position < -_minimumExcursion * 0.4 && _velocity < -0.02) { _state = 1; _began = time; phase = 'Lowering'; @@ -89,23 +104,29 @@ class PushUpDetector { } } else if (_state == 1) { _minimum = math.min(_minimum, _position); - if (_minimum <= -_minimumExcursion && _velocity > 0.025) { + if (_minimum <= -_minimumExcursion && _velocity > 0.015) { _state = 2; + _returnPeak = _velocity; phase = 'Returning to start'; } } else { - final returned = _position >= _minimum * 0.2 && - _position - _minimum >= _minimumExcursion * 0.8; - if (returned && _velocity.abs() <= 0.025 && time - _began >= 0.65) { + _returnPeak = math.max(_returnPeak, _velocity); + final returned = _position >= _minimum * 0.25 && + _position - _minimum >= _minimumExcursion * 0.75; + // Slow, small movements need a relative top-speed check. A fixed + // tolerance could otherwise confirm them while still moving upwards. + final nearTop = _velocity.abs() <= math.min(0.04, _returnPeak * 0.3) || + (previousVelocity > 0 && _velocity <= 0); + if (returned && nearTop && time - _began >= 0.5) { _state = 0; // Keep residual velocity for the start of the next continuous cycle; // resetting it mid-transition would inject integration drift. - _position = _minimum = 0; + _position = _minimum = _returnPeak = 0; if (_quiet >= 0.25) _velocity = 0; phase = 'Repetition complete'; return 1; } - if (!returned && _velocity < -0.035) reset(); + if (!returned && _velocity < -0.02) discardMovement(); } return 0; } diff --git a/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart b/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart index e304c66c5..9219aebb0 100644 --- a/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart +++ b/open_wearable/lib/apps/activity_tracker/widgets/activity_tracker_page.dart @@ -761,8 +761,10 @@ class _ExercisePage extends StatelessWidget { children: [ ActivitySignal(c.signal), const SizedBox(height: 10), - const ActivityCaption( - 'Hold the start position briefly. Lower and return fully; keep your head aligned with your body.', + ActivityCaption( + c.mode == ActivityKind.pushUps + ? 'Hold the start position once, then continue at your own pace. No pause is needed between repetitions. Keep your head aligned with your body.' + : 'Hold the start position briefly. Lower and return fully; keep your head aligned with your body.', ), ], ), diff --git a/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart index bb092b219..daafbef8c 100644 --- a/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart +++ b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart @@ -16,7 +16,7 @@ class _PushUpReplay { final MotionVector up; final engine = ActivityEngine(); double time = 0, bias = 0; - int repetitions = 0, steps = 0; + int repetitions = 0, steps = 0, restartSamples = 0; void sample(double acceleration, {MotionVector gyro = MotionVector.zero}) { time += dt; @@ -33,6 +33,7 @@ class _PushUpReplay { ); repetitions += update.repetitions; steps += update.steps; + if (engine.phase == 'Hold the start position') restartSamples++; } void hold(double seconds, {MotionVector gyro = MotionVector.zero}) { @@ -45,6 +46,7 @@ class _PushUpReplay { hold(2); engine.select(ActivityKind.pushUps); hold(1); + restartSamples = 0; } void move(double distance, double seconds) { @@ -133,10 +135,63 @@ void main() { r.hold(1); expect(r.repetitions, 0); }); + for (final period in [2.0, 3.0, 5.0]) { + test('counts smaller head excursions without repeated setup: $period s', + () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 8; i++) { + r.cycle(depth: 0.04, period: period); + } + r.hold(1); + expect(r.repetitions, 8); + expect(r.restartSamples, 0); + }); + } + test('a rejected rotation does not require holding the starting pose again', + () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 6; i++) { + r.sample(0, gyro: const MotionVector(0, 0, 2)); + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + expect(r.restartSamples, 0); + }); + test('a clipped sample does not require a new starting-pose hold', () { + final r = _PushUpReplay()..start(); + r.sample(20); + r.cycle(); + r.hold(1); + expect(r.repetitions, 1); + expect(r.restartSamples, 0); + }); + test('a partial return and reversal skips the cycle without rearming', () { + final r = _PushUpReplay()..start(); + r.move(-0.16, 0.9); + r.move(0.04, 0.6); + r.move(-0.04, 0.6); + r.hold(1); + expect(r.repetitions, 0); + expect(r.restartSamples, 0); + }); + for (final lowering in [0.6, 2.0]) { + test('uneven down/up tempo $lowering seconds needs no extra hold', () { + final r = _PushUpReplay()..start(); + for (var i = 0; i < 6; i++) { + r.move(-0.06, lowering); + r.move(0.06, 2.6 - lowering); + } + r.hold(1); + expect(r.repetitions, 6); + expect(r.restartSamples, 0); + }); + } test('returning only after the movement timeout does not count', () { final r = _PushUpReplay()..start(); r.move(-0.16, 0.9); r.hold(9); + expect(r.restartSamples, 0); r.move(0.16, 0.9); r.hold(1); expect(r.repetitions, 0); From 7272e37a66a280face36ed8c9c2f364f47fae4b3 Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 10 Nov 2026 18:56:47 +0100 Subject: [PATCH 6/7] fix(activity-tracker): reduce push-up drift and ease motion thresholds --- docs/algorithmen/liegestuetze.md | 26 +++++ docs/journal.md | 22 ++++ open_wearable/docs/activity_tracker.md | 19 ++-- .../models/push_up_detector.dart | 100 ++++++++++++++---- .../push_up_detector_test.dart | 14 +++ 5 files changed, 148 insertions(+), 33 deletions(-) diff --git a/docs/algorithmen/liegestuetze.md b/docs/algorithmen/liegestuetze.md index dd9bd5525..db33b141e 100644 --- a/docs/algorithmen/liegestuetze.md +++ b/docs/algorithmen/liegestuetze.md @@ -2,6 +2,23 @@ Stand: 10.11.2026. Manuell gewählter Push-up-Modus, ohne ML. +## Aktueller Stand nach der Lowering-Rückmeldung + +Die folgende Historie beschreibt auch verworfene Zwischenstände. Aktuell werden +Geschwindigkeit und Positionsproxy gedämpft, um fortlaufende Offsetdrift zu +begrenzen. Zusätzlich bleibt eine ungedämpfte Referenz für die Rückwegprüfung +erhalten; nahe Ruhe werden Geschwindigkeit und der aktive Proxy angehalten. +Eine zeitbegrenzte Restgeschwindigkeitskorrektur und ein Vergleich von +Spitzengeschwindigkeit × Phasendauer helfen bei kontinuierlichen Zyklen. Nach +einem Halt ist dieser alternative Phasenvergleich deaktiviert, damit die +Wartezeit keine unvollständige Rückkehr vervollständigt. Der Mindestproxyhub +beträgt nun 0,015 m und ist weiterhin keine gemessene anatomische Tiefe. + +Eine lokale Aufnahme (ca. 100 Hz, keine Lücken >120 ms) liefert im Replay +6 statt vorher 3 Zählungen. Die Selbstauskunft beträgt nur ungefähr 5–7, +exakte Referenzannotation fehlt. Alle 49 Push-up-Tests bestehen; das ersetzt +keine unabhängige Nutzerevaluation. Rohdaten werden nicht eingecheckt. + ## Befund und Entscheidung Der Prototyp verwendete denselben Detektor wie Squats, lediglich mit anderer @@ -96,3 +113,12 @@ erhalten. Es ist weiterhin keine Aussage über reale Zählgenauigkeit möglich. Prüfung nach der Anpassung: 46 Push-up-Tests und 213 Tests insgesamt bestanden; `flutter analyze` ohne Befund. Die bisherigen Negativfälle bleiben grün. + +## Leichte Sensitivitätsanpassung am 10.11.2026 + +Auf erneute Praxisrückmeldung: Absenkschwelle von 0,020 auf 0,019 m/s und +Beginn der Rückbewegung von 0,015 auf 0,013 m/s gesenkt. Mindesthub, +Rückwegvergleich, Mindestdauer und Gyroskop-Grenze bleiben unverändert. +Eine stärkere Absenkung des Mindesthubs wurde verworfen, weil dann eine +Teilbewegungs-Gegenprobe fälschlich zählte. Nur vorhandene Push-up-Tests und lokaler +Replay wurden erneut ausgeführt; keine neue vollständige Evaluationsrunde. diff --git a/docs/journal.md b/docs/journal.md index 7bbaaac8a..9c1e2da09 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -2,6 +2,28 @@ Neueste Einträge oben. Format siehe [README](README.md#journal-eintrag). +## 2026-11-10: Push-up-Erkennung leicht empfindlicher eingestellt + +Absenkschwelle 0,020 → 0,019 m/s und Rückkehrbeginn 0,015 → 0,013 m/s. +Mindesthub, Rückwegprüfung und Mindestdauer bleiben bestehen. Auf Wunsch +nur die vorhandenen Push-up-Tests und den lokalen Replay erneut geprüft. +Rohdaten unter `data/` bleiben außerhalb des Commits. + +## 2026-11-10: Lowering-Blockade anhand einer lokalen Aufnahme geprüft + +**Befund:** Die lokale Aufnahme hat etwa 100 Hz und keine Zeitlücken über 120 ms. +Mit dem bisherigen Code werden 3 Wiederholungen gezählt; die qualitative +Selbstauskunft liegt bei ungefähr 5–7, eine Videoannotation fehlt. + +**Änderung:** Gedämpfte Integratoren begrenzen Offsetdrift. Ein separater, +ungedämpfter Rückweg-Proxy schützt Teilbewegungen nach Pausen. Für kontinuierliche +Zyklen wird zusätzlich die Dauer und Stärke beider Bewegungsphasen verglichen. + +**Ergebnis:** Replay zählt 6 Wiederholungen. Alle 49 Push-up-Tests und 216 Tests +insgesamt bestehen, einschließlich Teilrückkehr und neuer Offsetwechsel-Fälle. +Das ist ein Entwicklungsvergleich an einer einzelnen, nicht exakt annotierten +Aufnahme; keine unabhängige Genauigkeitsevaluation. Rohdaten bleiben lokal. + ## 2026-11-10: Kontinuierliche Push-ups nach Praxisrückmeldung **Was:** Lokale Bewegungsabbrüche behalten jetzt die Satzkalibrierung. Kein neuer diff --git a/open_wearable/docs/activity_tracker.md b/open_wearable/docs/activity_tracker.md index 097df68a8..f077de0f9 100644 --- a/open_wearable/docs/activity_tracker.md +++ b/open_wearable/docs/activity_tracker.md @@ -68,17 +68,14 @@ heuristic starting point: head motions resembling gait can still cause errors. Squats use bounded integration of projected acceleration as a motion proxy. Push-ups have a separate detector: a quiet 0.7 s starting window estimates a -local acceleration offset after filter settling, then short-window integration -tracks descent and return without decaying displacement during a hold. A return -of at least 75% of the estimated descent and a speed-adaptive top check is -required. Residual velocity is preserved between continuous repetitions to avoid -injecting drift. Near-rest correction, an 8 s cycle timeout and motion bounds -limit integration errors. Strong head rotations, clipping, timeouts and rejected -cycles discard the movement but keep the set calibration, so no starting-pose -hold is required between repetitions. Clipped filter tails are also cleared. -Data gaps, pauses and mode changes still require fresh calibration. Minimum -motion-proxy excursion is 0.025 m; it is not a physical depth requirement. A brief -natural turnaround at the top lets the causal filter confirm the final repetition. See `models/push_up_detector.dart`. +local acceleration offset after filter settling. Damped velocity and position +proxies limit accumulating drift. An undamped reference checks the return; +continuous cycles also compare speed and duration of both phases. This alternate +comparison is disabled after a hold so waiting cannot complete a partial return. +The minimum proxy excursion is 0.015 m, not a physical depth measurement. +Local movement rejection keeps the set ready without another starting-pose hold; +data gaps, pauses and mode changes still require fresh calibration. See +`models/push_up_detector.dart`. These are heuristic motion proxies, not measured exercise depth or a form-quality assessment. Fixed thresholds and independent head movement still need real-data diff --git a/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart index 3bd1f432f..89c5fd7d2 100644 --- a/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart +++ b/open_wearable/lib/apps/activity_tracker/models/push_up_detector.dart @@ -4,15 +4,23 @@ import 'dart:math' as math; /// /// Inputs are sensor seconds, m/s², seconds and angular speed in rad/s. /// Displacement is a short-lived motion proxy, not measured exercise depth. -/// Unlike a leaky position integrator, a stationary hold cannot erase the -/// descent and make a small return look like a complete repetition. +/// Damped integration limits accumulating sensor-offset error during a set. +/// A separate undamped reference protects a held descent from being erased +/// by damping and then mistaken for a complete return. class PushUpDetector { - static const _minimumExcursion = 0.025; + static const _minimumExcursion = 0.015; double _velocity = 0, _position = 0, _minimum = 0; double _began = 0, _settle = 0, _quiet = 0, _returnPeak = 0; double _baseline = 0, _baselineSum = 0; double? _settleAnchor; int _baselineSamples = 0; + double _referenceVelocity = 0, + _referencePosition = 0, + _referenceMinimum = 0, + _referenceSeconds = 0, + _referenceInitialVelocity = 0; + double _loweringPeak = 0, _returnBegan = 0; + bool _heldDescent = false; int _state = 0; /// Current phase for live exercise feedback. @@ -25,6 +33,10 @@ class PushUpDetector { _baseline = _baselineSum = 0; _baselineSamples = 0; _settleAnchor = null; + _referenceVelocity = _referencePosition = + _referenceMinimum = _referenceSeconds = _referenceInitialVelocity = 0; + _loweringPeak = _returnBegan = 0; + _heldDescent = false; _state = 0; phase = 'Hold the start position'; } @@ -37,6 +49,10 @@ class PushUpDetector { return; } _velocity = _position = _minimum = _began = _quiet = _returnPeak = 0; + _referenceVelocity = _referencePosition = + _referenceMinimum = _referenceSeconds = _referenceInitialVelocity = 0; + _loweringPeak = _returnBegan = 0; + _heldDescent = false; _state = 0; phase = 'Ready · lower your body'; } @@ -73,17 +89,29 @@ class PushUpDetector { // Estimate a local residual offset in the starting pose. A small gravity // calibration error must not integrate into velocity throughout the set. acceleration -= _baseline; + _referenceSeconds += dt; + final previousReferenceVelocity = _referenceVelocity; + // The reference is never position-damped. Once a bottom hold is observed, + // its descent must still be physically retraced in this motion proxy. + _referenceVelocity += acceleration * dt; + _referencePosition += + (previousReferenceVelocity + _referenceVelocity) * 0.5 * dt; + _referenceMinimum = math.min(_referenceMinimum, _referencePosition); final previousVelocity = _velocity; - _velocity += acceleration * dt; - _position += (previousVelocity + _velocity) * 0.5 * dt; - // Apply a zero-velocity correction only near rest, never decay position. - // Small acceleration alone is insufficient: constant-speed motion also - // has near-zero acceleration, so velocity must already be near zero. - final restingVelocity = _state == 0 ? 0.025 : 0.005; - _quiet = acceleration.abs() < 0.008 && _velocity.abs() < restingVelocity - ? _quiet + dt - : 0; - if (_quiet >= 0.25) _velocity = 0; + _velocity = _velocity * math.exp(-dt / 1.5) + acceleration * dt; + if (_quiet < 0.25) { + _position = _position * math.exp(-dt / 4) + + (previousVelocity + _velocity) * 0.5 * dt; + } + // Freeze the motion proxy on a sustained near-zero signal. Keep the + // undamped excursion as a separate return requirement across this hold. + // This is a heuristic: constant-speed motion is indistinguishable from + // rest using acceleration alone, so it still needs hardware validation. + _quiet = acceleration.abs() < 0.008 ? _quiet + dt : 0; + if (_quiet >= 0.25) { + _velocity = _referenceVelocity = 0; + if (_state != 0) _heldDescent = true; + } if (_velocity.abs() > 2 || _position.abs() > 0.75) { discardMovement(); return 0; @@ -95,38 +123,66 @@ class PushUpDetector { if (_state == 0) { phase = 'Ready · lower your body'; _minimum = math.min(_minimum, _position); - if (_position < -_minimumExcursion * 0.4 && _velocity < -0.02) { + if (_position < -_minimumExcursion * 0.4 && _velocity < -0.019) { _state = 1; _began = time; + _loweringPeak = -_velocity; phase = 'Lowering'; } else if (_quiet >= 0.25) { - _position = _minimum = 0; + _position = _minimum = _referenceVelocity = + _referencePosition = _referenceMinimum = _referenceSeconds = 0; } } else if (_state == 1) { + _loweringPeak = math.max(_loweringPeak, -_velocity); _minimum = math.min(_minimum, _position); - if (_minimum <= -_minimumExcursion && _velocity > 0.015) { + if (_minimum <= -_minimumExcursion && _velocity > 0.013) { _state = 2; _returnPeak = _velocity; + _returnBegan = time; phase = 'Returning to start'; } } else { _returnPeak = math.max(_returnPeak, _velocity); - final returned = _position >= _minimum * 0.25 && + // Approximate negative endpoint-velocity drift over this short cycle; + // keep the uncorrected reference when the correction would reduce it. + final correctedReturn = math.max( + _referencePosition, + _referencePosition - + 0.5 * + (_referenceInitialVelocity + _referenceVelocity) * + _referenceSeconds, + ); + final referenceReturned = correctedReturn >= _referenceMinimum * 0.3 && + correctedReturn - _referenceMinimum >= _minimumExcursion * 0.75; + // Duration-normalised peak speeds provide an alternate consistency + // check for continuous cycles. Disable it after a hold so waiting + // cannot grow a small partial return into a completed repetition. + final downEnvelope = _loweringPeak * (_returnBegan - _began); + final upEnvelope = _returnPeak * (time - _returnBegan); + final comparableReturn = + !_heldDescent && downEnvelope > 0 && upEnvelope >= downEnvelope * 0.9; + final proxyReturned = _position >= _minimum * 0.25 && _position - _minimum >= _minimumExcursion * 0.75; + final returned = referenceReturned || (proxyReturned && comparableReturn); // Slow, small movements need a relative top-speed check. A fixed // tolerance could otherwise confirm them while still moving upwards. - final nearTop = _velocity.abs() <= math.min(0.04, _returnPeak * 0.3) || - (previousVelocity > 0 && _velocity <= 0); + final nearTop = _referenceVelocity.abs() <= 0.025 || + _velocity.abs() <= math.min(0.04, _returnPeak * 0.3) || + _velocity <= 0; if (returned && nearTop && time - _began >= 0.5) { _state = 0; // Keep residual velocity for the start of the next continuous cycle; // resetting it mid-transition would inject integration drift. - _position = _minimum = _returnPeak = 0; - if (_quiet >= 0.25) _velocity = 0; + _position = _minimum = _returnPeak = + _referencePosition = _referenceMinimum = _referenceSeconds = 0; + _referenceInitialVelocity = _referenceVelocity; + _loweringPeak = _returnBegan = 0; + _heldDescent = false; + if (_quiet >= 0.25) _velocity = _referenceVelocity = 0; phase = 'Repetition complete'; return 1; } - if (!returned && _velocity < -0.02) discardMovement(); + if (!returned && _velocity < -0.019) discardMovement(); } return 0; } diff --git a/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart index daafbef8c..089ac305b 100644 --- a/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart +++ b/open_wearable/test/apps/activity_tracker/push_up_detector_test.dart @@ -277,6 +277,20 @@ void main() { expect(r.repetitions, 6); }); } + for (final changedBias in [-0.005, -0.01, 0.01]) { + test( + 'a small offset change during the set does not stall Lowering: $changedBias', + () { + final r = _PushUpReplay()..start(); + r.bias = changedBias; + for (var i = 0; i < 6; i++) { + r.cycle(); + } + r.hold(1); + expect(r.repetitions, 6); + expect(r.restartSamples, 0); + }); + } test('small stationary acceleration residuals do not invent repetitions', () { for (final bias in [-0.1, -0.05, 0.05, 0.1]) { final r = _PushUpReplay()..start(); From f1226d5c42d78e80faccf41b05918cbbcef15dc1 Mon Sep 17 00:00:00 2001 From: AdrianMoenke Date: Wed, 18 Nov 2026 13:54:42 +0100 Subject: [PATCH 7/7] feat(activity-tracker): add dedicated squat detection - Replace the generic repetition detector with squat-specific calibration and cycle checks. - Add local recording replay tooling, regression tests, and anonymized results. - Document development decisions and accuracy limits; exclude private recordings. --- .gitignore | 4 + docs/README.md | 1 + docs/algorithmen/kniebeugen.md | 130 ++++++++++ docs/journal.md | 28 +++ open_wearable/docs/activity_tracker.md | 16 +- .../models/activity_engine.dart | 95 +++---- .../models/squat_detector.dart | 126 ++++++++++ .../activity_tracker/squat_detector_test.dart | 161 ++++++++++++ .../test/tool/squat_recordings_test.dart | 137 +++++++++++ open_wearable/tool/squats/README.md | 120 +++++++++ open_wearable/tool/squats/manifest.json | 57 +++++ open_wearable/tool/squats/recordings.dart | 231 ++++++++++++++++++ open_wearable/tool/squats/replay.dart | 135 ++++++++++ open_wearable/tool/squats/results.json | 180 ++++++++++++++ 14 files changed, 1360 insertions(+), 61 deletions(-) create mode 100644 docs/algorithmen/kniebeugen.md create mode 100644 open_wearable/lib/apps/activity_tracker/models/squat_detector.dart create mode 100644 open_wearable/test/apps/activity_tracker/squat_detector_test.dart create mode 100644 open_wearable/test/tool/squat_recordings_test.dart create mode 100644 open_wearable/tool/squats/README.md create mode 100644 open_wearable/tool/squats/manifest.json create mode 100644 open_wearable/tool/squats/recordings.dart create mode 100644 open_wearable/tool/squats/replay.dart create mode 100644 open_wearable/tool/squats/results.json diff --git a/.gitignore b/.gitignore index 96b05b0dc..2ce2bbb97 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,7 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk open_wearable/devtools_options.yaml + +# Private participant recordings and local squat development data. +/temp/ +/open_wearable/tool/squats/data/ diff --git a/docs/README.md b/docs/README.md index 3e6940e78..8f8ad6b83 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,6 +14,7 @@ Hier sammeln wir alles, was wir für Zwischenpräsentation, Abschlusspräsentati | `figures/` | Abbildungen samt Quelle oder Skript | Präsentationen und Paper | Erster Algorithmusbericht: [Liegestütz-Zählung](algorithmen/liegestuetze.md). +Kniebeugen mit lokalen Realaufnahmen: [Entwicklung und Messkriterien](algorithmen/kniebeugen.md). Weitere Pfade legen wir an, sobald es dafür Inhalt gibt. ## Journal-Eintrag diff --git a/docs/algorithmen/kniebeugen.md b/docs/algorithmen/kniebeugen.md new file mode 100644 index 000000000..9293e65ee --- /dev/null +++ b/docs/algorithmen/kniebeugen.md @@ -0,0 +1,130 @@ +# Kniebeugen: Entwicklung mit realen IMU-Aufnahmen + +Stand: 17.11.2026. Manuell gewählter Squat-Modus, kein ML-Klassifikator. + +## Wiederverwendung und Abgrenzung + +Wie bei Liegestützen liefert `ActivityEngine` Kalibrierung, Gyro-gestützte +Schwerkraftrichtung, vertikale Projektion und kausale Tiefpassfilterung. Sensorquelle, +Controller, Satzverwaltung, Export und Übungsbildschirm werden wiederverwendet. +`SquatDetector` ersetzt den bisherigen privaten `_RepetitionDetector`; der +Liegestütz-Detektor und die Schwellen anderer Modi bleiben unverändert. + +Der Entwicklungsbereich liegt unter `open_wearable/tool/squats/`: Manifest, +CSV-Replay und lokale, ignorierte Aufnahmen. Rohdaten aus `temp/` bleiben ebenfalls +ignoriert. Öffentlich dokumentiert werden nur aggregierte Ergebnisse. + +## Datensatz: Reihenfolge ist Teil der Referenz + +Die fünf Ordnernamen geben folgende Abfolgen vor: + +| Aufnahme | Abfolge | Referenz-Kniebeugen | +| --- | --- | ---: | +| squat-01 | 10 Kniebeugen | 10 | +| squat-02 | 5 Kniebeugen → Gehen → 5 Kniebeugen | 10 | +| squat-03 | 5 Kniebeugen → Hinsetzen → 5 Kniebeugen | 10 | +| squat-04 | 5 Kniebeugen → 3 Liegestütze → 5 Kniebeugen | 10 | +| squat-05 | 5 Kniebeugen → Treppen/Gehen → 5 Kniebeugen → Hinsetzen → 5 Kniebeugen → 3 Liegestütze | 15 | + +Die gesamte Aufnahme läuft im Squat-Modus. Fremdbewegungen sind Negativkontrollen, +nicht herausgefilterte Daten. Die Zählreferenz stammt ausschließlich aus den +Ordnernamen. Übergangszeitpunkte, Versuchspersonenzahl, Ausführungstempo, +Aufnahmedatum und synchronisierte Referenzvideos sind nicht angegeben. Der +17.11.2026 ist das Auswertungsdatum, nicht das behauptete Messdatum. +Beschleunigung: m/s²; Gyroskop: deg/s als explizite Annahme aus dem üblichen +OE2-Export, im Replay nach rad/s umgerechnet. Die Exportdateien enthalten keine +Einheitenmetadaten. Sensorrate ungefähr 99,9 Hz; die lange Aufnahme enthält zwei +Lücken über 120 ms. + +## Entscheidung: Wann zählt eine Wiederholung? + +1. Im Squat-Modus reicht ein stabiler 0,7-s-Start: Betrag nahe 1 g (±0,55 m/s²), + Gyro unter 0,35 rad/s und Betragsspanne höchstens 0,45 m/s². Andere Modi behalten + ihre 1,5-s-Kalibrierung. Damit werden kleine Kopfbewegungen toleriert, nicht + beliebig bewegte Daten nachträglich als kalibriert ausgegeben. +2. Der Detektor speichert die anfängliche Schwerkraftrichtung als Haltungsreferenz. + Starke Rotation (>1,2 rad/s) oder eine Abweichung von ungefähr 46° verwerfen + die aktuelle Bewegung; die ursprüngliche Haltungsreferenz bleibt bestehen. +3. Gefilterte vertikale Beschleunigung wird kurzfristig integriert. Gedämpfte + Geschwindigkeit (1,5 s) und Position (4 s) begrenzen Offsetdrift. Im ruhigen, + inaktiven Zustand wird der Restoffset nachgeführt. +4. Absenken beginnt unter −0,06 m/s und −0,048 m Positionsproxy. Der Mindesthub + ist 0,12 m; die Rückkehrphase beginnt über +0,04 m/s. +5. Die Position muss mindestens 70 % der Absenkung zurückkehren. Gezählt wird + nahe dem oberen Geschwindigkeitsumkehrpunkt, frühestens nach 0,8 s und vor + einem 6-s-Zeitlimit. Nach einem Halt verhindert eine separate ungedämpfte + Referenz, dass Positionsdämpfung allein eine Teilrückkehr vervollständigt. +6. Übersteuerung und starke Bewegungen verwerfen nur den laufenden Zyklus. + Pausen, Datenlücken, ungültige Daten und Moduswechsel setzen den Detektor + vollständig zurück. Bestätigte Zählungen bleiben im Controller erhalten. + +Mindesthub und Positionswerte sind **Bewegungsproxies**, keine gemessene +anatomische Tiefe oder Bewertung der Kniebeugentechnik. Die IMU am Ohr kann +Knie-/Hüftbewegung nicht direkt beobachten. Hinsetzen und Aufstehen ohne längeren +Halt können ähnlich aussehen; reine vertikale Kopfbewegungen ebenfalls. + +Verworfen: rohe Y-Achse als universelle Vertikale (sensor-/haltungsabhängig), +Übernahme der empfindlichen Liegestütz-Schwellen (kleine Fremdbewegungen), +unbegrenzte doppelte Integration (Drift), automatische Übungsklassifikation oder +ML mit diesen fünf nicht zeitannotierten Aufnahmen (keine belastbare Grundlage). +Keine nachträgliche künstliche Startkalibrierung und keine Sonderbehandlung +anhand des Dateinamens. + +## Messkriterien und Grenzen + +Entwicklungsvergleich: erwartete und gezählte Wiederholungen, signierter und +absoluter Zählfehler pro Aufnahme sowie Kalibrierungsabdeckung und Ereigniszeiten. +Baseline des bisherigen Codes in obiger Reihenfolge: 0, 0, 4, 0, 7. +Drei Aufnahmen bestanden nie die bisherige Startkalibrierung. + +**Gleiche Gesamtzahl ist kein Klassifikationsnachweis.** Fehlzählungen in +Negativabschnitten können ausgelassene Kniebeugen kompensieren. Ereignisbezogene +Precision/Recall, falsch positive Zählungen pro Fremdbewegung und Wiederholungsfehler +pro Satz benötigen unabhängig annotierte Zeitintervalle. Dieses Material dient +zur Entwicklung, nicht als unabhängiger Testdatensatz oder nutzerübergreifender +Genauigkeitsnachweis. Keine Parameter anhand von Dateinamen auswählen. + +## Ergebnisse vom 17.11.2026 + +Vollständiges kausales Replay mit deg/s-Annahme, 20-ms-Gyrozuordnung und ohne +künstliche Startdaten; Ergebnisse unter +[`results.json`](../../open_wearable/tool/squats/results.json). + +| Aufnahme | Soll | Bisher | Jetzt | Fehler jetzt | Erste Kalibrierung (s) | Kalibrierte Samples (%) | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| squat-01 | 10 | 0 | 10 | 0 | 0,72 | 96,84 | +| squat-02 | 10 | 0 | 9 | −1 | 0,73 | 97,68 | +| squat-03 | 10 | 4 | 9 | −1 | 0,71 | 97,68 | +| squat-04 | 10 | 0 | 7 | −3 | 1,80 | 95,05 | +| squat-05 | 15 | 7 | 8 | −7 | 43,70 | 71,24 | + +Summe: 43 Zählungen bei 55 laut Ordnernamen vorgegebenen Kniebeugen; vorher 11. +Mittlerer absoluter Gesamtzählfehler je Aufnahme: 2,4 statt 8,8. Das ist **keine +Recall- oder Genauigkeitsangabe**. Beim langen Ablauf fehlt die Kalibrierung bis +43,70 s; dort kann der kausale Detektor vorherige Bewegungen nicht zählen. +Auch spätere Bestätigungen können ohne Zeitannotation nicht sicher den +Kniebeugen statt Fremdbewegungen zugeordnet werden. Der Mischdatensatz zeigt +weiterhin erhebliche Zählfehler; robuste Fremdbewegungsabgrenzung ist nicht belegt. + +102 gezielte Tests bestanden: 21 neue synthetische Kniebeugenfälle, +49 bestehende Liegestützfälle, 14 Engine-Fälle und 18 CSV-/Zuordnungsfälle. +Synthetische Fälle umfassen vollständige Zyklen, verschiedene Tempi/Raten, +andere Sensororientierung, kleine Kopfbewegungen, Teilrückkehr, Halten, +Rotation, Übersteuerung, Datenlücke, Pause und Moduswechsel. Keine BLE-/Hardware- +oder Videoverifikation durchgeführt. +Die sechs geänderten/neuen Dart-Dateien wurden formatiert und gezielt mit +`dart analyze` geprüft: keine Befunde. Keine vollständige App-Testsuite oder +plattformweite Buildprüfung ausgeführt. + + +## Reproduktion + +Aus `open_wearable/`: + +```sh +dart run tool/squats/replay.dart +flutter test test/apps/activity_tracker/squat_detector_test.dart test/apps/activity_tracker/activity_engine_test.dart test/apps/activity_tracker/push_up_detector_test.dart test/tool/squat_recordings_test.dart +``` + +Die lokalen Dateien und genaue Replay-Aufrufvarianten beschreibt +[`tool/squats/README.md`](../../open_wearable/tool/squats/README.md). diff --git a/docs/journal.md b/docs/journal.md index 9c1e2da09..134b05d45 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -2,6 +2,34 @@ Neueste Einträge oben. Format siehe [README](README.md#journal-eintrag). +## 2026-11-17: Kniebeugen mit fünf lokalen Mischaufnahmen entwickelt + +**Was:** Liegestütz-Implementierung und gemeinsame IMU-Vorverarbeitung geprüft. +Eigener `SquatDetector` ersetzt den alten generischen Wiederholungsdetektor; +Übungsbildschirm, Sensorquelle, Controller und Export bleiben gemeinsam. +Lokaler Entwicklungsbereich `open_wearable/tool/squats/` mit Manifest, Replay +und anonymisierten Ergebnissen; Rohdaten unter `temp/` und `data/` ignoriert. + +**Warum:** Der bisherige Startfilter kalibrierte drei Aufnahmen nie. Im +Squat-Modus wird nun ein stabiler 0,7-s-Betragsverlauf bei kleinen Kopfrotationen +zugelassen; andere Modi unverändert. Größerer Mindest-Bewegungsproxy, +Rückkehrprüfung und stehende Haltungsreferenz statt empfindlicher Push-up- +Schwellen. Dämpfung darf nach einem Halt keine vollständige Rückkehr vortäuschen. + +**Referenz und Ergebnis:** Fünf Aufnahmen mit ungefähr 99,9 Hz während der +Übertragung; lange Mischaufnahme mit zwei Zeitlücken. Die Ordnernamen geben +geordnete Abfolgen von Kniebeugen, Gehen, Treppen, Hinsetzen und Liegestützen +und insgesamt 55 Kniebeugen vor. Keine Zeitannotation, Personenzahl und +Messdatum unbekannt. Gyro deg/s ist eine dokumentierte Exportannahme. +Replay zählt 43 statt 11; Gesamtzählfehler pro Aufnahme im Mittel 2,4 statt 8,8. +Keine Erkennungsgenauigkeit: Fremdbewegungszählungen können fehlende Kniebeugen +kompensieren. Die lange Aufnahme kalibriert erst nach 43,70 s und zählt 8/15. +102 gezielte Tests bestanden; keine neue Hardware-Messung. + +**Grenzen:** Erhebliche Fehler im Mischablauf; Fremdbewegungsabgrenzung und +personenunabhängige Genauigkeit nicht belegt. Begründungen, verworfene Ansätze, +Metriken und Reproduktion im [Kniebeugenbericht](algorithmen/kniebeugen.md). + ## 2026-11-10: Push-up-Erkennung leicht empfindlicher eingestellt Absenkschwelle 0,020 → 0,019 m/s und Rückkehrbeginn 0,015 → 0,013 m/s. diff --git a/open_wearable/docs/activity_tracker.md b/open_wearable/docs/activity_tracker.md index f077de0f9..c55360e05 100644 --- a/open_wearable/docs/activity_tracker.md +++ b/open_wearable/docs/activity_tracker.md @@ -66,7 +66,12 @@ window. The first three steps are credited together. One/two-step bouts can be missed. Isolated peaks and strong head rotations are suppressed. This is a heuristic starting point: head motions resembling gait can still cause errors. -Squats use bounded integration of projected acceleration as a motion proxy. +Squats use a separate `models/squat_detector.dart`: bounded integration of +projected acceleration, a 0.12 m minimum motion proxy, a substantial return and +an upper turning-point check. A standing orientation reference and angular-speed +gate reject large posture changes. An undamped return reference protects held +descents. Squat-mode calibration uses a stable 0.7 s magnitude window and permits +small head rotations; other modes retain their 1.5 s calibration gate. Push-ups have a separate detector: a quiet 0.7 s starting window estimates a local acceleration offset after filter settling. Damped velocity and position proxies limit accumulating drift. An undamped reference checks the return; @@ -90,9 +95,12 @@ clipped impacts produce an unavailable height. The ±2 g clipping heuristic is conservative for the OE2 compact format. Walking/running is not classified. Synthetic replay tests verify implementation behavior, **not real-world -accuracy**. No earable hardware or participant dataset was available during -implementation. User-independent performance and jump-height validity remain -to be measured. +accuracy**. Five local, mixed-activity recordings are available for squat +development. Their folder names supply ordered activity/count labels, not +timestamped event annotations. Matching totals can conceal false counts during +walking, stairs, sitting or push-ups. User-independent performance and +jump-height validity remain to be measured. See the German +[squat report](../../docs/algorithmen/kniebeugen.md) and `tool/squats/README.md`. ## Evaluation workflow diff --git a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart index 88291fd8f..93b2846f0 100644 --- a/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart +++ b/open_wearable/lib/apps/activity_tracker/models/activity_engine.dart @@ -1,6 +1,7 @@ import 'dart:math' as math; import 'activity_models.dart'; import 'push_up_detector.dart'; +import 'squat_detector.dart'; /// Events from one sample. Counts are deltas, so UI throttling loses no events. class ActivityUpdate { @@ -27,10 +28,12 @@ class ActivityEngine { MotionVector _calibrationSum = MotionVector.zero; double _calibrationSeconds = 0; int _calibrationSamples = 0; + double _calibrationMinMagnitude = double.infinity; + double _calibrationMaxMagnitude = double.negativeInfinity; double _gravityMagnitude = gravity; double _smooth = 0; final _steps = _StepDetector(); - final _reps = _RepetitionDetector(); + final _squats = SquatDetector(); final _pushUps = PushUpDetector(); final _jumps = _JumpDetector(); @@ -61,10 +64,12 @@ class ActivityEngine { _calibrationSum = MotionVector.zero; _calibrationSeconds = 0; _calibrationSamples = 0; + _calibrationMinMagnitude = double.infinity; + _calibrationMaxMagnitude = double.negativeInfinity; _smooth = 0; movement = 0; _steps.reset(); - _reps.reset(); + _squats.reset(); _pushUps.reset(); _jumps.reset(); phase = 'Stand still briefly'; @@ -74,7 +79,7 @@ class ActivityEngine { void select(ActivityKind next) { kind = next; _steps.reset(); - _reps.reset(); + _squats.reset(); _pushUps.reset(); _jumps.reset(); _smooth = 0; @@ -111,18 +116,38 @@ class ActivityEngine { final clipped = [s.acceleration.x, s.acceleration.y, s.acceleration.z] .any((v) => v.abs() >= 19.4); // OE2 compact IMU format is ±2 g. if (!calibrated) { - final still = (magnitude - gravity).abs() < 0.55 && angularSpeed < 0.18; + // Squat mode tolerates small head rotations, but additionally requires + // a stable magnitude window. Other activities keep their original gate. + final squatMode = kind == ActivityKind.squats; + final requiredSeconds = squatMode ? 0.7 : 1.5; + final still = (magnitude - gravity).abs() < 0.55 && + angularSpeed < (squatMode ? 0.35 : 0.18); if (!still || dt > 0.12) { _calibrationSum = MotionVector.zero; _calibrationSamples = 0; _calibrationSeconds = 0; + _calibrationMinMagnitude = double.infinity; + _calibrationMaxMagnitude = double.negativeInfinity; } else { _calibrationSeconds += dt; _calibrationSum = _calibrationSum + s.acceleration; _calibrationSamples++; + _calibrationMinMagnitude = + math.min(_calibrationMinMagnitude, magnitude); + _calibrationMaxMagnitude = + math.max(_calibrationMaxMagnitude, magnitude); } - calibrationProgress = (_calibrationSeconds / 1.5).clamp(0, 1); - if (_calibrationSeconds >= 1.5 && _calibrationSamples >= 50) { + if (squatMode && + _calibrationMaxMagnitude - _calibrationMinMagnitude > 0.45) { + _calibrationSum = MotionVector.zero; + _calibrationSamples = 0; + _calibrationSeconds = 0; + _calibrationMinMagnitude = double.infinity; + _calibrationMaxMagnitude = double.negativeInfinity; + } + calibrationProgress = (_calibrationSeconds / requiredSeconds).clamp(0, 1); + if (_calibrationSeconds >= requiredSeconds && + _calibrationSamples >= (squatMode ? 30 : 50)) { final mean = _calibrationSum * (1 / _calibrationSamples); _up = mean.unit; _gravityMagnitude = mean.length; @@ -176,9 +201,13 @@ class ActivityEngine { phase = _pushUps.phase; return ActivityUpdate(repetitions: count); } - final count = clipped ? 0 : _reps.add(s.seconds, _smooth, dt, 0.065); - if (clipped) _reps.reset(); - phase = _reps.phase; + if (clipped) { + _squats.discardMovement(); + _smooth = movement = 0; + } + final count = + clipped ? 0 : _squats.add(s.seconds, _smooth, dt, angularSpeed, _up); + phase = _squats.phase; return ActivityUpdate(repetitions: count); } } @@ -236,54 +265,6 @@ class _StepDetector { } } -class _RepetitionDetector { - double _velocity = 0, _position = 0, _minimum = 0, _began = 0, _settle = 0; - int _state = 0; - String phase = 'Hold the start position'; - void reset() { - _velocity = _position = _minimum = _began = _settle = 0; - _state = 0; - phase = 'Hold the start position'; - } - - int add(double t, double a, double dt, double minDepth) { - // Bounded integration is a movement proxy, not a measurement of depth. - // Rebase each complete cycle to prevent unbounded displacement drift. - if (_settle < 0.7) { - _settle = a.abs() < 0.5 ? _settle + dt : 0; - return 0; - } - _velocity = _velocity * math.exp(-dt / 1.5) + a * dt; - _position = _position * math.exp(-dt / 4) + _velocity * dt; - _minimum = math.min(_minimum, _position); - if (_state == 0) { - phase = 'Ready · lower your body'; - if (_position < -minDepth * 0.4 && _velocity < -0.035) { - _state = 1; - _began = t; - phase = 'Lowering'; - } else if (a.abs() < 0.15 && _velocity.abs() < 0.025) { - _position = _minimum = 0; - } - } else if (t - _began > 8) { - reset(); - } else if (_state == 1 && _minimum <= -minDepth && _velocity > 0.025) { - _state = 2; - phase = 'Returning to start'; - } else if (_state == 2 && - _velocity <= 0 && - _position >= _minimum * 0.2 && - _position - _minimum >= minDepth * 0.8 && - t - _began >= 0.65) { - _state = 0; - _velocity = _position = _minimum = 0; - phase = 'Repetition complete'; - return 1; - } - return 0; - } -} - class _JumpDetector { double _still = 0, _armedAt = -100, diff --git a/open_wearable/lib/apps/activity_tracker/models/squat_detector.dart b/open_wearable/lib/apps/activity_tracker/models/squat_detector.dart new file mode 100644 index 000000000..40649e884 --- /dev/null +++ b/open_wearable/lib/apps/activity_tracker/models/squat_detector.dart @@ -0,0 +1,126 @@ +import 'dart:math' as math; +import 'activity_models.dart'; + +/// Counts complete down/up cycles in manually selected squat mode. +/// +/// Uses the engine's filtered vertical acceleration (m/s²), sensor time (s), +/// angular speed (rad/s) and estimated gravity direction. Integrated position +/// is a bounded motion proxy, not anatomical squat depth. The starting gravity +/// direction rejects large posture changes, but cannot identify leg movement. +class SquatDetector { + static const _minimumExcursion = 0.12; + double _velocity = 0, _position = 0, _minimum = 0; + double _referenceVelocity = 0, _referencePosition = 0, _referenceMinimum = 0; + double _began = 0, _returnPeak = 0, _quiet = 0, _baseline = 0; + int _state = 0; + bool _held = false; + MotionVector? _startUp; + + /// Current movement phase for the existing exercise screen. + String phase = 'Hold the start position'; + + /// Requires a new posture reference after mode changes, pauses or data gaps. + void reset() { + _startUp = null; + _baseline = 0; + discardMovement(); + phase = 'Hold the start position'; + } + + /// Drops an uncertain cycle without requiring a new set calibration. + void discardMovement() { + _velocity = _position = _minimum = 0; + _referenceVelocity = _referencePosition = _referenceMinimum = 0; + _began = _returnPeak = _quiet = 0; + _state = 0; + _held = false; + phase = 'Ready · lower your body'; + } + + /// Returns one after sufficient lowering and a substantial return near rest. + int add( + double time, + double acceleration, + double dt, + double angularSpeed, + MotionVector up, + ) { + _startUp ??= up; + // Retain the standing reference across rejected movements: push-up setup + // or bending must not silently become the new upright squat posture. + if (angularSpeed > 1.2 || _startUp!.dot(up) < 0.7) { + discardMovement(); + phase = 'Return to your standing position'; + return 0; + } + acceleration -= _baseline; + _quiet = acceleration.abs() < 0.02 && angularSpeed < 0.3 ? _quiet + dt : 0; + final previousVelocity = _velocity; + _velocity = _velocity * math.exp(-dt / 1.5) + acceleration * dt; + // Never let damping erase a held descent and manufacture a return. + if (_quiet < 0.25) { + _position = _position * math.exp(-dt / 4) + + (previousVelocity + _velocity) * 0.5 * dt; + } else { + _velocity = _referenceVelocity = 0; + if (_state != 0) _held = true; + } + if (_velocity.abs() > 2 || + _position.abs() > 1 || + (_state != 0 && time - _began > 6)) { + discardMovement(); + return 0; + } + if (_state == 0) { + phase = 'Ready · lower your body'; + // Correct residual gravity offset only at an idle, quiet starting pose. + if (acceleration.abs() < 0.08 && + angularSpeed < 0.3 && + _velocity.abs() < 0.025) { + _baseline += dt / (0.5 + dt) * acceleration; + _position = _minimum = 0; + } + if (_position < -_minimumExcursion * 0.4 && _velocity < -0.06) { + _state = 1; + _began = time; + _minimum = _position; + _referenceVelocity = _velocity; + _referencePosition = _referenceMinimum = _position; + phase = 'Lowering'; + } + return 0; + } + final previousReferenceVelocity = _referenceVelocity; + _referenceVelocity += acceleration * dt; + _referencePosition += + (previousReferenceVelocity + _referenceVelocity) * 0.5 * dt; + _referenceMinimum = math.min(_referenceMinimum, _referencePosition); + _minimum = math.min(_minimum, _position); + if (_state == 1) { + if (_minimum <= -_minimumExcursion && _velocity > 0.04) { + _state = 2; + _returnPeak = _velocity; + phase = 'Returning to standing'; + } + return 0; + } + _returnPeak = math.max(_returnPeak, _velocity); + final returned = _position >= _minimum * 0.3 && + _position - _minimum >= _minimumExcursion * 0.7 && + (!_held || _referencePosition >= _referenceMinimum * 0.3); + final nearTop = _velocity.abs() <= math.min(0.07, _returnPeak * 0.3); + if (returned && nearTop && time - _began >= 0.8) { + // Preserve residual velocity across continuous repetitions, but rebase + // position so a completed cycle cannot accumulate into the next one. + _position = _minimum = _referencePosition = _referenceMinimum = 0; + _referenceVelocity = 0; + _returnPeak = 0; + _held = false; + _state = 0; + phase = 'Repetition complete'; + return 1; + } + if (!returned && _velocity < -0.06) discardMovement(); + return 0; + } +} diff --git a/open_wearable/test/apps/activity_tracker/squat_detector_test.dart b/open_wearable/test/apps/activity_tracker/squat_detector_test.dart new file mode 100644 index 000000000..886b583c9 --- /dev/null +++ b/open_wearable/test/apps/activity_tracker/squat_detector_test.dart @@ -0,0 +1,161 @@ +import 'dart:math' as math; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_engine.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; + +// Independent rest-to-rest kinematics, not a model of participant accuracy. +class _SquatReplay { + _SquatReplay({this.dt = 0.01, this.up = const MotionVector(0, 0, 1)}); + final double dt; + final MotionVector up; + final engine = ActivityEngine()..select(ActivityKind.squats); + double time = 0; + int count = 0, steps = 0; + + void sample(double acceleration, {MotionVector gyro = MotionVector.zero}) { + time += dt; + final event = engine.add( + MotionSample( + time, + up * (ActivityEngine.gravity + acceleration), + gyroscope: gyro, + ), + ); + count += event.repetitions; + steps += event.steps; + } + + void hold(double seconds, {MotionVector gyro = MotionVector.zero}) { + for (var i = 0; i < (seconds / dt).round(); i++) { + sample(0, gyro: gyro); + } + } + + void move(double distance, double seconds) { + final n = (seconds / dt).round(); + final duration = n * dt; + for (var i = 0; i < n; i++) { + final u = (i + 0.5) / n; + sample( + distance / + (duration * duration) * + (60 * u - 180 * u * u + 120 * u * u * u), + ); + } + } + + void cycle(double depth, double period) { + move(-depth, period / 2); + move(depth, period / 2); + } +} + +void main() { + for (final period in [1.5, 2.4, 4.0]) { + for (final depth in [0.3, 0.45]) { + test('counts continuous $depth m cycles at $period s', () { + final r = _SquatReplay()..hold(2); + for (var i = 0; i < 6; i++) { + r.cycle(depth, period); + } + r.hold(1); + expect(r.count, 6); + expect(r.steps, 0); + }); + } + } + for (final dt in [0.02, 0.005]) { + test('supports ${(1 / dt).round()} Hz and rotated sensor mounting', () { + final r = _SquatReplay(dt: dt, up: const MotionVector(0, 1, 0))..hold(2); + for (var i = 0; i < 3; i++) { + r.cycle(0.35, 2.4); + } + r.hold(1); + expect(r.count, 3); + }); + } + test('small repeated head bobs do not accumulate into squats', () { + final r = _SquatReplay()..hold(2); + for (var i = 0; i < 10; i++) { + r.cycle(0.025, 1.5); + } + r.hold(2); + expect(r.count, 0); + }); + for (final returned in [0.0, 0.04, 0.1]) { + test('a held descent with $returned m return is not completed', () { + final r = _SquatReplay()..hold(2); + r.move(-0.35, 1.2); + r.hold(1); + r.move(returned, 1.2); + r.hold(4); + expect(r.count, 0); + }); + } + test('a complete cycle with a short bottom hold counts once', () { + final r = _SquatReplay()..hold(2); + r.move(-0.35, 1.2); + r.hold(0.5); + r.move(0.35, 1.2); + r.hold(1); + expect(r.count, 1); + }); + test('long sitting-like holds time out rather than count on standing', () { + final r = _SquatReplay()..hold(2); + r.move(-0.35, 1.2); + r.hold(7); + r.move(0.35, 1.2); + r.hold(2); + expect(r.count, 0); + }); + test('walking-sized fast cycles with head rotation are rejected', () { + final r = _SquatReplay()..hold(2); + for (var i = 0; i < 500; i++) { + r.sample( + 2.8 * math.sin(2 * math.pi * 2 * i / 100), + gyro: const MotionVector(0, 0, 1.5), + ); + } + r.hold(2); + expect(r.count, 0); + }); + for (final interruption in ['rotation', 'clipping', 'gap', 'mode', 'pause']) { + test('$interruption discards a partial squat and allows the next set', () { + final r = _SquatReplay()..hold(2); + r.move(-0.35, 1.2); + switch (interruption) { + case 'rotation': + r.sample(0, gyro: const MotionVector(0, 0, 2)); + case 'clipping': + r.sample(15); + case 'gap': + r.time += 0.3; + r.sample(0); + case 'mode': + r.engine.select(ActivityKind.pushUps); + r.engine.select(ActivityKind.squats); + case 'pause': + r.engine.recalibrate(); + } + r.move(0.35, 1.2); + r.hold(2); + expect(r.count, 0); + r.cycle(0.35, 2.4); + r.hold(1); + expect(r.count, 1); + }); + } + test('squat startup tolerates small yaw jitter, but not acceleration swings', + () { + final r = _SquatReplay(); + r.hold(1, gyro: const MotionVector(0, 0, 0.25)); + expect(r.engine.calibrated, isTrue); + r.engine.recalibrate(); + for (var i = 0; i < 200; i++) { + r.sample(i.isEven ? 0.4 : -0.4); + } + expect(r.engine.calibrated, isFalse); + r.hold(1); + expect(r.engine.calibrated, isTrue); + }); +} diff --git a/open_wearable/test/tool/squat_recordings_test.dart b/open_wearable/test/tool/squat_recordings_test.dart new file mode 100644 index 000000000..3fc07b5b8 --- /dev/null +++ b/open_wearable/test/tool/squat_recordings_test.dart @@ -0,0 +1,137 @@ +import 'dart:io'; +import 'dart:math' as math; + +import 'package:flutter_test/flutter_test.dart'; + +import '../../tool/squats/recordings.dart'; + +List _rows(List timestamps, {String vector = '1,2,3'}) => + parseSensorCsv('timestamp,X,Y,Z\n' + '${timestamps.map((time) => '$time,$vector').join('\n')}\n'); + +void main() { + test('subtracts the common microsecond origin before converting to seconds', + () { + const origin = 1794414169562002; + final joined = joinSensorRows( + _rows([origin + 1, origin + 10001]), + _rows([origin, origin + 10000]), + ); + expect(joined.samples[0].motion.seconds, closeTo(0.000001, 1e-12)); + expect(joined.samples[1].motion.seconds, closeTo(0.010001, 1e-12)); + expect(joined.durationSeconds, closeTo(0.010001, 1e-12)); + expect(joined.accelerometerSampleRateHz, closeTo(100, 1e-12)); + expect(joined.gyroscopeSampleRateHz, closeTo(100, 1e-12)); + expect(joined.samples[0].motion.acceleration.x, 1); + }); + + test('chooses nearest gyro, including future rows, with earlier ties', () { + final joined = joinSensorRows( + _rows([9000, 15000, 19000, 39000]), + _rows([0, 10000, 20000, 40000]), + ); + expect(joined.samples.map((sample) => sample.gyroscopeIndex), [1, 1, 2, 3]); + expect(joined.samples[0].gyroscopeOffsetSeconds, closeTo(0.001, 1e-12)); + expect(joined.samples[1].gyroscopeOffsetSeconds, closeTo(-0.005, 1e-12)); + expect(joined.unmatchedAccelerometerSamples, 0); + expect(joined.unmatchedGyroscopeSamples, 1); + }); + + test('converts assumed degrees per second to radians on all axes', () { + final joined = joinSensorRows( + _rows([0]), + _rows([0], vector: '180,-90,360'), + ); + final gyro = joined.samples.single.motion.gyroscope!; + expect(gyro.x, closeTo(math.pi, 1e-12)); + expect(gyro.y, closeTo(-math.pi / 2, 1e-12)); + expect(gyro.z, closeTo(2 * math.pi, 1e-12)); + expect(joined.accelerometerSampleRateHz, isNull); + }); + + test('explicit rad/s retains the original gyroscope vector', () { + final joined = joinSensorRows( + _rows([0]), + _rows([0], vector: '1,-2,3'), + gyroUnit: GyroscopeUnit.radiansPerSecond, + ); + final gyro = joined.samples.single.motion.gyroscope!; + expect([gyro.x, gyro.y, gyro.z], [1, -2, 3]); + }); + + test('20 ms is inclusive and missing gyro preserves every acceleration row', + () { + final joined = joinSensorRows( + _rows([0, 20000, 40000, 40001, 200000]), + _rows([20000, 200000, 300000]), + ); + expect(joined.samples, hasLength(5)); + expect( + joined.samples.map((sample) => sample.motion.seconds), + [0, 0.02, 0.04, 0.040001, 0.2], + ); + expect( + joined.samples.map((sample) => sample.gyroscopeIndex), + [0, 0, 0, null, 1], + ); + expect(joined.samples[3].motion.gyroscope, isNull); + expect(joined.samples[3].gyroscopeOffsetSeconds, isNull); + expect(joined.unmatchedAccelerometerSamples, 1); + expect(joined.unmatchedGyroscopeSamples, 1); + expect(joined.durationSeconds, 0.3); + }); + + test('completely nonoverlapping sensor times are explicit unmatched samples', + () { + final joined = joinSensorRows(_rows([0, 10000]), _rows([1000000])); + expect(joined.samples, hasLength(2)); + expect( + joined.samples.every((sample) => sample.motion.gyroscope == null), + isTrue, + ); + expect(joined.unmatchedAccelerometerSamples, 2); + expect(joined.unmatchedGyroscopeSamples, 1); + }); + + test('accepts BOM, CRLF and surrounding numeric whitespace', () { + final rows = parseSensorCsv('\uFEFFtimestamp,X,Y,Z\r\n1, 2, 3, 4\r\n'); + expect(rows.single.timestampMicros, 1); + expect(rows.single.vector.x, 2); + }); + + for (final csv in [ + '', + 'timestamp,X,Y,Z\n', + 'time,X,Y,Z\n1,2,3,4\n', + 'timestamp,X,Y,Z\n1,2,3\n', + 'timestamp,X,Y,Z\n1.5,2,3,4\n', + 'timestamp,X,Y,Z\n1,NaN,3,4\n', + 'timestamp,X,Y,Z\n1,2,Infinity,4\n', + 'timestamp,X,Y,Z\n1,2,3,4\n1,2,3,4\n', + 'timestamp,X,Y,Z\n2,2,3,4\n1,2,3,4\n', + ]) { + test('rejects malformed CSV: ${csv.replaceAll('\n', r'\n')}', () { + expect(() => parseSensorCsv(csv), throwsFormatException); + }); + } + + test('empty gyro data cannot be replaced with synthetic samples', () { + expect(() => joinSensorRows(_rows([0]), []), throwsFormatException); + }); + + test('missing gyro file fails rather than supplying a zero vector', () async { + final root = await Directory.systemTemp.createTemp('squat-recording-test-'); + addTearDown(() => root.delete(recursive: true)); + final directory = + await Directory.fromUri(root.uri.resolve('squat-01/')).create(); + await File.fromUri(directory.uri.resolve('accelerometer.csv')) + .writeAsString('timestamp,X,Y,Z\n1,0,0,9.80665\n'); + await expectLater( + loadRecording( + root, + const RecordingDefinition(id: 'squat-01', expectedSquats: 10), + ), + throwsA(isA()), + ); + }); +} diff --git a/open_wearable/tool/squats/README.md b/open_wearable/tool/squats/README.md new file mode 100644 index 000000000..455c0eeea --- /dev/null +++ b/open_wearable/tool/squats/README.md @@ -0,0 +1,120 @@ +# Kniebeugen: lokale Entwicklung und Replay + +Aus `open_wearable/`: + +```bash +dart run tool/squats/replay.dart +# Optional: anderer Rohdatenordner, darunter dieselben anonymen IDs +dart run tool/squats/replay.dart /pfad/zu/lokalen/daten +# Nur wenn das Exportformat tatsächlich rad/s liefert: +dart run tool/squats/replay.dart --gyro-unit=rad/s +# Anonymisierte Replay-Ergebnisse aktualisieren: +dart run tool/squats/replay.dart > tool/squats/results.json +flutter test test/tool/squat_recordings_test.dart +``` + +`manifest.json` hält stabile, anonyme Aufnahme-IDs, ursprüngliche relative +Ordnernamen und die dort genannte Aktivitätsreihenfolge fest. Der letzte +Originalordnername hat ein abschließendes Leerzeichen; es ist im Manifest +absichtlich erhalten. Lokal liegen pro ID ausschließlich `accelerometer.csv` +und `gyroscope.csv` unter `data//`. Die CSVs wurden unverändert kopiert. +Ein anderer Datenordner muss diese Struktur verwenden; Originalordner mit +Gerätepräfixen werden nicht automatisch gesucht. + +| ID | Reihenfolge laut Ordnername | Erwartete Kniebeugen | +|---|---|---:| +| squat-01 | 10 Kniebeugen | 10 | +| squat-02 | 5 Kniebeugen, Gehen, 5 Kniebeugen | 10 | +| squat-03 | 5 Kniebeugen, Hinsetzen, 5 Kniebeugen | 10 | +| squat-04 | 5 Kniebeugen, 3 Liegestütze, 5 Kniebeugen | 10 | +| squat-05 | 5 Kniebeugen, Treppen und Gehen, 5 Kniebeugen, Hinsetzen, 5 Kniebeugen, 3 Liegestütze | 15 | + +Die Ordnernamen beschreiben **geordnete Aktivitätsfolgen**, nicht durchgehend +Kniebeugen. Gehen, Treppen, Hinsetzen und Liegestütze bleiben vollständig im +Replay und dienen als Negativkontrollen für den manuell gewählten Squat-Modus. + +## Aussagegrenzen + +Die Sollzahlen (insgesamt 55) stammen **nur aus den Ordnernamen**, nicht aus +unabhängig überprüfter zeitlicher Ground Truth. Es gibt keine annotierten +Start-/Endzeiten einzelner Übungen oder Wiederholungen. „Treppen und Gehen“ +ist daher ein gemeinsamer Abschnitt ohne gesicherte interne Reihenfolge. +Personenzahl, Personenidentität, tatsächliches Messdatum, genaue Ausführung, +Referenzmessung und Gerätekonfiguration sind unbekannt. Aus dem Downloadnamen +oder den Gerätezeitstempeln wird kein Messdatum abgeleitet. Die im Replay +berechneten Sensorraten sind beobachtete Durchschnittsraten einschließlich +Lücken, keine zugesicherten Konfigurationswerte. Übereinstimmende Gesamtzahlen +belegen weder korrekte Ereigniszuordnung noch personenunabhängige Genauigkeit. +Ohne zeitliche Annotationen lassen sich weder ereignisspezifische False +Positives/False Negatives noch Erkennungsgenauigkeit bestimmen; auch Fehler +in unterschiedlichen Abschnitten können sich in der Gesamtzahl aufheben. +Diese Aufnahmen sind Entwicklungsdaten, kein unabhängiger Validierungssatz. + +## Beobachtete Ausgangsbasis vor der Squat-Überarbeitung + +Die folgenden Gesamtzahlen wurden mit der bisherigen Engine im vollständigen +Replay beobachtet und vom Integrationslauf übernommen; sie sind keine Ergebnisse +des überarbeiteten Detektors und wurden hier nicht erneut ausgeführt. + +| ID | Soll laut Ordnername | Bisher erkannt | Differenz erkannt − Soll | +|---|---:|---:|---:| +| squat-01 | 10 | 0 | −10 | +| squat-02 (Gehen) | 10 | 0 | −10 | +| squat-03 (Hinsetzen) | 10 | 4 | −6 | +| squat-04 (Liegestütze) | 10 | 0 | −10 | +| squat-05 (lange Mischfolge) | 15 | 7 | −8 | + +Bei drei der fünf Aufnahmen wurde die bisherige Engine nie kalibriert; in der +langen Mischfolge wurden zwei Datenlücken beobachtet. Diese Beobachtungen +motivieren eine Squat-spezifische Startkalibrierung, ohne das Startverhalten +anderer Modi zu verändern. Ein Replay darf die Kalibrierungsprobleme nicht durch +künstliche Stilldaten oder Wiederherstellung einer Kalibrierung verdecken. +Auch die Ausgangszahlen erlauben keine Zuordnung einzelner Ereignisse zu +Kniebeugen oder Negativkontrollen. Neue Ergebnisse entstehen ausschließlich +durch den oben angegebenen Lauf mit der tatsächlich überarbeiteten Engine. + +## Verarbeitung und Ergebnisfelder + +- Header: `timestamp,X,Y,Z`; Zeitstempel werden als Unix-Mikrosekunden gelesen, + Beschleunigung als m/s². Die Gyroeinheit steht nicht im CSV: **deg/s ist eine + Annahme**, standardmäßig erfolgt die Umrechnung zu rad/s; explizit + `--gyro-unit=deg/s` oder `--gyro-unit=rad/s` wählen. Keine automatische + Einheitenheuristik, um stilles Verfälschen des Signals zu vermeiden. +- Sessionnull ist der früheste Zeitstempel beider Sensoren. Erst werden die + ganzzahligen Mikrosekunden subtrahiert, dann relative Sekunden gebildet. +- Für jeden Beschleunigungspunkt wird der zeitlich nächste Gyropunkt innerhalb + von einschließlich 20 ms gewählt, wie im Live-Datenpfad; bei Gleichstand der + frühere. Gyropunkte + dürfen wie im Live-Puffer mehrfach verwendet werden; nicht gewählte Gyropunkte + zählen als `unmatchedGyroscope`. Keine Interpolation oder künstlichen Werte. +- Jeder Beschleunigungspunkt bleibt erhalten. Fehlendes passendes Gyro ist + explizit `null`; das Replay setzt den echten `ActivityEngine` zurück, statt + Nullgeschwindigkeit anzunehmen oder kurze Unterbrechungen herauszufiltern. + `unmatchedGyroscopeInterruptionSpans` zählt zusammenhängende solche Lücken. + Größere Beschleunigungszeitlücken behandelt zusätzlich die Engine selbst. +- Der echte `ActivityEngine` läuft vom ersten Beschleunigungspunkt an im Modus + `squats`, auch während Gehen, Hinsetzen und Liegestützen. Keine künstliche + Stillphase, wiederhergestellte Kalibrierung oder nachträgliche Segmentauswahl. +- JSON enthält pro Aufnahme `expected`, `detected` und den vorzeichenbehafteten + `error = detected - expected`, Sessiondauer, Sensorraten, Punktzahlen und + nicht zugeordnete Punkte. `calibratedSampleFraction` ist der Anteil aller + Beschleunigungspunkte, nach deren Verarbeitung die Engine kalibriert war + (fehlendes Gyro zählt unkalibriert). `firstCalibrationSeconds` ist die erste + erfolgreiche Kalibrierung relativ zur Sessionnull, sonst `null`. + `detectedEventSeconds` sind Bestätigungszeiten der Engine, keine annotierten + Ground-Truth-Zeiten. Es gibt keinen erzwungenen Abschluss am Aufnahmeende. +- Fehlende/unlesbare Dateien, falsche Header, ungültige/nicht endliche Zahlen, + leere Sensorreihen und nicht streng steigende Zeitstempel ergeben Fehler auf + stderr und Exitcode 1. Ungültige CLI-Argumente ergeben 64. stdout enthält nur + erfolgreich geladene Aufnahmen; unvollständige Ergebnisse nicht als vollständige + Evaluation verwenden. Zähldifferenzen allein ändern den Exitcode nicht. + +## Datenschutz + +Das Repository ist öffentlich: `data/` und die ursprünglichen Rohaufnahmen +bleiben ausschließlich lokal und dürfen **nicht eingecheckt werden**. Auch +absolute Zeitstempel und Hardware-IDs gehören nicht in Ergebnisse. Manifest +und `results.json` enthalten nur anonyme Metadaten bzw. abgeleitete Kennzahlen +und relative Ereigniszeiten, keine Rohsensorreihen. Der dokumentierte Replay +wurde am 17.11.2026 ausgeführt. Ergebnisse, Begründungen und verbleibende +Zählfehler stehen im [Kniebeugenbericht](../../../docs/algorithmen/kniebeugen.md). diff --git a/open_wearable/tool/squats/manifest.json b/open_wearable/tool/squats/manifest.json new file mode 100644 index 000000000..3810eb1ab --- /dev/null +++ b/open_wearable/tool/squats/manifest.json @@ -0,0 +1,57 @@ +{ + "schemaVersion": 1, + "expectedCountsSource": "original_folder_names_only", + "participants": null, + "measurementDate": null, + "recordings": [ + { + "id": "squat-01", + "sourceFolder": "10 Squats", + "expectedSquats": 10, + "activities": [{"activity": "squats", "count": 10}] + }, + { + "id": "squat-02", + "sourceFolder": "5 Squats, Gehen, 5 squats", + "expectedSquats": 10, + "activities": [ + {"activity": "squats", "count": 5}, + {"activity": "walking"}, + {"activity": "squats", "count": 5} + ] + }, + { + "id": "squat-03", + "sourceFolder": "5 Squats, hinsetzten, 5 squats", + "expectedSquats": 10, + "activities": [ + {"activity": "squats", "count": 5}, + {"activity": "sitting_down"}, + {"activity": "squats", "count": 5} + ] + }, + { + "id": "squat-04", + "sourceFolder": "5 Squats, 3 push-ups, 5 squats", + "expectedSquats": 10, + "activities": [ + {"activity": "squats", "count": 5}, + {"activity": "push_ups", "count": 3}, + {"activity": "squats", "count": 5} + ] + }, + { + "id": "squat-05", + "sourceFolder": "5 Squats, Treppen & Gehen, 5 Squats, hinsetzten, 5 squats, 3 push-ups ", + "expectedSquats": 15, + "activities": [ + {"activity": "squats", "count": 5}, + {"activity": "stairs_and_walking"}, + {"activity": "squats", "count": 5}, + {"activity": "sitting_down"}, + {"activity": "squats", "count": 5}, + {"activity": "push_ups", "count": 3} + ] + } + ] +} diff --git a/open_wearable/tool/squats/recordings.dart b/open_wearable/tool/squats/recordings.dart new file mode 100644 index 000000000..63aeea32b --- /dev/null +++ b/open_wearable/tool/squats/recordings.dart @@ -0,0 +1,231 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:math' as math; + +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; + +/// The CSV export does not declare its gyroscope unit. +enum GyroscopeUnit { + degreesPerSecond('deg/s', math.pi / 180), + radiansPerSecond('rad/s', 1); + + const GyroscopeUnit(this.label, this.toRadiansFactor); + final String label; + final double toRadiansFactor; +} + +/// One unmodified sensor timestamp and vector, before synchronization. +class SensorRow { + const SensorRow(this.timestampMicros, this.vector); + + final int timestampMicros; + final MotionVector vector; +} + +/// Parses the numeric four-column export; malformed rows are never discarded. +List parseSensorCsv(String contents, {String source = 'CSV'}) { + final lines = const LineSplitter().convert(contents); + if (lines.isEmpty) throw FormatException('$source: empty CSV'); + final header = lines.first.replaceFirst('\uFEFF', '').split(','); + if (header.length != 4 || + header.map((cell) => cell.trim()).join(',') != 'timestamp,X,Y,Z') { + throw FormatException('$source: expected header timestamp,X,Y,Z'); + } + final rows = []; + for (var line = 1; line < lines.length; line++) { + if (lines[line].trim().isEmpty) continue; + final cells = lines[line].split(','); + if (cells.length != 4) { + throw FormatException('$source:${line + 1}: expected four columns'); + } + final timestamp = int.tryParse(cells[0].trim()); + final x = double.tryParse(cells[1].trim()); + final y = double.tryParse(cells[2].trim()); + final z = double.tryParse(cells[3].trim()); + if (timestamp == null || + x == null || + y == null || + z == null || + !x.isFinite || + !y.isFinite || + !z.isFinite) { + throw FormatException('$source:${line + 1}: invalid timestamp or vector'); + } + if (rows.isNotEmpty && timestamp <= rows.last.timestampMicros) { + throw FormatException('$source:${line + 1}: timestamps must increase'); + } + rows.add(SensorRow(timestamp, MotionVector(x, y, z))); + } + if (rows.isEmpty) throw FormatException('$source: no sensor samples'); + return rows; +} + +/// Keeps every acceleration packet, even when no usable gyroscope exists. +class JoinedSample { + const JoinedSample( + this.motion, + this.gyroscopeIndex, + this.gyroscopeOffsetSeconds, + ); + + final MotionSample motion; + final int? gyroscopeIndex; + + /// Signed gyroscope timestamp minus acceleration timestamp. + final double? gyroscopeOffsetSeconds; +} + +class JoinedRecording { + const JoinedRecording({ + required this.samples, + required this.durationSeconds, + required this.accelerometerSampleRateHz, + required this.gyroscopeSampleRateHz, + required this.gyroscopeSamples, + required this.unmatchedAccelerometerSamples, + required this.unmatchedGyroscopeSamples, + }); + + final List samples; + final double durationSeconds; + final double? accelerometerSampleRateHz; + final double? gyroscopeSampleRateHz; + final int gyroscopeSamples; + final int unmatchedAccelerometerSamples; + final int unmatchedGyroscopeSamples; +} + +double? _sampleRate(List rows) => rows.length < 2 + ? null + : (rows.length - 1) * + 1000000 / + (rows.last.timestampMicros - rows.first.timestampMicros); + +/// Nearest-time join within an inclusive 20 ms window, with earlier-time ties. +/// +/// Like the live buffer, a gyro row can serve multiple acceleration packets. +/// Unmatched gyro rows are rows never selected, not a one-to-one count delta. +/// Session zero is the earliest timestamp of either sensor. Subtract integers +/// before conversion, retaining microsecond precision for epoch timestamps. +JoinedRecording joinSensorRows( + List accelerometer, + List gyroscope, { + GyroscopeUnit gyroUnit = GyroscopeUnit.degreesPerSecond, +}) { + if (accelerometer.isEmpty || gyroscope.isEmpty) { + throw const FormatException('Both sensors require at least one sample'); + } + final origin = math.min( + accelerometer.first.timestampMicros, + gyroscope.first.timestampMicros, + ); + final end = math.max( + accelerometer.last.timestampMicros, + gyroscope.last.timestampMicros, + ); + final samples = []; + final usedGyros = List.filled(gyroscope.length, false); + var gyroIndex = 0; + var convertedGyroIndex = -1; + MotionVector? convertedGyro; + var unmatchedAccelerometer = 0; + for (final acceleration in accelerometer) { + final timestamp = acceleration.timestampMicros; + while (gyroIndex + 1 < gyroscope.length && + (gyroscope[gyroIndex + 1].timestampMicros - timestamp).abs() < + (gyroscope[gyroIndex].timestampMicros - timestamp).abs()) { + gyroIndex++; + } + final offset = gyroscope[gyroIndex].timestampMicros - timestamp; + final matched = offset.abs() <= 20000; + if (matched) { + usedGyros[gyroIndex] = true; + if (convertedGyroIndex != gyroIndex) { + final vector = gyroscope[gyroIndex].vector; + convertedGyro = gyroUnit == GyroscopeUnit.radiansPerSecond + ? vector + : vector * gyroUnit.toRadiansFactor; + convertedGyroIndex = gyroIndex; + } + } else { + unmatchedAccelerometer++; + } + samples.add( + JoinedSample( + MotionSample( + (timestamp - origin) / 1000000, + acceleration.vector, + gyroscope: matched ? convertedGyro : null, + ), + matched ? gyroIndex : null, + matched ? offset / 1000000 : null, + ), + ); + } + return JoinedRecording( + samples: samples, + durationSeconds: (end - origin) / 1000000, + accelerometerSampleRateHz: _sampleRate(accelerometer), + gyroscopeSampleRateHz: _sampleRate(gyroscope), + gyroscopeSamples: gyroscope.length, + unmatchedAccelerometerSamples: unmatchedAccelerometer, + unmatchedGyroscopeSamples: usedGyros.where((used) => !used).length, + ); +} + +/// Anonymous manifest entry; source folders and activities remain metadata. +class RecordingDefinition { + const RecordingDefinition({required this.id, required this.expectedSquats}); + + final String id; + final int expectedSquats; +} + +Future> loadManifest(File file) async { + final decoded = jsonDecode(await file.readAsString()); + if (decoded is! Map || + decoded['schemaVersion'] != 1 || + decoded['recordings'] is! List) { + throw const FormatException('Unsupported squat manifest'); + } + final definitions = []; + final ids = {}; + for (final entry in decoded['recordings'] as List) { + if (entry is! Map) { + throw const FormatException('Invalid recording manifest entry'); + } + final id = entry['id']; + final expected = entry['expectedSquats']; + if (id is! String || + !RegExp(r'^squat-\d{2}$').hasMatch(id) || + expected is! int || + expected < 0 || + !ids.add(id)) { + throw const FormatException('Invalid recording ID or expected count'); + } + definitions.add(RecordingDefinition(id: id, expectedSquats: expected)); + } + if (definitions.isEmpty) throw const FormatException('Empty squat manifest'); + return definitions; +} + +/// Loads only accelerometer.csv and gyroscope.csv under the anonymous ID. +Future loadRecording( + Directory dataRoot, + RecordingDefinition definition, { + GyroscopeUnit gyroUnit = GyroscopeUnit.degreesPerSecond, +}) async { + final directory = Directory.fromUri( + dataRoot.absolute.uri.resolve('${definition.id}/'), + ); + final acceleration = parseSensorCsv( + await File.fromUri(directory.uri.resolve('accelerometer.csv')) + .readAsString(), + source: '${definition.id}/accelerometer.csv', + ); + final gyroscope = parseSensorCsv( + await File.fromUri(directory.uri.resolve('gyroscope.csv')).readAsString(), + source: '${definition.id}/gyroscope.csv', + ); + return joinSensorRows(acceleration, gyroscope, gyroUnit: gyroUnit); +} diff --git a/open_wearable/tool/squats/replay.dart b/open_wearable/tool/squats/replay.dart new file mode 100644 index 000000000..68f67e6b0 --- /dev/null +++ b/open_wearable/tool/squats/replay.dart @@ -0,0 +1,135 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:open_wearable/apps/activity_tracker/models/activity_engine.dart'; +import 'package:open_wearable/apps/activity_tracker/models/activity_models.dart'; +import 'recordings.dart'; + +const _usage = 'Usage: dart run tool/squats/replay.dart ' + '[data-root] [--gyro-unit=deg/s|rad/s]'; + +/// Replays the entire recording in squat mode, with no calibration restoration, +/// synthetic pre-roll, activity-boundary labels, or omitted acceleration rows. +Map replayRecording( + RecordingDefinition definition, + JoinedRecording recording, { + GyroscopeUnit gyroUnit = GyroscopeUnit.degreesPerSecond, +}) { + final engine = ActivityEngine()..select(ActivityKind.squats); + final events = []; + var calibratedSamples = 0; + var interruptionCount = 0; + var interrupted = false; + double? firstCalibrationSeconds; + for (final sample in recording.samples) { + if (sample.motion.gyroscope == null) { + // Null gyro is not zero angular speed. Invalidate this explicit packet, + // including short holes that the engine's timestamp-gap test would miss. + engine.recalibrate(); + if (!interrupted) interruptionCount++; + interrupted = true; + continue; + } + interrupted = false; + final update = engine.add(sample.motion); + if (engine.calibrated) { + calibratedSamples++; + firstCalibrationSeconds ??= sample.motion.seconds; + } + for (var repetition = 0; repetition < update.repetitions; repetition++) { + events.add(sample.motion.seconds); + } + } + return { + 'id': definition.id, + 'expected': definition.expectedSquats, + 'detected': events.length, + 'error': events.length - definition.expectedSquats, + 'durationSeconds': recording.durationSeconds, + 'gyroInputUnit': gyroUnit.label, + 'samples': { + 'accelerometer': recording.samples.length, + 'gyroscope': recording.gyroscopeSamples, + 'matchedAccelerometer': + recording.samples.length - recording.unmatchedAccelerometerSamples, + 'unmatchedAccelerometer': recording.unmatchedAccelerometerSamples, + 'unmatchedGyroscope': recording.unmatchedGyroscopeSamples, + 'unmatchedGyroscopeInterruptionSpans': interruptionCount, + }, + 'sampleRatesHz': { + 'accelerometer': recording.accelerometerSampleRateHz, + 'gyroscope': recording.gyroscopeSampleRateHz, + }, + 'calibration': { + 'calibratedSampleFraction': calibratedSamples / recording.samples.length, + 'firstCalibrationSeconds': firstCalibrationSeconds, + 'calibratedAtEnd': engine.calibrated, + }, + 'detectedEventSeconds': events, + }; +} + +Future main(List arguments) async { + String? dataPath; + var gyroUnit = GyroscopeUnit.degreesPerSecond; + for (final argument in arguments) { + if (argument == '--help' || argument == '-h') { + stdout.writeln(_usage); + return; + } + if (argument.startsWith('--gyro-unit=')) { + final label = argument.substring('--gyro-unit='.length); + switch (label) { + case 'deg/s': + gyroUnit = GyroscopeUnit.degreesPerSecond; + case 'rad/s': + gyroUnit = GyroscopeUnit.radiansPerSecond; + default: + stderr.writeln('Unsupported gyroscope unit: $label\n$_usage'); + exitCode = 64; + return; + } + } else if (argument.startsWith('-') || dataPath != null) { + stderr.writeln(_usage); + exitCode = 64; + return; + } else { + dataPath = argument; + } + } + + final dataRoot = dataPath == null + ? Directory.fromUri(Platform.script.resolve('data/')) + : Directory(dataPath); + final results = >[]; + try { + final definitions = await loadManifest( + File.fromUri(Platform.script.resolve('manifest.json')), + ); + for (final definition in definitions) { + try { + final recording = await loadRecording( + dataRoot, + definition, + gyroUnit: gyroUnit, + ); + results.add(replayRecording(definition, recording, gyroUnit: gyroUnit)); + } on FileSystemException catch (error) { + stderr.writeln('${definition.id}: missing or unreadable data: $error'); + exitCode = 1; + } on FormatException catch (error) { + stderr.writeln('${definition.id}: malformed data: $error'); + exitCode = 1; + } + } + } on FileSystemException catch (error) { + stderr.writeln('Missing or unreadable manifest: $error'); + exitCode = 1; + } on FormatException catch (error) { + stderr.writeln('Malformed manifest: $error'); + exitCode = 1; + } + // Count mismatches are evaluation results, not process failures. When data + // fails, stdout contains only valid recordings and the process exits nonzero. + stdout.writeln(const JsonEncoder.withIndent(' ').convert(results)); +} diff --git a/open_wearable/tool/squats/results.json b/open_wearable/tool/squats/results.json new file mode 100644 index 000000000..7823a7ce9 --- /dev/null +++ b/open_wearable/tool/squats/results.json @@ -0,0 +1,180 @@ +[ + { + "id": "squat-01", + "expected": 10, + "detected": 10, + "error": 0, + "durationSeconds": 22.812256, + "gyroInputUnit": "deg/s", + "samples": { + "accelerometer": 2280, + "gyroscope": 2276, + "matchedAccelerometer": 2277, + "unmatchedAccelerometer": 3, + "unmatchedGyroscope": 0, + "unmatchedGyroscopeInterruptionSpans": 1 + }, + "sampleRatesHz": { + "accelerometer": 99.9024384085467, + "gyroscope": 99.90243813327442 + }, + "calibration": { + "calibratedSampleFraction": 0.968421052631579, + "firstCalibrationSeconds": 0.720703, + "calibratedAtEnd": true + }, + "detectedEventSeconds": [ + 3.87378, + 5.845673, + 8.057862, + 10.28003, + 12.332032, + 14.333985, + 16.345948, + 18.297852, + 20.239746, + 22.171631 + ] + }, + { + "id": "squat-02", + "expected": 10, + "detected": 9, + "error": -1, + "durationSeconds": 31.470764, + "gyroInputUnit": "deg/s", + "samples": { + "accelerometer": 3145, + "gyroscope": 3140, + "matchedAccelerometer": 3141, + "unmatchedAccelerometer": 4, + "unmatchedGyroscope": 0, + "unmatchedGyroscopeInterruptionSpans": 1 + }, + "sampleRatesHz": { + "accelerometer": 99.90224577960674, + "gyroscope": 99.90224283876917 + }, + "calibration": { + "calibratedSampleFraction": 0.9767885532591415, + "firstCalibrationSeconds": 0.730712, + "calibratedAtEnd": true + }, + "detectedEventSeconds": [ + 3.913818, + 5.825714, + 7.69754, + 9.639434, + 23.342803, + 25.034484, + 26.616027, + 28.337707, + 30.169494 + ] + }, + { + "id": "squat-03", + "expected": 10, + "detected": 9, + "error": -1, + "durationSeconds": 30.639923, + "gyroInputUnit": "deg/s", + "samples": { + "accelerometer": 3062, + "gyroscope": 3058, + "matchedAccelerometer": 3060, + "unmatchedAccelerometer": 2, + "unmatchedGyroscope": 0, + "unmatchedGyroscopeInterruptionSpans": 1 + }, + "sampleRatesHz": { + "accelerometer": 99.90233983290362, + "gyroscope": 99.90233949906477 + }, + "calibration": { + "calibratedSampleFraction": 0.9768125408229915, + "firstCalibrationSeconds": 0.710693, + "calibratedAtEnd": true + }, + "detectedEventSeconds": [ + 3.713623, + 5.685547, + 7.787598, + 9.759521, + 22.942413, + 24.644073, + 26.295685, + 27.957306, + 29.618927 + ] + }, + { + "id": "squat-04", + "expected": 10, + "detected": 7, + "error": -3, + "durationSeconds": 36.355499, + "gyroInputUnit": "deg/s", + "samples": { + "accelerometer": 3633, + "gyroscope": 3630, + "matchedAccelerometer": 3631, + "unmatchedAccelerometer": 2, + "unmatchedGyroscope": 0, + "unmatchedGyroscopeInterruptionSpans": 1 + }, + "sampleRatesHz": { + "accelerometer": 99.90235589944729, + "gyroscope": 99.90235776446548 + }, + "calibration": { + "calibratedSampleFraction": 0.9504541701073493, + "firstCalibrationSeconds": 1.801758, + "calibratedAtEnd": true + }, + "detectedEventSeconds": [ + 4.124024, + 6.166016, + 8.248047, + 10.470215, + 29.238556, + 31.150421, + 32.972199 + ] + }, + { + "id": "squat-05", + "expected": 15, + "detected": 8, + "error": -7, + "durationSeconds": 103.978913, + "gyroInputUnit": "deg/s", + "samples": { + "accelerometer": 8454, + "gyroscope": 8452, + "matchedAccelerometer": 8453, + "unmatchedAccelerometer": 1, + "unmatchedGyroscope": 0, + "unmatchedGyroscopeInterruptionSpans": 1 + }, + "sampleRatesHz": { + "accelerometer": 81.29532956360103, + "gyroscope": 81.2917467291615 + }, + "calibration": { + "calibratedSampleFraction": 0.7124438135793707, + "firstCalibrationSeconds": 43.700105, + "calibratedAtEnd": true + }, + "detectedEventSeconds": [ + 45.892243, + 47.694001, + 49.565827, + 51.417634, + 81.316804, + 96.771882, + 98.293367, + 100.045076 + ] + } +]