Skip to content

Commit 9249392

Browse files
committed
Add fabric method IMU::create for instantiating imu driver
Simplify inheritance model - merge IMUInterface and IMUBase to IMU. Make IMU class not pure-virtual, but dummy imu instead.
1 parent 84ca134 commit 9249392

11 files changed

Lines changed: 60 additions & 35 deletions

File tree

ICM20948.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ float ICM20948::getRate() {
268268
}
269269

270270
bool ICM20948::setupInterrupt() {
271-
bool res = IMUBase::setupInterrupt(intPin);
271+
bool res = IMU::setupInterrupt(intPin);
272272
if (intPin != -1 && res) {
273273
setIntPinPolarity(ICM20948_ACT_LOW);
274274
enableClearIntByAnyRead(true);

ICM20948.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ typedef enum AK09916_OP_MODE {
8484
AK09916_CONT_MODE_100HZ = 0x08
8585
} AK09916_opMode;
8686

87-
class ICM20948 : public IMUBase {
87+
class ICM20948 : public IMU {
8888
public:
8989
/* constants */
9090

ICM40609D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ float ICM40609D::getRate() {
282282
}
283283

284284
bool ICM40609D::setupInterrupt() {
285-
bool res = IMUBase::setupInterrupt(intPin);
285+
bool res = IMU::setupInterrupt(intPin);
286286
if (intPin != -1 && res) {
287287
enableDataReadyInterrupt();
288288
}

ICM40609D.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "IMU.h"
1313
#include "logger.h"
1414

15-
class ICM40609D : public IMUBase {
15+
class ICM40609D : public IMU {
1616
public:
1717
static constexpr uint8_t WHO_AM_I_VALUE = 0x3B;
1818

@@ -33,7 +33,6 @@ class ICM40609D : public IMUBase {
3333
int status() const override { return _status; }
3434
uint8_t whoAmI() override;
3535
bool read() override;
36-
void waitForData() override { IMUBase::waitForData(); }
3736
void getAccel(float& x, float& y, float& z) const override;
3837
void getGyro(float& x, float& y, float& z) const override;
3938
void getMag(float& x, float& y, float& z) const override;

IMU.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "FlixPeriph.h"
2+
3+
IMU* IMU::create(int model, SPIClass& spi, int cs, int drdy) {
4+
switch (model) {
5+
case 1: return new MPU9250(spi, cs, drdy);
6+
case 2: return new ICM20948(spi, cs, drdy);
7+
case 4: return new ICM40609D(spi, cs, drdy);
8+
default: return new IMU();
9+
}
10+
}
11+
12+
IMU* IMU::create(int model, TwoWire& i2c, int drdy) {
13+
switch (model) {
14+
case 1: return new MPU9250(i2c, drdy);
15+
case 2: return new ICM20948(i2c, drdy);
16+
case 3: return new MPU6050(i2c, drdy);
17+
case 4: return new ICM40609D(i2c, drdy);
18+
default: return new IMU();
19+
}
20+
}

IMU.h

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "freertos/task.h"
1111
#endif
1212

13-
// IMU driver interface
14-
class IMUInterface {
13+
// Base class for IMU drivers
14+
class IMU : public Logger {
1515
public:
1616
enum DLPF {
1717
DLPF_OFF,
@@ -44,27 +44,30 @@ class IMUInterface {
4444
RATE_8KHZ_APPROX,
4545
RATE_MAX
4646
};
47-
virtual bool begin() = 0;
48-
virtual void reset() = 0;
49-
virtual int status() const = 0; // 0 - success, otherwise error
50-
virtual uint8_t whoAmI() = 0;
51-
virtual bool read() = 0;
52-
virtual void waitForData() = 0;
53-
virtual void getAccel(float& x, float& y, float& z) const = 0;
54-
virtual void getGyro(float& x, float& y, float& z) const = 0;
55-
virtual void getMag(float& x, float& y, float& z) const = 0;
56-
virtual float getTemp() = 0;
57-
virtual bool setRate(const Rate rate) = 0;
58-
virtual float getRate() = 0;
59-
virtual bool setAccelRange(const AccelRange range) = 0;
60-
virtual bool setGyroRange(const GyroRange range) = 0;
61-
virtual bool setDLPF(const DLPF dlpf) = 0;
62-
virtual const char* getModel() const = 0;
63-
virtual bool setupInterrupt() = 0;
64-
};
6547

66-
// Base for all IMU drivers
67-
class IMUBase : public IMUInterface, public Logger {
48+
virtual bool begin() { return false; }
49+
virtual bool begin(SPIClass& spi, int cs = -1, int drdy = -1) { return false; }
50+
virtual bool begin(TwoWire& i2c, int drdy = -1) { return false; }
51+
virtual void reset() {}
52+
virtual int status() const { return -1; } // 0 - success, otherwise error
53+
virtual uint8_t whoAmI() { return 0; }
54+
virtual bool read() { return false; }
55+
virtual void getAccel(float& x, float& y, float& z) const { x = y = z = NAN; }
56+
virtual void getGyro(float& x, float& y, float& z) const { x = y = z = NAN; }
57+
virtual void getMag(float& x, float& y, float& z) const { x = y = z = NAN; }
58+
virtual float getTemp() { return NAN; }
59+
virtual bool setRate(const Rate rate) { return false; }
60+
virtual float getRate() { return 0; }
61+
virtual bool setAccelRange(const AccelRange range) { return false; }
62+
virtual bool setGyroRange(const GyroRange range) { return false; }
63+
virtual bool setDLPF(const DLPF dlpf) { return false; }
64+
virtual char const* getModel() const { return "None"; }
65+
virtual bool setupInterrupt() { return false; }
66+
67+
// Fabric methods
68+
static IMU *create(int model, SPIClass& spi, int cs = -1, int drdy = -1);
69+
static IMU *create(int model, TwoWire& i2c, int drdy = -1);
70+
6871
private:
6972
bool usingInterrupt = false;
7073
int interruptPin = -1;
@@ -96,7 +99,7 @@ class IMUBase : public IMUInterface, public Logger {
9699
return false;
97100
}
98101

99-
timerAttachInterruptArg(timer, IMUBase::interruptHandler, interruptSemaphore);
102+
timerAttachInterruptArg(timer, interruptHandler, interruptSemaphore);
100103
timerAlarm(timer, alarmValue, true, 0);
101104
usingInterrupt = true;
102105
return true;
@@ -105,7 +108,7 @@ class IMUBase : public IMUInterface, public Logger {
105108
bool setupInterruptPin(uint8_t pin) {
106109
interruptSemaphore = xSemaphoreCreateBinary();
107110
pinMode(pin, INPUT_PULLUP);
108-
attachInterruptArg(digitalPinToInterrupt(pin), IMUBase::interruptHandler, interruptSemaphore, FALLING);
111+
attachInterruptArg(digitalPinToInterrupt(pin), interruptHandler, interruptSemaphore, FALLING);
109112
usingInterrupt = true;
110113
interruptPin = pin;
111114
return true;
@@ -116,7 +119,7 @@ class IMUBase : public IMUInterface, public Logger {
116119
#endif
117120

118121
protected:
119-
bool setupInterrupt(int pin = -1) {
122+
bool setupInterrupt(int pin) {
120123
if (usingInterrupt) return true; // already set
121124

122125
if (pin == -1) {
@@ -127,7 +130,7 @@ class IMUBase : public IMUInterface, public Logger {
127130
}
128131

129132
public:
130-
void waitForData() override {
133+
virtual void waitForData() {
131134
if (this->status() && interruptPin != -1) return; // don't hang if error and interrupt pin is used
132135

133136
if (usingInterrupt) {

MPU6050.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ void MPU6050_Base::getGyro(float& x, float& y, float& z) const {
20972097

20982098
bool MPU6050_Base::setupInterrupt() {
20992099
// TODO: implement pin interrupt
2100-
return IMUBase::setupInterrupt();
2100+
return IMU::setupInterrupt();
21012101
}
21022102

21032103
// TEMP_OUT_* registers

MPU6050.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ enum class GYRO_FS {
451451
G2000DPS
452452
};
453453

454-
class MPU6050_Base : public IMUBase {
454+
class MPU6050_Base : public IMU {
455455
public:
456456
MPU6050_Base(TwoWire& wire, int drdy = -1, uint8_t addr = MPU6050_DEFAULT_ADDRESS) : wireObj(&wire), devAddr(addr) {};
457457
// TODO: support drdy

MPU9250.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ float MPU9250::getRate() {
509509
return 1000.0f / (srd_ + 1);
510510
}
511511
bool MPU9250::setupInterrupt() {
512-
if (!IMUBase::setupInterrupt(int_pin_)) {
512+
if (!IMU::setupInterrupt(int_pin_)) {
513513
log(errorFmt, status_ = 22);
514514
return false;
515515
}

MPU9250.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "invensense_imu.h" // NOLINT
4040
#include "logger.h"
4141

42-
class MPU9250 : public IMUBase {
42+
class MPU9250 : public IMU {
4343
public:
4444
/* Sensor and filter settings */
4545
enum I2cAddr : uint8_t {

0 commit comments

Comments
 (0)