From 2d26f030b20058a98147ca2781ead4fdad31e16a Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:35 +0100 Subject: [PATCH 1/6] Log STATUS messages with dedicated logger --- cri_lib/cri_controller.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index f5b6dc2..96d5002 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -122,6 +122,7 @@ def __init__(self) -> None: self.answer_events: dict[str, list[asyncio.Event]] = {} self.error_messages: dict[asyncio.Event, str] = {} + self.status_logger = logging.getLogger(__name__ + "/status") self.status_callback: Callable | None = None def connect( @@ -412,7 +413,9 @@ async def _wait_for_answer_async( def _parse_message(self, message: str) -> None: """Internal function to parse a message. If an answer event is registered for a certain msg_id it is triggered.""" - if "STATUS" not in message: + if "STATUS" in message: + self.status_logger.debug("Received status: %s", message) + else: logger.debug("Received: %s", message) if (notification := self.parser.parse_message(message)) is not None: From 2b5ef373f5dc02e1970962a1076bb3b3152b6f1b Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:35 +0100 Subject: [PATCH 2/6] Raise errors with the right context from `wait_for_kinematics_ready_async` --- cri_lib/cri_controller.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 96d5002..0b23176 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -492,6 +492,13 @@ async def wait_for_kinematics_ready_async(self, timeout: float = 30) -> bool: return True new_timeout = timeout - (time() - start_time) + if new_timeout < 0.025: + # We are in an error state and the remaining timeout is too short + # to reliably get another status update. Raise now with context instead of timing out. + raise CRICommandError( + f"Kinematics not ready within {timeout:.2f} seconds. " + f"State is: {self.robot_state.kinematics_state=}, {self.robot_state.combined_axes_error}" + ) return False From 8b6d8c6493a57dae11faf38075e0d3b8fb920927 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:35 +0100 Subject: [PATCH 3/6] Fix hints and docstring of `wait_for_kinematics_ready_async` --- cri_lib/cri_controller.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 0b23176..1a2183c 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -469,7 +469,9 @@ def callback(state: RobotState) """ self.status_callback = callback - async def wait_for_kinematics_ready_async(self, timeout: float = 30) -> bool: + async def wait_for_kinematics_ready_async( + self, timeout: float = 30 + ) -> Literal[True]: """Wait until drive state is indicated as ready. Parameters @@ -477,10 +479,12 @@ async def wait_for_kinematics_ready_async(self, timeout: float = 30) -> bool: timeout : float maximum time to wait in seconds - Returns - ------- - bool - `True`if drives are ready, `False` if not ready or timeout + Raises + ------ + CRITimeoutError + When waiting for a status update times out. + CRICommandError + When no kinematics ready state was not reached within the timeout. """ start_time = time() new_timeout = timeout @@ -499,8 +503,7 @@ async def wait_for_kinematics_ready_async(self, timeout: float = 30) -> bool: f"Kinematics not ready within {timeout:.2f} seconds. " f"State is: {self.robot_state.kinematics_state=}, {self.robot_state.combined_axes_error}" ) - - return False + raise NotImplementedError("This should be unreachable.") async def get_board_temperatures_async( self, From b171bedab2bbb8f46bfc6db847a90ff6585dca45 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:36 +0100 Subject: [PATCH 4/6] Emit logs about errors at WARNING level (previously: DEBUG) --- cri_lib/cri_controller.py | 60 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 1a2183c..4a471ca 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -179,7 +179,7 @@ def connect( # Request the axis count, this is needed for interpreting some messages self._send_command("CONFIG GetAxes") - logger.debug("Connected to %s:%d", host, port) + logger.info("Connected to %s:%d", host, port) return True except ConnectionRefusedError: @@ -659,7 +659,7 @@ async def reset_async(self) -> bool: """ msg_id = self._send_command("CMD Reset", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in RESET command: %s", error_msg) + logger.warning("Error in RESET command: %s", error_msg) return False else: return True @@ -677,7 +677,7 @@ async def enable_async(self) -> bool: """ msg_id = self._send_command("CMD Enable", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in ENABLE command: %s", error_msg) + logger.warning("Error in ENABLE command: %s", error_msg) return False else: return True @@ -693,7 +693,7 @@ async def disable_async(self) -> bool: """ msg_id = self._send_command("CMD Disable", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in DISABLE command: %s", error_msg) + logger.warning("Error in DISABLE command: %s", error_msg) return False else: return True @@ -713,7 +713,7 @@ async def set_active_control_async(self, active: bool) -> bool: f"Active_{str(active).lower()}", ) if (error_msg := await self._wait_for_answer_async(sent_command)) is not None: - logger.debug("Error in set active control command: %s", error_msg) + logger.warning("Error in set active control command: %s", error_msg) return False else: return True @@ -729,7 +729,7 @@ async def zero_all_joints_async(self) -> bool: """ msg_id = self._send_command("CMD SetJointsToZero", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in SetJointsToZero command: %s", error_msg) + logger.warning("Error in SetJointsToZero command: %s", error_msg) return False else: return True @@ -747,7 +747,7 @@ async def reference_all_joints_async(self, *, timeout: float = 30) -> bool: if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=timeout) ) is not None: - logger.debug("Error in ReferenceAllJoints command: %s", error_msg) + logger.warning("Error in ReferenceAllJoints command: %s", error_msg) return False else: return True @@ -780,7 +780,7 @@ async def reference_single_joint_async( if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=timeout) ) is not None: - logger.debug("Error in ReferenceSingleJoint command: %s", error_msg) + logger.warning("Error in ReferenceSingleJoint command: %s", error_msg) return False else: return True @@ -798,7 +798,7 @@ async def get_referencing_info_async(self): "CMD GetReferencingInfo", True, "info_referencing" ) if (error_msg := await self._wait_for_answer_async(sent_command)) is not None: - logger.debug("Error in GetReferencingInfo command: %s", error_msg) + logger.warning("Error in GetReferencingInfo command: %s", error_msg) return False else: return True @@ -892,11 +892,11 @@ async def move_joints_async( move_finished_timeout, ) if command_error is not None: - logger.debug("Error in Move Joints command: %s", command_error) + logger.warning("Error in Move Joints command: %s", command_error) return False if completion_error is not None: - logger.debug("Exec Error in Move Joints command: %s", completion_error) + logger.warning("Exec Error in Move Joints command: %s", completion_error) return False return True @@ -959,11 +959,11 @@ async def move_joints_relative_async( move_finished_timeout, ) if command_error is not None: - logger.debug("Error in Move Joints command: %s", command_error) + logger.warning("Error in Move Joints command: %s", command_error) return False if completion_error is not None: - logger.debug( + logger.warning( "Exec Error in Move Joints Relative command: %s", completion_error ) return False @@ -1034,11 +1034,11 @@ async def move_cartesian_async( move_finished_timeout, ) if command_error is not None: - logger.debug("Error in Move Joints command: %s", command_error) + logger.warning("Error in Move Joints command: %s", command_error) return False if completion_error is not None: - logger.debug("Exec Error in Move Cartesian command: %s", completion_error) + logger.warning("Exec Error in Move Cartesian command: %s", completion_error) return False return True @@ -1106,11 +1106,11 @@ async def move_base_relative_async( move_finished_timeout, ) if command_error is not None: - logger.debug("Error in Move Joints command: %s", command_error) + logger.warning("Error in Move Joints command: %s", command_error) return False if completion_error is not None: - logger.debug( + logger.warning( "Exec Error in Move BaseRelative command: %s", completion_error ) return False @@ -1180,11 +1180,11 @@ async def move_tool_relative_async( move_finished_timeout, ) if command_error is not None: - logger.debug("Error in Move Joints command: %s", command_error) + logger.warning("Error in Move Joints command: %s", command_error) return False if completion_error is not None: - logger.debug("Exec Error in Move BaseTool command: %s", completion_error) + logger.warning("Exec Error in Move BaseTool command: %s", completion_error) return False return True @@ -1203,7 +1203,7 @@ async def stop_move_async(self) -> bool: if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=5.0) ) is not None: - logger.debug("Error in Move Stop command: %s", error_msg) + logger.warning("Error in Move Stop command: %s", error_msg) return False else: return True @@ -1280,7 +1280,7 @@ async def set_motion_type_async(self, motion_type: MotionType): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in MotionType command: %s", error_msg) + logger.warning("Error in MotionType command: %s", error_msg) return False else: return True @@ -1303,7 +1303,7 @@ async def set_override_async(self, override: float): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in Override command: %s", error_msg) + logger.warning("Error in Override command: %s", error_msg) return False else: return True @@ -1332,7 +1332,7 @@ async def set_dout_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in DOUT command: %s", error_msg) + logger.warning("Error in DOUT command: %s", error_msg) return False else: return True @@ -1361,7 +1361,7 @@ async def set_din_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in DIN command: %s", error_msg) + logger.warning("Error in DIN command: %s", error_msg) return False else: return True @@ -1390,7 +1390,7 @@ async def set_global_signal_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in DIN command: %s", error_msg) + logger.warning("Error in DIN command: %s", error_msg) return False else: return True @@ -1419,7 +1419,7 @@ async def load_program_async(self, program_name: str) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in load_program command: %s", error_msg) + logger.warning("Error in load_program command: %s", error_msg) return False else: return True @@ -1448,7 +1448,7 @@ async def load_logic_program_async(self, program_name: str) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in load_logic_program command: %s", error_msg) + logger.warning("Error in load_logic_program command: %s", error_msg) return False else: return True @@ -1503,7 +1503,7 @@ async def start_program_async( msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in start_program command: %s", error_msg) + logger.warning("Error in start_program command: %s", error_msg) return False else: return True @@ -1521,7 +1521,7 @@ async def stop_program_async(self) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in stop_program command: %s", error_msg) + logger.warning("Error in stop_program command: %s", error_msg) return False else: return True @@ -1539,7 +1539,7 @@ async def pause_program_async(self) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.debug("Error in pause_program command: %s", error_msg) + logger.warning("Error in pause_program command: %s", error_msg) return False else: return True From 47978bc60873f00f38784a2eb99db30a5819f7cf Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:36 +0100 Subject: [PATCH 5/6] Include ID in extra fields of `cri_controller` logs --- cri_lib/cri_controller.py | 151 ++++++++++++++++++++++++++++---------- 1 file changed, 114 insertions(+), 37 deletions(-) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 4a471ca..505c5d8 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -115,6 +115,10 @@ def __init__(self) -> None: self.receive_thread = threading.Thread( target=self._bg_receive_thread, daemon=True ) + # Identify ourselves in the logs by the receiver thread ID because multiple CRIClients can exist in parallel. + self.log_extra = { + "receive_thread_id": self.receive_thread.ident, + } self.sent_command_counter_lock = threading.Lock() self.sent_command_counter = 0 @@ -274,7 +278,7 @@ def _send_command( try: with self.socket_write_lock: self.sock.sendall(message.encode()) - logger.debug("Sent command: %s", message) + logger.debug("Sent command: %s", message, extra=self.log_extra) return SentCommand(command_counter, answer_event) @@ -340,7 +344,8 @@ def _bg_receive_thread(self) -> None: except Exception as ex: logger.error( "Receive Thread: Failed to parse message, clearing buffer: " - + str(ex) + + str(ex), + extra=self.log_extra, ) message_buffer.clear() @@ -414,9 +419,11 @@ async def _wait_for_answer_async( def _parse_message(self, message: str) -> None: """Internal function to parse a message. If an answer event is registered for a certain msg_id it is triggered.""" if "STATUS" in message: - self.status_logger.debug("Received status: %s", message) + self.status_logger.debug( + "Received status: %s", message, extra=self.log_extra + ) else: - logger.debug("Received: %s", message) + logger.debug("Received: %s", message, extra=self.log_extra) if (notification := self.parser.parse_message(message)) is not None: if notification["answer"] == "status" and self.status_callback is not None: @@ -659,7 +666,9 @@ async def reset_async(self) -> bool: """ msg_id = self._send_command("CMD Reset", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in RESET command: %s", error_msg) + logger.warning( + "Error in RESET command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -677,7 +686,9 @@ async def enable_async(self) -> bool: """ msg_id = self._send_command("CMD Enable", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in ENABLE command: %s", error_msg) + logger.warning( + "Error in ENABLE command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -693,7 +704,9 @@ async def disable_async(self) -> bool: """ msg_id = self._send_command("CMD Disable", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in DISABLE command: %s", error_msg) + logger.warning( + "Error in DISABLE command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -713,7 +726,11 @@ async def set_active_control_async(self, active: bool) -> bool: f"Active_{str(active).lower()}", ) if (error_msg := await self._wait_for_answer_async(sent_command)) is not None: - logger.warning("Error in set active control command: %s", error_msg) + logger.warning( + "Error in set active control command: %s", + error_msg, + extra=self.log_extra, + ) return False else: return True @@ -729,7 +746,9 @@ async def zero_all_joints_async(self) -> bool: """ msg_id = self._send_command("CMD SetJointsToZero", True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in SetJointsToZero command: %s", error_msg) + logger.warning( + "Error in SetJointsToZero command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -747,7 +766,11 @@ async def reference_all_joints_async(self, *, timeout: float = 30) -> bool: if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=timeout) ) is not None: - logger.warning("Error in ReferenceAllJoints command: %s", error_msg) + logger.warning( + "Error in ReferenceAllJoints command: %s", + error_msg, + extra=self.log_extra, + ) return False else: return True @@ -780,7 +803,11 @@ async def reference_single_joint_async( if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=timeout) ) is not None: - logger.warning("Error in ReferenceSingleJoint command: %s", error_msg) + logger.warning( + "Error in ReferenceSingleJoint command: %s", + error_msg, + extra=self.log_extra, + ) return False else: return True @@ -798,7 +825,11 @@ async def get_referencing_info_async(self): "CMD GetReferencingInfo", True, "info_referencing" ) if (error_msg := await self._wait_for_answer_async(sent_command)) is not None: - logger.warning("Error in GetReferencingInfo command: %s", error_msg) + logger.warning( + "Error in GetReferencingInfo command: %s", + error_msg, + extra=self.log_extra, + ) return False else: return True @@ -892,11 +923,17 @@ async def move_joints_async( move_finished_timeout, ) if command_error is not None: - logger.warning("Error in Move Joints command: %s", command_error) + logger.warning( + "Error in Move Joints command: %s", command_error, extra=self.log_extra + ) return False if completion_error is not None: - logger.warning("Exec Error in Move Joints command: %s", completion_error) + logger.warning( + "Exec Error in Move Joints command: %s", + completion_error, + extra=self.log_extra, + ) return False return True @@ -959,12 +996,16 @@ async def move_joints_relative_async( move_finished_timeout, ) if command_error is not None: - logger.warning("Error in Move Joints command: %s", command_error) + logger.warning( + "Error in Move Joints command: %s", command_error, extra=self.log_extra + ) return False if completion_error is not None: logger.warning( - "Exec Error in Move Joints Relative command: %s", completion_error + "Exec Error in Move Joints Relative command: %s", + completion_error, + extra=self.log_extra, ) return False return True @@ -1034,11 +1075,17 @@ async def move_cartesian_async( move_finished_timeout, ) if command_error is not None: - logger.warning("Error in Move Joints command: %s", command_error) + logger.warning( + "Error in Move Joints command: %s", command_error, extra=self.log_extra + ) return False if completion_error is not None: - logger.warning("Exec Error in Move Cartesian command: %s", completion_error) + logger.warning( + "Exec Error in Move Cartesian command: %s", + completion_error, + extra=self.log_extra, + ) return False return True @@ -1106,12 +1153,16 @@ async def move_base_relative_async( move_finished_timeout, ) if command_error is not None: - logger.warning("Error in Move Joints command: %s", command_error) + logger.warning( + "Error in Move Joints command: %s", command_error, extra=self.log_extra + ) return False if completion_error is not None: logger.warning( - "Exec Error in Move BaseRelative command: %s", completion_error + "Exec Error in Move BaseRelative command: %s", + completion_error, + extra=self.log_extra, ) return False @@ -1180,11 +1231,17 @@ async def move_tool_relative_async( move_finished_timeout, ) if command_error is not None: - logger.warning("Error in Move Joints command: %s", command_error) + logger.warning( + "Error in Move Joints command: %s", command_error, extra=self.log_extra + ) return False if completion_error is not None: - logger.warning("Exec Error in Move BaseTool command: %s", completion_error) + logger.warning( + "Exec Error in Move BaseTool command: %s", + completion_error, + extra=self.log_extra, + ) return False return True @@ -1203,7 +1260,9 @@ async def stop_move_async(self) -> bool: if ( error_msg := await self._wait_for_answer_async(msg_id, timeout=5.0) ) is not None: - logger.warning("Error in Move Stop command: %s", error_msg) + logger.warning( + "Error in Move Stop command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1280,7 +1339,9 @@ async def set_motion_type_async(self, motion_type: MotionType): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in MotionType command: %s", error_msg) + logger.warning( + "Error in MotionType command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1303,7 +1364,9 @@ async def set_override_async(self, override: float): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in Override command: %s", error_msg) + logger.warning( + "Error in Override command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1332,7 +1395,7 @@ async def set_dout_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in DOUT command: %s", error_msg) + logger.warning("Error in DOUT command: %s", error_msg, extra=self.log_extra) return False else: return True @@ -1361,7 +1424,7 @@ async def set_din_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in DIN command: %s", error_msg) + logger.warning("Error in DIN command: %s", error_msg, extra=self.log_extra) return False else: return True @@ -1390,7 +1453,7 @@ async def set_global_signal_async(self, id: int, value: bool): msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in DIN command: %s", error_msg) + logger.warning("Error in DIN command: %s", error_msg, extra=self.log_extra) return False else: return True @@ -1419,7 +1482,9 @@ async def load_program_async(self, program_name: str) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in load_program command: %s", error_msg) + logger.warning( + "Error in load_program command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1448,7 +1513,11 @@ async def load_logic_program_async(self, program_name: str) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in load_logic_program command: %s", error_msg) + logger.warning( + "Error in load_logic_program command: %s", + error_msg, + extra=self.log_extra, + ) return False else: return True @@ -1503,7 +1572,9 @@ async def start_program_async( msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in start_program command: %s", error_msg) + logger.warning( + "Error in start_program command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1521,7 +1592,9 @@ async def stop_program_async(self) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in stop_program command: %s", error_msg) + logger.warning( + "Error in stop_program command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1539,7 +1612,9 @@ async def pause_program_async(self) -> bool: msg_id = self._send_command(command, True) if (error_msg := await self._wait_for_answer_async(msg_id)) is not None: - logger.warning("Error in pause_program command: %s", error_msg) + logger.warning( + "Error in pause_program command: %s", error_msg, extra=self.log_extra + ) return False else: return True @@ -1573,11 +1648,13 @@ def upload_file(self, path: str | Path, target_directory: str) -> bool: with open(file_path, "r") as fp: lines = [] while line := fp.readline(): - logger.debug(line) + logger.debug(line, extra=self.log_extra) lines.append(line) except OSError as e: - logger.error("Error reading %s: %s", str(Path), str(e)) + logger.error( + "Error reading %s: %s", str(Path), str(e), extra=self.log_extra + ) return False command = f"CMD UploadFileInit {target_directory + '/' + str(file_path.name)} {len(lines)} 0" @@ -1623,7 +1700,7 @@ def can_send(self, msg_id: int, length: int, data: bytearray) -> None: data for CAN message always 8 bytes """ if not self.can_mode: - logger.debug("can_send: CAN mode not enabled") + logger.debug("can_send: CAN mode not enabled", extra=self.log_extra) return command = f"CANBridge Msg ID {msg_id} Len {length} Data " + " ".join( @@ -1643,7 +1720,7 @@ def can_receive( Returns a tuple of (msg_id, length, data) if a message was received or None if nothing was received within the timeout. """ if not self.can_mode: - logger.debug("can_receive: CAN mode not enabled") + logger.debug("can_receive: CAN mode not enabled", extra=self.log_extra) return None try: From c3413465d37a033b12b8cfe6a946124777a2e873 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 12 Nov 2026 12:12:36 +0100 Subject: [PATCH 6/6] Add auto-reset option to `CRIConnector.control` --- cri_lib/cri_controller.py | 21 ++++++++++++++++++--- pyproject.toml | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 505c5d8..d89fb61 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -1832,7 +1832,9 @@ async def observe(self) -> AsyncIterator[CRIClient]: return @contextlib.asynccontextmanager - async def control(self, *, auto_disable: bool) -> AsyncIterator[CRIController]: + async def control( + self, *, auto_disable: bool, auto_reset: bool = False + ) -> AsyncIterator[CRIController]: """Establish connection for controlling robot state. Parameters @@ -1840,6 +1842,9 @@ async def control(self, *, auto_disable: bool) -> AsyncIterator[CRIController]: auto_disable If ``True`` a graceful exit will call :meth:`CRIConnector.disable` to stop movements and turn off motors. + auto_reset + If `True` a :meth:`self.reset_async` will be called when 50 % of the timeout has passed. + Use with caution. """ controller = CRIController() try: @@ -1854,10 +1859,20 @@ async def control(self, *, auto_disable: bool) -> AsyncIterator[CRIController]: # Take active control if not await controller.set_active_control_async(True): raise CRICommandError("Failed to acquire active control.") + if ( + auto_reset + and controller.robot_state.kinematics_state != KinematicsState.NO_ERROR + ): + logger.warning( + "Auto-Resetting from kinematics state %s", + controller.robot_state.kinematics_state, + ) + if not await controller.reset_async(): + raise CRICommandError("Failed to reset.") if not await controller.enable_async(): raise CRICommandError("Failed to enable robot.") - if not await controller.wait_for_kinematics_ready_async(10): - raise CRICommandError("Kinematics not ready.") + await controller.wait_for_kinematics_ready_async(10) + yield controller # Graceful context exit: give up control (maybe disable robot) then disconnect in finally block. if auto_disable: diff --git a/pyproject.toml b/pyproject.toml index 1494e18..e9144da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "cri_lib" description = "Client library for accessing igus Robot Control via CRI network interface." -version = "0.4.0" +version = "0.4.1" readme = "README.md" requires-python = ">=3.10" authors = [