|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +""" |
| 6 | +Message channel example using TeleopSession + retargeting source/sink nodes. |
| 7 | +
|
| 8 | +Behavior: |
| 9 | +- Prints any incoming messages each frame. |
| 10 | +- Once channel status is CONNECTED, sends one message every second. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import sys |
| 15 | +import time |
| 16 | +import uuid |
| 17 | + |
| 18 | +from isaacteleop.retargeting_engine.deviceio_source_nodes import ( |
| 19 | + MessageChannelConnectionStatus, |
| 20 | + message_channel_config, |
| 21 | +) |
| 22 | +from isaacteleop.retargeting_engine.interface import TensorGroup |
| 23 | +from isaacteleop.schema import MessageChannelMessages, MessageChannelMessagesTrackedT |
| 24 | +from isaacteleop.teleop_session_manager import TeleopSession, TeleopSessionConfig |
| 25 | + |
| 26 | + |
| 27 | +def _parse_uuid_bytes(uuid_text: str) -> bytes: |
| 28 | + """Parse canonical UUID text to 16-byte payload.""" |
| 29 | + return uuid.UUID(uuid_text).bytes |
| 30 | + |
| 31 | + |
| 32 | +def _enqueue_outbound_message(sink, payload: bytes) -> None: |
| 33 | + """Push one outbound message through MessageChannelSink.""" |
| 34 | + tg = TensorGroup(sink.input_spec()["messages_tracked"]) |
| 35 | + tg[0] = MessageChannelMessagesTrackedT([MessageChannelMessages(payload)]) |
| 36 | + sink.compute({"messages_tracked": tg}, {}) |
| 37 | + |
| 38 | + |
| 39 | +def main() -> int: |
| 40 | + parser = argparse.ArgumentParser( |
| 41 | + description="Message channel TeleopSession example" |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--channel-uuid", |
| 45 | + type=str, |
| 46 | + required=True, |
| 47 | + help="Message channel UUID (canonical form, e.g. 550e8400-e29b-41d4-a716-446655440000)", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "--channel-name", |
| 51 | + type=str, |
| 52 | + default="example_message_channel", |
| 53 | + help="Optional channel display name", |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--outbound-queue-capacity", |
| 57 | + type=int, |
| 58 | + default=256, |
| 59 | + help="Bounded outbound queue length", |
| 60 | + ) |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + channel_uuid = _parse_uuid_bytes(args.channel_uuid) |
| 64 | + |
| 65 | + source, sink = message_channel_config( |
| 66 | + name="message_channel", |
| 67 | + channel_uuid=channel_uuid, |
| 68 | + channel_name=args.channel_name, |
| 69 | + outbound_queue_capacity=args.outbound_queue_capacity, |
| 70 | + ) |
| 71 | + |
| 72 | + config = TeleopSessionConfig( |
| 73 | + app_name="MessageChannelExample", |
| 74 | + pipeline=source, |
| 75 | + ) |
| 76 | + |
| 77 | + print("=" * 80) |
| 78 | + print("Message Channel TeleopSession Example") |
| 79 | + print("=" * 80) |
| 80 | + print(f"Channel UUID: {args.channel_uuid}") |
| 81 | + print(f"Channel Name: {args.channel_name}") |
| 82 | + print("Press Ctrl+C to exit.") |
| 83 | + print() |
| 84 | + |
| 85 | + send_counter = 0 |
| 86 | + last_send_time = 0.0 |
| 87 | + |
| 88 | + with TeleopSession(config) as session: |
| 89 | + while True: |
| 90 | + result = session.step() |
| 91 | + status = result["status"][0] |
| 92 | + messages_tracked = result["messages_tracked"][0] |
| 93 | + messages = ( |
| 94 | + messages_tracked.data if messages_tracked.data is not None else [] |
| 95 | + ) |
| 96 | + |
| 97 | + for msg in messages: |
| 98 | + payload = bytes(msg.payload) |
| 99 | + try: |
| 100 | + decoded = payload.decode("utf-8") |
| 101 | + print(f"[rx] {decoded}") |
| 102 | + except UnicodeDecodeError: |
| 103 | + print(f"[rx] 0x{payload.hex()}") |
| 104 | + |
| 105 | + now = time.monotonic() |
| 106 | + if ( |
| 107 | + status == MessageChannelConnectionStatus.CONNECTED |
| 108 | + and now - last_send_time >= 1.0 |
| 109 | + ): |
| 110 | + payload_text = f"hello #{send_counter} @ {time.time():.3f}" |
| 111 | + _enqueue_outbound_message(sink, payload_text.encode("utf-8")) |
| 112 | + print(f"[tx] {payload_text}") |
| 113 | + last_send_time = now |
| 114 | + send_counter += 1 |
| 115 | + |
| 116 | + time.sleep(0.01) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + try: |
| 121 | + sys.exit(main()) |
| 122 | + except KeyboardInterrupt: |
| 123 | + print("\nExiting.") |
| 124 | + sys.exit(0) |
0 commit comments