Skip to content

steami_config: Add accelerometer calibration storage.#323

Merged
nedseb merged 15 commits intomainfrom
feat/add-persistent-config-ism330dl
Apr 1, 2026
Merged

steami_config: Add accelerometer calibration storage.#323
nedseb merged 15 commits intomainfrom
feat/add-persistent-config-ism330dl

Conversation

@Charly-sketch
Copy link
Copy Markdown
Contributor

@Charly-sketch Charly-sketch commented Mar 30, 2026

Summary

Add support for persistent accelerometer bias calibration (XYZ) for the ISM330DL.
This PR introduces helpers to store, retrieve, and apply accelerometer offsets using the config zone.

Closes #171
Part of #166
Depends on #167


Changes

  • Add accelerometer calibration storage in steami_config

    • set_accelerometer_calibration(ox, oy, oz)
    • get_accelerometer_calibration()
    • apply_accelerometer_calibration(sensor)
  • Store calibration in config zone using JSON key cal_accel

  • Add accelerometer offset support in ISM330DL

    • _accel_offset_x/y/z attributes
    • set_accel_offset() and get_accel_offset()
    • offsets applied in acceleration_g() (propagates to ms2 and orientation)
  • Add example: calibrate_accelerometer.py

    • compute offsets from static measurement
    • persist calibration
    • reload and verify behavior
  • Add mock tests for:

    • set/get/apply accelerometer calibration
    • persistence across save/load
  • Add hardware tests for:

    • config zone persistence
    • application to real ISM330DL
  • Update README:

    • steami_config: new "Accelerometer Calibration" section + JSON format
    • ism330dl: accelerometer offset support documentation

Checklist

  • ruff check passes
  • python -m pytest tests/ -k mock -v passes (no mock test broken)
  • Tested on hardware (if applicable)
  • README updated (if adding/changing public API)
  • Examples added/updated (if applicable)
  • Commit messages follow <scope>: <Description.> format

Test Results

'steami_config' tests

=== 45 passed in 80.43s (0:01:20) ===

'ism330dl' tests

=== 61 passed in 45.20s ===

@Charly-sketch Charly-sketch changed the title WIP | steami_config: Add accelerometer calibration storage. steami_config: Add accelerometer calibration storage. Mar 30, 2026
@Charly-sketch Charly-sketch requested a review from nedseb March 30, 2026 13:01
@Charly-sketch Charly-sketch marked this pull request as ready for review March 30, 2026 13:01
@nedseb nedseb requested a review from Copilot March 30, 2026 14:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds persistent accelerometer bias calibration support for the STeaMi config zone and integrates per-axis accelerometer offset correction into the ISM330DL driver, with accompanying examples, docs, and scenario tests (mock + hardware).

Changes:

  • Add SteamiConfig helpers to store/load/apply accelerometer bias offsets under JSON key cal_accel.
  • Add accelerometer offset support to ISM330DL and apply offsets in acceleration_g() (propagating to ms2 and orientation()).
  • Add scenario tests plus a calibration example and documentation updates.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/scenarios/steami_config.yaml Adds mock + hardware scenario coverage for accelerometer calibration persistence and application.
tests/scenarios/ism330dl.yaml Adds mock + hardware scenario coverage for accel offset getters/setters and offset effects on readings/orientation.
lib/steami_config/steami_config/device.py Implements set/get/apply_accelerometer_calibration() storing cal_accel in config JSON.
lib/steami_config/examples/calibrate_accelerometer.py New example to compute, persist, reload, and verify accelerometer bias offsets.
lib/steami_config/README.md Documents accelerometer calibration API and JSON schema update.
lib/ism330dl/ism330dl/device.py Adds _accel_offset_* state and set/get_accel_offset(), applying offsets in acceleration_g().
lib/ism330dl/README.md Documents accelerometer offset correction and how to apply persistent calibration via steami_config.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@nedseb nedseb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La PR est bien structurée : driver, config, exemple, tests mock + hardware, et READMEs mis à jour. Quelques retours ci-dessous.


✅ Points positifs

  • Pattern cohérent avec la calibration magnétomètre existante (set_*, get_*, apply_*) — bonne approche
  • Tests complets : mock + hardware + coexistence avec les autres calibrations (le test "coexists with temperature calibration" est excellent)
  • Exemple bien pensé : mesure → calcul offset → sauvegarde → reload → vérification
  • Propagation correcte : les offsets dans acceleration_g() se propagent à acceleration_ms2() et orientation()

📝 À corriger

1. README steami_config : code Python cassé dans le bloc (ligne 132)

