Skip to content

Commit 17145d2

Browse files
WIP Add minimal TimeSource implementation
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 7450810 commit 17145d2

File tree

6 files changed

+373
-11
lines changed

6 files changed

+373
-11
lines changed

rclrs/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ tempfile = "3.3.0"
3131
# Needed for FFI
3232
bindgen = "0.66.1"
3333

34+
[dependencies.rosgraph_msgs]
35+
version = "*"
36+
3437
[features]
3538
dyn_msg = ["ament_rs", "libloading"]

rclrs/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
mod arguments;
99
mod client;
10+
mod clock;
1011
mod context;
1112
mod error;
1213
mod node;
@@ -15,6 +16,8 @@ mod publisher;
1516
mod qos;
1617
mod service;
1718
mod subscription;
19+
mod time;
20+
mod time_source;
1821
mod vendor;
1922
mod wait;
2023

@@ -23,11 +26,12 @@ mod rcl_bindings;
2326
#[cfg(feature = "dyn_msg")]
2427
pub mod dynamic_message;
2528

26-
use std::sync::Arc;
29+
use std::sync::{Arc, Mutex};
2730
use std::time::Duration;
2831

2932
pub use arguments::*;
3033
pub use client::*;
34+
pub use clock::*;
3135
pub use context::*;
3236
pub use error::*;
3337
pub use node::*;
@@ -38,6 +42,8 @@ use rcl_bindings::rcl_context_is_valid;
3842
pub use rcl_bindings::rmw_request_id_t;
3943
pub use service::*;
4044
pub use subscription::*;
45+
pub use time::*;
46+
pub use time_source::*;
4147
pub use wait::*;
4248

