1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use core::marker::PhantomData;

use config::Config;
use embedded_hal::blocking::{delay, i2c};
use nalgebra::{Quaternion, Vector3};

use self::result::{Error, Result};
use crate::{ak8963, mpu6500};

pub type Madgwick = ahrs::Madgwick<f32>;
pub type Mahony = ahrs::Mahony<f32>;

pub mod config;
pub mod result;

const MADGWICK_BETA: f32 = 0.60459978807; // sqrt(3.0 / 4.0) * PI * (40.0 / 180.0)

const MAHONY_KP: f32 = 10.0;
const MAHONY_KI: f32 = 0.0;

pub struct Mpu925x<I2C, AHRS>
where
    I2C: i2c::Read + i2c::Write,
    AHRS: ahrs::Ahrs<f32>,
{
    ak: ak8963::Ak8963<I2C>,
    mpu: mpu6500::Mpu6500<I2C>,
    ahrs: AHRS,
    _i2c: PhantomData<I2C>,
}

impl<I2C, AHRS, I2cError> Mpu925x<I2C, AHRS>
where
    I2C: i2c::Read<Error = I2cError> + i2c::Write<Error = I2cError>,
    AHRS: ahrs::Ahrs<f32>,
{
    fn with_configuration_ahrs<DELAY>(
        ak_addr: u8,
        mpu_addr: u8,
        i2c: &mut I2C,
        delay: &mut DELAY,
        cfg: Config,
        ahrs: AHRS,
    ) -> Result<Self, I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        let mpu = match mpu6500::Mpu6500::with_configuration(mpu_addr, i2c, delay, cfg.mpu) {
            Ok(v) => v,
            Err(e) => return Err(Error::Mpu6500Error(e)),
        };

        let ak = match ak8963::Ak8963::with_configuration(ak_addr, i2c, delay, cfg.ak) {
            Ok(v) => v,
            Err(e) => return Err(Error::Ak8963Error(e)),
        };

        Ok(Self {
            ak,
            mpu,
            ahrs,
            _i2c: PhantomData::default(),
        })
    }

    pub fn read(&mut self, i2c: &mut I2C) -> Result<(), I2cError> {
        if let Err(e) = self.mpu.read_imu(i2c) {
            return Err(Error::Mpu6500Error(e));
        }

        if let Err(e) = self.ak.read(i2c) {
            return Err(Error::Ak8963Error(e));
        }

        match self.ahrs.update(
            &self.mpu.angular_velocity,
            &self.mpu.acceleration,
            &self.ak.magnetic_field,
        ) {
            Ok(_) => {}
            Err(e) => {
                return match e {
                    "Accelerometer norm divided by zero." => Err(Error::AhrsUpdateAccelerometer),
                    "Magnetometer norm divided by zero." => Err(Error::AhrsUpdateMagnetometer),
                    _ => unreachable!(),
                }
            }
        }

        Ok(())
    }

    pub fn acceleration(&self) -> Vector3<f32> {
        self.mpu.acceleration
    }

    pub fn angular_velocity(&self) -> Vector3<f32> {
        self.mpu.angular_velocity
    }

    pub fn magnetic_field(&self) -> Vector3<f32> {
        self.ak.magnetic_field
    }
}

impl<I2C, I2cError> Mpu925x<I2C, Madgwick>
where
    I2C: i2c::Read<Error = I2cError> + i2c::Write<Error = I2cError>,
{
    pub fn new<DELAY>(
        ak_addr: u8,
        mpu_addr: u8,
        i2c: &mut I2C,
        delay: &mut DELAY,
    ) -> Result<Self, I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        Self::with_configuration(ak_addr, mpu_addr, i2c, delay, config::Config::default())
    }

    pub fn with_configuration<DELAY>(
        ak_addr: u8,
        mpu_addr: u8,
        i2c: &mut I2C,
        delay: &mut DELAY,
        cfg: Config,
    ) -> Result<Self, I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        let sample_rate_freq = cfg.mpu.fifo_sample_rate.get_freq();
        let sample_period = sample_rate_freq as f32 / 1000.0;

        Self::with_configuration_ahrs(
            ak_addr,
            mpu_addr,
            i2c,
            delay,
            cfg,
            ahrs::Madgwick::new(sample_period, MADGWICK_BETA),
        )
    }

    pub fn calibrate_mpu<DELAY>(&mut self, i2c: &mut I2C, delay: &mut DELAY) -> Result<(), I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        match self.mpu.calibrate(i2c, delay) {
            Ok(()) => Ok(()),
            Err(e) => Err(Error::Mpu6500Error(e)),
        }
    }

    pub fn calibrate_ak<DELAY>(&mut self, i2c: &mut I2C, delay: &mut DELAY) -> Result<(), I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        match self.ak.calibrate(i2c, delay) {
            Ok(()) => Ok(()),
            Err(e) => Err(Error::Ak8963Error(e)),
        }
    }

    pub fn rotation(&self) -> Quaternion<f32> {
        self.ahrs.quat
    }
}

impl<I2C, I2cError> Mpu925x<I2C, Mahony>
where
    I2C: i2c::Read<Error = I2cError> + i2c::Write<Error = I2cError>,
{
    pub fn new<DELAY>(
        ak_addr: u8,
        mpu_addr: u8,
        i2c: &mut I2C,
        delay: &mut DELAY,
    ) -> Result<Self, I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        Self::with_configuration(ak_addr, mpu_addr, i2c, delay, config::Config::default())
    }

    pub fn with_configuration<DELAY>(
        ak_addr: u8,
        mpu_addr: u8,
        i2c: &mut I2C,
        delay: &mut DELAY,
        cfg: Config,
    ) -> Result<Self, I2cError>
    where
        DELAY: delay::DelayMs<u16>,
    {
        let sample_rate_freq = cfg.mpu.fifo_sample_rate.get_freq();
        let sample_period = sample_rate_freq as f32 / 1000.0;

        Self::with_configuration_ahrs(
            ak_addr,
            mpu_addr,
            i2c,
            delay,
            cfg,
            ahrs::Mahony::new(sample_period, MAHONY_KP, MAHONY_KI),
        )
    }

    pub fn rotation(&self) -> Quaternion<f32> {
        self.ahrs.quat
    }
}