Copilot l'a déjà signalé : dans la section "Store calibration", il y a du texte brut (Read calibration) à l'intérieur du bloc de code Python. Ça rend le copier-coller invalide. Il faut soit séparer en deux blocs, soit commenter :

### Store calibration

\`\`\`python
config.set_accelerometer_calibration(ox=0.01, oy=-0.02, oz=0.03)
\`\`\`

### Read calibration

\`\`\`python
cal = config.get_accelerometer_calibration()
# -> {"ox": 0.01, "oy": -0.02, "oz": 0.03}   or   None
\`\`\`

2. Ligne vide en trop dans device.py (steami_config)

Il y a une double ligne vide après set_accelerometer_calibration() (après la fermeture du dict self._data["cal_accel"]). Une seule suffit entre les méthodes — c'est la convention du reste du fichier.

3. Clé JSON : cal_accel vs convention existante

La calibration magnétomètre utilise "cm" (clé courte) et les offsets sont "hx", "hy", "hz", "sx", "sy", "sz". La calibration température utilise "tc" avec "g" et "o".

La nouvelle calibration utilise "cal_accel" avec "ox", "oy", "oz" — c'est plus long et inconsistant avec le pattern existant de clés courtes. Le JSON doit tenir dans 1 KB (config zone). Je suggère :

# Au lieu de "cal_accel" → "ca" (calibration accelerometer)
# Cohérent avec "cm" (calibration magnetometer) et "tc" (temperature calibration)
self._data["ca"] = {"ox": float(ox), "oy": float(oy), "oz": float(oz)}

Ce n'est pas bloquant mais c'est important pour la cohérence et la taille JSON. À discuter avec Sébastien.

4. Vérification de type par string dans apply_accelerometer_calibration

if type(ism330dl_instance).__name__.lower() != "ism330dl":
    return

C'est le même pattern que apply_magnetometer_calibration — donc c'est cohérent. Mais il faut noter que c'est fragile : si quelqu'un sous-classe ISM330DL, la vérification échouera silencieusement. Un hasattr(ism330dl_instance, 'set_accel_offset') serait plus robuste (duck typing). Ce n'est pas à corriger ici car c'est un pattern existant — mais à garder en tête pour une future harmonisation.

5. L'exemple calibrate_accelerometer.py suppose az ≈ -1g (screen up)

Ligne 51 : oz = az + 1.0

C'est correct pour la STeaMi posée écran vers le haut (Z pointe vers le bas, donc az ≈ -1g, et l'offset attendu est az - (-1) = az + 1). Mais un commentaire expliquant le signe serait utile :

# Board flat, screen up: expected (0, 0, -1g)
# Offset = measured - expected
ox = ax - 0.0
oy = ay - 0.0
oz = az - (-1.0)  # az + 1.0

6. Manque un --- entre les sections du README ism330dl

Le bloc "Accelerometer calibration" est ajouté mais il manque un séparateur --- entre "Get offsets" et "Notes on persistent calibration" pour rester cohérent avec le reste du README.


🔧 Résumé des actions

# Action Priorité
1 Fix README steami_config code block Bloquant
2 Supprimer double ligne vide dans device.py Mineur
3 Discuter cal_accelca À discuter
4 (Rien à faire — note pour le futur)
5 Ajouter commentaire sur le signe dans l'exemple Recommandé
6 Ajouter --- manquant dans README ism330dl Mineur

Bonne PR dans l'ensemble, le plus important est le fix du README (#1). Le reste est cosmétique.

@Charly-sketch
Copy link
Copy Markdown
Contributor Author

# Action Commit Expliquation
1 Fix README steami_config code block docs(steami_config): update accelerometer calibration section in README Separate the two blocks
2 Supprimer double ligne vide dans device.py style(steami_config): remove unnecessary blank lines in device.py Remove blank line
3 Discuter cal_accelca refactor(steami_config): rename accelerometer calibration key to redu… Renamed 'cal_accel' to 'ca'
4 (Rien à faire — note pour le futur)
5 Ajouter commentaire sur le signe dans l'exemple docs(steami_config): clarify Z offset calculation in comments Added a comment to explain the situation
6 Ajouter --- manquant dans README ism330dl docs(ism330dl): add separation after get offset Added the missing separator
test(steami_config): rename accelerometer calibration key for ISM330DL Renamed 'cal_accel' to 'ca' in test file

@nedseb nedseb merged commit 75d2f9e into main Apr 1, 2026
9 checks passed
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nedseb nedseb deleted the feat/add-persistent-config-ism330dl branch April 1, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

steami_config: Add accelerometer calibration storage.

3 participants