Skip to content

Commit 6cf4ee8

Browse files
committed
Fix IMU scaling at different ranges
mg/LSB and dps/LSB are defined for the minimum range of each sensor, so need to scale when using different ranges
1 parent 7445655 commit 6cf4ee8

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

XRPLib/imu.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def __init__(self, scl_pin: int, sda_pin: int, addr):
3939

4040
# Vector of IMU measurements
4141
self.irq_v = [[0, 0, 0], [0, 0, 0]]
42+
43+
# Scale factors when ranges are changed
44+
self._acc_scale_factor = 1
45+
self._gyro_scale_factor = 1
4246

4347
# Copies of registers. Bytes and structs share the same memory
4448
# addresses, so changing one changes the other
@@ -166,10 +170,10 @@ def _set_if_inc(self, if_inc = True):
166170
self._setreg(LSM_REG_CTRL3_C, self.reg_ctrl3_c_byte[0])
167171

168172
def _raw_to_mg(self, raw):
169-
return self._int16((raw[1] << 8) | raw[0]) * LSM_MG_PER_LSB
173+
return self._int16((raw[1] << 8) | raw[0]) * LSM_MG_PER_LSB_2G * self._acc_scale_factor
170174

171175
def _raw_to_mdps(self, raw):
172-
return self._int16((raw[1] << 8) | raw[0]) * LSM_MDPS_PER_LSB
176+
return self._int16((raw[1] << 8) | raw[0]) * LSM_MDPS_PER_LSB_125DPS * self._gyro_scale_factor
173177

174178
def _get_gyro_x_rate(self):
175179
"""
@@ -406,6 +410,8 @@ def acc_scale(self, value=None):
406410
# Set value as requested
407411
self.reg_ctrl1_xl_bits.FS_XL = LSM_ACCEL_FS[value]
408412
self._setreg(LSM_REG_CTRL1_XL, self.reg_ctrl1_xl_byte[0])
413+
# Update scale factor for converting raw data
414+
self._acc_scale_factor = int(value.rstrip('g')) // 2
409415

410416
def gyro_scale(self, value=None):
411417
"""
@@ -424,6 +430,8 @@ def gyro_scale(self, value=None):
424430
# Set value as requested
425431
self.reg_ctrl2_g_bits.FS_G = LSM_GYRO_FS[value]
426432
self._setreg(LSM_REG_CTRL2_G, self.reg_ctrl2_g_byte[0])
433+
# Update scale factor for converting raw data
434+
self._gyro_scale_factor = int(value.rstrip('dps')) // 125
427435

428436
def acc_rate(self, value=None):
429437
"""

XRPLib/imu_defs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@
7878
"""
7979
Other contants
8080
"""
81-
LSM_MG_PER_LSB = 0.061
82-
LSM_MDPS_PER_LSB = 4.375
81+
LSM_MG_PER_LSB_2G = 0.061
82+
LSM_MDPS_PER_LSB_125DPS = 4.375

0 commit comments

Comments
 (0)