Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ROS2 service calls from C++ nodes #441

Merged
merged 5 commits into from
Mar 26, 2024
Merged

Enable ROS2 service calls from C++ nodes #441

merged 5 commits into from
Mar 26, 2024

Conversation

phil-opp
Copy link
Collaborator

@phil-opp phil-opp commented Mar 21, 2024

Allows creating ROS2 service clients in C++ nodes using our dora-ros2-bridge.

Builds upon #439

Usage

  • Initialize the ROS2 context using init_ros2_context()
  • Create ROS2 node using ros2_context->new_node
  • Define QoS settings for service, e.g.:
    auto service_qos = qos_default();
    service_qos.reliable = true;
    service_qos.max_blocking_time = 0.1;
    service_qos.keep_last = 1;
  • Create service client using one of the node->create_client_* functions. For example, to create a client to the add_two_ints service:
    auto add_two_ints = node->create_client_example_interfaces_AddTwoInts("/", "add_two_ints", service_qos, merged_events);
    The merged_events argument is the combined event stream that should receive the response data.
  • Wait until the service is available using: add_two_ints->wait_for_service(node)
  • Send one or multiple requests using add_two_ints->send_request(request). The responses will be sent to the combined event stream.
  • To check if an incoming event is a service response, use the matches and downcast functions:
    if (add_two_ints->matches(event))
    {
        auto response = add_two_ints->downcast(std::move(event)); 
        ...
    }

@phil-opp phil-opp force-pushed the ros2-services-c++ branch from a35e81f to 147bcc4 Compare March 21, 2024 16:19
@phil-opp phil-opp marked this pull request as ready for review March 21, 2024 16:19
how to run service client sample
Copy link
Collaborator

@haixuanTao haixuanTao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks!

Base automatically changed from ros2-services to main March 26, 2024 11:50
@phil-opp
Copy link
Collaborator Author

Thanks @bobd988 for the README update!

@phil-opp phil-opp merged commit 7ede5ba into main Mar 26, 2024
17 checks passed
@phil-opp phil-opp deleted the ros2-services-c++ branch March 26, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants