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

更新时间:2023-06-17 01:18:21 阅读: 评论:0

ROS2第⼀个C++程序(talker和listener为例)ament 安装
在ROS2安装完成后,查看ament是否安装成功,如果未安装成功使⽤
sudo apt install ament*
安装ament软件包
创建软件开发⼯作区⽬录
$ mkdir -p ~/ros2_ws/src/ros2_demo/src
<和l⽂件
进⼊ ~/ros2_ws/src/⽬录
cd ~/ros2_ws/src/ros2_demo
⼿⼯创建l⽂件,内容如下:
<?xml version="1.0"?>
<package format="2">
<name>ros2_demo</name>
<version>0.0.0</version>
<description>Package containing examples of how to u the rcl API.</description>
<maintainer email="huchunxu@hust.edu">Hu Chunxu</maintainer>
<licen>Apache Licen 2.0</licen>
<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>
guy是什么意思⼿⼯创建⽂件,内容如下:
cmake_minimum_required(VERSION 3.5)
project(ros2_demo)
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
t(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/
2016高考成绩查询入口
$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 bad on the following u cas:
// Default QoS ttings for publishers and subscriptions (rmw_qos_profile_default).
// Services (rmw_qos_profile_rvices_default).
// Sensor data (rmw_qos_profile_nsor_data).
rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default;classify
// t the depth to the QoS profile
custom_qos_profile.depth = 7;
//ros::Publisher chatter_pub = n.adverti<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())
{eskimo kiss
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;bachelor什么意思
}
创建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;
tennis elbowauto 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/ ⽬录修改⽂件,增加上⾯两个cpp的编译内容。⽂件内容如下:
cmake_minimum_required(VERSION 3.5)
project(ros2_demo)
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
grandpa
t(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")  add_compile_options(-Wall -Wextra -Wpedantic)
endif()
marathonfind_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/tup.bash
ros2 run ros2_demo ros2_talker
confirming
ros2 run ros2_demo ros2_listerner
查看ros2 运⾏的节点列表
运⾏:
ros2 node list 结果:
talker
listener

本文发布于:2023-06-17 01:18:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/147616.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:编译   完成   查看   安装   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图