Skip to content

Commit

Permalink
Add a service lambda example
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Jul 3, 2021
1 parent fd5e898 commit c72ca06
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rclcpp/services/minimal_client/member_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MinimalClient : public rclcpp::Node
{
public:
MinimalClient()
: Node("minimal_service")
: Node("minimal_client")
{
client_ = this->create_client<AddTwoInts>("add_two_ints");
while (!client_->wait_for_service(1s)) {
Expand Down
8 changes: 6 additions & 2 deletions rclcpp/services/minimal_service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ find_package(ament_cmake REQUIRED)
find_package(example_interfaces REQUIRED)
find_package(rclcpp REQUIRED)

add_executable(service_not_composable not_composable.cpp)
ament_target_dependencies(service_not_composable rclcpp example_interfaces)
add_executable(service_lambda lambda.cpp)
ament_target_dependencies(service_lambda rclcpp example_interfaces)

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

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

install(TARGETS
service_lambda
service_not_composable
service_member_function
DESTINATION lib/${PROJECT_NAME})
Expand Down
55 changes: 55 additions & 0 deletions rclcpp/services/minimal_service/lambda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 <cinttypes>
#include <functional>
#include <memory>

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

using example_interfaces::srv::AddTwoInts;

/* This example creates a subclass of Node and uses a fancy C++11 lambda
* function to shorten the callback syntax, at the expense of making the
* code somewhat more difficult to understand at first glance. */

class MinimalService : public rclcpp::Node
{
public:
MinimalService()
: Node("minimal_service")
{
server_ = this->create_service<AddTwoInts>(
"add_two_ints",
[this](const AddTwoInts::Request::SharedPtr request,
AddTwoInts::Response::SharedPtr response) {
RCLCPP_INFO(
this->get_logger(),
"request: %" PRId64 " + %" PRId64, request->a, request->b);
response->sum = request->a + request->b;
});
}

private:
rclcpp::Service<AddTwoInts>::SharedPtr server_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalService>());
rclcpp::shutdown();
return 0;
}
2 changes: 1 addition & 1 deletion rclcpp/topics/minimal_publisher/lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MinimalPublisher : public rclcpp::Node
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
auto timer_callback =
[this]() -> void {
[this]() {
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(this->count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
Expand Down
6 changes: 5 additions & 1 deletion rclcpp/topics/minimal_subscriber/lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

/* This example creates a subclass of Node and uses a fancy C++11 lambda
* function to shorten the callback syntax, at the expense of making the
* code somewhat more difficult to understand at first glance. */

class MinimalSubscriber : public rclcpp::Node
{
public:
Expand All @@ -26,7 +30,7 @@ class MinimalSubscriber : public rclcpp::Node
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic",
10,
[this](std_msgs::msg::String::UniquePtr msg) {
[this](const std_msgs::msg::String::SharedPtr msg) {
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
});
}
Expand Down

0 comments on commit c72ca06

Please sign in to comment.