0
点赞
收藏
分享

微信扫一扫

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++入门教程-生命周期节点演示​​

说明 :

  • 介绍如何增加命名空间

步骤:

  • 新建一个包cpp_namespace

cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake cpp_namespace

  • 进入include,新建头文件minimal_publisher_namespace.hpp

cd ~/dev_ws/src/cpp_namespace/include/cpp_namespace
touch minimal_publisher_namespace.hpp

  • 内容如下:

#ifndef PUBLISHER_NAMESPACE_H_
#define PUBLISHER_NAMESPACE_H_

#include <chrono>
#include <functional>
#include <memory>
#include <string>

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

using namespace std::chrono_literals;

namespace minimal
{

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

private:
void timer_callback();
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};

} //namespace minimal
#endif /* PUBLISHER_NAMESPACE_H_ */

  • 进入src目录,新建文件minimal_publisher_namespace.cpp

cd ~/dev_ws/src/cpp_namespace/src
touch minimal_publisher_namespace.cpp

  • 内容如下:

#include "cpp_namespace/minimal_publisher_namespace.hpp"

namespace minimal
{

MinimalPublisher::MinimalPublisher ()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("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! NameSpace" + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "NameSpace Publishing : '%s'", message.data.c_str());
publisher_->publish(message);
}

} //namespace minimal

  • 进入src目录,新建文件minimal_publisher_namespace_node.cpp

cd ~/dev_ws/src/cpp_namespace/src
touch minimal_publisher_namespace_node.cpp

  • 内容如下:

#include "cpp_namespace/minimal_publisher_namespace.hpp"

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<minimal::MinimalPublisher>());
rclcpp::shutdown();
return 0;
}

  • 编辑package.xml
  • 在<buildtool_depend>ament_cmake</buildtool_depend>后增加

<depend>rclcpp</depend>
<depend>std_msgs</depend>

  • 编辑 CMakelist.txt
  • 在find_package(ament_cmake REQUIRED)后增加

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

  • 再增加可执行文件,ros2 run能够调用的名称

include_directories(include)
add_executable(talker_namespace src/minimal_publisher_namespace_node.cpp src/minimal_publisher_namespace.cpp)
ament_target_dependencies(talker_namespace rclcpp std_msgs)

  • 增加可执行文件位置,ros2 run可以找到这个可执行文件

install(TARGETS
talker_namespace
DESTINATION lib/${PROJECT_NAME})

  • 安装相关依赖

cd ~/dev_ws/
rosdep install -i --from-path src --rosdistro galactic -y

  • 编译包

colcon build --symlink-install --packages-select cpp_namespace

  • 加载工作空间

. install/setup.bash

  • 执行

ros2 run cpp_namespace talker_namespace

  • 效果如下:

$ ros2 run cpp_namespace talker_namespace 
[INFO] [1651806323.835469215] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace0'
[INFO] [1651806324.335468986] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace1'
[INFO] [1651806324.835470570] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace2'
[INFO] [1651806325.335460061] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace3'
[INFO] [1651806325.835478477] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace4'
[INFO] [1651806326.335481898] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace5'
[INFO] [1651806326.835490994] [minimal_publisher]: NameSpace Publishing : 'Hello, world! NameSpace6'

举报

相关推荐

0 条评论