Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions i2rt/motor_drivers/dm_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def __init__(
self.command_lock = threading.RLock()

self.start_thread_flag = False
self._control_thread: Optional[threading.Thread] = None
if start_thread:
self.start_thread()

Expand Down Expand Up @@ -532,8 +533,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:
Expand Down Expand Up @@ -748,6 +749,13 @@ def get_same_bus_device_states(self) -> Any:

def close(self) -> None:
self.running = False
# Give the control loop a chance to see running=False and fall out
# before its CAN socket is closed underneath it. Without this it can
# be inside _set_commands when the socket goes, and dies with
# "file descriptor cannot be a negative integer (-1)" on every
# shutdown. The timeout keeps close() bounded if the loop is wedged.
if self._control_thread is not None:
self._control_thread.join(timeout=1.0)
self.motor_interface.close()


Expand Down