# ROS 2 ## Prerequisites :::info docker 並非必要,但為了部屬方便以及讓大家盡快熟悉之後常用的開發工具,因此使用 docker 進行 ROS2 教學 ::: - ubuntu 22.04 (recommended) - [docker](https://docs.docker.com/get-docker/) ## Installation Please refer to: https://github.com/NCTU-AUV/orca_docker ## ROS (Robot Operating System) ### Node 在 ROS 中,Node 是最基本的運行單元(一個 process 就是一個 Node)  ### Topic Topic 是 Node 間進行通訊的方式,內容可以為字串、數字、或各種自訂的資料結構。  ### Service Service 跟 Topic 最大的差別在於 client、server 的溝通模式,且只會有一個 Service server。  ## Turtlesim - 開啟 turtlesim node: ``` ros2 run turtlesim turtlesim_node ```  - 用鍵盤控制烏龜: ``` ros2 run turtlesim turtle_teleop_key ``` - 用 rqt_graph 看 nodes 和 topics: ``` rqt_graph ```  - 其他常用指令: - node: ``` ros2 node list ros2 node info <node name> ``` - topic: ``` ros2 topic list ros2 topic info <topic name> ros2 topic echo <topic name> ros2 topic pub <topic name> <message type> <message> ``` ## Publisher and Subscriber in Python ### Create a workspace ``` mkdir -p /demo_ws/src ``` 所有的 package 都要放在一個 workspace 底下: ``` demo_ws/ └── src ├── pkg_1 ├── pkg_2 ├── ... ├── ... └── pkg_n ``` ### Create a ROS2 package ```shell cd /demo_ws/src ros2 pkg create --build-type ament_python py_pubsub ``` File structure of a ROS2 package:  ### Publisher Node `py_pubsub/publisher.py`: ```python= import rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalPublisher(Node): def __init__(self): super().__init__('minimal_publisher') self.publisher_ = self.create_publisher(String, 'topic', 10) timer_period = 0.5 # seconds self.timer = self.create_timer(timer_period, self.timer_callback) self.i = 0 def timer_callback(self): msg = String() msg.data = 'Hello World: %d' % self.i self.publisher_.publish(msg) self.get_logger().info('Publishing: "%s"' % msg.data) self.i += 1 def main(args=None): rclpy.init(args=args) minimal_publisher = MinimalPublisher() rclpy.spin(minimal_publisher) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) minimal_publisher.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` ### Subscriber Node `py_pubsub/subscriber.py`: ```python= import rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalSubscriber(Node): def __init__(self): super().__init__('minimal_subscriber') self.subscription = self.create_subscription( String, 'topic', self.listener_callback, 10) self.subscription # prevent unused variable warning def listener_callback(self, msg): self.get_logger().info('I heard: "%s"' % msg.data) def main(args=None): rclpy.init(args=args) minimal_subscriber = MinimalSubscriber() rclpy.spin(minimal_subscriber) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) minimal_subscriber.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` ### Build - In `package.xml`: ```xml <exec_depend>rclpy</exec_depend> <exec_depend>std_msgs</exec_depend> ``` - In `setup.py`: ```python entry_points={ 'console_scripts': [ 'talker = py_pubsub.publisher:main', 'listener = py_pubsub.subscriber:main', ], }, ``` 以 'talker = py_pubsub.publisher:main' 為例: - talker: executable name - py_pubsub: package name - publisher: python file name (`publisher.py`) - main: entry function (`main()` in `publisher.py`) - Command line: ``` cd /demo_ws rosdep install -i --from-path src --rosdistro humble -y ``` ``` colcon build --symlink-install --packages-select py_pubsub ``` ``` source install/setup.bash ``` ### Run ``` ros2 run py_pubsub talker ``` On the other terminal: ``` ros2 run py_pubsub listener ```  ``` rqt_graph ```  ## Referecnes - https://docs.ros.org/en/humble/index.html - https://docs.ros.org/en/humble/Tutorials/Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim.html - https://www.youtube.com/playlist?list=PLLSegLrePWgJudpPUof4-nVFHGkB62Izy
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up