Skip to content

Commit

Permalink
Create common interface for different IMU drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed Dec 1, 2024
1 parent 6b5d822 commit e1de156
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
57 changes: 57 additions & 0 deletions IMU.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "Arduino.h"
#include "Wire.h"
#include "SPI.h"

// IMU driver interface

class IMUInterface {
public:
enum IMUType {
UNKNOWN,
MPU9250,
MPU9252,
MPU6500,
ICM20948,
};
enum DlpfBandwidth : int8_t {
DLPF_BANDWIDTH_184HZ = 0x01,
DLPF_BANDWIDTH_92HZ = 0x02,
DLPF_BANDWIDTH_41HZ = 0x03,
DLPF_BANDWIDTH_20HZ = 0x04,
DLPF_BANDWIDTH_10HZ = 0x05,
DLPF_BANDWIDTH_5HZ = 0x06
};
enum AccelRange : int8_t {
ACCEL_RANGE_2G = 0x00,
ACCEL_RANGE_4G = 0x08,
ACCEL_RANGE_8G = 0x10,
ACCEL_RANGE_16G = 0x18
};
enum GyroRange : int8_t {
GYRO_RANGE_250DPS = 0x00,
GYRO_RANGE_500DPS = 0x08,
GYRO_RANGE_1000DPS = 0x10,
GYRO_RANGE_2000DPS = 0x18
};
enum Rate {
RATE_MIN,
RATE_50HZ_APPROX,
RATE_1KHZ_APPROX,
RATE_8KHZ_APPROX,
RATE_MAX
};

virtual bool begin() = 0;
virtual void reset() = 0;
virtual uint8_t whoAmI() const = 0;
virtual bool read() = 0;
virtual void waitForData() = 0;
virtual void getAccel(float& x, float& y, float& z) const = 0;
virtual void getGyro(float& x, float& y, float& z) const = 0;
virtual void getMag(float& x, float& y, float& z) const = 0;
virtual float getTemp() const = 0;
virtual bool setRate(Rate rate) = 0;
virtual const char* getModel() const = 0;
};
7 changes: 6 additions & 1 deletion MPU9250.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,12 @@ void MPU9250::getMag(float& x, float& y, float& z) const {
y = mag_[1];
z = mag_[2];
}
const char* MPU9250::getType() const {
bool MPU9250::setRate(Rate rate) {
// TODO:
log("Rate setting is not implemented");
return false;
}
const char* MPU9250::getModel() const {
switch (who_am_i_) {
case WHOAMI_MPU6500_: return "MPU-6500";
case WHOAMI_MPU9250_: return "MPU-9250";
Expand Down
36 changes: 13 additions & 23 deletions MPU9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
#include <cstdint>
#include "core/core.h"
#endif
#include "IMU.h"
#include "invensense_imu.h" // NOLINT
#include "logger.h"

class MPU9250 : public Logger {
class MPU9250 : public IMUInterface, public Logger {
public:
/* Sensor and filter settings */
enum I2cAddr : uint8_t {
Expand All @@ -53,18 +54,6 @@ class MPU9250 : public Logger {
DLPF_BANDWIDTH_10HZ = 0x05,
DLPF_BANDWIDTH_5HZ = 0x06
};
enum AccelRange : int8_t {
ACCEL_RANGE_2G = 0x00,
ACCEL_RANGE_4G = 0x08,
ACCEL_RANGE_8G = 0x10,
ACCEL_RANGE_16G = 0x18
};
enum GyroRange : int8_t {
GYRO_RANGE_250DPS = 0x00,
GYRO_RANGE_500DPS = 0x08,
GYRO_RANGE_1000DPS = 0x10,
GYRO_RANGE_2000DPS = 0x18
};
enum WomRate : int8_t {
WOM_RATE_0_24HZ = 0x00,
WOM_RATE_0_49HZ = 0x01,
Expand All @@ -88,7 +77,7 @@ class MPU9250 : public Logger {
imu_(&spi, cs) {}
void config(TwoWire *i2c, const I2cAddr addr);
void config(SPIClass *spi, const uint8_t cs);
bool begin();
bool begin() override;
bool enableDrdyInt();
bool disableDrdyInt();
bool setAccelRange(const AccelRange range);
Expand All @@ -100,13 +89,14 @@ class MPU9250 : public Logger {
bool setDlpfBandwidth(const DlpfBandwidth dlpf);
inline DlpfBandwidth dlpf_bandwidth() const {return dlpf_bandwidth_;}
bool enableWom(int16_t threshold_mg, const WomRate wom_rate);
void reset();
bool read();
void waitForData();
void getAccel(float& x, float& y, float& z) const;
void getGyro(float& x, float& y, float& z) const;
void getMag(float& x, float& y, float& z) const;
const char* getType() const;
void reset() override;
bool read() override;
void waitForData() override;
void getAccel(float& x, float& y, float& z) const override;
void getGyro(float& x, float& y, float& z) const override;
void getMag(float& x, float& y, float& z) const override;
bool setRate(Rate rate) override;
const char* getModel() const override;
inline bool new_imu_data() const {return new_imu_data_;}
inline float accel_x_mps2() const {return accel_[0];}
inline float accel_y_mps2() const {return accel_[1];}
Expand All @@ -118,8 +108,8 @@ class MPU9250 : public Logger {
inline float mag_x_ut() const {return mag_[0];}
inline float mag_y_ut() const {return mag_[1];}
inline float mag_z_ut() const {return mag_[2];}
inline float die_temp_c() const {return temp_;}
inline uint8_t whoAmI() const {return who_am_i_;}
inline float getTemp() const override {return temp_;}
inline uint8_t whoAmI() const override {return who_am_i_;}

private:
InvensenseImu imu_;
Expand Down

0 comments on commit e1de156

Please sign in to comment.