File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " worker_demo"
3+ version = " 0.1.0"
4+ edition = " 2021"
5+
6+ [dependencies ]
7+ rclrs = " 0.4"
8+ std_msgs = " *"
Original file line number Diff line number Diff line change 1+ use rclrs:: * ;
2+ use std:: time:: Duration ;
3+
4+ fn main ( ) -> Result < ( ) , RclrsError > {
5+ let mut executor = Context :: default_from_env ( ) ?. create_basic_executor ( ) ;
6+ let node = executor. create_node ( "worker_demo" ) ?;
7+
8+ let publisher = node. create_publisher ( "output_topic" ) ?;
9+ let worker = node. create_worker ( String :: new ( ) ) ;
10+
11+ let _timer = worker. create_timer_repeating (
12+ Duration :: from_secs ( 1 ) ,
13+ move |data : & mut String | {
14+ let msg = std_msgs:: msg:: String {
15+ data : data. clone ( )
16+ } ;
17+
18+ publisher. publish ( msg) . ok ( ) ;
19+ }
20+ ) ?;
21+
22+ let _subscription = worker. create_subscription (
23+ "input_topic" ,
24+ move |data : & mut String , msg : std_msgs:: msg:: String | {
25+ * data = msg. data ;
26+ }
27+ ) ?;
28+
29+ println ! (
30+ "Beginning repeater... \n >> \
31+ Publish a std_msg::msg::String to \" input_topic\" and we will periodically republish it to \" output_topic\" .\n \n \
32+ To see this in action run the following commands in two different terminals:\n \
33+ $ ros2 topic echo output_topic\n \
34+ $ ros2 topic pub input_topic std_msgs/msg/String \" {{data: Hello}}\" "
35+ ) ;
36+ executor. spin ( SpinOptions :: default ( ) ) ;
37+
38+ Ok ( ( ) )
39+ }
You can’t perform that action at this time.
0 commit comments