Skip to content

Commit

Permalink
Add a client lambda example and remove unused header
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Jul 3, 2021
1 parent c72ca06 commit f6173ae
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 13 deletions.
10 changes: 7 additions & 3 deletions rclcpp/services/minimal_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ find_package(ament_cmake REQUIRED)
find_package(example_interfaces REQUIRED)
find_package(rclcpp REQUIRED)

add_executable(client_not_composable not_composable.cpp)
ament_target_dependencies(client_not_composable rclcpp example_interfaces)
add_executable(client_lambda lambda.cpp)
ament_target_dependencies(client_lambda rclcpp example_interfaces)

add_executable(client_member_function member_function.cpp)
ament_target_dependencies(client_member_function rclcpp example_interfaces)

add_executable(client_not_composable not_composable.cpp)
ament_target_dependencies(client_not_composable rclcpp example_interfaces)

install(TARGETS
client_not_composable
client_lambda
client_member_function
client_not_composable
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
Expand Down
67 changes: 67 additions & 0 deletions rclcpp/services/minimal_client/lambda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <cinttypes>
#include <memory>

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

using namespace std::chrono_literals;
using example_interfaces::srv::AddTwoInts;

/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the client. */

class MinimalClient : public rclcpp::Node
{
public:
MinimalClient()
: Node("minimal_client")
{
client_ = this->create_client<AddTwoInts>("add_two_ints");
while (!client_->wait_for_service(1s)) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(this->get_logger(), "client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(this->get_logger(), "waiting for service to appear...");
}
auto request = std::make_shared<AddTwoInts::Request>();
request->a = 41;
request->b = 1;
auto result_future = client_->async_send_request(
request,
[this](rclcpp::Client<AddTwoInts>::SharedFutureWithRequest result_future) {
auto result = result_future.get();
auto request = result.first;
auto response = result.second;
RCLCPP_INFO(
this->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64,
request->a, request->b, response->sum);
rclcpp::shutdown();
});
}

private:
rclcpp::Client<AddTwoInts>::SharedPtr client_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalClient>());
rclcpp::shutdown();
return 0;
}
2 changes: 1 addition & 1 deletion rclcpp/services/minimal_service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ ament_target_dependencies(service_not_composable rclcpp example_interfaces)

install(TARGETS
service_lambda
service_not_composable
service_member_function
service_not_composable
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
Expand Down
1 change: 0 additions & 1 deletion rclcpp/services/minimal_service/lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <cinttypes>
#include <functional>
#include <memory>

#include "example_interfaces/srv/add_two_ints.hpp"
Expand Down
1 change: 0 additions & 1 deletion rclcpp/services/minimal_service/member_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <cinttypes>
#include <functional>
#include <memory>

#include "example_interfaces/srv/add_two_ints.hpp"
Expand Down
1 change: 0 additions & 1 deletion rclcpp/topics/minimal_publisher/member_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <chrono>
#include <functional>
#include <memory>
#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <chrono>
#include <functional>
#include <memory>
#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <chrono>
#include <functional>
#include <memory>
#include <sstream>
#include <string>
Expand Down
1 change: 0 additions & 1 deletion rclcpp/topics/minimal_subscriber/member_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <functional>
#include <memory>

#include "rclcpp/rclcpp.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <functional>
#include <memory>
#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.


#include <functional>
#include <memory>
#include <sstream>
#include <string>
Expand Down

0 comments on commit f6173ae

Please sign in to comment.