Skip to content

Commit f6173ae

Browse files
committed
Add a client lambda example and remove unused header
1 parent c72ca06 commit f6173ae

11 files changed

+75
-13
lines changed

rclcpp/services/minimal_client/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ find_package(ament_cmake REQUIRED)
1414
find_package(example_interfaces REQUIRED)
1515
find_package(rclcpp REQUIRED)
1616

17-
add_executable(client_not_composable not_composable.cpp)
18-
ament_target_dependencies(client_not_composable rclcpp example_interfaces)
17+
add_executable(client_lambda lambda.cpp)
18+
ament_target_dependencies(client_lambda rclcpp example_interfaces)
1919

2020
add_executable(client_member_function member_function.cpp)
2121
ament_target_dependencies(client_member_function rclcpp example_interfaces)
2222

23+
add_executable(client_not_composable not_composable.cpp)
24+
ament_target_dependencies(client_not_composable rclcpp example_interfaces)
25+
2326
install(TARGETS
24-
client_not_composable
27+
client_lambda
2528
client_member_function
29+
client_not_composable
2630
DESTINATION lib/${PROJECT_NAME})
2731

2832
if(BUILD_TESTING)
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2016 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <chrono>
16+
#include <cinttypes>
17+
#include <memory>
18+
19+
#include "example_interfaces/srv/add_two_ints.hpp"
20+
#include "rclcpp/rclcpp.hpp"
21+
22+
using namespace std::chrono_literals;
23+
using example_interfaces::srv::AddTwoInts;
24+
25+
/* This example creates a subclass of Node and uses std::bind() to register a
26+
* member function as a callback from the client. */
27+
28+
class MinimalClient : public rclcpp::Node
29+
{
30+
public:
31+
MinimalClient()
32+
: Node("minimal_client")
33+
{
34+
client_ = this->create_client<AddTwoInts>("add_two_ints");
35+
while (!client_->wait_for_service(1s)) {
36+
if (!rclcpp::ok()) {
37+
RCLCPP_ERROR(this->get_logger(), "client interrupted while waiting for service to appear.");
38+
}
39+
RCLCPP_INFO(this->get_logger(), "waiting for service to appear...");
40+
}
41+
auto request = std::make_shared<AddTwoInts::Request>();
42+
request->a = 41;
43+
request->b = 1;
44+
auto result_future = client_->async_send_request(
45+
request,
46+
[this](rclcpp::Client<AddTwoInts>::SharedFutureWithRequest result_future) {
47+
auto result = result_future.get();
48+
auto request = result.first;
49+
auto response = result.second;
50+
RCLCPP_INFO(
51+
this->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64,
52+
request->a, request->b, response->sum);
53+
rclcpp::shutdown();
54+
});
55+
}
56+
57+
private:
58+
rclcpp::Client<AddTwoInts>::SharedPtr client_;
59+
};
60+
61+
int main(int argc, char * argv[])
62+
{
63+
rclcpp::init(argc, argv);
64+
rclcpp::spin(std::make_shared<MinimalClient>());
65+
rclcpp::shutdown();
66+
return 0;
67+
}

rclcpp/services/minimal_service/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ ament_target_dependencies(service_not_composable rclcpp example_interfaces)
2525

2626
install(TARGETS
2727
service_lambda
28-
service_not_composable
2928
service_member_function
29+
service_not_composable
3030
DESTINATION lib/${PROJECT_NAME})
3131

3232
if(BUILD_TESTING)

rclcpp/services/minimal_service/lambda.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include <cinttypes>
16-
#include <functional>
1716
#include <memory>
1817

1918
#include "example_interfaces/srv/add_two_ints.hpp"

rclcpp/services/minimal_service/member_function.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include <cinttypes>
16-
#include <functional>
1716
#include <memory>
1817

1918
#include "example_interfaces/srv/add_two_ints.hpp"

rclcpp/topics/minimal_publisher/member_function.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16-
#include <functional>
1716
#include <memory>
1817
#include <string>
1918

rclcpp/topics/minimal_publisher/member_function_with_type_adapter.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16-
#include <functional>
1716
#include <memory>
1817
#include <string>
1918

rclcpp/topics/minimal_publisher/member_function_with_unique_network_flow_endpoints.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16-
#include <functional>
1716
#include <memory>
1817
#include <sstream>
1918
#include <string>

rclcpp/topics/minimal_subscriber/member_function.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <functional>
1615
#include <memory>
1716

1817
#include "rclcpp/rclcpp.hpp"

rclcpp/topics/minimal_subscriber/member_function_with_type_adapter.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <functional>
1615
#include <memory>
1716
#include <string>
1817

rclcpp/topics/minimal_subscriber/member_function_with_unique_network_flow_endpoints.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
16-
#include <functional>
1715
#include <memory>
1816
#include <sstream>
1917
#include <string>

0 commit comments

Comments
 (0)