From c78e0fd71ff07250e1f2fdac1855ce7ac6e2c6c2 Mon Sep 17 00:00:00 2001 From: Florian Paproth Date: Thu, 12 Nov 2026 22:48:09 +0100 Subject: [PATCH 1/2] fix(backend): isolate mixed transport status and add Pi CAN LED profile --- .../openuc2-first-cord-30059-mixed.yaml | 85 +++++++++++++++++++ backend/newswitch/managers/uc2/canopen_bus.py | 7 +- backend/newswitch/managers/uc2/rest_bus.py | 7 +- .../newswitch/managers/uc2/yaml_runtime.py | 7 +- backend/newswitch/protocols/uc2.py | 11 ++- backend/tests/test_yaml_hardware.py | 33 +++++++ 6 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 backend/configs/machines/openuc2-first-cord-30059-mixed.yaml diff --git a/backend/configs/machines/openuc2-first-cord-30059-mixed.yaml b/backend/configs/machines/openuc2-first-cord-30059-mixed.yaml new file mode 100644 index 0000000..ec97294 --- /dev/null +++ b/backend/configs/machines/openuc2-first-cord-30059-mixed.yaml @@ -0,0 +1,85 @@ +# Pi 192.168.0.46: migrated from the selected ImSwitch FRAME.json on 2026-11-03. +# Mixed test setup 2026-11-12: Serial axes/LED plus CAN RGB LED node 20. +# The CAN light uses the firmware LED-array interface; it is a separate USB bus. +# X/Y: 0.396 um/step; Z: 0.3125 um/step. Speeds below are conservative initial settings. +# No automatic homing. A retains the legacy linear calibration in um. +# Camera omitted: installed MVS SDK is incompatible with the pinned camera driver. +newswitch_version: v1.0.2 +devices: + serial-master: + type: controller + name: UC2 Serial controller + connection: + protocol: uc2-rest + transport: + type: serial + port: /dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0 + baudrate: 115200 + mapping: + axis-x: {type: serial-motor, id: 1} + axis-y: {type: serial-motor, id: 2} + axis-z: {type: serial-motor, id: 3} + axis-a: {type: serial-motor, id: 0} + illumination-led: {type: serial-laser, id: 1, pwm_max: 1023} + sample-stage: + type: stage + axes: {x: axis-x, y: axis-y, z: axis-z, a: axis-a} + homing_order: [z, x, y, a] + registered_step_sizes: [1, 10, 100] + axis-x: + type: axis + steps_per_um: 2.525252525252525 + homing_required: false + homing_direction: -1 + homing_speed_steps: 10000 + homing_endstop_polarity: 1 + homing_timeout_ms: 20000 + vel: {value: 1000, min: 1, max: 5940, unit: um/s} + axis-y: + type: axis + steps_per_um: 2.525252525252525 + homing_required: false + homing_direction: -1 + homing_speed_steps: 10000 + homing_endstop_polarity: -1 + homing_timeout_ms: 20000 + vel: {value: 1000, min: 1, max: 5940, unit: um/s} + axis-z: + type: axis + steps_per_um: 3.2 + homing_required: false + homing_direction: 1 + homing_speed_steps: 10000 + homing_endstop_polarity: -1 + homing_timeout_ms: 20000 + vel: {value: 500, min: 1, max: 4687.5, unit: um/s} + axis-a: + type: axis + steps_per_um: 2.525252525252525 + homing_required: false + homing_direction: -1 + homing_speed_steps: 10000 + homing_endstop_polarity: 1 + homing_timeout_ms: 20000 + vel: {value: 1000, min: 1, max: 5940, unit: um/s} + illumination-led: + type: lightsource + name: Illumination LED + coherence: incoherent + can-led-controller: + type: controller + name: USB CAN LED controller + connection: + protocol: canopen + transport: + type: can + interface: waveshare + port: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0 + bitrate: 500000 + mapping: + can-illumination-led: {type: canopen-led-matrix, id: 20} + can-illumination-led: + type: lightsource + name: CAN RGB LED + coherence: incoherent + channels: 3 diff --git a/backend/newswitch/managers/uc2/canopen_bus.py b/backend/newswitch/managers/uc2/canopen_bus.py index 3b86c01..f434752 100644 --- a/backend/newswitch/managers/uc2/canopen_bus.py +++ b/backend/newswitch/managers/uc2/canopen_bus.py @@ -30,6 +30,7 @@ PositionUpdate, UC2Event, UC2State, + UC2TransportState, ) if TYPE_CHECKING: # pragma: no cover @@ -86,9 +87,11 @@ class UC2CanBusConfig: class UC2CanBus: """UC2BusManager implementation speaking CANopen via uc2canopen.""" - state: UC2State + state: UC2State | UC2TransportState - def __init__(self, state: UC2State, config: Optional[UC2CanBusConfig] = None) -> None: + def __init__( + self, state: UC2State | UC2TransportState, config: Optional[UC2CanBusConfig] = None + ) -> None: """Initialize the CAN transport with shared bus state (no I/O yet).""" self.state = state self.config = config or UC2CanBusConfig() diff --git a/backend/newswitch/managers/uc2/rest_bus.py b/backend/newswitch/managers/uc2/rest_bus.py index a91b5e3..e9c48f1 100644 --- a/backend/newswitch/managers/uc2/rest_bus.py +++ b/backend/newswitch/managers/uc2/rest_bus.py @@ -30,6 +30,7 @@ PositionUpdate, UC2Event, UC2State, + UC2TransportState, ) if TYPE_CHECKING: # pragma: no cover @@ -69,9 +70,11 @@ class UC2RestBusConfig: class UC2RestBus: """UC2BusManager implementation speaking the UC2-REST serial JSON protocol.""" - state: UC2State + state: UC2State | UC2TransportState - def __init__(self, state: UC2State, config: Optional[UC2RestBusConfig] = None) -> None: + def __init__( + self, state: UC2State | UC2TransportState, config: Optional[UC2RestBusConfig] = None + ) -> None: """Initialize the serial transport with shared bus state (no I/O yet).""" self.state = state self.config = config or UC2RestBusConfig() diff --git a/backend/newswitch/managers/uc2/yaml_runtime.py b/backend/newswitch/managers/uc2/yaml_runtime.py index e58bd26..491a7d9 100644 --- a/backend/newswitch/managers/uc2/yaml_runtime.py +++ b/backend/newswitch/managers/uc2/yaml_runtime.py @@ -35,6 +35,7 @@ from rekuest_next.state.lock import acquired_locks from newswitch.protocols.uc2 import ( UC2State, + UC2TransportState, UC2Event, PositionUpdate, MotionDone, @@ -340,7 +341,9 @@ def _bus_for(self, connection: Connection) -> HardwareBus: raise ValueError( "UC2 Serial timing uses client defaults; configure movement/homing deadlines per axis" ) - bus = UC2RestBus(UC2State(), UC2RestBusConfig(serialport=t.port, baudrate=t.baudrate)) + bus = UC2RestBus( + UC2TransportState(), UC2RestBusConfig(serialport=t.port, baudrate=t.baudrate) + ) else: if not isinstance(t, CanTransport): raise ValueError("Direct CANopen requires a CAN transport") @@ -349,7 +352,7 @@ def _bus_for(self, connection: Connection) -> HardwareBus: if t.interface not in ("socketcan", "waveshare"): raise ValueError(f"UC2 CAN runtime does not support {t.interface}") bus = UC2CanBus( - UC2State(), + UC2TransportState(), UC2CanBusConfig( interface=t.interface, channel=t.channel, diff --git a/backend/newswitch/protocols/uc2.py b/backend/newswitch/protocols/uc2.py index e4afa1b..c654547 100644 --- a/backend/newswitch/protocols/uc2.py +++ b/backend/newswitch/protocols/uc2.py @@ -106,10 +106,9 @@ class BusError: # --------------------------------------------------------------------------- -@state @dataclass -class UC2State: - """Connection, identity, and safety state of the UC2 hardware bus.""" +class UC2TransportState: + """Private connection state of one physical transport; never publishes UI patches.""" connected: bool = False transport: str = "virtual" @@ -119,6 +118,12 @@ class UC2State: last_error: str = "" +@state +@dataclass +class UC2State(UC2TransportState): + """Connection, identity, and safety state of the UC2 hardware bus.""" + + # --------------------------------------------------------------------------- # Bus protocol # --------------------------------------------------------------------------- diff --git a/backend/tests/test_yaml_hardware.py b/backend/tests/test_yaml_hardware.py index 8d75bd9..d76e497 100644 --- a/backend/tests/test_yaml_hardware.py +++ b/backend/tests/test_yaml_hardware.py @@ -729,3 +729,36 @@ def test_serial_stable_symlink_resolves_for_uc2_port_lookup(tmp_path: Path) -> N doc["devices"]["my-standalone-board"]["connection"]["transport"]["port"] = str(stable) bus = runtime(doc) assert bus.axes["X"].bus.config.serialport == str(target) + + +@pytest.mark.asyncio +async def test_private_transport_updates_do_not_publish_false_connected_state() -> None: + """A connected Serial bus must not overwrite the aggregate when CAN is down.""" + from rekuest_next.state.publish import BasePublisher + + doc = yaml.safe_load( + (DEMOS.parent / "machines" / "openuc2-first-cord-30059-mixed.yaml").read_text() + ) + bus = runtime(doc) + published = [] + holder = SimpleNamespace(publish_patch=lambda name, patch: published.append((name, patch))) + with BasePublisher(holder): + serial, can = list(bus.buses.values()) + serial.state.connected = True + serial.state.transport = "rest" + can.state.transport = "canopen" + can.state.last_error = "CAN offline" + assert published == [] + assert not bus.state.connected + task = asyncio.create_task(bus._mirror_state()) + await asyncio.sleep(0) + assert not bus.state.connected + assert bus.state.last_error == "CAN offline" + assert not any(p.path == "/connected" and p.value is True for _, p in published) + can.state.connected = True + can.state.last_error = "" + await asyncio.sleep(0.12) + assert bus.state.connected + assert bus.state.last_error == "" + task.cancel() + await asyncio.gather(task, return_exceptions=True) From 0d8253c3ce8934a7d8c44a5895e0f876378e7458 Mon Sep 17 00:00:00 2001 From: Florian Paproth Date: Fri, 13 Nov 2026 08:36:51 +0100 Subject: [PATCH 2/2] Add assertion for UC2State connection in tests Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- backend/tests/test_yaml_hardware.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/tests/test_yaml_hardware.py b/backend/tests/test_yaml_hardware.py index d76e497..ff74862 100644 --- a/backend/tests/test_yaml_hardware.py +++ b/backend/tests/test_yaml_hardware.py @@ -760,5 +760,9 @@ async def test_private_transport_updates_do_not_publish_false_connected_state() await asyncio.sleep(0.12) assert bus.state.connected assert bus.state.last_error == "" + assert any( + name == "UC2State" and p.path == "/connected" and p.value is True + for name, p in published + ) task.cancel() await asyncio.gather(task, return_exceptions=True)