From a576bb2969fb4fa120e27a6f736b901fb0e7a065 Mon Sep 17 00:00:00 2001 From: Kaloyan Raev Date: Sun, 30 Nov 2025 15:38:00 +0200 Subject: [PATCH] Fix SIGFPE in MotionController Avoid division by zero when `time - lastTime == 0`. This happenedwhen I started InfiniSim for the first time. --- src/components/motion/MotionController.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 6cfff61f9d..4053f401ea 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -56,9 +56,12 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) // Update accumulated speed // Currently polling at 10Hz, if this ever goes faster scalar and EMA might need adjusting - int32_t speed = std::abs(zHistory[0] - zHistory[histSize - 1] + ((yHistory[0] - yHistory[histSize - 1]) / 2) + - ((xHistory[0] - xHistory[histSize - 1]) / 4)) * - 100 / (time - lastTime); + int32_t speed = 0; + if (time != lastTime) { + speed = std::abs(zHistory[0] - zHistory[histSize - 1] + ((yHistory[0] - yHistory[histSize - 1]) / 2) + + ((xHistory[0] - xHistory[histSize - 1]) / 4)) * + 100 / (time - lastTime); + } // integer version of (.2 * speed) + ((1 - .2) * accumulatedSpeed); accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5;