Skip to content
Snippets Groups Projects

imu: make it part of InputState

Merged schneider requested to merge schneider/imu-inputstate into main
All threads resolved!
+ 42
41
@@ -3,8 +3,29 @@ from st3m.goose import List, Optional, Enum, Tuple
import hardware
import captouch
import imu
from st3m.power import Power
import machine
power = Power()
class IMUState:
"""
State of the Inertial Measurement Unit
Acceleration in m/s**2, roation rate in deg/s, pressure in Pascal
"""
__slots__ = ("acc", "gyro", "pressure")
def __init__(
self,
acc: Tuple[float, float, float],
gyro: Tuple[float, float, float],
pressure: float,
) -> None:
self.acc = acc
self.gyro = gyro
self.pressure = pressure
class InputState:
@@ -20,11 +41,17 @@ class InputState:
captouch: captouch.CaptouchState,
left_button: int,
right_button: int,
imu: IMUState,
temperature: float,
battery_voltage: float,
) -> None:
# self.petal_pads = petal_pads
self.captouch = captouch
self.left_button = left_button
self.right_button = right_button
self.imu = imu
self.temperature = temperature
self.battery_voltage = battery_voltage
@classmethod
def gather(cls, swapped_buttons: bool = False) -> "InputState":
@@ -38,7 +65,20 @@ class InputState:
if swapped_buttons:
left_button, right_button = right_button, left_button
return InputState(cts, left_button, right_button)
acc = imu.acc_read()
gyro = imu.gyro_read()
pressure, temperature = imu.pressure_read()
imu_state = IMUState(acc, gyro, pressure)
battery_voltage = power.battery_voltage
return InputState(
cts,
left_button,
right_button,
imu_state,
temperature,
battery_voltage,
)
class RepeatSettings:
@@ -460,39 +500,6 @@ class TriSwitchState:
self.right._ignore_pressed()
class IMUState:
__slots__ = ("acc", "gyro", "pressure", "temperature")
def __init__(self) -> None:
self.acc = (0.0, 0.0, 0.0)
self.gyro = (0.0, 0.0, 0.0)
self.pressure = 0.0
self.temperature = 0.0
def _update(self, ts: int, hr: InputState) -> None:
self.acc = imu.acc_read()
self.gyro = imu.gyro_read()
self.pressure, self.temperature = imu.pressure_read()
class PowerState:
__slots__ = ("_ts", "_adc_pin", "_adc", "battery_voltage")
def __init__(self) -> None:
self._adc_pin = machine.Pin(9, machine.Pin.IN)
self._adc = machine.ADC(self._adc_pin, atten=machine.ADC.ATTN_11DB)
self.battery_voltage = self._battery_voltage_sample()
self._ts = 0
def _battery_voltage_sample(self) -> float:
return self._adc.read_uv() * 2 / 1e6
def _update(self, ts: int, hr: InputState) -> None:
if ts >= self._ts + 1000:
self.battery_voltage = self._battery_voltage_sample()
self._ts = ts
class InputController:
"""
A stateful input controller. It accepts InputState updates from the Reactor
@@ -509,8 +516,6 @@ class InputController:
"captouch",
"left_shoulder",
"right_shoulder",
"imu",
"power",
"_ts",
)
@@ -518,8 +523,6 @@ class InputController:
self.captouch = CaptouchState()
self.left_shoulder = TriSwitchState(TriSwitchHandedness.left)
self.right_shoulder = TriSwitchState(TriSwitchHandedness.right)
self.imu = IMUState()
self.power = PowerState()
self._ts = 0
def think(self, hr: InputState, delta_ms: int) -> None:
@@ -527,8 +530,6 @@ class InputController:
self.captouch._update(self._ts, hr)
self.left_shoulder._update(self._ts, hr)
self.right_shoulder._update(self._ts, hr)
self.imu._update(self._ts, hr)
self.power._update(self._ts, hr)
def _ignore_pressed(self) -> None:
"""
Loading