-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add preliminary support for parameters #332
Changes from 69 commits
17145d2
5de661a
7676a41
8f2e1b8
978fd2e
eda7103
213978f
1f5ea5c
19a73c3
fde0f35
b808cc4
f11b4b0
90b0938
debaac9
cc59f0a
d7bb9f2
5df379f
0fcd296
524475d
013cce5
a5bdb07
d02ccc5
9c3d987
99401aa
90fe308
0ee8954
4645935
edce20e
632e082
7e536e0
2fed18e
744f4aa
df4010d
7286ce1
ed583f6
382f780
e665dfd
878a500
cbb9aa9
a84e375
0bd5052
c62ca72
9512807
d5ffc59
2ca517c
35e5d94
b3788e7
0a77427
c140672
01eb4dc
3febf78
8bb26c5
660bce5
a069d41
347320e
efaf8d7
4bd53d9
e7af874
26bf4ce
79404ff
71e7e42
b95008c
7b66d9b
ccc91e9
6dcc21f
3f1b9ac
8d3096a
4b9fb5d
b142cec
25b1bb8
3e2a7bf
25cd2a8
be4b977
768535a
29d6c76
67b0994
bc9477c
e20ea35
e6636dd
df55acc
009d4ae
190cf0d
1c845db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,9 @@ pub use self::builder::*; | |
pub use self::graph::*; | ||
use crate::rcl_bindings::*; | ||
use crate::{ | ||
Client, ClientBase, Context, GuardCondition, ParameterOverrideMap, Publisher, QoSProfile, | ||
RclrsError, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback, | ||
ToResult, | ||
Client, ClientBase, Context, Declarable, GuardCondition, ParameterBuilder, ParameterInterface, | ||
ParameterVariant, Parameters, Publisher, QoSProfile, RclrsError, Service, ServiceBase, | ||
Subscription, SubscriptionBase, SubscriptionCallback, ToResult, | ||
}; | ||
|
||
impl Drop for rcl_node_t { | ||
|
@@ -71,7 +71,7 @@ pub struct Node { | |
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>, | ||
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>, | ||
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>, | ||
_parameter_map: ParameterOverrideMap, | ||
_parameter: ParameterInterface, | ||
} | ||
|
||
impl Eq for Node {} | ||
|
@@ -360,6 +360,84 @@ impl Node { | |
domain_id | ||
} | ||
|
||
/// Creates a `ParameterBuilder` that can be used to set parameter declaration options and | ||
/// declare a parameter as `Optional`, `Mandatory` or `ReadOnly`. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use rclrs::{Context, ParameterRange, RclrsError}; | ||
/// let context = Context::new([])?; | ||
/// let node = rclrs::create_node(&context, "domain_id_node")?; | ||
/// // Set it to a range of 0-100, with a step of 2 | ||
luca-della-vedova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// let range = ParameterRange { | ||
/// lower: Some(-100), | ||
/// upper: Some(100), | ||
/// step: Some(2), | ||
/// }; | ||
/// let param = node.declare_parameter("int_param", 10) | ||
/// .range(range) | ||
/// .mandatory() | ||
/// .unwrap(); | ||
/// assert_eq!(param.get(), 10); | ||
/// param.set(50).unwrap(); | ||
/// assert_eq!(param.get(), 50); | ||
/// // Out of range, will return an error | ||
/// assert!(param.set(200).is_err()); | ||
/// # Ok::<(), RclrsError>(()) | ||
/// ``` | ||
pub fn declare_parameter<T: Declarable>( | ||
&self, | ||
name: &str, | ||
default_value: T, | ||
luca-della-vedova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> ParameterBuilder<'_, T> { | ||
self._parameter.declare(name, default_value) | ||
} | ||
|
||
/// Creates a `ParameterBuilder` that can be used to set parameter declaration options and | ||
/// declare a parameter from an iterable as `Optional`, `Mandatory` or `ReadOnly`. | ||
pub fn declare_parameter_from_iter<U: IntoIterator>( | ||
&self, | ||
name: &str, | ||
default_value: U, | ||
) -> ParameterBuilder<'_, Arc<[U::Item]>> | ||
where | ||
Arc<[U::Item]>: ParameterVariant, | ||
{ | ||
self._parameter.declare_from_iter(name, default_value) | ||
} | ||
|
||
/// Creates a `ParameterBuilder` that can be used to set parameter declaration options and | ||
/// declare a string array parameter as `Optional`, `Mandatory` or `ReadOnly`. | ||
pub fn declare_string_array_parameter<U>( | ||
&self, | ||
name: &str, | ||
default_value: U, | ||
) -> ParameterBuilder<'_, Arc<[Arc<str>]>> | ||
where | ||
U: IntoIterator, | ||
U::Item: Into<Arc<str>>, | ||
{ | ||
self._parameter.declare_string_array(name, default_value) | ||
} | ||
|
||
/// Helper function to declare an `Optional` parameter with a default unset value. | ||
pub fn declare_unset_parameter<T: ParameterVariant>( | ||
&self, | ||
name: &str, | ||
) -> ParameterBuilder<'_, Option<T>> { | ||
self._parameter.declare::<Option<T>>(name, None) | ||
} | ||
|
||
/// Enables usage of undeclared parameters for this node. | ||
/// | ||
/// Returns a `Parameter` struct that can be used to get and set all parameters. | ||
pub fn use_undeclared_parameters(&self) -> Parameters { | ||
self._parameter.allow_undeclared(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this get reset? In fact, is it used at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current API, once undeclared parameters are allowed, they're allowed for the rest of the node's lifespan. It's hard for me to imagine a use case for undoing that allowance or even what the behavior should be to undo it. In rclcpp this decision is set in stone at initialization, so I think it's reasonable for us to not offer a way to reverse it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I don't see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been added in anticipation of parameter services. When parameter services are implemented on top of this in the future, this flag will determine whether to reject parameter service requests on undeclared parameters. Until then it's true that it doesn't have any effect, simply because we've designed the rclrs API in a way that makes it unnecessary to check this for rclrs API calls. |
||
Parameters { | ||
interface: &self._parameter, | ||
} | ||
} | ||
|
||
/// Creates a [`NodeBuilder`][1] with the given name. | ||
/// | ||
/// Convenience function equivalent to [`NodeBuilder::new()`][2]. | ||
|
@@ -384,7 +462,7 @@ impl Node { | |
// function, which is why it's not merged into Node::call_string_getter(). | ||
// This function is unsafe since it's possible to pass in an rcl_node_t with dangling | ||
// pointers etc. | ||
unsafe fn call_string_getter_with_handle( | ||
pub(crate) unsafe fn call_string_getter_with_handle( | ||
rcl_node: &rcl_node_t, | ||
getter: unsafe extern "C" fn(*const rcl_node_t) -> *const c_char, | ||
) -> String { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this pattern from
rclcpp
(interfaces for each group of features), we should do the same for services, subscriptions, etc.