当前位置:   article > 正文

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

ros2例程

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文件,内容如下:

<?xml version="1.0"?>
<package format="2">
    <name>ros2_demo</name>
    <version>0.0.0</version>
    <description>Package containing examples of how to use the rcl API.</description>
    <maintainer email="huchunxu@hust.edu.cn">Hu Chunxu</maintainer>
    <license>Apache License 2.0</license>

    <buildtool_depend>ament_cmake</buildtool_depend>

    <buildtool_depend>rosidl_default_generators</buildtool_depend>

    <build_depend>example_interfaces</build_depend>
    <build_depend>rcl</build_depend>
    <build_depend>rmw_implementation</build_depend>

    <exec_depend>example_interfaces</exec_depend>
    <exec_depend>rcl</exec_depend>
    <exec_depend>rmw_implementation</exec_depend>
    <exec_depend>rosidl_default_runtime</exec_depend>

    <export>
        <build_type>ament_cmake</build_type>
    </export>
</package>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

手工创建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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

编译工作区

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

编写talker和listener代码

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

#include <iostream>
#include <memory>

#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<std_msgs::String>("chatter", 1000);
    auto chatter_pub = node->create_publisher<std_msgs::msg::String>("chatter", custom_qos_profile);

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

    auto msg = std::make_shared<std_msgs::msg::String>();
    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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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

#include <iostream>
#include <memory>

#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<std_msgs::msg::String>(
    "chatter", chatterCallback, rmw_qos_profile_default);

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

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

修改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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

编译程序

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

程序执行

执行
source ~/ros2_ws/install/setup.bash
ros2 run ros2_demo ros2_talker
ros2 run ros2_demo ros2_listerner
在这里插入图片描述

查看ros2 运行的节点列表

运行:
ros2 node list
结果:
talker
listener

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/982468
推荐阅读
相关标签
  

闽ICP备14008679号