|
1 | 1 | use color_eyre::eyre::Context; |
2 | 2 | use color_eyre::Result; |
3 | | -use ros2_client::{MessageTypeName, Name, Node, Publisher, Subscription}; |
4 | | -use serde::Serialize; |
| 3 | +use ros2_client::{ |
| 4 | + ros2::{ |
| 5 | + policy::{Durability, History}, |
| 6 | + Duration, QosPolicyBuilder, |
| 7 | + }, |
| 8 | + rustdds::Topic, |
| 9 | + MessageTypeName, Name, Node, |
| 10 | +}; |
5 | 11 |
|
6 | | -pub trait RosNode { |
7 | | - fn subscribe<T: 'static>( |
8 | | - &mut self, |
9 | | - namespace: &'static str, |
10 | | - topic_name: &'static str, |
11 | | - type_name: MessageTypeName, |
12 | | - ) -> Result<Subscription<T>>; |
13 | | - |
14 | | - fn publisher<T: Serialize>( |
15 | | - &mut self, |
16 | | - namespace: &'static str, |
17 | | - topic_name: &'static str, |
18 | | - type_name: MessageTypeName, |
19 | | - ) -> Result<Publisher<T>>; |
20 | | -} |
21 | | - |
22 | | -impl RosNode for Node { |
23 | | - fn subscribe<T: 'static>( |
24 | | - &mut self, |
25 | | - namespace: &'static str, |
26 | | - topic_name: &'static str, |
27 | | - type_name: MessageTypeName, |
28 | | - ) -> Result<Subscription<T>> { |
29 | | - let topic = self |
30 | | - .create_topic( |
31 | | - &Name::new(namespace, topic_name).wrap_err("failed to create ROS topic name")?, |
32 | | - type_name, |
33 | | - &ros2_client::DEFAULT_SUBSCRIPTION_QOS, |
34 | | - ) |
35 | | - .wrap_err("failed to create ROS topic")?; |
36 | | - |
37 | | - self.create_subscription(&topic, None) |
38 | | - .wrap_err("failed to create subscription") |
39 | | - } |
40 | | - |
41 | | - fn publisher<T: Serialize>( |
42 | | - &mut self, |
43 | | - namespace: &'static str, |
44 | | - topic_name: &'static str, |
45 | | - type_name: MessageTypeName, |
46 | | - ) -> Result<Publisher<T>> { |
47 | | - let topic = self |
48 | | - .create_topic( |
49 | | - &Name::new(namespace, topic_name).wrap_err("failed to create ROS topic name")?, |
50 | | - type_name, |
51 | | - &ros2_client::DEFAULT_SUBSCRIPTION_QOS, |
52 | | - ) |
53 | | - .wrap_err("failed to create ROS topic")?; |
54 | | - |
55 | | - self.create_publisher(&topic, None) |
56 | | - .wrap_err("failed to create publisher") |
57 | | - } |
| 12 | +pub fn create_topic( |
| 13 | + node: &mut Node, |
| 14 | + namespace: &'static str, |
| 15 | + topic_name: &'static str, |
| 16 | + type_name: MessageTypeName, |
| 17 | +) -> Result<Topic> { |
| 18 | + node.create_topic( |
| 19 | + &Name::new(namespace, topic_name).wrap_err("failed to create ROS topic name")?, |
| 20 | + type_name, |
| 21 | + &QosPolicyBuilder::new() |
| 22 | + .reliable(Duration::INFINITE) |
| 23 | + .history(History::KeepLast { depth: 1 }) |
| 24 | + .durability(Durability::TransientLocal) |
| 25 | + .build(), |
| 26 | + ) |
| 27 | + .wrap_err("failed to create ROS topic") |
58 | 28 | } |
0 commit comments