-
Notifications
You must be signed in to change notification settings - Fork 139
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
Execution structure (spin-off of #427) #428
Open
mxgrey
wants to merge
19
commits into
ros2-rust:main
Choose a base branch
from
mxgrey:execution_structure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+798
−601
Open
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
29fe10e
Migrate Context, Executor, and Node creation to new API
mxgrey 1b5c187
Update examples
mxgrey 1ec9f10
Fix documentation
mxgrey 0874d8d
Fix formatting
mxgrey 4c2a67b
Fix example formatting
mxgrey 6059506
Implicitly cast &str to NodeOptions
mxgrey 259fcb3
Remove debug outputs
mxgrey e1ceb70
Fix formatting
mxgrey 0918476
Merge with latest main
mxgrey b465f6f
Return a Vec of errors from spinning
mxgrey 9a04993
update examples
mxgrey 4de816a
Fix example formatting
mxgrey a75cd24
Introduce CreateBasicExecutor trait
mxgrey 71c69a8
Fix formatting
mxgrey 99bdf8a
Merge remote-tracking branch 'origin/main' into execution_structure
mxgrey b14e56e
Add documentation for Context::default
mxgrey d047214
Automatically clear dead nodes from the executor
mxgrey e7f8bac
Update reference index
mxgrey 5c37fa8
Fix style
mxgrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
use rclrs::{create_node, Context, Node, Publisher, RclrsError, QOS_PROFILE_DEFAULT}; | ||
use rclrs::{Context, Executor, Publisher, RclrsError, SpinOptions, QOS_PROFILE_DEFAULT}; | ||
use std::{sync::Arc, thread, time::Duration}; | ||
use std_msgs::msg::String as StringMsg; | ||
struct SimplePublisherNode { | ||
node: Arc<Node>, | ||
_publisher: Arc<Publisher<StringMsg>>, | ||
|
||
struct SimplePublisher { | ||
publisher: Arc<Publisher<StringMsg>>, | ||
} | ||
impl SimplePublisherNode { | ||
fn new(context: &Context) -> Result<Self, RclrsError> { | ||
let node = create_node(context, "simple_publisher").unwrap(); | ||
let _publisher = node | ||
|
||
impl SimplePublisher { | ||
fn new(executor: &Executor) -> Result<Self, RclrsError> { | ||
let node = executor.create_node("simple_publisher").unwrap(); | ||
let publisher = node | ||
.create_publisher("publish_hello", QOS_PROFILE_DEFAULT) | ||
.unwrap(); | ||
Ok(Self { node, _publisher }) | ||
Ok(Self { publisher }) | ||
} | ||
|
||
fn publish_data(&self, increment: i32) -> Result<i32, RclrsError> { | ||
let msg: StringMsg = StringMsg { | ||
data: format!("Hello World {}", increment), | ||
}; | ||
self._publisher.publish(msg).unwrap(); | ||
self.publisher.publish(msg).unwrap(); | ||
Ok(increment + 1_i32) | ||
} | ||
} | ||
|
||
fn main() -> Result<(), RclrsError> { | ||
let context = Context::new(std::env::args()).unwrap(); | ||
let publisher = Arc::new(SimplePublisherNode::new(&context).unwrap()); | ||
let mut executor = Context::default_from_env().unwrap().create_basic_executor(); | ||
let publisher = Arc::new(SimplePublisher::new(&executor).unwrap()); | ||
let publisher_other_thread = Arc::clone(&publisher); | ||
let mut count: i32 = 0; | ||
thread::spawn(move || loop { | ||
thread::sleep(Duration::from_millis(1000)); | ||
count = publisher_other_thread.publish_data(count).unwrap(); | ||
}); | ||
rclrs::spin(publisher.node.clone()) | ||
executor.spin(SpinOptions::default()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@mxgrey how would a custom executor work here? It'd take a
Context
as a argument, right? I'd remove thecreate_basic_executor
function and be slightly more verbose (SingleThreadedExecutor::new(&context)
) to show how the API works and basically to not make any distinction between the executors.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.
If we look several PRs into the future we can see how the basic executor will be created once we have the ability to support custom executors.
The hook for making a custom executor will be the runtime trait which downstream users can implement for their custom struct. Once they've implemented that trait, anyone can use the custom runtime to create an opaque
Executor
instance. TheExecutor
hides away what kind of runtime is being used so the rest of the application doesn't have to care about it.I've gone ahead and made a repo to demonstrate what this would look like. In that repo I've made two different custom executors:
DoNothingSimple
andDoNothingAdvanced
. Both are custom executor runtimes which, as their name implies, do nothing. They're not useful as far as executors go, but they can show us how the custom executor API will work. If you clone that repo into a colcon workspace and checkout the worker branch of my fork you'll be able to compile and run the demo.Simple
The simplest way to create a custom executor is to use the
Context::create_executor
method as demonstrated here. This will work perfectly fine as long as it's okay for downstream users to directly initialize your custom runtime.Advanced
There are two reasons you might want to use a more advanced approach for a custom runtime:
Context
right away during instantiation, and you want to guarantee that the user can't pass the wrong context to you.To achieve this you can use the extension trait pattern. The repo demonstrates an implementation of this pattern here. Then the pattern gets used here.
This pattern allows you to add new methods to the
Context
struct from downstream crates. In this case we're addingContext::create_do_nothing_executor
which bears an uncanny resemblance to our built-in methodContext::create_basic_executor
.All that is to say, the API for custom downstream executors can be fully consistent with this out-of-the-box upstream API of
Context::create_basic_executor
.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.
As discussed in the Working Group meeting, I've change this PR to use an extension trait to provide the
Context::create_basic_executor
method: a75cd24At the same time I've simplified all the tests and demos to just do
use rclrs::*;
since that's probably how most users will want to use rclrs anyway.