栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ROS2与C++入门教程-增加头文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ROS2与C++入门教程-增加头文件

系列文章目录
  • ROS2与C++入门教程-目录

  • ROS2与C++入门教程-新建ros2工作空间

  • ROS2与C++入门教程-新建ros2包

  • ROS2与C++入门教程-编写订阅和发布

  • ROS2与C++入门教程-编写服务端和客户端

  • ROS2与C++入门教程-创建消息(msg)文件

  • ROS2与C++入门教程-创建服务(srv)文件

  • ROS2与C++入门教程-创建ros2接口

  • ROS2与C++入门教程-使用参数

  • ROS2与C++入门教程-创建和使用插件

  • ROS2与C++入门教程-编写动作服务器

  • ROS2与C++入门教程-编写动作客户端

  • ROS2与C++入门教程-Topic Statistics查看话题

  • ROS2与C++入门教程-增加头文件

  • ROS2与C++入门教程-在C++包里增加python支持

  • ROS2与C++入门教程-增加命名空间

  • ROS2与C++入门教程-多线程演示

  • ROS2与C++入门教程-单线程演示

  • ROS2与C++入门教程-话题组件演示

  • ROS2与C++入门教程-话题组件增加launch启动演示

  • ROS2与C++入门教程-服务组件演示

  • ROS2与C++入门教程-服务组件增加launch启动演示

  • ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示

  • ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示2

  • ROS2与C++入门教程-进程内(intra_process)图像处理演示

  • ROS2与C++入门教程-生命周期节点演示

文章目录
  • 系列文章目录
  • 前言
  • 总结


前言
  • 介绍如何增加头文件来声明类或函数
  • 目前在ros2里一般声明和定义写在一个源文件里。 对于小项目这样处理也很方便,比如一个订阅或发布的小程序。
  • 但是对于大项目,都放在一个源文件,管理起来就不太方便。一般都按照类型分到不同的源文件。那就是要把声明和定义分开。

步骤:

  • 新建一个包cpp_header
cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake cpp_header
  • 进入include,新建头文件minimal_publisher.hpp
cd ~/dev_ws/src/cpp_header/include/cpp_header
touch minimal_publisher.hpp
  • 内容如下:
#include 
#include 
#include 
#include 

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

using namespace std::chrono_literals;

class MinimalPublisher : public rclcpp::Node
{
public:
  MinimalPublisher();

  private:
  void timer_callback();
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher::SharedPtr publisher_;
  size_t count_;
};
  • 进入src目录,新建文件minimal_publisher.cpp
cd ~/dev_ws/src/cpp_header/src
touch minimal_publisher.cpp
  • 内容如下:
#include "cpp_header/minimal_publisher.hpp"


MinimalPublisher::MinimalPublisher ()
: Node("minimal_publisher"), count_(0)
{  
    publisher_ = this->create_publisher("topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));
}

void MinimalPublisher::timer_callback()
{
    auto message = std_msgs::msg::String();
    message.data = "Hello, world! " + std::to_string(count_++);
    RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
    publisher_->publish(message);
 }
  • 进入src目录,新建文件minimal_publisher_node.cpp
cd ~/dev_ws/src/cpp_header/src
touch minimal_publisher_node.cpp
  • 内容如下:
#include "cpp_header/minimal_publisher.hpp"

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared());
  rclcpp::shutdown();
  return 0;
}
  • 编辑package.xml
  • 在ament_cmake后增加
rclcpp
std_msgs
  • 编辑 CMakelist.txt
  • 在find_package(ament_cmake REQUIRED)后增加
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
  • 再增加可执行文件,ros2 run能够调用的名称
include_directories(include)
add_executable(talker src/minimal_publisher_node.cpp src/minimal_publisher.cpp) 
ament_target_dependencies(talker rclcpp std_msgs)
  • 增加可执行文件位置,ros2 run可以找到这个可执行文件
install(TARGETS
  talker
  DESTINATION lib/${PROJECT_NAME})
  • 安装相关依赖
cd ~/dev_ws/
rosdep install -i --from-path src --rosdistro galactic -y
  • 编译包
colcon build --symlink-install --packages-select cpp_header
  • 加载工作空间
. install/setup.bash
  • 执行
ros2 run cpp_header talker 
  • 效果如下:
$ ros2 run cpp_header talker 
[INFO] [1651805635.082660387] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [1651805635.582651708] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [1651805636.082660773] [minimal_publisher]: Publishing: 'Hello, world! 2'
[INFO] [1651805636.582671075] [minimal_publisher]: Publishing: 'Hello, world! 3'
[INFO] [1651805637.082674760] [minimal_publisher]: Publishing: 'Hello, world! 4'

总结

以上就是今天要讲的内容,本文仅仅简单介绍如何增加头文件来声明类或函数

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号