ros2 服务通讯示范源代码2023年9月27

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

#include 
#include 
#include 

using namespace std::chrono_literals;
int main(int argc,char ** argv)
{
    rclcpp::init(argc,argv);
    auto pnode=std::make_shared("clientNode");
    auto pclient=pnode->create_client("service1"); //
    auto prequest=std::make_shared();
    prequest->a=atoll(argv[1]);
    prequest->b=atoll(argv[2]);

    while(!pclient->wait_for_service(3s))
    {

        RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not availabel,waiting again...");
    }

    auto result=pclient->async_send_request(prequest);

    if(rclcpp::spin_until_future_complete(pnode,result)==rclcpp::FutureReturnCode::SUCCESS)
    {
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"result=%d",result.get()->sum );
    }
    else
    {
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"failed to call service im sorry");
    };
    rclcpp::shutdown();
    return 0;




}

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

#include "memory.h"

void add(const std::shared_ptr prequest,
  std::shared_ptr presponse)
{ 
    presponse->sum=prequest->a+prequest->b;
    RCLCPP_INFO( rclcpp::get_logger("rclcpp"),"a=%d,b=%d,result:%d",prequest->a,prequest->b,presponse->sum );

}

int main(int argc,char ** argv)
{
    rclcpp::init(argc,argv);
    //define a node
    auto pnode=std::make_shared("serverNode");
    auto pservice=pnode->create_service("service1",&add);
    rclcpp::spin(pnode);
    rclcpp::shutdown();
}

//cmakelists.txt
cmake_minimum_required(VERSION 3.8)
project(server)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(example_interfaces REQUIRED)
add_executable(server_cpp  src/server_cpp.cpp)
add_executable(client_cpp  src/client_cpp.cpp)
ament_target_dependencies(server_cpp rclcpp example_interfaces)
ament_target_dependencies(client_cpp rclcpp example_interfaces)


if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()
install(TARGETS
    server_cpp
    client_cpp
    DESTINATION lib/${PROJECT_NAME}
)
ament_package()

//package.xml



  server
  0.0.0
  TODO: Package description
  actorsun
  TODO: License declaration

  ament_cmake

  rclcpp
  example_interfaces

  ament_lint_auto
  ament_lint_common

  
    ament_cmake
  

ros2 服务通讯示范源代码2023年9月27_第1张图片

你可能感兴趣的:(c++,机器人,ros2,ros,嵌入式系统)