action에서 사용할 multi thread를 먼저 다루어 보겠습니다.
first_package에서 multi_thread.py 파일을 생성합니다.
import rclpy as rp
from rclpy.executors import MultiThreadedExecutor
from rclpy.node import Node
# 만든 package안의 publisher와 subscriber
from first_package.my_publisher import MyPublihser
from first_package.my_subscriber import MySubscriber
def main(args=None):
rp.init()
sub = MySubscriber()
pub = MyPublihser()
executor = MultiThreadedExecutor()
executor.add_node(sub)
executor.add_node(pub)
try:
executor.spin()
finally:
executor.shutdown()
sub.destroy_node()
pub.destroy_node()
rp.shutdown()
if __name__ == '__main__':
main()
'multi_thread = first_package.multi_thread:main'
colcon build --packages-select first_package
ros2 run turtlesim turtlesim_node
ros2 run first_package multi_thread