Skip to content

Commit 63c0e88

Browse files
committed
Add timer demo
Signed-off-by: Michael X. Grey <[email protected]>
1 parent e482b2d commit 63c0e88

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

examples/logging_demo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "logging_demo"
33
version = "0.1.0"
4-
edition = "2024"
4+
edition = "2021"
55

66
[dependencies]
77
rclrs = "0.4"

examples/logging_demo/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
href="http://download.ros.org/schema/package_format3.xsd"
44
schematypens="http://www.w3.org/2001/XMLSchema"?>
55
<package format="3">
6-
<name>examples_worker_demo</name>
6+
<name>examples_logging_demo</name>
77
<maintainer email="[email protected]">Esteve Fernandez</maintainer>
88
<!-- This project is not military-sponsored, Jacob's employment contract just requires him to use this email address -->
99
<maintainer email="[email protected]">Jacob Hassold</maintainer>

examples/timer_demo/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "timer_demo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rclrs = "0.4"
8+
example_interfaces = "*"

examples/timer_demo/package.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<?xml-model
3+
href="http://download.ros.org/schema/package_format3.xsd"
4+
schematypens="http://www.w3.org/2001/XMLSchema"?>
5+
<package format="3">
6+
<name>examples_timer_demo</name>
7+
<maintainer email="[email protected]">Esteve Fernandez</maintainer>
8+
<!-- This project is not military-sponsored, Jacob's employment contract just requires him to use this email address -->
9+
<maintainer email="[email protected]">Jacob Hassold</maintainer>
10+
<version>0.4.1</version>
11+
<description>Package containing an example of how to use a worker in rclrs.</description>
12+
<license>Apache License 2.0</license>
13+
14+
<depend>rclrs</depend>
15+
<depend>rosidl_runtime_rs</depend>
16+
<depend>example_interfaces</depend>
17+
18+
<export>
19+
<build_type>ament_cargo</build_type>
20+
</export>
21+
</package>

examples/timer_demo/src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// Creates a SimpleTimerNode, initializes a node and the timer with a callback
2+
/// that prints the timer callback execution iteration. The callback is executed
3+
/// thanks to the spin, which is in charge of executing the timer's events among
4+
/// other entities' events.
5+
use rclrs::*;
6+
use std::time::Duration;
7+
8+
fn main() -> Result<(), RclrsError> {
9+
let mut executor = Context::default_from_env()?.create_basic_executor();
10+
let node = executor.create_node("timer_demo")?;
11+
let worker = node.create_worker::<usize>(0);
12+
let timer_period = Duration::from_secs(1);
13+
let _timer = worker.create_timer_repeating(
14+
timer_period,
15+
move |count: &mut usize| {
16+
*count += 1;
17+
println!(
18+
"Drinking 🧉 for the {}th time every {:?}.",
19+
*count,
20+
timer_period,
21+
);
22+
}
23+
)?;
24+
25+
executor.spin(SpinOptions::default()).first_error()
26+
}

0 commit comments

Comments
 (0)