SensorManager: add mounted_position to IMUs

This commit is contained in:
Thomas Farstrike
2025-12-07 14:43:22 +01:00
parent f61ca5632d
commit e581843469
4 changed files with 63 additions and 26 deletions
@@ -49,16 +49,19 @@ class CalibrateIMUActivity(Activity):
screen.set_style_pad_all(mpos.ui.pct_of_display_width(3), 0)
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
screen.set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.START, lv.FLEX_ALIGN.CENTER)
focusgroup = lv.group_get_default()
if focusgroup:
focusgroup.add_obj(screen)
# Title
self.title_label = lv.label(screen)
self.title_label.set_text("IMU Calibration")
self.title_label.set_style_text_font(lv.font_montserrat_20, 0)
self.title_label.set_style_text_font(lv.font_montserrat_16, 0)
# Status label
self.status_label = lv.label(screen)
self.status_label.set_text("Initializing...")
self.status_label.set_style_text_font(lv.font_montserrat_16, 0)
self.status_label.set_style_text_font(lv.font_montserrat_12, 0)
self.status_label.set_long_mode(lv.label.LONG_MODE.WRAP)
self.status_label.set_width(lv.pct(90))
@@ -71,7 +74,7 @@ class CalibrateIMUActivity(Activity):
# Detail label (for additional info)
self.detail_label = lv.label(screen)
self.detail_label.set_text("")
self.detail_label.set_style_text_font(lv.font_montserrat_12, 0)
self.detail_label.set_style_text_font(lv.font_montserrat_10, 0)
self.detail_label.set_style_text_color(lv.color_hex(0x888888), 0)
self.detail_label.set_long_mode(lv.label.LONG_MODE.WRAP)
self.detail_label.set_width(lv.pct(90))
@@ -82,6 +85,7 @@ class CalibrateIMUActivity(Activity):
btn_cont.set_height(lv.SIZE_CONTENT)
btn_cont.set_style_border_width(0, 0)
btn_cont.set_flex_flow(lv.FLEX_FLOW.ROW)
btn_cont.set_style_pad_all(1,0)
btn_cont.set_style_flex_main_place(lv.FLEX_ALIGN.SPACE_BETWEEN, 0)
# Action button
@@ -36,8 +36,12 @@ class CheckIMUCalibrationActivity(Activity):
def onCreate(self):
screen = lv.obj()
screen.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
screen.set_style_pad_all(mpos.ui.pct_of_display_width(1), 0)
#screen.set_style_pad_all(0, 0)
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
focusgroup = lv.group_get_default()
if focusgroup:
focusgroup.add_obj(screen)
self.setContentView(screen)
def onResume(self, screen):
@@ -50,11 +54,6 @@ class CheckIMUCalibrationActivity(Activity):
self.accel_labels = []
self.gyro_labels = []
# Title
title = lv.label(screen)
title.set_text("IMU Calibration Check")
title.set_style_text_font(lv.font_montserrat_20, 0)
# Status label
self.status_label = lv.label(screen)
self.status_label.set_text("Checking...")
@@ -68,34 +67,57 @@ class CheckIMUCalibrationActivity(Activity):
# Quality score (large, prominent)
self.quality_score_label = lv.label(screen)
self.quality_score_label.set_text("Quality: --")
self.quality_score_label.set_style_text_font(lv.font_montserrat_20, 0)
self.quality_score_label.set_style_text_font(lv.font_montserrat_16, 0)
data_cont = lv.obj(screen)
data_cont.set_width(lv.pct(100))
data_cont.set_height(lv.SIZE_CONTENT)
data_cont.set_style_pad_all(0, 0)
data_cont.set_style_bg_opa(lv.OPA.TRANSP, 0)
data_cont.set_style_border_width(0, 0)
data_cont.set_flex_flow(lv.FLEX_FLOW.ROW)
data_cont.set_style_flex_main_place(lv.FLEX_ALIGN.SPACE_BETWEEN, 0)
# Accelerometer section
accel_title = lv.label(screen)
accel_title.set_text("Accelerometer (m/s²)")
accel_title.set_style_text_font(lv.font_montserrat_14, 0)
acc_cont = lv.obj(data_cont)
acc_cont.set_height(lv.SIZE_CONTENT)
acc_cont.set_width(lv.pct(45))
acc_cont.set_style_border_width(0, 0)
acc_cont.set_style_pad_all(0, 0)
acc_cont.set_flex_flow(lv.FLEX_FLOW.COLUMN)
accel_title = lv.label(acc_cont)
accel_title.set_text("Accel. (m/s^2)")
accel_title.set_style_text_font(lv.font_montserrat_12, 0)
for axis in ['X', 'Y', 'Z']:
label = lv.label(screen)
label = lv.label(acc_cont)
label.set_text(f"{axis}: --")
label.set_style_text_font(lv.font_montserrat_12, 0)
label.set_style_text_font(lv.font_montserrat_10, 0)
self.accel_labels.append(label)
# Gyroscope section
gyro_title = lv.label(screen)
gyro_title.set_text("Gyroscope (deg/s)")
gyro_title.set_style_text_font(lv.font_montserrat_14, 0)
gyro_cont = lv.obj(data_cont)
gyro_cont.set_width(mpos.ui.pct_of_display_width(45))
gyro_cont.set_height(lv.SIZE_CONTENT)
gyro_cont.set_style_border_width(0, 0)
gyro_cont.set_style_pad_all(0, 0)
gyro_cont.set_flex_flow(lv.FLEX_FLOW.COLUMN)
gyro_title = lv.label(gyro_cont)
gyro_title.set_text("Gyro (deg/s)")
gyro_title.set_style_text_font(lv.font_montserrat_12, 0)
for axis in ['X', 'Y', 'Z']:
label = lv.label(screen)
label = lv.label(gyro_cont)
label.set_text(f"{axis}: --")
label.set_style_text_font(lv.font_montserrat_12, 0)
label.set_style_text_font(lv.font_montserrat_10, 0)
self.gyro_labels.append(label)
# Separator
sep2 = lv.obj(screen)
sep2.set_size(lv.pct(100), 2)
sep2.set_style_bg_color(lv.color_hex(0x666666), 0)
#sep2 = lv.obj(screen)
#sep2.set_size(lv.pct(100), 2)
#sep2.set_style_bg_color(lv.color_hex(0x666666), 0)
# Issues label
self.issues_label = lv.label(screen)
@@ -107,6 +129,7 @@ class CheckIMUCalibrationActivity(Activity):
# Button container
btn_cont = lv.obj(screen)
btn_cont.set_style_pad_all(5, 0)
btn_cont.set_width(lv.pct(100))
btn_cont.set_height(lv.SIZE_CONTENT)
btn_cont.set_style_border_width(0, 0)
@@ -323,7 +323,7 @@ import mpos.sensor_manager as SensorManager
# Create I2C bus for IMU (different pins from display)
from machine import I2C
imu_i2c = I2C(0, sda=Pin(9), scl=Pin(18))
SensorManager.init(imu_i2c, address=0x6B)
SensorManager.init(imu_i2c, address=0x6B, mounted_position=SensorManager.FACING_EARTH)
print("Fri3d hardware: Audio, LEDs, and sensors initialized")
+12 -2
View File
@@ -25,6 +25,7 @@ try:
except ImportError:
_lock = None
# Sensor type constants (matching Android SensorManager)
TYPE_ACCELEROMETER = 1 # Units: m/s² (meters per second squared)
TYPE_GYROSCOPE = 4 # Units: deg/s (degrees per second)
@@ -32,6 +33,10 @@ TYPE_TEMPERATURE = 13 # Units: °C (generic, returns first available - dep
TYPE_IMU_TEMPERATURE = 14 # Units: °C (IMU chip temperature)
TYPE_SOC_TEMPERATURE = 15 # Units: °C (MCU/SoC internal temperature)
# mounted_position:
FACING_EARTH = 20 # underside of PCB, like fri3d_2024
FACING_SKY = 21 # top of PCB, like waveshare_esp32_s3_lcd_touch_2 (default)
# Gravity constant for unit conversions
_GRAVITY = 9.80665 # m/s²
@@ -41,6 +46,7 @@ _imu_driver = None
_sensor_list = []
_i2c_bus = None
_i2c_address = None
_mounted_position = FACING_SKY
_has_mcu_temperature = False
@@ -71,7 +77,7 @@ class Sensor:
return f"Sensor({self.name}, type={self.type})"
def init(i2c_bus, address=0x6B):
def init(i2c_bus, address=0x6B, mounted_position=FACING_SKY):
"""Initialize SensorManager. MCU temperature initializes immediately, IMU initializes on first use.
Args:
@@ -85,6 +91,7 @@ def init(i2c_bus, address=0x6B):
_i2c_bus = i2c_bus
_i2c_address = address
_mounted_position = mounted_position
# Initialize MCU temperature sensor immediately (fast, no I2C needed)
try:
@@ -218,7 +225,10 @@ def read_sensor(sensor):
try:
if sensor.type == TYPE_ACCELEROMETER:
if _imu_driver:
return _imu_driver.read_acceleration()
ax, ay, az = _imu_driver.read_acceleration()
if _mounted_position == SensorManager.FACING_EARTH:
az += _GRAVITY
return (ax, ay, az)
elif sensor.type == TYPE_GYROSCOPE:
if _imu_driver:
return _imu_driver.read_gyroscope()