Skip to content

Commit 1cf93fb

Browse files
committed
feat(steami_config): add accelerometer calibration example for ISM330DL
1 parent 1f80aac commit 1cf93fb

1 file changed

Lines changed: 15 additions & 25 deletions

File tree

lib/steami_config/examples/calibrate_accelerometer.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@
1717
from steami_config import SteamiConfig
1818

1919
# --- Init ---
20-
print("Initializing...")
2120

2221
i2c = I2C(1)
23-
print("I2C initialized.")
2422
bridge = DaplinkBridge(i2c)
25-
print("DaplinkBridge initialized.")
2623
config = SteamiConfig(bridge)
27-
print("SteamiConfig initialized.")
2824
config.load()
2925

30-
print("Initialization complete.\n")
3126
imu = ISM330DL(i2c)
3227

3328
print("=== Accelerometer Calibration ===\n")
@@ -39,7 +34,7 @@
3934
samples = 100
4035
sx = sy = sz = 0.0
4136

42-
for _i in range(samples):
37+
for _ in range(samples):
4338
ax, ay, az = imu.acceleration_g()
4439
sx += ax
4540
sy += ay
@@ -50,51 +45,46 @@
5045
ay = sy / samples
5146
az = sz / samples
5247

53-
# Expected: (0, 0, -1g) when flat (screen up)
48+
# Expected resting orientation: flat, screen up -> (0, 0, -1g)
5449
ox = ax
5550
oy = ay
56-
oz = az + 1.0 # compensate gravity
51+
oz = az + 1.0
5752

5853
print("\nMeasured average:")
5954
print(" ax = {:.3f} g".format(ax))
6055
print(" ay = {:.3f} g".format(ay))
6156
print(" az = {:.3f} g".format(az))
6257

6358
print("\nComputed offsets:")
64-
print(" ox = {:.3f}".format(ox))
65-
print(" oy = {:.3f}".format(oy))
66-
print(" oz = {:.3f}".format(oz))
59+
print(" ox = {:.3f} g".format(ox))
60+
print(" oy = {:.3f} g".format(oy))
61+
print(" oz = {:.3f} g".format(oz))
6762

68-
# --- Step 2: Save ---
63+
# --- Step 2: Save to config zone ---
6964

7065
config.set_accelerometer_calibration(ox=ox, oy=oy, oz=oz)
7166
config.save()
7267

7368
print("\nCalibration saved to config zone.")
7469

75-
# --- Step 3: Verify ---
70+
# --- Step 3: Verify after reload ---
7671

7772
config2 = SteamiConfig(bridge)
7873
config2.load()
7974

8075
imu2 = ISM330DL(i2c)
8176
config2.apply_accelerometer_calibration(imu2)
8277

83-
print("\nVerification (5 readings):")
78+
print("\nApplied offsets after reload:")
79+
ox2, oy2, oz2 = imu2.get_accel_offset()
80+
print(" ox = {:.3f} g".format(ox2))
81+
print(" oy = {:.3f} g".format(oy2))
82+
print(" oz = {:.3f} g".format(oz2))
8483

84+
print("\nVerification (5 corrected readings):")
8585
for i in range(5):
8686
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-
9787
print(" {}: ax={:.3f} ay={:.3f} az={:.3f}".format(i + 1, ax, ay, az))
9888
sleep_ms(500)
9989

100-
print("\nDone! Calibration will persist across reboots.")
90+
print("\nDone! Calibration is stored and will be restored at next boot.")

0 commit comments

Comments
 (0)