Skip to content
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ members = [
"r2r_msg_gen",
"r2r_macros",
"r2r_rcl",
"r2r_tracing",
]
2 changes: 2 additions & 0 deletions r2r/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ r2r_rcl = { path = "../r2r_rcl", version = "0.9.5" }
r2r_msg_gen = { path = "../r2r_msg_gen", version = "0.9.5" }
r2r_actions = { path = "../r2r_actions", version = "0.9.5" }
r2r_macros = { path = "../r2r_macros", version = "0.9.5" }
r2r_tracing = { path = "../r2r_tracing", version = "0.9.5" }
uuid = { version = "1.2.2", features = ["serde", "v4"] }
futures = "0.3.25"
log = "0.4.18"
Expand Down Expand Up @@ -50,6 +51,7 @@ prettyplease = "0.2.6"
[features]
save-bindgen = ["r2r_rcl/save-bindgen", "r2r_msg_gen/save-bindgen", "r2r_actions/save-bindgen"]
doc-only = ["r2r_common/doc-only", "r2r_rcl/doc-only", "r2r_msg_gen/doc-only", "r2r_actions/doc-only"]
tracing = ["r2r_tracing/tracing"]

[package.metadata.docs.rs]
features = ["doc-only"]
Expand Down
64 changes: 64 additions & 0 deletions r2r/examples/callback_tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::time::Duration;

use futures::{executor::LocalPool, task::LocalSpawnExt};

use r2r::{std_msgs::msg, std_srvs::srv, QosProfile};

/// This example demonstrates creation of a service,
/// subscriber and timers with their callback execution traced.
fn main() -> Result<(), Box<dyn std::error::Error>> {
let ctx = r2r::Context::create()?;
let mut node = r2r::Node::create(ctx, "testnode", "")?;

let mut pool = LocalPool::new();
let spawner = pool.spawner();

// The traced callback is supplied directly to `subscribe_traced`
// and `create_service_traced`functions.
let subscriber_future = node
.subscribe("/print", QosProfile::default())?
.traced_callback(|msg: msg::String| {
println!("Received message: '{}'", msg.data);
});
spawner.spawn_local(subscriber_future)?;

let mut value = false;
let service_future = node
.create_service::<srv::SetBool::Service>("/set_value", QosProfile::default())?
.traced_callback(move |req| {
if value == req.message.data {
req.respond(srv::SetBool::Response {
success: false,
message: format!("Value is already {value}."),
})
.expect("could not send service response");
} else {
value = req.message.data;
req.respond(srv::SetBool::Response {
success: true,
message: format!("Value set to {value}."),
})
.expect("could not send service response");
}
});
spawner.spawn_local(service_future)?;

let mut counter = 0;
let wall_timer_future =
node.create_wall_timer(Duration::from_millis(500))?
.on_tick(move |_| {
counter += 1;
println!("Wall timer tick: {counter}");
});
spawner.spawn_local(wall_timer_future)?;

let ros_timer_future = node.create_timer(Duration::from_secs(1))?.on_tick(|diff| {
println!("ROS timer tick. Time elapsed since last tick: {diff:?}");
});
spawner.spawn_local(ros_timer_future)?;

loop {
node.spin_once(std::time::Duration::from_millis(5));
pool.run_until_stalled();
}
}
14 changes: 7 additions & 7 deletions r2r/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,27 +318,27 @@ impl Client_ for UntypedClient_ {
}
}

pub fn create_client_helper(
node: *mut rcl_node_t, service_name: &str, service_ts: *const rosidl_service_type_support_t,
qos_profile: QosProfile,
) -> Result<rcl_client_t> {
let mut client_handle = unsafe { rcl_get_zero_initialized_client() };
pub unsafe fn create_client_helper(
client_handle: &mut rcl_client_t, node: *mut rcl_node_t, service_name: &str,
service_ts: *const rosidl_service_type_support_t, qos_profile: QosProfile,
) -> Result<()> {
let service_name_c_string =
CString::new(service_name).map_err(|_| Error::RCL_RET_INVALID_ARGUMENT)?;

let result = unsafe {
let mut client_options = rcl_client_get_default_options();
client_options.qos = qos_profile.into();
rcl_client_init(
&mut client_handle,
client_handle,
node,
service_ts,
service_name_c_string.as_ptr(),
&client_options,
)
};

if result == RCL_RET_OK as i32 {
Ok(client_handle)
Ok(())
} else {
Err(Error::from_rcl_error(result))
}
Expand Down
1 change: 1 addition & 0 deletions r2r/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub use clocks::{Clock, ClockType};

mod nodes;
pub use nodes::{Node, Timer};
pub use r2r_tracing::StreamWithTracingData;

pub mod qos;

Expand Down
Loading