"""Fetch one current pose without blocking the UI or starting polling.""" import math from threading import Event from typing import Optional import grpc from PyQt5 import QtCore from clients._path_setup import ensure_paths ensure_paths() from cmvr.api import arm_command_pb2 as pb from cmvr.api import arm_service_pb2_grpc as rpc class PoseQueryWorker(QtCore.QThread): updated = QtCore.pyqtSignal(dict) error = QtCore.pyqtSignal(str) def __init__( self, address: str, device_id: str, base_link: str, ee_link: str, timeout: float = 10.0, ) -> None: super().__init__() self.address = address self.device_id = device_id self.base_link = base_link self.ee_link = ee_link self.timeout = timeout self._stop = Event() self._channel: Optional[grpc.Channel] = None def stop(self) -> None: self._stop.set() if self._channel is not None: self._channel.close() def run(self) -> None: if self._stop.is_set(): return try: self._channel = grpc.insecure_channel(self.address) if self._stop.is_set(): return stub = rpc.ArmServiceStub(self._channel) request = pb.GetPose.Request(base_link=self.base_link, ee_link=self.ee_link) request.header.device_id = self.device_id request.header.timestamp.GetCurrentTime() response = stub.getPose(request, timeout=self.timeout) if self._stop.is_set(): return if not response.header.success: raise ValueError(response.header.error_message or "Server rejected the pose query.") if not response.HasField("pose"): raise ValueError("Server returned no pose.") pose = { name: float(getattr(response.pose, name)) for name in ("x", "y", "z", "rx", "ry", "rz") } if not all(math.isfinite(value) for value in pose.values()): raise ValueError("Server returned a non-finite pose value.") if not self._stop.is_set(): self.updated.emit(pose) except Exception as exc: if not self._stop.is_set(): self.error.emit(f"GetPose failed: {exc}") finally: if self._channel is not None: self._channel.close()