ROS2入门之话题

准备工作

创建工作空间

工作空间就是文件夹,在workspace下创建src目录

mkdir src && cd src

创建节点

使用ament-cmake作为编译类型的,依赖rclcpp和std_msgs

ros2 pkg create hello_world --build-type ament_cmake --dependencies rclcpp std_msgs

编写话题发布者

代码编写

创建hello_world_publisher.cpp文件

#include <chrono>
#include <functional>
#include <memory>
#include <string>
 
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
 
using namespace std::chrono_literals;
 
class HelloWorldPublisher : public rclcpp::Node
{
public:
  HelloWorldPublisher()
  : Node("hello_world_publisher"), count_(0)
  {
    publisher_ = this->create_publisher<std_msgs::msg::String>("hello_world_topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&HelloWorldPublisher::timer_callback, this));
  }
 
private:
  void 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);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
  size_t count_;
};
 
int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<HelloWorldPublisher>());
  rclcpp::shutdown();
  return 0;
}

修改CMakeLists.txt文件

cmake_minimum_required(VERSION 3.8)
project(hello_world)

# 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 dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

add_executable(hello_world_publisher_node src/hello_world_publisher.cpp)
ament_target_dependencies(hello_world_publisher_node rclcpp std_msgs)

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

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

执行编译

回到src的上级目录

cd ..

执行编译

colcon build

运行

source install/setup.bash
ros2 run hello_world hello_world_publisher_node

调试

source /opt/ros/humble/setup.bash
ros2 topic list
ros2 topic echo /hello_world_topic

编写话题订阅者

代码编写

创建HelloWorldSubscriber.cpp文件

#include <memory>
 
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
 
class HelloWorldSubscriber : public rclcpp::Node
{
public:
  HelloWorldSubscriber()
  : Node("hello_world_subscriber")
  {
    subscription_ = this->create_subscription<std_msgs::msg::String>(
      "hello_world_topic",
      10,
      [this](std_msgs::msg::String::UniquePtr msg) {
        RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
      });
  }
 
private:
  rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
 
int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<HelloWorldSubscriber>());
  rclcpp::shutdown();
  return 0;
}
修改CMakeLists.txt文件
add_executable(hello_world_subscriber_node src/hello_world_subscriber.cpp)
ament_target_dependencies(hello_world_subscriber_node rclcpp std_msgs)
 
install(TARGETS
hello_world_publisher_node
hello_world_subscriber_node
  DESTINATION lib/${PROJECT_NAME}
)

执行编译

colcon build

调试

借助之前编写的hello_world_publisher_node作为发布者进行调试

source install/setup.bash
ros2 run hello_world hello_world_publisher_node
ros2 run hello_world hello_world_subscriber_node
posted @ 2022-07-04 20:32  卡尔的思索  阅读(81)  评论(0编辑  收藏  举报