diff --git a/i2rt/motor_drivers/dm_driver.py b/i2rt/motor_drivers/dm_driver.py index 1ef69541..7048e8cc 100644 --- a/i2rt/motor_drivers/dm_driver.py +++ b/i2rt/motor_drivers/dm_driver.py @@ -532,8 +532,8 @@ def start_thread(self) -> None: if self.start_thread_flag: return logging.info("starting separate thread for control loop") - thread = threading.Thread(target=self._set_torques_and_update_state) - thread.start() + self._control_thread = threading.Thread(target=self._set_torques_and_update_state) + self._control_thread.start() self.start_thread_flag = True time.sleep(0.1) while self.state is None: @@ -748,6 +748,8 @@ def get_same_bus_device_states(self) -> Any: def close(self) -> None: self.running = False + if hasattr(self, "_control_thread") and self._control_thread.is_alive(): + self._control_thread.join(timeout=2.0) self.motor_interface.close()