Skip to content

Commit dadf4e8

Browse files
SensorManager: cleanup calibration
1 parent 3cd1e79 commit dadf4e8

File tree

3 files changed

+17
-50
lines changed

3 files changed

+17
-50
lines changed

internal_filesystem/builtin/apps/com.micropythonos.settings/assets/calibrate_imu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def onCreate(self):
6262
self.status_label.set_text("Initializing...")
6363
self.status_label.set_style_text_font(lv.font_montserrat_12, 0)
6464
self.status_label.set_long_mode(lv.label.LONG_MODE.WRAP)
65-
self.status_label.set_width(lv.pct(90))
65+
self.status_label.set_width(lv.pct(100))
6666

6767
# Detail label (for additional info)
6868
self.detail_label = lv.label(screen)

internal_filesystem/lib/mpos/hardware/drivers/wsen_isds.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,9 @@ def __init__(self, i2c, address=0x6B, acc_range="2g", acc_data_rate="1.6Hz",
133133
self.i2c = i2c
134134
self.address = address
135135

136-
self.acc_offset_x = 0
137-
self.acc_offset_y = 0
138-
self.acc_offset_z = 0
139136
self.acc_range = 0
140137
self.acc_sensitivity = 0
141138

142-
self.gyro_offset_x = 0
143-
self.gyro_offset_y = 0
144-
self.gyro_offset_z = 0
145139
self.gyro_range = 0
146140
self.gyro_sensitivity = 0
147141

@@ -261,20 +255,6 @@ def _acc_calc_sensitivity(self):
261255
else:
262256
print("Invalid range value:", self.acc_range)
263257

264-
def read_accelerations(self):
265-
"""Read calibrated accelerometer data.
266-
267-
Returns:
268-
Tuple (x, y, z) in mg (milligrams)
269-
"""
270-
raw_a_x, raw_a_y, raw_a_z = self._read_raw_accelerations()
271-
272-
a_x = (raw_a_x - self.acc_offset_x)
273-
a_y = (raw_a_y - self.acc_offset_y)
274-
a_z = (raw_a_z - self.acc_offset_z)
275-
276-
return a_x, a_y, a_z
277-
278258
def _read_raw_accelerations(self):
279259
"""Read raw accelerometer data."""
280260
if not self._acc_data_ready():
@@ -289,20 +269,6 @@ def _read_raw_accelerations(self):
289269
return raw_a_x * self.acc_sensitivity, raw_a_y * self.acc_sensitivity, raw_a_z * self.acc_sensitivity
290270

291271

292-
def read_angular_velocities(self):
293-
"""Read calibrated gyroscope data.
294-
295-
Returns:
296-
Tuple (x, y, z) in mdps (milli-degrees per second)
297-
"""
298-
raw_g_x, raw_g_y, raw_g_z = self._read_raw_angular_velocities()
299-
300-
g_x = (raw_g_x - self.gyro_offset_x)
301-
g_y = (raw_g_y - self.gyro_offset_y)
302-
g_z = (raw_g_z - self.gyro_offset_z)
303-
304-
return g_x, g_y, g_z
305-
306272
@property
307273
def temperature(self) -> float:
308274
temp_raw = self._read_raw_temperature()

internal_filesystem/lib/mpos/sensor_manager.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -684,24 +684,26 @@ def __init__(self, i2c_bus, address):
684684
self.accel_offset = [0.0, 0.0, 0.0]
685685
self.gyro_offset = [0.0, 0.0, 0.0]
686686

687+
687688
def read_acceleration(self):
688689
"""Read acceleration in m/s² (converts from mg)."""
689-
ax, ay, az = self.sensor.read_accelerations()
690-
# Convert mg to m/s²: mg → g → m/s²
690+
ax, ay, az = self.sensor._read_raw_accelerations()
691+
# Convert G to m/s² and apply calibration
691692
return (
692-
(ax / 1000.0) * _GRAVITY,
693-
(ay / 1000.0) * _GRAVITY,
694-
(az / 1000.0) * _GRAVITY
693+
((ax / 1000) * _GRAVITY) - self.accel_offset[0],
694+
((ay / 1000) * _GRAVITY) - self.accel_offset[1],
695+
((az / 1000) * _GRAVITY) - self.accel_offset[2]
695696
)
696697

698+
697699
def read_gyroscope(self):
698700
"""Read gyroscope in deg/s (converts from mdps)."""
699-
gx, gy, gz = self.sensor.read_angular_velocities()
700-
# Convert mdps to deg/s
701+
gx, gy, gz = self.sensor._read_raw_angular_velocities()
702+
# Convert mdps to deg/s and apply calibration
701703
return (
702-
gx / 1000.0,
703-
gy / 1000.0,
704-
gz / 1000.0
704+
gx / 1000.0 - self.gyro_offset[0],
705+
gy / 1000.0 - self.gyro_offset[1],
706+
gz / 1000.0 - self.gyro_offset[2]
705707
)
706708

707709
def read_temperature(self):
@@ -722,13 +724,12 @@ def calibrate_accelerometer(self, samples):
722724
z_offset = 0
723725
if _mounted_position == FACING_EARTH:
724726
sum_z *= -1
725-
z_offset = (1000 / samples) + _GRAVITY
726727
print(f"sumz: {sum_z}")
727728

728729
# Average offsets (assuming Z-axis should read +9.8 m/s²)
729730
self.accel_offset[0] = sum_x / samples
730731
self.accel_offset[1] = sum_y / samples
731-
self.accel_offset[2] = (sum_z / samples) - _GRAVITY - z_offset
732+
self.accel_offset[2] = (sum_z / samples) - _GRAVITY
732733
print(f"offsets: {self.accel_offset}")
733734

734735
return tuple(self.accel_offset)
@@ -739,9 +740,9 @@ def calibrate_gyroscope(self, samples):
739740

740741
for _ in range(samples):
741742
gx, gy, gz = self.sensor._read_raw_angular_velocities()
742-
sum_x += gx
743-
sum_y += gy
744-
sum_z += gz
743+
sum_x += gx / 1000.0
744+
sum_y += gy / 1000.0
745+
sum_z += gz / 1000.0
745746
time.sleep_ms(10)
746747

747748
# Average offsets (should be 0 when stationary)

0 commit comments

Comments
 (0)