4 releases

0.1.3 Oct 4, 2024
0.1.2 Oct 1, 2024
0.1.1 Sep 30, 2024
0.1.0 Sep 30, 2024

#65 in Robotics

Download history 363/week @ 2024-09-28 64/week @ 2024-10-05 71/week @ 2024-10-12

498 downloads per month

MIT license

17KB
337 lines

ros2_helpers

A wrapper around safe drive to make things easy to work

This lib not intended to be used instead of safe drive but as a starter to play with ros and rust ^^

Implements:

  • into_stream for subscribers
  • provides some methods to read all available message

lib.rs:

ROS2 comms to make working with safe_drive more easy

Example

use std::time::Duration;

use ros2_helpers::prelude::*;
use ros2_helpers::safe_drive::msg::common_interfaces::std_msgs;
use tokio::{runtime::Builder, signal::ctrl_c, time::interval};

static NAME: &str = "EX1";

async fn ros2_main() -> Result<()> {
    // Create a context.
    let ctx = Context::new()?;
    // Create a node.
    let node = ctx.create_node(NAME, None, Default::default())?;
    let publisher = node.new_publisher::<std_msgs::msg::String>(&Attributes::new("ex1_pub"))?;
    let mut subscriber = node
        .new_subscriber::<std_msgs::msg::String>(&Attributes::new("ex2_pub"))?
        .into_stream();
    let mut counter: usize = 0;
    let mut interval = interval(Duration::from_millis(100));
    loop {
        tokio::select! {
            msg = subscriber.next() => {
                let Some(Ok(v)) = msg else {
                    continue;
                };
                println!("Received message {:?}", v.data.get_string());
                let mut message = std_msgs::msg::String::new().unwrap();
                message.data.assign(&format!("{} -> {}", NAME, counter));
                println!("Sending: {:?}", message.data.get_string());
                publisher.send_many([&message, &message])?;
                counter = counter.wrapping_add(1);
            },
            elapsed = interval.tick() => {
                println!("elapsed : {elapsed:?}");
            },
            _ = ctrl_c() => {
                break;
            }
        }
    }
    Ok(())
}

fn main() -> Result<()> {
    let rt = Builder::new_multi_thread()
        .thread_name(NAME)
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(ros2_main())
}

Dependencies

~16–24MB
~407K SLoC