|
1 | 1 | use std::time::Duration; |
2 | 2 |
|
3 | 3 | use ros_z::{Builder, Result, context::ZContextBuilder}; |
4 | | -use ros_z_msgs::std_msgs::{ByteMultiArray, MultiArrayLayout}; |
| 4 | +use ros_z_msgs::std_msgs::ByteMultiArray; |
5 | 5 |
|
6 | | -use clap::Parser; |
7 | | -#[derive(Debug, Parser)] |
8 | | -struct Args { |
9 | | - #[arg(short, long, default_value = "64")] |
10 | | - payload: usize, |
11 | | - #[arg(short, long, default_value = "1")] |
12 | | - rate: usize, |
13 | | - #[arg(short, long, default_value = "default_topic")] |
14 | | - topic: String, |
15 | | - #[arg(short, long, default_value = "0")] |
16 | | - duration: f64, |
17 | | - #[arg(short, long, default_value = "sub")] |
18 | | - mode: String, |
19 | | -} |
20 | | - |
21 | | -fn run_subscriber(topic: String, duration: Duration) -> Result<()> { |
| 6 | +async fn run_subscriber(topic: String) -> Result<()> { |
22 | 7 | let ctx = ZContextBuilder::default().build()?; |
23 | | - let node = ctx.create_node("MyNode").build()?; |
| 8 | + let node = ctx.create_node("Sub").build()?; |
24 | 9 | let zsub = node.create_sub::<ByteMultiArray>(&topic).build()?; |
25 | | - |
26 | | - let mut counter = 0; |
27 | | - if duration.is_zero() { |
28 | | - loop { |
29 | | - let _msg = zsub.recv()?; |
30 | | - tracing::info!("Recv {counter}-th msg"); |
31 | | - counter += 1; |
32 | | - } |
33 | | - } else { |
34 | | - let fut = async { |
35 | | - loop { |
36 | | - match zsub.async_recv().await { |
37 | | - Ok(_msg) => { |
38 | | - tracing::info!("Recv {counter}-th msg"); |
39 | | - } |
40 | | - Err(err) => { |
41 | | - tracing::error!(err); |
42 | | - break; |
43 | | - } |
44 | | - } |
45 | | - counter += 1; |
46 | | - } |
47 | | - }; |
48 | | - tokio::runtime::Runtime::new()?.block_on(async { |
49 | | - tokio::time::timeout(duration, fut).await?; |
50 | | - Result::Ok(()) |
51 | | - })?; |
| 10 | + let mut counter = 1; |
| 11 | + while let Ok(msg) = zsub.async_recv().await { |
| 12 | + println!("sub:>> #{counter}: {}", String::from_utf8_lossy(&msg.data)); |
| 13 | + counter += 1; |
52 | 14 | } |
53 | | - |
54 | 15 | Ok(()) |
55 | 16 | } |
56 | 17 |
|
57 | | -fn run_publisher( |
58 | | - topic: String, |
59 | | - duration: Duration, |
60 | | - period: Duration, |
61 | | - payload_size: usize, |
62 | | -) -> Result<()> { |
| 18 | +async fn run_publisher(topic: String, period: Duration, payload: String) -> Result<()> { |
63 | 19 | let ctx = ZContextBuilder::default().build()?; |
64 | | - let node = ctx.create_node("MyNode").build()?; |
| 20 | + let node = ctx.create_node("Pub").build()?; |
65 | 21 | let zpub = node.create_pub::<ByteMultiArray>(&topic).build()?; |
66 | | - let now = std::time::Instant::now(); |
67 | | - let msg = ByteMultiArray { |
68 | | - layout: MultiArrayLayout::default(), |
69 | | - data: vec![0u8; payload_size as _], |
70 | | - }; |
| 22 | + |
| 23 | + let mut count = 0; |
71 | 24 | loop { |
72 | | - zpub.publish(&msg)?; |
73 | | - std::thread::sleep(period); |
74 | | - if !duration.is_zero() && now.elapsed() >= duration { |
75 | | - break; |
76 | | - } |
| 25 | + let bs = format!("{payload} - #{count}"); |
| 26 | + println!("pub:>> {bs}"); |
| 27 | + let msg = ByteMultiArray { |
| 28 | + data: bs.into(), |
| 29 | + ..Default::default() |
| 30 | + }; |
| 31 | + zpub.async_publish(&msg).await?; |
| 32 | + let _ = tokio::time::sleep(period).await; |
| 33 | + count += 1; |
77 | 34 | } |
78 | | - Ok(()) |
79 | 35 | } |
80 | 36 |
|
81 | | -fn main() -> Result<()> { |
| 37 | +#[tokio::main] |
| 38 | +async fn main() -> Result<()> { |
82 | 39 | let args = Args::parse(); |
83 | | - let duration = std::time::Duration::from_secs_f64(args.duration); |
84 | | - let period = std::time::Duration::from_secs_f64(1.0 / args.rate as f64); |
| 40 | + let period = std::time::Duration::from_secs_f64(args.period); |
85 | 41 | zenoh::init_log_from_env_or("error"); |
86 | | - tracing::info!( |
87 | | - "Begin testing with topic: {}, duration: {duration:?}, payload: {}, rate: {} Hz", |
88 | | - args.topic, |
89 | | - args.payload, |
90 | | - args.rate |
91 | | - ); |
92 | 42 | if args.mode == "sub" { |
93 | | - run_subscriber(args.topic, duration)?; |
| 43 | + run_subscriber(args.topic).await?; |
94 | 44 | } else if args.mode == "pub" { |
95 | | - run_publisher(args.topic, duration, period, args.payload)?; |
| 45 | + run_publisher(args.topic, period, args.data).await?; |
96 | 46 | } else { |
97 | | - tracing::error!("The mode {} is not supported.", args.mode); |
| 47 | + println!( |
| 48 | + "Please use \"pub\" or \"sub\" as mode, {} is not supported.", |
| 49 | + args.mode |
| 50 | + ); |
98 | 51 | } |
99 | 52 | Ok(()) |
100 | 53 | } |
| 54 | + |
| 55 | +use clap::Parser; |
| 56 | +#[derive(Debug, Parser)] |
| 57 | +struct Args { |
| 58 | + #[arg(short, long, default_value = "Hello ROS-Z")] |
| 59 | + data: String, |
| 60 | + #[arg(short, long, default_value = "rosz/hello")] |
| 61 | + topic: String, |
| 62 | + #[arg(short, long, default_value = "1.0")] |
| 63 | + period: f64, |
| 64 | + #[arg(short, long, default_value = "sub")] |
| 65 | + mode: String, |
| 66 | +} |
0 commit comments