4349
/// Polls the node for new messages and executes the corresponding callbacks.
@@ -109,7 +115,12 @@ pub fn spin(node: Arc<Node>) -> Result<(), RclrsError> {
109115
/// # Ok::<(), RclrsError>(())
110116
/// ```
111117
pub fn create_node(context: &Context, node_name: &str) -> Result<Arc<Node>, RclrsError> {
112-
Ok(Arc::new(Node::builder(context, node_name).build()?))
118+
println!("Creating node");
119+
let mut node = Arc::new(Node::builder(context, node_name).build()?);
120+
*node._time_source.lock().unwrap() =
121+
Some(TimeSourceBuilder::new(node.clone(), node.get_clock()).build());
122+
Ok(node)
123+
//Ok(Arc::new(Node::builder(context, node_name).build()?))
113124
}
114125

115126
/// Creates a [`NodeBuilder`][1].

rclrs/src/node.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub use self::builder::*;
1313
pub use self::graph::*;
1414
use crate::rcl_bindings::*;
1515
use crate::{
16-
Client, ClientBase, Context, GuardCondition, ParameterOverrideMap, Publisher, QoSProfile,
17-
RclrsError, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback,
18-
ToResult,
16+
Client, ClientBase, Clock, Context, GuardCondition, ParameterOverrideMap, ParameterValue,
17+
Publisher, QoSProfile, RclrsError, Service, ServiceBase, Subscription, SubscriptionBase,
18+
SubscriptionCallback, TimeSource, TimeSourceBuilder, ToResult,
1919
};
2020

2121
impl Drop for rcl_node_t {
@@ -71,6 +71,9 @@ pub struct Node {
7171
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>,
7272
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>,
7373
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
74+
_clock: Arc<Mutex<Clock>>,
75+
// TODO(luca) set to private
76+
pub _time_source: Arc<Mutex<Option<TimeSource>>>,
7477
_parameter_map: ParameterOverrideMap,
7578
}
7679

@@ -96,7 +99,11 @@ impl Node {
9699
/// See [`NodeBuilder::new()`] for documentation.
97100
#[allow(clippy::new_ret_no_self)]
98101
pub fn new(context: &Context, node_name: &str) -> Result<Node, RclrsError> {
99-
Self::builder(context, node_name).build()
102+
Self::builder(context, node_name).build().into()
103+
}
104+
105+
pub fn get_clock(&self) -> Arc<Mutex<Clock>> {
106+
self._clock.clone()
100107
}
101108

102109
/// Returns the name of the node.
@@ -355,6 +362,12 @@ impl Node {
355362
domain_id
356363
}
357364

365+
// TODO(luca) There should really be parameter callbacks, this is only for testing
366+
// temporarily
367+
pub fn get_parameter(&self, name: &str) -> Option<ParameterValue> {
368+
self._parameter_map.get(name).cloned()
369+
}
370+
358371
/// Creates a [`NodeBuilder`][1] with the given name.
359372
///
360373
/// Convenience function equivalent to [`NodeBuilder::new()`][2].

rclrs/src/node/builder.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::{Arc, Mutex};
33

44
use crate::rcl_bindings::*;
55
use crate::{
6-
node::call_string_getter_with_handle, resolve_parameter_overrides, Context, Node, RclrsError,
7-
ToResult,
6+
node::call_string_getter_with_handle, resolve_parameter_overrides, Clock, ClockType, Context,
7+
Node, RclrsError, TimeSource, TimeSourceBuilder, ToResult,
88
};
99

1010
/// A builder for creating a [`Node`][1].
@@ -17,6 +17,7 @@ use crate::{
1717
/// - `use_global_arguments: true`
1818
/// - `arguments: []`
1919
/// - `enable_rosout: true`
20+
/// - `clock_type: ClockType::RosTime`
2021
///
2122
/// # Example
2223
/// ```
@@ -46,6 +47,7 @@ pub struct NodeBuilder {
4647
use_global_arguments: bool,
4748
arguments: Vec<String>,
4849
enable_rosout: bool,
50+
clock_type: ClockType,
4951
}
5052

5153
impl NodeBuilder {
@@ -92,6 +94,7 @@ impl NodeBuilder {
9294
use_global_arguments: true,
9395
arguments: vec![],
9496
enable_rosout: true,
97+
clock_type: ClockType::RosTime,
9598
}
9699
}
97100

@@ -262,6 +265,8 @@ impl NodeBuilder {
262265
.ok()?;
263266
};
264267

268+
let clock = Clock::new(self.clock_type)?;
269+
265270
let _parameter_map = unsafe {
266271
let fqn = call_string_getter_with_handle(&rcl_node, rcl_node_get_fully_qualified_name);
267272
resolve_parameter_overrides(
@@ -271,16 +276,24 @@ impl NodeBuilder {
271276
)?
272277
};
273278
let rcl_node_mtx = Arc::new(Mutex::new(rcl_node));
274-
275-
Ok(Node {
279+
let mut node = Node {
276280
rcl_node_mtx,
277281
rcl_context_mtx: self.context.clone(),
278282
clients_mtx: Mutex::new(vec![]),
279283
guard_conditions_mtx: Mutex::new(vec![]),
280284
services_mtx: Mutex::new(vec![]),
281285
subscriptions_mtx: Mutex::new(vec![]),
286+
_clock: Arc::new(Mutex::new(clock)),
287+
_time_source: Arc::new(Mutex::new(None)),
282288
_parameter_map,
283-
})
289+
};
290+
/*
291+
let node_mtx = Arc::new(node);
292+
node._time_source = Some(Arc::new(Mutex::new(TimeSourceBuilder::new(node_mtx).build())));
293+
*/
294+
//node._time_source = Some(Arc::new(Mutex::new(TimeSourceBuilder::new(node_mtx).build())));
295+
296+
Ok(node)
284297
}
285298

286299
/// Creates a rcl_node_options_t struct from this builder.

rclrs/src/qos.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ pub const QOS_PROFILE_SENSOR_DATA: QoSProfile = QoSProfile {
296296
avoid_ros_namespace_conventions: false,
297297
};
298298

299+
/// Equivalent to `ClockQos` from the [`rclcpp` package][1].
300+
/// Same as sensor data but with a history depth of 1
301+
///
302+
/// [1]: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/include/rclcpp/qos.hpp
303+
pub const QOS_PROFILE_CLOCK: QoSProfile = QoSProfile {
304+
history: QoSHistoryPolicy::KeepLast { depth: 1 },
305+
reliability: QoSReliabilityPolicy::BestEffort,
306+
durability: QoSDurabilityPolicy::Volatile,
307+
deadline: QoSDuration::SystemDefault,
308+
lifespan: QoSDuration::SystemDefault,
309+
liveliness: QoSLivelinessPolicy::SystemDefault,
310+
liveliness_lease_duration: QoSDuration::SystemDefault,
311+
avoid_ros_namespace_conventions: false,
312+
};
313+
299314
/// Equivalent to `rmw_qos_profile_parameters` from the [`rmw` package][1].
300315
///
301316
/// [1]: https://github.com/ros2/rmw/blob/master/rmw/include/rmw/qos_profiles.h

0 commit comments

Comments
 (0)