Skip to content

Commit

Permalink
Added basic create_action_client function
Browse files Browse the repository at this point in the history
  • Loading branch information
esteve committed Nov 28, 2022
1 parent 795a3ac commit 9d0b2d8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/minimal_action_client_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ version = "0.3"
[dependencies.rosidl_runtime_rs]
version = "0.3"

[dependencies.action_msgs]
[dependencies.example_interfaces]
version = "*"
4 changes: 2 additions & 2 deletions examples/minimal_action_client_server/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

<build_depend>rclrs</build_depend>
<build_depend>rosidl_runtime_rs</build_depend>
<build_depend>action_msgs</build_depend>
<build_depend>example_interfaces</build_depend>

<exec_depend>rclrs</exec_depend>
<exec_depend>rosidl_runtime_rs</exec_depend>
<exec_depend>action_msgs</exec_depend>
<exec_depend>example_interfaces</exec_depend>

<export>
<build_type>ament_cargo</build_type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use anyhow::{Error, Result};
fn main() -> Result<(), Error> {
let context = rclrs::Context::new(env::args())?;

let _node = rclrs::create_node(&context, "minimal_action_client")?;
let mut node = rclrs::create_node(&context, "minimal_client")?;

let _client = node.create_action_client::<example_interfaces::action::Fibonacci>("fibonacci")?;

Ok(())
}
2 changes: 2 additions & 0 deletions rclrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! [1]: https://github.com/ros2-rust/ros2_rust/blob/main/README.md

mod action;
mod arguments;
mod client;
mod context;
Expand All @@ -25,6 +26,7 @@ pub mod dynamic_message;

use std::time::Duration;

pub use action::*;
pub use arguments::*;
pub use client::*;
pub use context::*;
Expand Down
26 changes: 23 additions & 3 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub use self::builder::*;
pub use self::graph::*;
use crate::rcl_bindings::*;
use crate::{
Client, ClientBase, Context, GuardCondition, ParameterOverrideMap, Publisher, QoSProfile,
RclrsError, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback,
ToResult,
ActionClient, Client, ClientBase, Context, GuardCondition, ParameterOverrideMap, Publisher,
QoSProfile, RclrsError, Service, ServiceBase, Subscription, SubscriptionBase,
SubscriptionCallback, ToResult,
};

impl Drop for rcl_node_t {
Expand Down Expand Up @@ -190,6 +190,26 @@ impl Node {
Ok(client)
}

/// Creates a [`Client`][1].
///
/// [1]: crate::ActionClient
// TODO: make action client's lifetime depend on node's lifetime
pub fn create_action_client<T>(
&mut self,
topic: &str,
) -> Result<Arc<ActionClient<T>>, RclrsError>
where
T: rosidl_runtime_rs::Action,
{
let client = Arc::new(ActionClient::<T>::new(
Arc::clone(&self.rcl_node_mtx),
topic,
)?);
// self.clients
// .push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
Ok(client)
}

/// Creates a [`GuardCondition`][1] with no callback.
///
/// A weak pointer to the `GuardCondition` is stored within this node.
Expand Down
17 changes: 17 additions & 0 deletions rosidl_generator_rs/resource/action.rs.em
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,20 @@ TEMPLATE(
get_idiomatic_rs_type=get_idiomatic_rs_type,
constant_value_to_rs=constant_value_to_rs)
}@

@[for subfolder, action_spec in action_specs]

@{
type_name = action_spec.namespaced_type.name
}@

// Corresponds to @(package_name)__@(subfolder)__@(type_name)
pub struct @(type_name);

impl rosidl_runtime_rs::Action for @(type_name) {
type Goal = crate::@(subfolder)::rmw::@(type_name)_Goal;
type Result = crate::@(subfolder)::rmw::@(type_name)_Result;
type Feedback = crate::@(subfolder)::rmw::@(type_name)_Feedback;
}

@[end for]
4 changes: 4 additions & 0 deletions rosidl_generator_rs/resource/lib.rs.em
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ pub mod msg;
@[if len(srv_specs) > 0]@
pub mod srv;
@[end if]@

@[if len(action_specs) > 0]@
pub mod action;
@[end if]@
2 changes: 1 addition & 1 deletion rosidl_runtime_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mod string;
pub use string::{BoundedString, BoundedWString, String, StringExceedsBoundsError, WString};

mod traits;
pub use traits::{Message, RmwMessage, SequenceAlloc, Service};
pub use traits::{Action, Message, RmwMessage, SequenceAlloc, Service};
14 changes: 14 additions & 0 deletions rosidl_runtime_rs/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ pub trait Service: 'static {
/// Get a pointer to the correct `rosidl_service_type_support_t` structure.
fn get_type_support() -> *const std::os::raw::c_void;
}

/// Trait for actions.
///
/// User code never needs to call this trait's method, much less implement this trait.
pub trait Action: 'static {
/// The goal message associated with this service.
type Goal: Message;

/// The result message associated with this service.
type Result: Message;

/// The feedback message associated with this service.
type Feedback: Message;
}

0 comments on commit 9d0b2d8

Please sign in to comment.