ROS2 第一个C++程序(talker和listener为例)

ament 安装

在ROS2安装完成后,查看ament是否安装成功,如果未安装成功使用
sudo apt install ament*
安装ament软件包

创建软件开发工作区目录

$ mkdir -p ~/ros2_ws/src/ros2_demo/src

CMakeLists.txt和package.xml文件

进入 ~/ros2_ws/src/目录
cd ~/ros2_ws/src/ros2_demo
手工创建package.xml文件,内容如下:



    ros2_demo
    0.0.0
    Package containing examples of how to use the rcl API.
    Hu Chunxu
    Apache License 2.0

    ament_cmake

    rosidl_default_generators

    example_interfaces
    rcl
    rmw_implementation

    example_interfaces
    rcl
    rmw_implementation
    rosidl_default_runtime

    
        ament_cmake
    

手工创建CMakeLists.txt文件,内容如下:

cmake_minimum_required(VERSION 3.5)
project(ros2_demo)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

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

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

ament_package()

编译工作区

$cd ~/ros2_ws/
$ament build
在这里插入图片描述
编译完成后增加build和install两个目录

编写talker和listener代码

在 ~/ros2_ws/src/ros2_demo/src
创建ros2_talker.cpp文件,内容如下:

#include 
#include 

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

int main(int argc, char * argv[])
{
    //ros::init(argc, argv, "talker");
    rclcpp::init(argc, argv);

    //ros::NodeHandle n;
    auto node = rclcpp::Node::make_shared("talker");

    // Set the QoS. ROS 2 will provide QoS profiles based on the following use cases:
    // Default QoS settings for publishers and subscriptions (rmw_qos_profile_default).
    // Services (rmw_qos_profile_services_default).
    // Sensor data (rmw_qos_profile_sensor_data).
    rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default;
    // set the depth to the QoS profile
    custom_qos_profile.depth = 7;

    //ros::Publisher chatter_pub = n.advertise("chatter", 1000);
    auto chatter_pub = node->create_publisher("chatter", custom_qos_profile);

    //ros::Rate loop_rate(10);
    rclcpp::WallRate loop_rate(2);

    auto msg = std::make_shared();
    auto i = 1;

    //while (ros::ok())
    while (rclcpp::ok()) 
    {
        msg->data = "Hello World: " + std::to_string(i++);
        std::cout << "Publishing: '" << msg->data << "'" << std::endl;

        //chatter_pub.publish(msg);
        chatter_pub->publish(msg);

        //ros::spinOnce();
        rclcpp::spin_some(node);

        //loop_rate.sleep();
        loop_rate.sleep();
    }

    return 0;
}

创建ros2_listerner.cpp文件,内容如下:

#include 
#include 

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

//void chatterCallback(const std_msgs::String::ConstPtr& msg)
void chatterCallback(const std_msgs::msg::String::SharedPtr msg)
{
    std::cout << "I heard: [" << msg->data << "]" << std::endl;
}

int main(int argc, char * argv[])
{
    //ros::init(argc, argv, "listener");
    rclcpp::init(argc, argv);

    //ros::NodeHandle n;
    auto node = rclcpp::Node::make_shared("listener");

    //ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
    auto sub = node->create_subscription(
    "chatter", chatterCallback, rmw_qos_profile_default);

    //ros::spin();
    rclcpp::spin(node);

    return 0;
}

修改CMakeLists.txt文件

进入~ros2_ws/src/ros2_demo/ 目录修改CMakeLists.txt文件,增加上面两个cpp的编译内容。
文件内容如下:

cmake_minimum_required(VERSION 3.5)
project(ros2_demo)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

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

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)



add_executable(ros2_talker src/ros2_talker.cpp)
ament_target_dependencies(ros2_talker rclcpp std_msgs)

add_executable(ros2_listerner src/ros2_listerner.cpp)
ament_target_dependencies(ros2_listerner rclcpp std_msgs)

install(TARGETS
  ros2_talker
  ros2_listerner
  DESTINATION lib/${PROJECT_NAME}
)


ament_package()

编译程序

在~/ros2_ws/ 目录下执行命令,进行程序编译
ament build

程序执行

执行
source ~/ros2_ws/install/setup.bash
ros2 run ros2_demo ros2_talker
ros2 run ros2_demo ros2_listerner
ROS2 第一个C++程序(talker和listener为例)_第1张图片

查看ros2 运行的节点列表

运行:
ros2 node list
结果:
talker
listener

你可能感兴趣的:(ROS2 第一个C++程序(talker和listener为例))