**Autonomous driving tech./*ROS2( using gazebo )

[ROS2] Turtlesim 원 그리기

2wnswoo 2024. 12. 14. 01:18

import rclpy
from geometry_msgs.msg import Twist
from rclpy.node import Node
import time


class TurtleMover(Node):
    def __init__(self):
        super().__init__('turtle_mover')
        self.publisher_ = self.create_publisher(Twist, '/turtle1/cmd_vel', 10)
        self.timer = self.create_timer(0.1, self.timer_callback)
        self.msg = Twist()

    def timer_callback(self):
        self.move_in_circle()

    def move_in_circle(self):
        # 원을 그리기 위한 속도 설정
        self.msg.linear.x = 2.0  # 직선 속도
        self.msg.angular.z = 12.0  # 각속도 (회전 속도)

        self.publisher_.publish(self.msg)

def main(args=None):
    rclpy.init(args=args)
    turtle_mover = TurtleMover()
    rclpy.spin(turtle_mover)
    turtle_mover.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()