Skip to content

Commit 7d17712

Browse files
committed
IMU default calibration time update -> 1 sec, can now actually be a float
1 parent d4f9f04 commit 7d17712

File tree

1 file changed

+5
-36
lines changed

1 file changed

+5
-36
lines changed

XRPLib/imu.py

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ def power(self, on:bool=None):
343343
self._r_w_reg(LSM6DSO_CTRL1_XL, 0, 0x0F)
344344
self._r_w_reg(LSM6DSO_CTRL2_G, 0, 0x0F)
345345

346-
def calibrate(self, calibration_time:float=3, vertical_axis:int= 2, update_time:int=4):
346+
def calibrate(self, calibration_time:float=1, vertical_axis:int= 2, update_time:int=4):
347347
"""
348-
Collect readings for 3 seconds and calibrate the IMU based on those readings
348+
Collect readings for [calibration_time] seconds and calibrate the IMU based on those readings
349349
Do not move the robot during this time
350350
Assumes the board to be parallel to the ground. Please use the vertical_axis parameter if that is not correct
351351
@@ -357,12 +357,12 @@ def calibrate(self, calibration_time:float=3, vertical_axis:int= 2, update_time:
357357
:type update_time: int
358358
"""
359359
self.update_timer.deinit()
360-
start_time = time.time()
360+
start_time = time.ticks_ms()
361361
self.acc_offsets = [0,0,0]
362362
self.gyro_offsets = [0,0,0]
363363
avg_vals = [[0,0,0],[0,0,0]]
364364
num_vals = 0
365-
while time.time() < start_time + calibration_time:
365+
while time.ticks_ms() < start_time + calibration_time*1000:
366366
cur_vals = self._get_acc_gyro_rates()
367367
# Accelerometer averages
368368
avg_vals[0][0] = (avg_vals[0][0]*num_vals+cur_vals[0][0])/(num_vals+1)
@@ -393,35 +393,4 @@ def _update_imu_readings(self):
393393
self.running_pitch += delta_pitch
394394
self.running_roll += delta_roll
395395
self.running_yaw += delta_yaw
396-
enable_irq(state)
397-
398-
399-
## REMOVE BELOW BEFORE OFFICIAL RELEASE
400-
401-
# We have two ways of obtaining the pitch of the robot:
402-
# - A noisy but accurate reading from the gyroscope
403-
# - A more consistent reading from the accelerometer that is affected by linear acceleration
404-
# We use a complementary filter to combine these two readings into a steady accurate signal.
405-
406-
"""
407-
scale = self.update_time
408-
measured_angle = math.atan2(self._get_acc_y_rate(), self._get_acc_z_rate()) *180/math.pi * 1000
409-
410-
self.gyro_pitch_running_total += (self._get_gyro_x_rate()-self.gyro_pitch_bias) * scale
411-
412-
if self.gyro_pitch_running_total > math.pi:
413-
self.gyro_pitch_running_total -= 2*math.pi
414-
elif self.gyro_pitch_running_total < -math.pi:
415-
self.gyro_pitch_running_total += 2*math.pi
416-
417-
possible_error = measured_angle - self.gyro_pitch_running_total
418-
419-
# The comp factor is the main tuning value of the complementary filter. A value 0 to 1
420-
# Skews the adjusted pitch to either the gyro total (closer to 0) or the accelerometer (closer to 1)
421-
comp_factor = 0.7
422-
self.adjusted_pitch = (self.gyro_pitch_running_total + comp_factor * possible_error) / 1000
423-
424-
# Bias growth factor
425-
epsilon = 0.015
426-
self.gyro_pitch_bias -= epsilon / scale * possible_error
427-
"""
396+
enable_irq(state)

0 commit comments

Comments
 (0)