def main(address, selector): # client configuration conf = { 'mode': 'client', 'peer': '{}'.format(address), } print("Openning session...") zenoh = Zenoh(conf) print("New workspace...") workspace = zenoh.workspace() # getting the data from zenoh, it can come from a storage or an eval print("Get Data from '{}'...".format(selector)) output_data = [] for data in workspace.get(selector): _, _, m, s, n = data.path.split('/') m = int(m) s = int(s) n = int(n) output_entry = { 'm': m, 's': s, 'n': n, 'value': data.value.get_content(), 'timestamp': data.timestamp } output_data.append(output_entry) sorted_output_data = sorted(output_data, key=lambda k: k['n']) for data in sorted_output_data: print(data) zenoh.close()
parser.add_argument('--selector', '-s', dest='selector', default='/demo/example/**', type=str, help='The selection of resources to get.') args = parser.parse_args() conf = { "mode": args.mode } if args.peer is not None: conf["peer"] = ",".join(args.peer) if args.listener is not None: conf["listener"] = ",".join(args.listener) selector = args.selector # zenoh-net code --- --- --- --- --- --- --- --- --- --- --- # initiate logging zenoh.init_logger() print("Openning session...") zenoh = Zenoh(conf) print("New workspace...") workspace = zenoh.workspace() print("Get Data from '{}'...".format(selector)) for data in workspace.get(selector): print(' {} : {} (encoding: {} , timestamp: {})'.format( data.path, data.value.get_content(), data.value.encoding_descr(), data.timestamp)) zenoh.close()
def main(stdscr): # --- Command line argument parsing --- --- --- --- --- --- parser = argparse.ArgumentParser(prog='zn_sub', description='zenoh-net sub example') parser.add_argument('--mode', '-m', dest='mode', choices=['peer', 'client'], type=str, help='The zenoh session mode.') parser.add_argument( '--peer', '-e', dest='peer', metavar='LOCATOR', action='append', type=str, help='Peer locators used to initiate the zenoh session.') parser.add_argument('--listener', '-l', dest='listener', metavar='LOCATOR', action='append', type=str, help='Locators to listen on.') parser.add_argument('--config', '-c', dest='config', metavar='FILE', type=str, help='A configuration file.') parser.add_argument('--cmd_vel', dest='cmd_vel', default='/rt/turtle1/cmd_vel', type=str, help='The "cmd_vel" ROS2 topic.') parser.add_argument('--rosout', dest='rosout', default='/rt/rosout', type=str, help='The "rosout" ROS2 topic.') parser.add_argument('--angular_scale', '-a', dest='angular_scale', default='2.0', type=float, help='The angular scale.') parser.add_argument('--linear_scale', '-x', dest='linear_scale', default='2.0', type=float, help='The linear scale.') args = parser.parse_args() conf = zenoh.config_from_file( args.config) if args.config is not None else {} if args.mode is not None: conf["mode"] = args.mode if args.peer is not None: conf["peer"] = ",".join(args.peer) if args.listener is not None: conf["listener"] = ",".join(args.listener) cmd_vel = args.cmd_vel rosout = args.rosout angular_scale = args.angular_scale linear_scale = args.linear_scale # zenoh-net code --- --- --- --- --- --- --- --- --- --- --- # initiate logging zenoh.init_logger() print("Openning session...") z = Zenoh(conf) workspace = z.workspace() def rosout_callback(change): if change.value is not None: log = Log.deserialize(change.value.get_content()) print('[{}.{}] [{}]: {}'.format(log.stamp.sec, log.stamp.nanosec, log.name, log.msg)) print("Subscriber on '{}'...".format(rosout)) sub = workspace.subscribe(rosout, rosout_callback) def pub_twist(linear, angular): print("Pub twist: {} - {}".format(linear, angular)) t = Twist(linear=Vector3(x=linear, y=0.0, z=0.0), angular=Vector3(x=0.0, y=0.0, z=angular)) workspace.put(cmd_vel, t.serialize()) while True: c = stdscr.getch() if c == curses.KEY_UP: pub_twist(1.0 * linear_scale, 0.0) elif c == curses.KEY_DOWN: pub_twist(-1.0 * linear_scale, 0.0) elif c == curses.KEY_LEFT: pub_twist(0.0, 1.0 * angular_scale) elif c == curses.KEY_RIGHT: pub_twist(0.0, -1.0 * angular_scale) elif c == 27 or c == ord('q'): break sub.close() zenoh.close()