-
Notifications
You must be signed in to change notification settings - Fork 164
Timers #480
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
base: main
Are you sure you want to change the base?
Timers #480
Changes from all commits
2d20c4e
c9c2127
7ad4ece
802925b
f24b978
9b6b1d1
73b34b3
030f59c
82f4da6
a589997
a4fa5cb
35fa24a
775b170
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "examples_timer_demo" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "timer_demo" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
rclrs = "0.4" | ||
example_interfaces = "*" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0"?> | ||
<?xml-model | ||
href="http://download.ros.org/schema/package_format3.xsd" | ||
schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
<package format="3"> | ||
<name>examples_timer_demo</name> | ||
<maintainer email="[email protected]">Esteve Fernandez</maintainer> | ||
<!-- This project is not military-sponsored, Jacob's employment contract just requires him to use this email address --> | ||
<maintainer email="[email protected]">Jacob Hassold</maintainer> | ||
<version>0.4.1</version> | ||
<description>Package containing an example of how to use a worker in rclrs.</description> | ||
<license>Apache License 2.0</license> | ||
|
||
<depend>rclrs</depend> | ||
<depend>rosidl_runtime_rs</depend> | ||
<depend>example_interfaces</depend> | ||
|
||
<export> | ||
<build_type>ament_cargo</build_type> | ||
</export> | ||
</package> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// Creates a SimpleTimerNode, initializes a node and the timer with a callback | ||
/// that prints the timer callback execution iteration. The callback is executed | ||
/// thanks to the spin, which is in charge of executing the timer's events among | ||
/// other entities' events. | ||
use rclrs::*; | ||
use std::time::Duration; | ||
|
||
fn main() -> Result<(), RclrsError> { | ||
let mut executor = Context::default_from_env()?.create_basic_executor(); | ||
let node = executor.create_node("timer_demo")?; | ||
let worker = node.create_worker::<usize>(0); | ||
let timer_period = Duration::from_secs(1); | ||
let _timer = worker.create_timer_repeating(timer_period, move |count: &mut usize| { | ||
*count += 1; | ||
println!( | ||
"Drinking 🧉 for the {}th time every {:?}.", | ||
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. Works for me and is a nice example :) Does ROS2 have an example like this actually? Not used in the tutorials right? I guess the only nitpick would be, that all other python/cpp examples use 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. See this reply. |
||
*count, timer_period, | ||
); | ||
})?; | ||
|
||
executor.spin(SpinOptions::default()).first_error() | ||
} |
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.
Maybe good to have a bin here so that we can run it through ROS2 command?
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.
Is this what you had in mind?
Since the example is in
src/main.rs
, by default it will generate a binary with the same name as the package,examples_timer_demo
. But I see in the other example packages there's a certain naming pattern for the binaries, so now I've copied that pattern here.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.
ahhh gotcha.
And yes exactly like that. naming consistency is good :)