jhelovuo / ros2-client

ROS2 client library based on RustDDS
66 stars 14 forks source link

Help with initial setup #28

Open muralimanohar0212 opened 3 months ago

muralimanohar0212 commented 3 months ago

Hello,

This is my first time trying to setup ros2-client in rust. I want to thank you for your patience in advance.

As mentioned, I am trying to get a simple publisher in place. I first started by creating a new rust project using cargo new <project_name . In this I have put a simple basic subscriber as below

`use ros2_client::{Context, NodeOptions, NodeName, Name, MessageTypeName, Subscription, DEFAULT_SUBSCRIPTION_QOS}; use futures::stream::StreamExt; fn main() { smol::block_on(async { // Create a new ROS 2 context let context = Context::new().unwrap();

    // Create a new node with rosout logging enabled
    let mut node = context
        .new_node(
            NodeName::new("/", "rustdds_listener").unwrap(),
            NodeOptions::new().enable_rosout(true),
        )
        .unwrap();
    let chatter_topic = node
        .create_topic(
            &Name::new("/", "topic").unwrap(),
            MessageTypeName::new("std_msgs", "String"),
            &ros2_client::DEFAULT_SUBSCRIPTION_QOS,
        )
        .unwrap();

    // Subscribe to the 'topic'
    let chatter_subscription = node
        .create_subscription::<String>(&chatter_topic, None)
        .unwrap();

    // Process the messages received on this subscription
    chatter_subscription
        .async_stream()
        .for_each(|result| async {
            match result {
                Ok((msg, _)) => println!("I heard: {msg}"),
                Err(e) => eprintln!("Receive request error: {:?}", e),
            }
        })
        .await;

    }); }

`

Now, when I run the commands cargo build and cargo run , I do not see the listeners on the terminal when running ros2 node list . Even when running ros2 topic pub /topic std_msgs/msg/String "data: 'Hello, world'" on a separate terminal, I don't see the library listening and printing these messages.

Is there something wrong that I am doing, or am I supposed to create a package using ros2 pkg create ? Is there anything I need to check or do before creating the project? The node created using rust cannot be seen. I am currently using humble version of ROS2.

Any help is appreciated. Many thanks in advance

jhelovuo commented 1 month ago

Hello, The advice is a bit delayed, but here is a short answer:

You should be able to run the example program async_listener , included in the ros2-client source codes. Download the sources from GitHub, and use the command cargo run --example=async_listener. Then run (in another terminal) ros2 topic pub /topic std_msgs/msg/String "data: 'Hello, world'" like you suggest.

Expected results:

$ ros2 topic pub /topic std_msgs/msg/String "data: 'Hello, world'"
publisher: beginning loop
publishing #1: std_msgs.msg.String(data='Hello, world')

publishing #2: std_msgs.msg.String(data='Hello, world')

publishing #3: std_msgs.msg.String(data='Hello, world')

publishing #4: std_msgs.msg.String(data='Hello, world')

publishing #5: std_msgs.msg.String(data='Hello, world')

and

$ cargo run --example=async_listener
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/examples/async_listener`
I heard: Hello, world
I heard: Hello, world
I heard: Hello, world
I heard: Hello, world
I heard: Hello, world

You obviously need to have ROS2 installed to be able to run ros2 command-line tool, but there is no need to create any packages. ROS2 distrbutions Foxy, Galactic, and Humble should work.

Thank you for your patience in waiting for answer.