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

#57 in Robotics

Download history 308/week @ 2024-09-27 115/week @ 2024-10-04 73/week @ 2024-10-11 60/week @ 2024-10-18 80/week @ 2024-10-25 311/week @ 2024-11-01

529 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
~408K SLoC