{"id":68,"date":"2026-03-29T16:42:23","date_gmt":"2026-03-29T08:42:23","guid":{"rendered":"https:\/\/374969782.xyz\/?p=68"},"modified":"2026-04-05T18:08:20","modified_gmt":"2026-04-05T10:08:20","slug":"ros2%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95%ef%bc%883%ef%bc%89-%e8%af%9d%e9%a2%98","status":"publish","type":"post","link":"https:\/\/374969782.xyz\/index.php\/2026\/03\/29\/ros2%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95%ef%bc%883%ef%bc%89-%e8%af%9d%e9%a2%98\/","title":{"rendered":"ROS2\u5b66\u4e60\u8bb0\u5f55\uff083\uff09-\u8bdd\u9898"},"content":{"rendered":"\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u8bdd\u9898\u662f\u8282\u70b9\u95f4\u4f20\u9012\u6570\u636e\u7684\u6865\u6881\uff0c\u7528\u4e8e\u8fde\u63a5\u8282\u70b9\uff0c\u670d\u52a1\u4e8e\u8282\u70b9\u95f4\u7684\u4fe1\u53f7\u4f20\u9012\u3002<\/summary>\n<ul class=\"wp-block-list\">\n<li>\u53d1\u5e03\u6570\u636e\u7684\u8282\u70b9\u4e3a\u8bdd\u9898\u7684\u53d1\u5e03\u8005\uff0c\u63a5\u53d7\u6570\u636e\u7684\u8282\u70b9\u4e3a\u8bdd\u9898\u7684\u8ba2\u9605\u8005\uff0c\u662f\u591a\u5bf9\u591a\u7684\u901a\u8baf\u6a21\u578b\uff0c\u5728\u591a\u4e2a\u53d1\u5e03\u8005\u65f6\u9700\u6ce8\u610f\u8ba2\u9605\u8005\u7684\u6267\u884c\u4f18\u5148\u7ea7\u3002<\/li>\n\n\n\n<li>\u8bdd\u9898\u662f\u4e00\u4e2a\u5f02\u6b65\u901a\u4fe1\u673a\u5236\uff0c\u53d1\u5e03\u8005\u5904\u5e76\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u88ab\u8ba2\u9605\u8005\u63a5\u6536\uff0c\u4e5f\u4e0d\u9700\u8981\u7b49\u5230\u56de\u5e94\uff0c\u53ea\u7ba1\u53d1\u5e03\uff08\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u53c2\u6570\u4fee\u6539\uff09\u3002<\/li>\n\n\n\n<li>\u53d1\u5e03\u4e0e\u63a5\u53d7\u7684\u6570\u636e\u5fc5\u987b\u7b26\u5408\u7edf\u4e00\u7684\u6570\u636e\u63cf\u8ff0\u683c\u5f0f\uff0c.msg\u6587\u4ef6\u5c06\u5b9a\u4e49\u8bdd\u9898\u901a\u4fe1\u7684\u6d88\u606f\u7ed3\u6784<\/li>\n<\/ul>\n<\/details>\n\n\n\n<p>\u8bdd\u9898\u7f16\u5199\u5728\u8282\u70b9\u6e90\u7801\u5185\u90e8\uff0c\u53ea\u8981\u8282\u70b9\u8fd0\u884c\uff0c\u5c31\u4f1a\u81ea\u52a8\u6309\u4ee3\u7801\u5185\u7684\u89c4\u5219\u6f14\u7ece\u8bdd\u9898\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u53d1\u5e03\u8005\u8282\u70b9\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import rclpy                                     # ROS2 Python\u63a5\u53e3\u5e93\nfrom rclpy.node import Node                      # ROS2 \u8282\u70b9\u7c7b\nfrom std_msgs.msg import String                  # \u5b57\u7b26\u4e32\u6d88\u606f\u7c7b\u578b\n\nclass PublisherNode(Node):\n\n    def __init__(self, name):\n        super().__init__(name)                                    # ROS2\u8282\u70b9\u7236\u7c7b\u521d\u59cb\u5316\n        self.pub = self.create_publisher(String, \"chatter\", 10)   # \u521b\u5efa\u53d1\u5e03\u8005\u5bf9\u8c61\uff08\u6d88\u606f\u7c7b\u578b\u3001\u8bdd\u9898\u540d\u3001\u961f\u5217\u957f\u5ea6\uff09\uff0c\u961f\u5217\u957f\u5ea6\u5373\u7f13\u5b58\u5927\u5c0f\uff0c\u5f85\u53d1\u9001\u6570\u636e\u5927\u4e8e\u961f\u5217\u957f\u5ea6\u65f6\u4f1a\u5220\u53bb\u8f83\u65e9\u7684\u672a\u53d1\u9001\u6d88\u606f\n        self.timer = self.create_timer(0.5, self.timer_callback)  # \u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\uff08\u5355\u4f4d\u4e3a\u79d2\u7684\u5468\u671f\uff0c\u5b9a\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\uff09\n\n    def timer_callback(self):                                     # \u521b\u5efa\u5b9a\u65f6\u5668\u5468\u671f\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\n        msg = String()                                            # \u521b\u5efa\u4e00\u4e2aString\u7c7b\u578b\u7684\u6d88\u606f\u5bf9\u8c61\n        msg.data = 'Hello World'                                  # \u586b\u5145\u6d88\u606f\u5bf9\u8c61\u4e2d\u7684\u6d88\u606f\u6570\u636e\n        self.pub.publish(msg)                                     # \u53d1\u5e03\u8bdd\u9898\u6d88\u606f\n        self.get_logger().info('Publishing: \"%s\"' % msg.data)     # \u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\uff0c\u63d0\u793a\u5df2\u7ecf\u5b8c\u6210\u8bdd\u9898\u53d1\u5e03\n\ndef main(args=None):                                 # ROS2\u8282\u70b9\u4e3b\u5165\u53e3main\u51fd\u6570\n    rclpy.init(args=args)                            # ROS2 Python\u63a5\u53e3\u521d\u59cb\u5316\n    node = PublisherNode(\"topic_helloworld_pub\")     # \u521b\u5efaROS2\u8282\u70b9\u5bf9\u8c61\u5e76\u8fdb\u884c\u521d\u59cb\u5316\n    rclpy.spin(node)                                 # \u5faa\u73af\u7b49\u5f85ROS2\u9000\u51fa\n    node.destroy_node()                              # \u9500\u6bc1\u8282\u70b9\u5bf9\u8c61\n    rclpy.shutdown()                                 # \u5173\u95edROS2 Python\u63a5\u53e3<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2drclpy.spin\u8868\u793a\u8282\u70b9\u5faa\u73af\u7b49\u5f85\uff0c\u6301\u7eed\u8fd0\u884c\uff0c\u662f\u963b\u585e\u7684\uff0c\u4f1a\u8fdb\u5165ros2\u7684\u4e3b\u5faa\u73af\u6301\u7eed\u76d1\u542c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u60c5\u51b5\u4e0b\u8c03\u7528\u56de\u8c03\u51fd\u6570\uff0c\u76f4\u5230\u89e6\u53d1\u9000\u51fa\u6761\u4ef6\uff08\u5373\u6309Ctrl+C\u6216\u8c03\u7528rclpy.shutdown()\uff09\uff0c\u671f\u95f4\u8fdb\u7a0b\u5c06\u505c\u5728\u8fd9\u4e00\u884c\uff0c\u65e0\u6cd5\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u4e0e\u4e4b\u76f8\u5bf9\u7684\u662frclpy.spin_once\uff0c\u4ec5\u6267\u884c\u4e00\u6b21\u8282\u70b9\u4efb\u52a1\u540e\u5c31\u81ea\u52a8\u9000\u51fa\uff0c\u5728\u6267\u884c\u5b8c\u4e00\u6b21\u524d\u4e5f\u4f1a\u6301\u7eed\u8fd0\u884c\u5bfc\u81f4\u963b\u585e\uff0c\u4f46\u4e00\u6b21\u5f80\u5f80\u5f88\u5feb\u5c31\u4f1a\u7ed3\u675f\uff0c\u63d0\u4f9b\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3atimeout_sec\uff0c\u8868\u793a\u7b49\u5f85\uff08\u963b\u585e\uff09\u65f6\u957f\uff0c\u5728timeout_sec\u65f6\u95f4\u5185\u6ca1\u6709\u63a5\u6536\u5230ros\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u5219\u8df3\u8fc7\uff0c\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u5176\u4ed6\u4ee3\u7801\u3002\u5e38\u7528\u4e8e\u5faa\u73af\u5185\u6267\u884c\u591a\u79cd\u64cd\u4f5c\uff0c\u95f4\u6b47\u6027\u68c0\u67e5ros2\u8282\u70b9\u7684\u5de5\u4f5c\u961f\u5217\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u8ba2\u9605\u8005\u8282\u70b9\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import rclpy                      # ROS2 Python\u63a5\u53e3\u5e93\nfrom rclpy.node   import Node     # ROS2 \u8282\u70b9\u7c7b\nfrom std_msgs.msg import String   # ROS2\u6807\u51c6\u5b9a\u4e49\u7684String\u6d88\u606f\n\nclass SubscriberNode(Node):\n\n    def __init__(self, name):\n        super().__init__(name)                             # ROS2\u8282\u70b9\u7236\u7c7b\u521d\u59cb\u5316\n        self.sub = self.create_subscription(\\\n            String, \"chatter\", self.listener_callback, 10) # \u521b\u5efa\u8ba2\u9605\u8005\u5bf9\u8c61\uff08\u6d88\u606f\u7c7b\u578b\u3001\u8bdd\u9898\u540d\u3001\u8ba2\u9605\u8005\u56de\u8c03\u51fd\u6570\u3001\u961f\u5217\u957f\u5ea6\uff09\uff0c\u540c\u7406\uff0c\u672a\u5904\u7406\u6570\u636e\u5927\u4e8e10\u65f6\u4f1a\u5148\u5220\u6389\u63a5\u6536\u8f83\u65e9\u7684\u6570\u636e\n\n    def listener_callback(self, msg):                      # \u521b\u5efa\u56de\u8c03\u51fd\u6570\uff0c\u6267\u884c\u6536\u5230\u8bdd\u9898\u6d88\u606f\u540e\u5bf9\u6570\u636e\u7684\u5904\u7406\n        self.get_logger().info('I heard: \"%s\"' % msg.data) # \u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\uff0c\u63d0\u793a\u8ba2\u9605\u6536\u5230\u7684\u8bdd\u9898\u6d88\u606f\n\ndef main(args=None):                               # ROS2\u8282\u70b9\u4e3b\u5165\u53e3main\u51fd\u6570\n    rclpy.init(args=args)                          # ROS2 Python\u63a5\u53e3\u521d\u59cb\u5316\n    node = SubscriberNode(\"topic_helloworld_sub\")  # \u521b\u5efaROS2\u8282\u70b9\u5bf9\u8c61\u5e76\u8fdb\u884c\u521d\u59cb\u5316\n    rclpy.spin(node)                               # \u5faa\u73af\u7b49\u5f85ROS2\u9000\u51fa\n    node.destroy_node()                            # \u9500\u6bc1\u8282\u70b9\u5bf9\u8c61\n    rclpy.shutdown()                               # \u5173\u95edROS2 Python\u63a5\u53e3<\/code><\/pre>\n\n\n\n<p>\u540c\u6837\uff0c\u4f9d\u65e7\u9700\u8981\u5728setup.py\u5185\u65b0\u589e\u7a0b\u5e8f\u5165\u53e3\u624d\u80fd\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u53e6\uff1a\u4f20\u9012\u56fe\u50cf\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165sensor_msgs.msg\u5e93\u7684Image\u7c7b\u6765\u5b9e\u73b0\uff0c\u8fd9\u662fros2\u7684\u4e00\u4e2a\u5de5\u5177\u5305\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cOpenCV\u7684\u56fe\u50cf\u683c\u5f0f\u548cros2\u4e2dsensor_msgs.msg\u5e93\u7684Image\u7684\u56fe\u50cf\u683c\u5f0f\u4e0d\u540c\uff0c\u9700\u8981\u989d\u5916\u5f15\u5165ros2\u7684\u5e93cv-bridge\u6765\u5904\u7406\u3002\u8be5\u5e93\u5e76\u4e0d\u9ed8\u8ba4\u4e0b\u8f7d\uff0c\u6240\u4ee5\u5982\u8981\u5b89\u88c5\u8be5\u5e93\u53ef\u5728\u547d\u4ee4\u884c\u6267\u884c\uff08\u4ec5\u9002\u7528\u4e8ehumble\u7248\u672c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install ros-humble-cv-bridge<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u65f6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from cv_bridge import CvBridge\nself.cv_bridge = CvBridge()    \ncv_bridge.cv2_to_imgmsg(frame, 'bgr8'))\n.cv_bridge.imgmsg_to_cv2(data, 'bgr8')<\/code><\/pre>\n\n\n\n<p>\u5373\u53ef\u5b9e\u73b0OpenCV\u4e0eros2\u56fe\u50cf\u683c\u5f0f\u7684\u76f8\u4e92\u8f6c\u5316\u4e86\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>ros2\u8fd8\u63d0\u4f9b\u4e13\u95e8\u7684\u5de5\u5177\u5305\u7528\u4ee5\u9002\u914d\u5e76\u4f7f\u7528\u4e0d\u540c\u8bbe\u5907\u7684\u6444\u50cf\u5934\uff0c\u5373usb-cam\u3002\u901a\u8fc7apt\u53ef\u4ee5\u4e0b\u8f7d\u5b83\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install ros-humble-usb-cam<\/code><\/pre>\n\n\n\n<p>\u8be5\u5de5\u5177\u5305\u63a7\u5236\u6444\u50cf\u5934\u5f55\u5236\u7684\u5185\u5bb9\u5c06\u53d1\u5e03\u5230\u540d\u4e3a\u201cimage_raw\u201d\u7684\u8bdd\u9898\u4e0a\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7535\u8111\u51fa\u4e8e\u5b89\u5168\u8003\u8651\uff0c\u865a\u62df\u673a\u7b49\u7a0b\u5e8f\u662f\u65e0\u6cd5\u76f4\u63a5\u5524\u8d77\u6444\u50cf\u5934\u7684\u3002\u603b\u4e4b\u5c31\u633a\u9ebb\u70e6\u7684\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u5728\u547d\u4ee4\u884c\uff0cros2\u4e5f\u6709\u4e00\u4e9b\u7ba1\u7406\u8bdd\u9898\u7684\u65b9\u6cd5\u3002<\/summary>\n<p>\u5217\u51fa\u6240\u6709\u6d3b\u52a8\u8bdd\u9898:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic list<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u8bdd\u9898\u8be6\u7ec6\u4fe1\u606f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic info &lt;topic_name&gt;<\/code><\/pre>\n\n\n\n<p>\u76d1\u542c\u8bdd\u9898\u6570\u636e:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic echo &lt;topic_name&gt;<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u8bdd\u9898\u53d1\u5e03\u9891\u7387:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic hz &lt;topic_name&gt;<\/code><\/pre>\n\n\n\n<p>\u624b\u52a8\u53d1\u5e03\u8bdd\u9898\u6d88\u606f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic pub &lt;topic_name&gt; &lt;msg_type&gt; &lt;data&gt;<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u8bdd\u9898\u6d88\u606f\u7c7b\u578b:<\/p>\n\n\n\n<p>ros2 topic type &lt;topic_name&gt;<\/p>\n<\/details>\n\n\n\n<p>\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6d88\u606f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ros2 topic find &lt;msg_type&gt;<\/code><\/pre>\n\n\n\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u542f\u7528\u53ef\u89c6\u5316\u754c\u9762\u89c2\u5bdf\u8bdd\u9898\u5173\u7cfb\u3002\u5728\u547d\u4ee4\u884c\u5185\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rqt_graph<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bdd\u9898\u7f16\u5199\u5728\u8282\u70b9\u6e90\u7801\u5185\u90e8\uff0c\u53ea\u8981\u8282\u70b9\u8fd0\u884c\uff0c\u5c31\u4f1a\u81ea\u52a8\u6309\u4ee3\u7801\u5185\u7684\u89c4\u5219\u6f14\u7ece\u8bdd\u9898\u3002 \u4e00\u4e2a\u53d1\u5e03\u8005\u8282\u70b9\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a \u5176\u4e2drc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-ros2"],"_links":{"self":[{"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":15,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions\/112"}],"wp:attachment":[{"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/374969782.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}