ROS 2 rclcpp + rcl - jazzy
jazzy
ROS 2 C++ Client Library with ROS Client Library
|
Public Member Functions | |
RCLCPP_PUBLIC | ServiceBase (std::shared_ptr< rcl_node_t > node_handle) |
RCLCPP_PUBLIC const char * | get_service_name () |
Return the name of the service. More... | |
RCLCPP_PUBLIC std::shared_ptr< rcl_service_t > | get_service_handle () |
Return the rcl_service_t service handle in a std::shared_ptr. More... | |
RCLCPP_PUBLIC std::shared_ptr< const rcl_service_t > | get_service_handle () const |
Return the rcl_service_t service handle in a std::shared_ptr. More... | |
RCLCPP_PUBLIC bool | take_type_erased_request (void *request_out, rmw_request_id_t &request_id_out) |
Take the next request from the service as a type erased pointer. More... | |
virtual std::shared_ptr< void > | create_request ()=0 |
virtual std::shared_ptr< rmw_request_id_t > | create_request_header ()=0 |
virtual void | handle_request (std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0 |
RCLCPP_PUBLIC bool | exchange_in_use_by_wait_set_state (bool in_use_state) |
Exchange the "in use by wait set" state for this service. More... | |
RCLCPP_PUBLIC rclcpp::QoS | get_response_publisher_actual_qos () const |
Get the actual response publisher QoS settings, after the defaults have been determined. More... | |
RCLCPP_PUBLIC rclcpp::QoS | get_request_subscription_actual_qos () const |
Get the actual request subscription QoS settings, after the defaults have been determined. More... | |
void | set_on_new_request_callback (std::function< void(size_t)> callback) |
Set a callback to be called when each new request is received. More... | |
void | clear_on_new_request_callback () |
Unset the callback registered for new requests, if any. | |
Protected Member Functions | |
RCLCPP_PUBLIC rcl_node_t * | get_rcl_node_handle () |
RCLCPP_PUBLIC const rcl_node_t * | get_rcl_node_handle () const |
RCLCPP_PUBLIC void | set_on_new_request_callback (rcl_event_callback_t callback, const void *user_data) |
Protected Attributes | |
std::shared_ptr< rcl_node_t > | node_handle_ |
std::recursive_mutex | callback_mutex_ |
std::function< void(size_t)> | on_new_request_callback_ {nullptr} |
std::shared_ptr< rcl_service_t > | service_handle_ |
bool | owns_rcl_handle_ = true |
rclcpp::Logger | node_logger_ |
std::atomic< bool > | in_use_by_wait_set_ {false} |
Definition at line 51 of file service.hpp.
bool ServiceBase::exchange_in_use_by_wait_set_state | ( | bool | in_use_state | ) |
Exchange the "in use by wait set" state for this service.
This is used to ensure this service is not used by multiple wait sets at the same time.
[in] | in_use_state | the new state to exchange into the state, true indicates it is now in use by a wait set, and false is that it is no longer in use by a wait set. |
Definition at line 83 of file service.cpp.
rclcpp::QoS ServiceBase::get_request_subscription_actual_qos | ( | ) | const |
Get the actual request subscription QoS settings, after the defaults have been determined.
The actual configuration applied when using RMW_QOS_POLICY_*_SYSTEM_DEFAULT can only be resolved after the creation of the service, and it depends on the underlying rmw implementation. If the underlying setting in use can't be represented in ROS terms, it will be set to RMW_QOS_POLICY_*_UNKNOWN. May throw runtime_error when an unexpected error occurs.
std::runtime_error | if failed to get qos settings |
Definition at line 108 of file service.cpp.
References rclcpp::QoSInitialization::from_rmw(), and rcl_service_request_subscription_get_actual_qos().
rclcpp::QoS ServiceBase::get_response_publisher_actual_qos | ( | ) | const |
Get the actual response publisher QoS settings, after the defaults have been determined.
The actual configuration applied when using RMW_QOS_POLICY_*_SYSTEM_DEFAULT can only be resolved after the creation of the service, and it depends on the underlying rmw implementation. If the underlying setting in use can't be represented in ROS terms, it will be set to RMW_QOS_POLICY_*_UNKNOWN. May throw runtime_error when an unexpected error occurs.
std::runtime_error | if failed to get qos settings |
Definition at line 89 of file service.cpp.
References rclcpp::QoSInitialization::from_rmw(), and rcl_service_response_publisher_get_actual_qos().
std::shared_ptr< rcl_service_t > ServiceBase::get_service_handle | ( | ) |
Return the rcl_service_t service handle in a std::shared_ptr.
This handle remains valid after the Service is destroyed. The actual rcl service is not finalized until it is out of scope everywhere.
Definition at line 59 of file service.cpp.
Referenced by get_service_name(), rclcpp::Service< ServiceT >::Service(), and take_type_erased_request().
std::shared_ptr< const rcl_service_t > ServiceBase::get_service_handle | ( | ) | const |
Return the rcl_service_t service handle in a std::shared_ptr.
This handle remains valid after the Service is destroyed. The actual rcl service is not finalized until it is out of scope everywhere.
Definition at line 65 of file service.cpp.
const char * ServiceBase::get_service_name | ( | ) |
Return the name of the service.
Definition at line 53 of file service.cpp.
References get_service_handle(), and rcl_service_get_service_name().
|
inline |
Set a callback to be called when each new request is received.
The callback receives a size_t which is the number of requests received since the last time this callback was called. Normally this is 1, but can be > 1 if requests were received before any callback was set.
Since this callback is called from the middleware, you should aim to make it fast and not blocking. If you need to do a lot of work or wait for some other event, you should spin it off to another thread, otherwise you risk blocking the middleware.
Calling it again will clear any previously set callback.
An exception will be thrown if the callback is not callable.
This function is thread-safe.
If you want more information available in the callback, like the service or other information, you may use a lambda with captures or std::bind.
[in] | callback | functor to be called when a new request is received |
Definition at line 193 of file service.hpp.
Referenced by clear_on_new_request_callback().
bool ServiceBase::take_type_erased_request | ( | void * | request_out, |
rmw_request_id_t & | request_id_out | ||
) |
Take the next request from the service as a type erased pointer.
This type erased version of
[out] | request_out | The type erased pointer to a service request object into which the middleware will copy the taken request. |
[out] | request_id_out | The output id for the request which can be used to associate response with this request in the future. |
rclcpp::exceptions::RCLError | based exceptions if the underlying rcl calls fail. |
Definition at line 38 of file service.cpp.
References get_service_handle(), RCL_RET_OK, RCL_RET_SERVICE_TAKE_FAILED, and rcl_take_request().
Referenced by rclcpp::Service< ServiceT >::take_request().