You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
magn: Start magnetometer support
Add magnetometer support for iio. With suitable application, this allows painting compass.
This commit is contained in:
@@ -18,6 +18,10 @@ class IMUDriverBase:
|
||||
"""Returns (x, y, z) in deg/s"""
|
||||
raise NotImplementedError
|
||||
|
||||
def read_magnetometer(self):
|
||||
"""Returns (x, y, z) in uT"""
|
||||
raise NotImplementedError
|
||||
|
||||
def read_temperature(self):
|
||||
"""Returns temperature in °C"""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -12,11 +12,12 @@ class IIODriver(IMUDriverBase):
|
||||
"""
|
||||
|
||||
accel_path: str
|
||||
mag_path: str
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.accel_path = self.find_iio_device_with_file("in_accel_x_raw")
|
||||
print("path:", self.accel_path)
|
||||
self.mag_path = self.find_iio_device_with_file("in_magn_x_raw")
|
||||
|
||||
def _p(self, name: str):
|
||||
return self.accel_path + "/" + name
|
||||
@@ -138,3 +139,10 @@ class IIODriver(IMUDriverBase):
|
||||
gy - self.gyro_offset[1],
|
||||
gz - self.gyro_offset[2],
|
||||
)
|
||||
|
||||
def read_magnetometer(self) -> tuple[float, float, float]:
|
||||
gx = self._read_raw_scaled(self.mag_path + "/" + "in_magn_x_raw", self.mag_path + "/" + "in_magn_x_scale")
|
||||
gy = self._read_raw_scaled(self.mag_path + "/" + "in_magn_y_raw", self.mag_path + "/" + "in_magn_y_scale")
|
||||
gz = self._read_raw_scaled(self.mag_path + "/" + "in_magn_z_raw", self.mag_path + "/" + "in_magn_z_scale")
|
||||
|
||||
return (gx, gy, gz)
|
||||
|
||||
@@ -3,6 +3,7 @@ import time
|
||||
from mpos.imu.constants import (
|
||||
TYPE_ACCELEROMETER,
|
||||
TYPE_GYROSCOPE,
|
||||
TYPE_MAGNETIC_FIELD,
|
||||
TYPE_IMU_TEMPERATURE,
|
||||
TYPE_SOC_TEMPERATURE,
|
||||
TYPE_TEMPERATURE,
|
||||
@@ -50,6 +51,15 @@ class ImuManager:
|
||||
def init_iio(self):
|
||||
self._imu_driver = IIODriver()
|
||||
self._sensor_list = [
|
||||
Sensor(
|
||||
name="Magnetometer",
|
||||
sensor_type=TYPE_MAGNETIC_FIELD,
|
||||
vendor="Linux IIO",
|
||||
version=1,
|
||||
max_range="?",
|
||||
resolution="?",
|
||||
power_ma=0.2
|
||||
),
|
||||
Sensor(
|
||||
name="Accelerometer",
|
||||
sensor_type=TYPE_ACCELEROMETER,
|
||||
@@ -156,6 +166,9 @@ class ImuManager:
|
||||
elif sensor.type == TYPE_GYROSCOPE:
|
||||
if self._imu_driver:
|
||||
return self._imu_driver.read_gyroscope()
|
||||
elif sensor.type == TYPE_MAGNETIC_FIELD:
|
||||
if self._imu_driver:
|
||||
return self._imu_driver.read_magnetometer()
|
||||
elif sensor.type == TYPE_IMU_TEMPERATURE:
|
||||
if self._imu_driver:
|
||||
return self._imu_driver.read_temperature()
|
||||
|
||||
Reference in New Issue
Block a user