Skip to content
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

More convinent one time parameter access #107

Merged
merged 5 commits into from
Oct 13, 2024

Conversation

IamTheCarl
Copy link
Contributor

TLDR: If you just want to read a single ROS parameter and not receive updates for it in the future, it's now this easy:

let serial_device: String = node
    .get_parameter("serial_device")
    .context("Failed to get serial device name.")?; // Graceful error handling with the anyhow crate.

let baud_rate: Option<i64> = node
    .get_parameter("baud_rate")
    .context("Failed to get device baud rate.")?;
let baud_rate = baud_rate.unwrap_or(115200); // By fetching an optional type, we can assume a default when the value is not set.

I write a lot of ROS drivers. The usual way I configure the drivers is to load their settings from ROS params.
The current method to access a ROS parameter once is quite tedious and I wanted to make it easy.

The following is an explanation of my development process and why each commit was made.

The current way to read a parameter just once on startup looks something like this:

let params = node.params.lock().unwrap();
let serial_device = params.get("serial_device").unwrap();
let serial_device = match serial_device.value.clone() {
    ParameterValue::String(value) => value,
    _ => panic!("I don't feel like dealing with this error."),
};

That's usable but ugly, so I set out to try and make it less ugly.
My first step was to generalize converting the type of a value.
That resulted in this:

let parameters = node.params.lock().unwrap();
let serial_device: String = parameters
    .get("serial_device")
    .context("Serial device name was not provided.")?
    .value
    .clone()
    .try_into()
    .context("`serial_device` parameter is of wrong type")?;

Next was to add a "get_parameter" method to the node, simplifying to this:

let serial_device: String = node
    .get_parameter("serial_device")
    .context("Failed to get serial device name.")?;

Now that looks pretty good, except for when you want to give a parameter a default value.

let baud_rate: i64 = match node.get_parameter("baud_rate") {
    Ok(baud_rate) => Ok(baud_rate),
    Err(r2r::Error::ParameterNotSet { .. }) => Ok(115200), // Default value when not set.
    Err(error) => Err(error),
}
.context("Failed to get device baud rate.")?;

To fix that issue, I decided to not make ParameterNotSet its own error and instead make it a type error. "I was expecting an Integer but got a NotSet" instead of "Expected parameter is not set". By doing this, I could then have a try_into for Option types, enabling the following patern:

let baud_rate: Option<i64> = node
    .get_parameter("baud_rate")
    .context("Failed to get device baud rate.")?;
let baud_rate = baud_rate.unwrap_or(115200);

@m-dahl
Copy link
Collaborator

m-dahl commented Oct 12, 2024

Thanks for you contribution! This is a nice improvement in usability and I am happy to merge it.

Before that, can I ask you to add one or two examples based on your writeup to examples/paramters.rs?

@IamTheCarl
Copy link
Contributor Author

Hopefully that explains it well. I tried to make the comments match the styling of the ones already there.

@m-dahl m-dahl merged commit d8fe90b into sequenceplanner:master Oct 13, 2024
7 checks passed
@m-dahl
Copy link
Collaborator

m-dahl commented Oct 13, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants