ROS2 python 通信demo

已创建包:test_kiki

test_kiki/test_kiki文件夹

两个py文件即两个node

talk.py

https://raw.githubusercontent.com/ros2/examples/humble/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py

listen.py

https://raw.githubusercontent.com/ros2/examples/humble/rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/subscriber_member_function.py

package.xml文件中添加依赖

找到ament_python

下面添加

rclpy

std_msgs

即import 的包

setup.py中添加入口

entry_points={
        'console_scripts': [
            'talk_node = test_kiki.talk:main',
            'listen_node = test_kiki.listen:main'
        ],
    },

编译

colcon build

启动

ros2 run test_kiki talk_node

ros2 run test_kiki listen_node

也可以手动发布话题消息

ros2 topic pub /topic std_msgs/msg/String ' hello how are you '

你可能感兴趣的:(ros)