From 1a1568c38b7b4e9bd5358eb0125d54652789d4aa Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 17 Dec 2023 16:30:11 -0500 Subject: [PATCH] mpu9250: Fix incorrect use of time.sleep() It is not valid to call time.sleep() in the host python code (it could causes glitches in other processing, and it does not ensure there is a pause between operations on the mcu). Use minclock instead of time.sleep() to ensure there is a sufficient pause during chip startup. Signed-off-by: Kevin O'Connor --- klippy/extras/mpu9250.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/klippy/extras/mpu9250.py b/klippy/extras/mpu9250.py index c975f989..04a33eb2 100644 --- a/klippy/extras/mpu9250.py +++ b/klippy/extras/mpu9250.py @@ -4,7 +4,7 @@ # Copyright (C) 2020-2021 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import logging, time +import logging from . import bus, adxl345, bulk_sensor MPU9250_ADDR = 0x68 @@ -167,8 +167,12 @@ class MPU9250: # Setup chip in requested query rate self.set_reg(REG_PWR_MGMT_1, SET_PWR_MGMT_1_WAKE) self.set_reg(REG_PWR_MGMT_2, SET_PWR_MGMT_2_ACCEL_ON) - time.sleep(20. / 1000) # wait for accelerometer chip wake up - self.set_reg(REG_SMPLRT_DIV, SAMPLE_RATE_DIVS[self.data_rate]) + # Add 20ms pause for accelerometer chip wake up + self.read_reg(REG_DEVID) # Dummy read to ensure queues flushed + systime = self.printer.get_reactor().monotonic() + next_time = self.mcu.estimated_print_time(systime) + 0.020 + self.set_reg(REG_SMPLRT_DIV, SAMPLE_RATE_DIVS[self.data_rate], + minclock=self.mcu.print_time_to_clock(next_time)) self.set_reg(REG_CONFIG, SET_CONFIG) self.set_reg(REG_ACCEL_CONFIG, SET_ACCEL_CONFIG) self.set_reg(REG_ACCEL_CONFIG2, SET_ACCEL_CONFIG2)