ROS2与C++入门教程-增加命名空间
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'
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号