|
| 1 | +import os |
| 2 | + |
| 3 | +from mpos.imu.drivers.base import IMUDriverBase |
| 4 | + |
| 5 | + |
| 6 | +class IIODriver(IMUDriverBase): |
| 7 | + """ |
| 8 | + Read sensor data via Linux IIO sysfs. |
| 9 | +
|
| 10 | + Typical base path: |
| 11 | + /sys/bus/iio/devices/iio:device0 |
| 12 | + """ |
| 13 | + |
| 14 | + accel_path: str |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + super().__init__() |
| 18 | + self.accel_path = self.find_iio_device_with_file("in_accel_x_raw") |
| 19 | + print("path:", self.accel_path) |
| 20 | + |
| 21 | + def _p(self, name: str): |
| 22 | + return self.accel_path + "/" + name |
| 23 | + |
| 24 | + def _exists(self, name): |
| 25 | + try: |
| 26 | + os.stat(name) |
| 27 | + return True |
| 28 | + except OSError: |
| 29 | + return False |
| 30 | + |
| 31 | + def _is_dir(self, path): |
| 32 | + # MicroPython: stat tuple, mode is [0] |
| 33 | + try: |
| 34 | + st = os.stat(path) |
| 35 | + mode = st[0] |
| 36 | + # directory bit (POSIX): 0o040000 |
| 37 | + return (mode & 0o170000) == 0o040000 |
| 38 | + except OSError: |
| 39 | + return False |
| 40 | + |
| 41 | + def find_iio_device_with_file(self, filename, base_dir="/sys/bus/iio/devices/"): |
| 42 | + """ |
| 43 | + Returns full path to iio:deviceX that contains given filename, |
| 44 | + e.g. "/sys/bus/iio/devices/iio:device0" |
| 45 | +
|
| 46 | + Returns None if not found. |
| 47 | + """ |
| 48 | + |
| 49 | + print("Is dir? ", self._is_dir(base_dir), base_dir) |
| 50 | + try: |
| 51 | + entries = os.listdir(base_dir) |
| 52 | + except OSError: |
| 53 | + print("Error listing dir") |
| 54 | + return None |
| 55 | + |
| 56 | + for entry in entries: |
| 57 | + print("Entry:", entry) |
| 58 | + if not entry.startswith("iio:device"): |
| 59 | + continue |
| 60 | + |
| 61 | + dev_path = base_dir + "/" + entry |
| 62 | + if not self._is_dir(dev_path): |
| 63 | + continue |
| 64 | + |
| 65 | + if self._exists(dev_path + "/" + filename): |
| 66 | + return dev_path |
| 67 | + |
| 68 | + return None |
| 69 | + |
| 70 | + def _read_text(self, name: str) -> str: |
| 71 | + print("Read: ", name) |
| 72 | + f = open(name, "r") |
| 73 | + try: |
| 74 | + return f.readline().strip() |
| 75 | + finally: |
| 76 | + f.close() |
| 77 | + |
| 78 | + def _read_float(self, name: str) -> float: |
| 79 | + return float(self._read_text(name)) |
| 80 | + |
| 81 | + def _read_int(self, name: str) -> int: |
| 82 | + return int(self._read_text(name), 10) |
| 83 | + |
| 84 | + def _read_raw_scaled(self, raw_name: str, scale_name: str) -> float: |
| 85 | + raw = self._read_int(raw_name) |
| 86 | + scale = self._read_float(scale_name) |
| 87 | + return raw * scale |
| 88 | + |
| 89 | + def read_temperature(self) -> float: |
| 90 | + """ |
| 91 | + Tries common IIO patterns: |
| 92 | + - in_temp_input (already scaled, usually millidegree C) |
| 93 | + - in_temp_raw + in_temp_scale |
| 94 | + """ |
| 95 | + if not self.accel_path: |
| 96 | + return None |
| 97 | + |
| 98 | + raw_path = self.accel_path + "/" + "in_temp_raw" |
| 99 | + scale_path = self.accel_path + "/" + "in_temp_scale" |
| 100 | + if not self._exists(raw_path) or not self._exists(scale_path): |
| 101 | + return None |
| 102 | + return self._read_raw_scaled(raw_path, scale_path) |
| 103 | + |
| 104 | + def _raw_acceleration_mps2(self): |
| 105 | + if not self.accel_path: |
| 106 | + return (0.0, 0.0, 0.0) |
| 107 | + scale_name = self.accel_path + "/" + "in_accel_scale" |
| 108 | + |
| 109 | + ax = self._read_raw_scaled(self.accel_path + "/" + "in_accel_x_raw", scale_name) |
| 110 | + ay = self._read_raw_scaled(self.accel_path + "/" + "in_accel_y_raw", scale_name) |
| 111 | + az = self._read_raw_scaled(self.accel_path + "/" + "in_accel_z_raw", scale_name) |
| 112 | + |
| 113 | + return (ax, ay, az) |
| 114 | + |
| 115 | + def _raw_gyroscope_dps(self): |
| 116 | + if not self.accel_path: |
| 117 | + return (0.0, 0.0, 0.0) |
| 118 | + scale_name = self.accel_path + "/" + "in_anglvel_scale" |
| 119 | + |
| 120 | + gx = self._read_raw_scaled(self.accel_path + "/" + "in_anglvel_x_raw", scale_name) |
| 121 | + gy = self._read_raw_scaled(self.accel_path + "/" + "in_anglvel_y_raw", scale_name) |
| 122 | + gz = self._read_raw_scaled(self.accel_path + "/" + "in_anglvel_z_raw", scale_name) |
| 123 | + |
| 124 | + return (gx, gy, gz) |
| 125 | + |
| 126 | + def read_acceleration(self): |
| 127 | + ax, ay, az = self._raw_acceleration_mps2() |
| 128 | + return ( |
| 129 | + ax - self.accel_offset[0], |
| 130 | + ay - self.accel_offset[1], |
| 131 | + az - self.accel_offset[2], |
| 132 | + ) |
| 133 | + |
| 134 | + def read_gyroscope(self): |
| 135 | + gx, gy, gz = self._raw_gyroscope_dps() |
| 136 | + return ( |
| 137 | + gx - self.gyro_offset[0], |
| 138 | + gy - self.gyro_offset[1], |
| 139 | + gz - self.gyro_offset[2], |
| 140 | + ) |
0 commit comments