pub struct Context { /* private fields */ }
Expand description
Shared state between nodes and similar entities.
It is possible, but not usually necessary, to have several contexts in an application.
Ownership of the context is shared by the Context
itself and all nodes created from it.
§Details
A context stores, among other things
- command line arguments (used for e.g. name remapping)
- middleware-specific data, e.g. the domain participant in DDS
- the allocator used (left as the default by
rclrs
)
The context also configures the rcl_logging_* layer to allow publication to /rosout (as well as the terminal). TODO: This behaviour should be configurable using an “auto logging initialise” flag as per rclcpp and rclpy.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new(
args: impl IntoIterator<Item = String>,
options: InitOptions,
) -> Result<Self, RclrsError>
pub fn new( args: impl IntoIterator<Item = String>, options: InitOptions, ) -> Result<Self, RclrsError>
Creates a new context.
-
args
- A sequence of strings that resembles command line arguments that users can pass into a ROS executable. See the official tutorial to know what these arguments may look like. To simply pass in the arguments that the user has provided from the command line, callSelf::from_env
orSelf::default_from_env
instead. -
options
- Additional options that your application can use to override settings that would otherwise be determined by the environment.
Creating a context will fail if args
contains invalid ROS arguments.
§Example
use rclrs::{Context, InitOptions};
let context = Context::new(
std::env::args(),
InitOptions::new().with_domain_id(Some(5)),
).unwrap();
assert_eq!(context.domain_id(), 5);
Sourcepub fn from_env(options: InitOptions) -> Result<Self, RclrsError>
pub fn from_env(options: InitOptions) -> Result<Self, RclrsError>
Same as Self::new
but std::env::args
is automatically passed in
for args
.
Sourcepub fn default_from_env() -> Result<Self, RclrsError>
pub fn default_from_env() -> Result<Self, RclrsError>
Same as Self::from_env
but the default InitOptions
is passed in
for options
.
Sourcepub fn domain_id(&self) -> usize
pub fn domain_id(&self) -> usize
Returns the ROS domain ID that the context is using.
The domain ID controls which nodes can send messages to each other, see the ROS 2 concept article.
It can be set through the ROS_DOMAIN_ID
environment variable.
Trait Implementations§
Source§impl CreateBasicExecutor for Context
impl CreateBasicExecutor for Context
Source§fn create_basic_executor(&self) -> Executor
fn create_basic_executor(&self) -> Executor
Context
.Source§impl Default for Context
impl Default for Context
Source§fn default() -> Self
fn default() -> Self
This will produce a Context
without providing any command line
arguments and using only the default InitOptions
. This is always
guaranteed to produce a valid Context
instance.
This is not the same as the “default context” defined for rclcpp
which is a globally shared context instance. rclrs
does not offer a
globally shared context instance.