Skip to content

Commit 7c2f19a

Browse files
committed
Reset member IMU variables when reset() is called
Also add _reset_member_variables() to reduce duplicate code in reset() and __init__()
1 parent f4cfb94 commit 7c2f19a

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

XRPLib/imu.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ def __init__(self, scl_pin: int, sda_pin: int, addr):
3333
self.i2c = I2C(id=1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=400000)
3434
self.addr = addr
3535

36+
# Initialize member variables
37+
self._reset_member_variables()
38+
3639
# Transmit and recieve buffers
3740
self.tb = bytearray(1)
3841
self.rb = bytearray(1)
3942

40-
# Vector of IMU measurements
41-
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
46-
4743
# Copies of registers. Bytes and structs share the same memory
4844
# addresses, so changing one changes the other
4945
self.reg_ctrl1_xl_byte = bytearray(1)
@@ -75,19 +71,27 @@ def __init__(self, scl_pin: int, sda_pin: int, addr):
7571
self.acc_rate('208Hz')
7672
self.gyro_rate('208Hz')
7773

78-
# Initialize offsets to zero
74+
"""
75+
The following are private helper methods to read and write registers, as well as to convert the read values to the correct unit.
76+
"""
77+
78+
def _reset_member_variables(self):
79+
# Vector of IMU measurements
80+
self.irq_v = [[0, 0, 0], [0, 0, 0]]
81+
82+
# Sensor offsets
7983
self.gyro_offsets = [0,0,0]
8084
self.acc_offsets = [0,0,0]
8185

82-
# Initialize integrators to zero
86+
# Scale factors when ranges are changed
87+
self._acc_scale_factor = 1
88+
self._gyro_scale_factor = 1
89+
90+
# Angle integrators
8391
self.running_pitch = 0
8492
self.running_yaw = 0
8593
self.running_roll = 0
8694

87-
"""
88-
The following are private helper methods to read and write registers, as well as to convert the read values to the correct unit.
89-
"""
90-
9195
def _int16(self, d):
9296
return d if d < 0x8000 else d - 0x10000
9397

@@ -223,6 +227,12 @@ def reset(self, wait_for_reset = True, wait_timeout_ms = 100):
223227
:return: False if timeout occurred, otherwise True
224228
:rtype: bool
225229
"""
230+
# Stop timer
231+
self._stop_timer()
232+
233+
# Reset member variables
234+
self._reset_member_variables()
235+
226236
# Set BOOT and SW_RESET bits
227237
self.reg_ctrl3_c_byte[0] = self._getreg(LSM_REG_CTRL3_C)
228238
self.reg_ctrl3_c_bits.BOOT = 1

0 commit comments

Comments
 (0)