赞
踩
在ROS2安装完成后,查看ament是否安装成功,如果未安装成功使用
sudo apt install ament*
安装ament软件包
$ mkdir -p ~/ros2_ws/src/ros2_demo/src
进入 ~/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>
手工创建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两个目录
在 ~/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; }
创建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; }
进入~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 node list
结果:
talker
listener
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。