diff --git a/userland/CMakeLists.txt b/userland/CMakeLists.txt index 143a0760..ceebe649 100644 --- a/userland/CMakeLists.txt +++ b/userland/CMakeLists.txt @@ -10,6 +10,7 @@ find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(ros_middleware_implementation REQUIRED) find_package(simple_msgs REQUIRED) +find_package(userland_msgs REQUIRED) ament_package() @@ -27,7 +28,8 @@ function(build_executable executable) ament_target_dependencies(${executable} "rclcpp" "ros_middleware_implementation" - "simple_msgs") + "simple_msgs" + "userland_msgs") install(TARGETS ${executable} DESTINATION bin) @@ -37,7 +39,8 @@ function(build_executable executable) ament_target_dependencies(${executable}__${middleware_impl_tmp} "rclcpp" "${middleware_impl_tmp}" - "simple_msgs") + "simple_msgs" + "userland_msgs") install(TARGETS ${executable}__${middleware_impl_tmp} DESTINATION bin) endforeach() @@ -110,3 +113,7 @@ build_executable(listener src/ros1_like/listener.cpp) build_executable(different_groups src/explicit/different_groups.cpp) build_executable(two_nodes src/explicit/two_nodes.cpp) # build_executable(two_nodes_no_intra src/explicit/two_nodes_no_intra.cpp) + +# Build services examples +build_executable(add_two_ints_client src/add_two_ints_client.cpp) +build_executable(add_two_ints_server src/add_two_ints_server.cpp) diff --git a/userland/package.xml b/userland/package.xml index 4d74cb3e..f8e11bc9 100644 --- a/userland/package.xml +++ b/userland/package.xml @@ -11,8 +11,14 @@ rclcpp ros_middleware_implementation simple_msgs + userland_msgs + + rosidl_default_generators rclcpp ros_middleware_implementation simple_msgs + userland_msgs + + rosidl_default_runtime diff --git a/userland/src/add_two_ints_client.cpp b/userland/src/add_two_ints_client.cpp new file mode 100644 index 00000000..cffca423 --- /dev/null +++ b/userland/src/add_two_ints_client.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +int main(int argc, char** argv) +{ + rclcpp::init(argc, argv); + + auto node = rclcpp::Node::make_shared("add_two_ints_client"); + + auto client = node->create_client("add_two_ints"); + auto request = std::make_shared(); + auto response = std::make_shared(); + request->a = 2; + request->b = 3; + + auto f = client->async_send_request(request, response); + + std::future_status status; + do + { + rclcpp::spin_some(node); + status = f.wait_for(std::chrono::milliseconds(100)); + } while(status != std::future_status::ready && rclcpp::ok()); + + if(std::future_status::ready == status) + { + std::cout << "FUTURE READY" << std::endl; + std::cout << f.get()->sum << std::endl; + } + + return 0; +} diff --git a/userland/src/add_two_ints_server.cpp b/userland/src/add_two_ints_server.cpp new file mode 100644 index 00000000..c7104772 --- /dev/null +++ b/userland/src/add_two_ints_server.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +void add(const std::shared_ptr req, + std::shared_ptr res) +{ + std::cout << "Incoming request" << std::endl; + std::cout << "a: " << req->a << " b: " << req->b << std::endl; + res->sum = req->a + req->b; +} + +int main(int argc, char** argv) +{ + rclcpp::init(argc, argv); + + auto node = rclcpp::Node::make_shared("add_two_ints_server"); + + node->create_service("add_two_ints", add); + + rclcpp::spin(node); + + return 0; +} diff --git a/userland_msgs/CMakeLists.txt b/userland_msgs/CMakeLists.txt new file mode 100644 index 00000000..6ddcee48 --- /dev/null +++ b/userland_msgs/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 2.8.3) + +project(userland_msgs) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +find_package(ament_cmake REQUIRED) +find_package(builtin_msgs REQUIRED) +find_package(rosidl_default_generators REQUIRED) + +include_directories(${rosidl_default_generators_INCLUDE_DIRS}) + +rosidl_generate_interfaces(${PROJECT_NAME} + "srv/AddTwoInts.srv" + DEPENDENCIES builtin_msgs +) + +ament_package() diff --git a/userland_msgs/package.xml b/userland_msgs/package.xml new file mode 100644 index 00000000..437d2bb9 --- /dev/null +++ b/userland_msgs/package.xml @@ -0,0 +1,20 @@ + + + userland_msgs + 0.0.0 + A package containing simple message definitions. + Dirk Thomas + Apache License 2.0 + + ament_cmake + + rosidl_default_generators + + builtin_msgs + simple_msgs + + builtin_msgs + simple_msgs + + rosidl_default_runtime + diff --git a/userland_msgs/srv/AddTwoInts.srv b/userland_msgs/srv/AddTwoInts.srv new file mode 100644 index 00000000..3a68808e --- /dev/null +++ b/userland_msgs/srv/AddTwoInts.srv @@ -0,0 +1,4 @@ +int64 a +int64 b +--- +int64 sum