@@ -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 """
0 commit comments