Skip to content

Commit

Permalink
Universal interface for setting accel and gyro range
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed Dec 4, 2024
1 parent 1b6e328 commit ba093a3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 58 deletions.
44 changes: 42 additions & 2 deletions ICM20948.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,32 @@ void ICM20948::enableAcc(bool enAcc) {
writeRegister8(0, ICM20948_PWR_MGMT_2, regVal);
}

void ICM20948::setAccRange(ICM20948_accRange accRange) {
bool ICM20948::setAccelRange(const AccelRange range) {
int accRange;
switch (range) {
case ACCEL_RANGE_MIN:
case ACCEL_RANGE_2G:
accRange = ICM20948_ACC_RANGE_2G;
break;
case ACCEL_RANGE_4G:
accRange = ICM20948_ACC_RANGE_4G;
break;
case ACCEL_RANGE_8G:
accRange = ICM20948_ACC_RANGE_8G;
break;
case ACCEL_RANGE_16G:
accRange = ICM20948_ACC_RANGE_16G;
break;
default:
log("Unsupported accel range: %d", range);
return false;
}
regVal = readRegister8(2, ICM20948_ACCEL_CONFIG);
regVal &= ~(0x06);
regVal |= (accRange<<1);
writeRegister8(2, ICM20948_ACCEL_CONFIG, regVal);
accRangeFactor = 1<<accRange;
return true;
}

void ICM20948::setAccDLPF(ICM20948_dlpf dlpf) {
Expand Down Expand Up @@ -99,12 +119,32 @@ void ICM20948::enableGyr(bool enGyr) {
writeRegister8(0, ICM20948_PWR_MGMT_2, regVal);
}

void ICM20948::setGyrRange(ICM20948_gyroRange gyroRange) {
bool ICM20948::setGyroRange(const GyroRange range) {
int gyroRange;
switch (range) {
case GYRO_RANGE_MIN:
case GYRO_RANGE_250DPS:
gyroRange = ICM20948_GYRO_RANGE_250;
break;
case GYRO_RANGE_500DPS:
gyroRange = ICM20948_GYRO_RANGE_500;
break;
case GYRO_RANGE_1000DPS:
gyroRange = ICM20948_GYRO_RANGE_1000;
break;
case GYRO_RANGE_2000DPS:
gyroRange = ICM20948_GYRO_RANGE_2000;
break;
default:
log("Unsupported gyro range: %d", range);
return false;
}
regVal = readRegister8(2, ICM20948_GYRO_CONFIG_1);
regVal &= ~(0x06);
regVal |= (gyroRange<<1);
writeRegister8(2, ICM20948_GYRO_CONFIG_1, regVal);
gyrRangeFactor = (1<<gyroRange);
return true;
}

void ICM20948::setGyrDLPF(ICM20948_dlpf dlpf) {
Expand Down
2 changes: 2 additions & 0 deletions ICM20948.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ class ICM20948 : public IMUInterface, public Logger
uint8_t whoAmI() override;
const char* getModel() const override { return "ICM20948"; }
void enableAcc(bool enAcc);
bool setAccelRange(const AccelRange range) override;
void setAccRange(ICM20948_accRange accRange);
void setAccDLPF(ICM20948_dlpf dlpf);
void setAccSampleRateDivider(uint16_t accSplRateDiv);
void enableGyr(bool enGyr);
bool setGyroRange(const GyroRange range) override;
void setGyrRange(ICM20948_gyroRange gyroRange);
void setGyrDLPF(ICM20948_dlpf dlpf);
void setGyrSampleRateDivider(uint8_t gyrSplRateDiv);
Expand Down
47 changes: 23 additions & 24 deletions IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@

class IMUInterface {
public:
enum IMUType {
UNKNOWN,
MPU9250,
MPU9252,
MPU6500,
ICM20948,
enum DLPF {
DLPF_OFF,
DLPF_MAX,
DLPF_BANDWIDTH_100HZ_APPROX,
DLPF_BANDWIDTH_50HZ_APPROX,
DLPF_BANDWIDTH_5HZ_APPROX,
DLPF_BANDWIDTH_MIN
};
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 {
ACCEL_RANGE_MIN,
ACCEL_RANGE_2G,
ACCEL_RANGE_4G,
ACCEL_RANGE_8G,
ACCEL_RANGE_16G,
ACCEL_RANGE_MAX
};
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 GyroRange {
GYRO_RANGE_MIN,
GYRO_RANGE_250DPS,
GYRO_RANGE_500DPS,
GYRO_RANGE_1000DPS,
GYRO_RANGE_2000DPS,
GYRO_RANGE_MAX
};
enum Rate {
RATE_MIN,
Expand All @@ -53,5 +50,7 @@ class IMUInterface {
virtual void getMag(float& x, float& y, float& z) const = 0;
virtual float getTemp() const = 0;
virtual bool setRate(Rate rate) = 0;
virtual bool setAccelRange(const AccelRange range) = 0;
virtual bool setGyroRange(const GyroRange range) = 0;
virtual const char* getModel() const = 0;
};
55 changes: 25 additions & 30 deletions MPU9250.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,75 +194,70 @@ bool MPU9250::setAccelRange(const AccelRange range) {
spi_clock_ = SPI_CFG_CLOCK_;
/* Check input is valid and set requested range and scale */
switch (range) {
case ACCEL_RANGE_2G: {
requested_accel_range_ = range;
case ACCEL_RANGE_MIN:
case ACCEL_RANGE_2G:
requested_accel_range_ = 0x00;
requested_accel_scale_ = 2.0f / 32767.5f;
break;
}
case ACCEL_RANGE_4G: {
requested_accel_range_ = range;
case ACCEL_RANGE_4G:
requested_accel_range_ = 0x08;
requested_accel_scale_ = 4.0f / 32767.5f;
break;
}
case ACCEL_RANGE_8G: {
requested_accel_range_ = range;
case ACCEL_RANGE_8G:
requested_accel_range_ = 0x10;
requested_accel_scale_ = 8.0f / 32767.5f;
break;
}
case ACCEL_RANGE_16G: {
requested_accel_range_ = range;
case ACCEL_RANGE_MAX:
case ACCEL_RANGE_16G:
requested_accel_range_ = 0x18;
requested_accel_scale_ = 16.0f / 32767.5f;
break;
}
default: {
default:
log("Unsupported accel range: %d", range);
return false;
}
}
/* Try setting the requested range */
if (!WriteRegister(ACCEL_CONFIG_, requested_accel_range_)) {
log("Failed to set accel range");
return false;
}
/* Update stored range and scale */
accel_range_ = requested_accel_range_;
accel_range_ = range;
accel_scale_ = requested_accel_scale_;
return true;
}
bool MPU9250::setGyroRange(const GyroRange range) {
spi_clock_ = SPI_CFG_CLOCK_;
/* Check input is valid and set requested range and scale */
switch (range) {
case GYRO_RANGE_250DPS: {
requested_gyro_range_ = range;
case GYRO_RANGE_MIN:
case GYRO_RANGE_250DPS:
requested_gyro_range_ = 0x00;
requested_gyro_scale_ = 250.0f / 32767.5f;
break;
}
case GYRO_RANGE_500DPS: {
requested_gyro_range_ = range;
case GYRO_RANGE_500DPS:
requested_gyro_range_ = 0x08;
requested_gyro_scale_ = 500.0f / 32767.5f;
break;
}
case GYRO_RANGE_1000DPS: {
requested_gyro_range_ = range;
case GYRO_RANGE_1000DPS:
requested_gyro_range_ = 0x10;
requested_gyro_scale_ = 1000.0f / 32767.5f;
break;
}
case GYRO_RANGE_2000DPS: {
requested_gyro_range_ = range;
case GYRO_RANGE_2000DPS:
requested_gyro_range_ = 0x18;
requested_gyro_scale_ = 2000.0f / 32767.5f;
break;
}
default: {
default:
log("Unsupported gyro range: %d", range);
return false;
}
}
/* Try setting the requested range */
if (!WriteRegister(GYRO_CONFIG_, requested_gyro_range_)) {
log("Failed to set gyro range");
return false;
}
/* Update stored range and scale */
gyro_range_ = requested_gyro_range_;
gyro_range_ = range;
gyro_scale_ = requested_gyro_scale_;
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions MPU9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ class MPU9250 : public IMUInterface, public Logger {
static constexpr int32_t SPI_CFG_CLOCK_ = 1000000;
static constexpr int32_t SPI_READ_CLOCK_ = 15000000;
/* Configuration */
AccelRange accel_range_, requested_accel_range_;
GyroRange gyro_range_, requested_gyro_range_;
AccelRange accel_range_;
GyroRange gyro_range_;
uint8_t requested_accel_range_, requested_gyro_range_;
DlpfBandwidth dlpf_bandwidth_, requested_dlpf_;
float accel_scale_, requested_accel_scale_;
float gyro_scale_, requested_gyro_scale_;
Expand Down

0 comments on commit ba093a3

Please sign in to comment.