Skip to content

Commit

Permalink
Tidies up the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Feb 13, 2022
1 parent 7723f39 commit c74d54a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 104 deletions.
92 changes: 17 additions & 75 deletions LSM9DS1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,85 +40,27 @@ LSM9DS1::LSM9DS1(DeviceSettings deviceSettings) {
fprintf(stderr,"LSM9DS1: bus=%02x, agAddr=%02x, mAddr=%02x\n",
device.i2c_bus,device.agAddress,device.mAddress);
#endif

// gyro scale can be 245, 500, or 2000
gyro.scale = 245;
// gyro sample rate: value between 1-6
// 1 = 14.9 4 = 238
// 2 = 59.5 5 = 476
// 3 = 119 6 = 952
gyro.sampleRate = 3;
// gyro cutoff frequency: value between 0-3
// Actual value of cutoff frequency depends
// on sample rate.
gyro.bandwidth = 0;
gyro.lowPowerEnable = false;
gyro.HPFEnable = false;
// Gyro HPF cutoff frequency: value between 0-9
// Actual value depends on sample rate. Only applies
// if gyroHPFEnable is true.
gyro.HPFCutoff = 0;
gyro.flipX = false;
gyro.flipY = false;
gyro.flipZ = false;
gyro.orientation = 0;
gyro.latchInterrupt = true;

accel.enabled = true;
accel.enableX = true;
accel.enableY = true;
accel.enableZ = true;
// accel scale can be 2, 4, 8, or 16
accel.scale = 16;
// accel sample rate can be 1-6
// 1 = 10 Hz 4 = 238 Hz
// 2 = 50 Hz 5 = 476 Hz
// 3 = 119 Hz 6 = 952 Hz
accel.sampleRate = 3;
// Accel cutoff freqeuncy can be any value between -1 - 3.
// -1 = bandwidth determined by sample rate
// 0 = 408 Hz 2 = 105 Hz
// 1 = 211 Hz 3 = 50 Hz
accel.bandwidth = -1;
accel.highResEnable = false;
// accelHighResBandwidth can be any value between 0-3
// LP cutoff is set to a factor of sample rate
// 0 = ODR/50 2 = ODR/9
// 1 = ODR/100 3 = ODR/400
accel.highResBandwidth = 0;

mag.enabled = true;
// mag scale can be 4, 8, 12, or 16
mag.scale = 4;
// mag data rate can be 0-7
// 0 = 0.625 Hz 4 = 10 Hz
// 1 = 1.25 Hz 5 = 20 Hz
// 2 = 2.5 Hz 6 = 40 Hz
// 3 = 5 Hz 7 = 80 Hz
mag.sampleRate = 7;
mag.tempCompensationEnable = false;
// magPerformance can be any value between 0-3
// 0 = Low power mode 2 = high performance
// 1 = medium performance 3 = ultra-high performance
mag.XYPerformance = 3;
mag.ZPerformance = 3;
mag.lowPowerEnable = false;

temp.enabled = true;
for (int i=0; i<3; i++)
{
gBias[i] = 0;
aBias[i] = 0;
mBias[i] = 0;
gBiasRaw[i] = 0;
aBiasRaw[i] = 0;
mBiasRaw[i] = 0;
}
for (int i=0; i<3; i++) {
gBias[i] = 0;
aBias[i] = 0;
mBias[i] = 0;
gBiasRaw[i] = 0;
aBiasRaw[i] = 0;
mBiasRaw[i] = 0;
}
_autoCalc = false;
}

uint16_t LSM9DS1::begin()
uint16_t LSM9DS1::begin(AccelSettings accelSettings,
GyroSettings gyroSettings,
MagSettings magSettings,
TemperatureSettings temperatureSettings)
{
accel = accelSettings;
gyro = gyroSettings;
mag = magSettings;
temp = temperatureSettings;

if (device.initPIGPIO) {
int cfg = gpioCfgGetInternals();
cfg |= PI_CFG_NOSIGHANDLER;
Expand Down
62 changes: 33 additions & 29 deletions LSM9DS1.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,6 @@ static const char could_not_open_i2c[] = "Could not open I2C.\n";

#define ISR_TIMEOUT 1000

// Gyroscope settings:
struct GyroSettings
{
uint8_t enabled = true;

// gyro scale can be 245, 500, or 2000
uint16_t scale = 245;

// gyro sample rate (Hz): value between 1-6
// 1 = 14.9 4 = 238
// 2 = 59.5 5 = 476
// 3 = 119 6 = 952
uint8_t sampleRate;

uint8_t bandwidth = 0;
uint8_t lowPowerEnable = false;
uint8_t HPFEnable = false;
uint8_t HPFCutoff = 0;
uint8_t flipX = false;
uint8_t flipY = false;
uint8_t flipZ = false;
uint8_t orientation = 0;
uint8_t enableX = true;
uint8_t enableY = true;
uint8_t enableZ = true;
uint8_t latchInterrupt = true;
};

#define LSM9DS1_AG_ADDR 0x6B
#define LSM9DS1_M_ADDR 0x1E
#define LSM9DS1_DEFAULT_I2C_BUS 1
Expand Down Expand Up @@ -101,6 +73,34 @@ struct AccelSettings
uint8_t highResBandwidth = 0;
};

// Gyroscope settings:
struct GyroSettings
{
uint8_t enabled = true;

// gyro scale can be 245, 500, or 2000
uint16_t scale = 245;

// gyro sample rate (Hz): value between 1-6
// 1 = 14.9 4 = 238
// 2 = 59.5 5 = 476
// 3 = 119 6 = 952
uint8_t sampleRate = 2;

uint8_t bandwidth = 0;
uint8_t lowPowerEnable = false;
uint8_t HPFEnable = false;
uint8_t HPFCutoff = 0;
uint8_t flipX = false;
uint8_t flipY = false;
uint8_t flipZ = false;
uint8_t orientation = 0;
uint8_t enableX = true;
uint8_t enableY = true;
uint8_t enableZ = true;
uint8_t latchInterrupt = true;
};

// Magnetometer settings:
struct MagSettings
{
Expand Down Expand Up @@ -180,7 +180,11 @@ class LSM9DS1
// begin() -- Initialize the gyro, accelerometer, and magnetometer.
// This will set up the scale and output rate of each sensor. The values set
// in the IMUSettings struct will take effect after calling this function.
uint16_t begin();
uint16_t begin(AccelSettings accelSettings = AccelSettings(),
GyroSettings gyroSettings = GyroSettings(),
MagSettings magSettings = MagSettings(),
TemperatureSettings temperatureSettings = TemperatureSettings()
);

// ends a possible thread in the background
void end();
Expand Down

0 comments on commit c74d54a

Please sign in to comment.