学习ROS——Rosnode&Rostopic&message

本文通过比喻的方式介绍了消息发布订阅模式的基本概念。将这一模式比作在网上不同贴吧发布帖子的过程,任何人都可以订阅并查看这些帖子,如果有需要则可以下载相关的附件。通过这种形象的解释帮助读者理解发布订阅模式的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   

    整个过程就像我们(node)上网发帖子,发(publish)的帖子所有需要的人都能浏览(subscribe),但并非所有人都会浏览。

    如果觉得有用那别人就把附件(message)下载下来。

    我们可能不止在一个贴吧发帖子,那不同的贴吧就是(topic)。

 

rror: ‘amp’ was not declared in this scope 17 | "/camera/image", 10, std::bind(&ImageSubscriber::image_callback, this, _1)); | ^~~ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:17:60: error: invalid use of non-static member function ‘void ImageSubscriber::image_callback(sensor_msgs::msg::Image_<std::allocator<void> >::SharedPtr) const’ 17 | "/camera/image", 10, std::bind(&ImageSubscriber::image_callback, this, _1)); | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:22:8: note: declared here 22 | void image_callback(const sensor_msgs::msg::Image::SharedPtr msg) const | ^~~~~~~~~~~~~~ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp: In member function ‘void ImageSubscriber::image_callback(sensor_msgs::msg::Image_<std::allocator<void> >::SharedPtr) const’: /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:31:33: error: ‘process_image’ was not declared in this scope; did you mean ‘processed_image’? 31 | cv::Mat processed_image = process_image(image); | ^~~~~~~~~~~~~ | processed_image /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:37:36: error: expected ‘)’ before ‘;’ token 37 | catch (cv_bridge::Exception& e) | ^ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:37:11: note: to match this ‘(’ 37 | catch (cv_bridge::Exception& e) | ^ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:37:36: error: expected ‘{’ before ‘;’ token 37 | catch (cv_bridge::Exception& e) | ^ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:37:38: error: ‘e’ was not declared in this scope 37 | catch (cv_bridge::Exception& e) | ^ gmake[2]: *** [CMakeFiles/image_subscriber.dir/build.make:76:CMakeFiles/image_subscriber.dir/src/image_subscriber.cpp.o] 错误 1 gmake[1]: *** [CMakeFiles/Makefile2:137:CMakeFiles/image_subscriber.dir/all] 错误 2 gmake: *** [Makefile:146:all] 错误 2 --- Failed <<< image_subscriber_cpp [4.89s, exited with code 2] Summary: 0 packages finished [5.09s] 1 package failed: image_subscriber_cpp 1 package had stderr output: image_subscriber_cpp not found: "/home/lizhuo/ros2_ws/install/image_subscriber_cpp/share/image_subscriber_cpp/local_setup.bash"#include <rclcpp/rclcpp.hpp> #include <sensor_msgs/msg/image.hpp> #include <cv_bridge/cv_bridge.h> #include <opencv2/opencv.hpp> using std::placeholders::_1; class ImageSubscriber : public rclcpp::Node { public: ImageSubscriber() : Node("image_subscriber") { // 订阅话题,根据实际bag中的话题名修改 subscription_ = this->create_subscription<sensor_msgs::msg::Image>( "/camera/image", 10, std::bind(&ImageSubscriber::image_callback, this, _1)); RCLCPP_INFO(this->get_logger(), "图像订阅器已启动,等待消息..."); } private: void image_callback(const sensor_msgs::msg::Image::SharedPtr msg) const { try { // 将ROS图像消息转换为OpenCV图像 cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "bgr8"); cv::Mat image = cv_ptr->image; // 在此添加图像处理代码 cv::Mat processed_image = process_image(image); // 显示图像 cv::imshow("Bag Image", processed_image); cv::waitKey(1); // 等待1ms,允许OpenCV刷新窗口 } catch (cv_bridge::Exception& e) { RCLCPP_ERROR(this->get_logger(), "cv_bridge异常: %s", e.what()); } } rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); auto node = std::make_shared<ImageSubscriber>(); rclcpp::spin(node); rclcpp::shutdown(); cv::destroyAllWindows(); return 0; } x修改
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值