Skip to content

Commit c72ca06

Browse files
committed
Add a service lambda example
1 parent fd5e898 commit c72ca06

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

rclcpp/services/minimal_client/member_function.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MinimalClient : public rclcpp::Node
3030
{
3131
public:
3232
MinimalClient()
33-
: Node("minimal_service")
33+
: Node("minimal_client")
3434
{
3535
client_ = this->create_client<AddTwoInts>("add_two_ints");
3636
while (!client_->wait_for_service(1s)) {

rclcpp/services/minimal_service/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ find_package(ament_cmake REQUIRED)
1414
find_package(example_interfaces REQUIRED)
1515
find_package(rclcpp REQUIRED)
1616

17-
add_executable(service_not_composable not_composable.cpp)
18-
ament_target_dependencies(service_not_composable rclcpp example_interfaces)
17+
add_executable(service_lambda lambda.cpp)
18+
ament_target_dependencies(service_lambda rclcpp example_interfaces)
1919

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

23+
add_executable(service_not_composable not_composable.cpp)
24+
ament_target_dependencies(service_not_composable rclcpp example_interfaces)
25+
2326
install(TARGETS
27+
service_lambda
2428
service_not_composable
2529
service_member_function
2630
DESTINATION lib/${PROJECT_NAME})
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 <cinttypes>
16+
#include <functional>
17+
#include <memory>
18+
19+
#include "example_interfaces/srv/add_two_ints.hpp"
20+
#include "rclcpp/rclcpp.hpp"
21+
22+
using example_interfaces::srv::AddTwoInts;
23+
24+
/* This example creates a subclass of Node and uses a fancy C++11 lambda
25+
* function to shorten the callback syntax, at the expense of making the
26+
* code somewhat more difficult to understand at first glance. */
27+
28+
class MinimalService : public rclcpp::Node
29+
{
30+
public:
31+
MinimalService()
32+
: Node("minimal_service")
33+
{
34+
server_ = this->create_service<AddTwoInts>(
35+
"add_two_ints",
36+
[this](const AddTwoInts::Request::SharedPtr request,
37+
AddTwoInts::Response::SharedPtr response) {
38+
RCLCPP_INFO(
39+
this->get_logger(),
40+
"request: %" PRId64 " + %" PRId64, request->a, request->b);
41+
response->sum = request->a + request->b;
42+
});
43+
}
44+
45+
private:
46+
rclcpp::Service<AddTwoInts>::SharedPtr server_;
47+
};
48+
49+
int main(int argc, char * argv[])
50+
{
51+
rclcpp::init(argc, argv);
52+
rclcpp::spin(std::make_shared<MinimalService>());
53+
rclcpp::shutdown();
54+
return 0;
55+
}

rclcpp/topics/minimal_publisher/lambda.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MinimalPublisher : public rclcpp::Node
3333
{
3434
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
3535
auto timer_callback =
36-
[this]() -> void {
36+
[this]() {
3737
auto message = std_msgs::msg::String();
3838
message.data = "Hello, world! " + std::to_string(this->count_++);
3939
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());

rclcpp/topics/minimal_subscriber/lambda.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "rclcpp/rclcpp.hpp"
1818
#include "std_msgs/msg/string.hpp"
1919

20+
/* This example creates a subclass of Node and uses a fancy C++11 lambda
21+
* function to shorten the callback syntax, at the expense of making the
22+
* code somewhat more difficult to understand at first glance. */
23+
2024
class MinimalSubscriber : public rclcpp::Node
2125
{
2226
public:
@@ -26,7 +30,7 @@ class MinimalSubscriber : public rclcpp::Node
2630
subscription_ = this->create_subscription<std_msgs::msg::String>(
2731
"topic",
2832
10,
29-
[this](std_msgs::msg::String::UniquePtr msg) {
33+
[this](const std_msgs::msg::String::SharedPtr msg) {
3034
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
3135
});
3236
}

0 commit comments

Comments
 (0)