|
| 1 | +/// Creates a SimplePublisherNode, initializes a node and publisher, and provides |
| 2 | +/// methods to publish a simple "Hello World" message on a loop in separate threads. |
| 3 | +
|
| 4 | +/// Imports the Arc type from std::sync, used for thread-safe reference counting pointers, |
| 5 | +/// and the StringMsg message type from std_msgs for publishing string messages. |
| 6 | +use std::{sync::Arc,time::Duration,iter,thread}; |
| 7 | +use rclrs::{RclrsError,QOS_PROFILE_DEFAULT,Context,create_node,Node,Publisher}; |
| 8 | +use std_msgs::msg::String as StringMsg; |
| 9 | +// / SimplePublisherNode struct contains node and publisher members. |
| 10 | +// / Used to initialize a ROS 2 node and publisher, and publish messages. |
| 11 | +struct SimplePublisherNode { |
| 12 | + node: Arc<Node>, |
| 13 | + _publisher: Arc<Publisher<StringMsg>>, |
| 14 | +} |
| 15 | +/// The `new` function takes a context and returns a Result containing the |
| 16 | +/// initialized SimplePublisherNode or an error. It creates a node with the |
| 17 | +/// given name and creates a publisher on the "publish_hello" topic. |
| 18 | +/// |
| 19 | +/// The SimplePublisherNode contains the node and publisher members. |
| 20 | +impl SimplePublisherNode { |
| 21 | + /// Creates a new SimplePublisherNode by initializing a node and publisher. |
| 22 | + /// |
| 23 | + /// This function takes a context and returns a Result containing the |
| 24 | + /// initialized SimplePublisherNode or an error. It creates a node with the |
| 25 | + /// given name and creates a publisher on the "publish_hello" topic. |
| 26 | + /// |
| 27 | + /// The SimplePublisherNode contains the node and publisher members. |
| 28 | + fn new(context: &Context) -> Result<Self,RclrsError> { |
| 29 | + let node = create_node(context, "simple_publisher").unwrap(); |
| 30 | + let _publisher = node |
| 31 | + .create_publisher("publish_hello", QOS_PROFILE_DEFAULT) |
| 32 | + .unwrap(); |
| 33 | + Ok(Self { node, _publisher, }) |
| 34 | + } |
| 35 | + |
| 36 | + /// Publishes a "Hello World" message on the publisher. |
| 37 | + /// |
| 38 | + /// Creates a StringMsg with "Hello World" as the data, publishes it on |
| 39 | + /// the `_publisher`, and returns a Result. This allows regularly publishing |
| 40 | + /// a simple message on a loop. |
| 41 | + fn publish_data(&self,inkrement:i32) -> Result<i32,RclrsError> { |
| 42 | + |
| 43 | + let msg: StringMsg = StringMsg { |
| 44 | + data: format!("Hello World {}",inkrement), |
| 45 | + }; |
| 46 | + self._publisher.publish(msg).unwrap(); |
| 47 | + Ok(inkrement+1_i32) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// The main function initializes a ROS 2 context, node and publisher, |
| 52 | +/// spawns a thread to publish messages repeatedly, and spins the node |
| 53 | +/// to receive callbacks. |
| 54 | +/// |
| 55 | +/// It creates a context, initializes a SimplePublisherNode which creates |
| 56 | +/// a node and publisher, clones the publisher to pass to the thread, |
| 57 | +/// spawns a thread to publish "Hello World" messages repeatedly, and |
| 58 | +/// calls spin() on the node to receive callbacks. This allows publishing |
| 59 | +/// messages asynchronously while spinning the node. |
| 60 | +fn main() -> Result<(),RclrsError> { |
| 61 | + let context = Context::new(std::env::args()).unwrap(); |
| 62 | + let publisher = Arc::new(SimplePublisherNode::new(&context).unwrap()); |
| 63 | + let publisher_other_thread = Arc::clone(&publisher); |
| 64 | + let mut iterator: i32=0; |
| 65 | + thread::spawn(move || -> () { |
| 66 | + iter::repeat(()).for_each(|()| { |
| 67 | + thread::sleep(Duration::from_millis(1000)); |
| 68 | + iterator=publisher_other_thread.publish_data(iterator).unwrap(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + rclrs::spin(publisher.node.clone()) |
| 72 | +} |
0 commit comments