Skip to content

Commit 53415d5

Browse files
committed
feat(ism330dl): add example for permanent config
1 parent 47c91c7 commit 53415d5

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""Calibrate ISM330DL accelerometer bias and save to persistent config.
2+
3+
This example measures accelerometer offsets while the board is lying flat.
4+
The computed offsets (ox, oy, oz) are stored in the config zone and
5+
survive power cycles.
6+
7+
Instructions:
8+
- Place the board flat and still (screen up)
9+
- Wait for measurement
10+
"""
11+
12+
from time import sleep_ms
13+
14+
from daplink_bridge import DaplinkBridge
15+
from ism330dl import ISM330DL
16+
from machine import I2C
17+
from steami_config import SteamiConfig
18+
19+
# --- Init ---
20+
print("Initializing...")
21+
22+
i2c = I2C(1)
23+
print("I2C initialized.")
24+
bridge = DaplinkBridge(i2c)
25+
print("DaplinkBridge initialized.")
26+
config = SteamiConfig(bridge)
27+
print("SteamiConfig initialized.")
28+
config.load()
29+
30+
print("Initialization complete.\n")
31+
imu = ISM330DL(i2c)
32+
33+
print("=== Accelerometer Calibration ===\n")
34+
print("Place the board flat (screen up) and keep it still...")
35+
sleep_ms(2000)
36+
37+
# --- Step 1: Collect samples ---
38+
39+
samples = 100
40+
sx = sy = sz = 0.0
41+
42+
for _i in range(samples):
43+
ax, ay, az = imu.acceleration_g()
44+
sx += ax
45+
sy += ay
46+
sz += az
47+
sleep_ms(20)
48+
49+
ax = sx / samples
50+
ay = sy / samples
51+
az = sz / samples
52+
53+
# Expected: (0, 0, -1g) when flat (screen up)
54+
ox = ax
55+
oy = ay
56+
oz = az + 1.0 # compensate gravity
57+
58+
print("\nMeasured average:")
59+
print(" ax = {:.3f} g".format(ax))
60+
print(" ay = {:.3f} g".format(ay))
61+
print(" az = {:.3f} g".format(az))
62+
63+
print("\nComputed offsets:")
64+
print(" ox = {:.3f}".format(ox))
65+
print(" oy = {:.3f}".format(oy))
66+
print(" oz = {:.3f}".format(oz))
67+
68+
# --- Step 2: Save ---
69+
70+
config.set_accelerometer_calibration(ox=ox, oy=oy, oz=oz)
71+
config.save()
72+
73+
print("\nCalibration saved to config zone.")
74+
75+
# --- Step 3: Verify ---
76+
77+
config2 = SteamiConfig(bridge)
78+
config2.load()
79+
80+
imu2 = ISM330DL(i2c)
81+
config2.apply_accelerometer_calibration(imu2)
82+
83+
print("\nVerification (5 readings):")
84+
85+
for i in range(5):
86+
ax, ay, az = imu2.acceleration_g()
87+
88+
# Apply offsets manually (driver not patched yet)
89+
ox = getattr(imu2, "_accel_offset_x", 0.0)
90+
oy = getattr(imu2, "_accel_offset_y", 0.0)
91+
oz = getattr(imu2, "_accel_offset_z", 0.0)
92+
93+
ax -= ox
94+
ay -= oy
95+
az -= oz
96+
97+
print(" {}: ax={:.3f} ay={:.3f} az={:.3f}".format(i + 1, ax, ay, az))
98+
sleep_ms(500)
99+
100+
print("\nDone! Calibration will persist across reboots.")

0 commit comments

Comments
 (0)