Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/timer_demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "examples_timer_demo"
version = "0.1.0"
edition = "2021"

[dependencies]
rclrs = "0.4"
example_interfaces = "*"
21 changes: 21 additions & 0 deletions examples/timer_demo/package.xml
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>
22 changes: 22 additions & 0 deletions examples/timer_demo/src/main.rs
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 {:?}.",
*count, timer_period,
);
})?;

executor.spin(SpinOptions::default()).first_error()
}
27 changes: 6 additions & 21 deletions examples/worker_demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rclrs::*;
use std::sync::Arc;
use std::time::Duration;

fn main() -> Result<(), RclrsError> {
let mut executor = Context::default_from_env()?.create_basic_executor();
Expand All @@ -15,27 +15,12 @@ fn main() -> Result<(), RclrsError> {
},
)?;

// // Use this timer-based implementation when timers are available instead
// // of using std::thread::spawn.
// let _timer = worker.create_timer_repeating(
// Duration::from_secs(1),
// move |data: &mut String| {
// let msg = example_interfaces::msg::String {
// data: data.clone()
// };

// publisher.publish(msg).ok();
// }
// )?;

std::thread::spawn(move || loop {
std::thread::sleep(std::time::Duration::from_secs(1));
let publisher = Arc::clone(&publisher);
let _ = worker.run(move |data: &mut String| {
let _timer =
worker.create_timer_repeating(Duration::from_secs(1), move |data: &mut String| {
let msg = example_interfaces::msg::String { data: data.clone() };
publisher.publish(msg).unwrap();
});
});

publisher.publish(msg).ok();
})?;

println!(
"Beginning repeater... \n >> \
Expand Down
Loading