Struct safe_drive::context::Context
source · pub struct Context { /* private fields */ }
Expand description
Context of ROS2.
Implementations§
source§impl Context
impl Context
sourcepub fn new() -> Result<Arc<Self>, DynError>
pub fn new() -> Result<Arc<Self>, DynError>
Create a new context.
Example
use safe_drive::context::Context;
// Create a context.
let ctx = Context::new().unwrap();
sourcepub fn create_node(
self: &Arc<Self>,
name: &str,
namespace: Option<&str>,
options: NodeOptions
) -> RCLResult<Arc<Node>>
pub fn create_node(
self: &Arc<Self>,
name: &str,
namespace: Option<&str>,
options: NodeOptions
) -> RCLResult<Arc<Node>>
Create a new node of ROS2.
Example
use safe_drive::context::Context;
// Create a context.
let ctx = Context::new().unwrap();
// Create a node.
let node = ctx
.create_node("context_rs", None, Default::default())
.unwrap();
Errors
RCLError::AlreadyInit
if the node has already be initialized, orRCLError::NotInit
if the given context is invalid, orRCLError::InvalidArgument
if any arguments are invalid, orRCLError::BadAlloc
if allocating memory failed, orRCLError::NodeInvalidName
if the name is invalid, orRCLError::NodeInvalidNamespace
if the namespace_ is invalid, orRCLError::Error
if an unspecified error occurs.
sourcepub fn create_selector(self: &Arc<Self>) -> RCLResult<Selector>
pub fn create_selector(self: &Arc<Self>) -> RCLResult<Selector>
Create a new selector. The selector is used to wait event and invoke callback for single threaded execution.
Example
use safe_drive::context::Context;
// Create a context.
let ctx = Context::new().unwrap();
// Create a selector.
let selector = ctx.create_selector().unwrap();
Errors
RCLError::AlreadyInit
if the wait set is not zero initialized, orRCLError::NotInit
if the given context is invalid, orRCLError::InvalidArgument
if any arguments are invalid, orRCLError::BadAlloc
if allocating memory failed, orRCLError::WaitSetInvalid
if the wait set is not destroyed properly, orRCLError::Error
if an unspecified error occurs.
Examples found in repository?
src/parameter.rs (line 838)
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
fn param_server(
node: Arc<Node>,
params: Arc<RwLock<Parameters>>,
cond_halt: GuardCondition,
cond_callback: GuardCondition,
) -> Result<(), DynError> {
if let Ok(mut selector) = node.context.create_selector() {
add_srv_list(&node, &mut selector, params.clone())?;
add_srv_set(
&node,
&mut selector,
params.clone(),
"set_parameters",
cond_callback.clone(),
)?;
add_srv_set(
&node,
&mut selector,
params.clone(),
"set_parameters_atomically",
cond_callback,
)?;
add_srv_get(&node, &mut selector, params.clone())?;
add_srv_get_types(&node, &mut selector, params.clone())?;
add_srv_describe(&node, &mut selector, params)?;
let is_halt = Rc::new(Cell::new(false));
let is_halt_cloned = is_halt.clone();
selector.add_guard_condition(
&cond_halt,
Some(Box::new(move || {
is_halt_cloned.set(true);
CallbackResult::Remove
})),
false,
);
while !is_halt.get() {
selector.wait()?;
}
} else {
let logger = Logger::new("safe_drive");
pr_error_in!(logger, "failed to start a parameter server");
}
Ok(())
}