Comprehensive motion sensor driver library with accelerometer, gyroscope, magnetometer support, sensor fusion algorithms (Complementary, Kalman, Madgwick), and gesture recognition for motion control applications
moon add maria/sensor_driver{
"deps": {
"maria/sensor_driver": "0.1.0"
}
}let config = SensorConfig::default()
let controller = MotionController::new(config)
// Update with sensor readings (typically called at 100Hz)
let state = controller.update(
accel_raw, // Vec3: accelerometer (m/s²)
gyro_raw, // Vec3: gyroscope (rad/s)
mag_raw, // Vec3: magnetometer (μT)
timestamp, // Int64: milliseconds
dt, // Double: seconds
)
// Get orientation
let (roll, pitch, yaw) = controller.get_euler_degrees()
// Detect gestures
match controller.detect_gesture() {
Shaking => println("Device is shaking!")
FreeFall => println("Free fall!")
Impact(force) => println("Impact: \{force}")
Tilting(p, r) => println("Tilted")
Rotating(speed) => println("Rotating")
Stationary => ()
}
// Get compass heading
let heading = controller.get_heading_degrees()impl Eq for AccelCalibrationimpl Show for AccelCalibrationimpl Eq for AccelReadingimpl Show for AccelReadingpub struct Accelerometer {
calibration : AccelCalibration
last_reading : AccelReading
raw_readings : Array[Vec3]
config : SensorConfig
}fn Accelerometer::detect_shake(_self : Accelerometer, recent_readings : ArrayView[AccelReading]) -> Boolfn ComplementaryFilter::update(self : ComplementaryFilter, accel : Vec3, gyro : Vec3, mag : Vec3, dt : Double) -> Quaternionpub enum Gesture {
Stationary
Shaking
Tilting(Double, Double)
Rotating(Double)
FreeFall
Impact(Double)
}impl Eq for GyroCalibrationimpl Show for GyroCalibrationimpl Eq for GyroReadingimpl Show for GyroReadingpub struct Gyroscope {
calibration : GyroCalibration
last_reading : GyroReading
integrated_angle : Vec3
raw_readings : Array[Vec3]
config : SensorConfig
}pub struct KalmanFilter {
orientation : Quaternion
error_covariance : Double
process_noise : Double
measurement_noise : Double
}fn KalmanFilter::update(self : KalmanFilter, accel : Vec3, gyro : Vec3, mag : Vec3, dt : Double) -> Quaternionfn MadgwickFilter::update(self : MadgwickFilter, accel : Vec3, gyro : Vec3, mag : Vec3, dt : Double) -> Quaternionimpl Eq for MagCalibrationimpl Show for MagCalibrationimpl Eq for MagReadingimpl Show for MagReadingpub struct Magnetometer {
calibration : MagCalibration
last_reading : MagReading
raw_readings : Array[Vec3]
config : SensorConfig
}fn Magnetometer::get_tilt_compensated_heading(self : Magnetometer, pitch : Double, roll : Double) -> Doublepub struct MotionController {
accel : Accelerometer
gyro : Gyroscope
mag : Magnetometer
filter : ComplementaryFilter
state : MotionState
recent_accels : Array[AccelReading]
max_history : Int
}fn MotionController::add_calibration_sample(self : MotionController, accel_raw : Vec3, gyro_raw : Vec3, mag_raw : Vec3) -> Unitfn MotionController::update(self : MotionController, accel_raw : Vec3, gyro_raw : Vec3, mag_raw : Vec3, timestamp : Int64, dt : Double) -> MotionStatepub(all) struct MotionState {
acceleration : Vec3
angular_velocity : Vec3
magnetic_field : Vec3
orientation : Quaternion
timestamp : Int64
}impl Show for MotionStatepub(all) struct Quaternion {
w : Double
x : Double
y : Double
z : Double
}impl Eq for Quaternionimpl Show for Quaternionpub(all) struct SensorConfig {
accel_range : Double
gyro_range : Double
mag_range : Double
sample_rate : Int
}impl Eq for SensorConfigimpl Show for SensorConfigpub(all) struct Vec3 {
x : Double
y : Double
z : Double
}Install
Download zipComprehensive motion sensor driver library with accelerometer, gyroscope, magnetometer support, sensor fusion algorithms (Complementary, Kalman, Madgwick), and gesture recognition for motion control applications