From 43b5d2fd97eaca2e6f1e41a600387a6e0dbf4b47 Mon Sep 17 00:00:00 2001 From: NoData Date: Sat, 3 Feb 2024 18:09:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- launch/step_5.launch.py | 25 ++++++++++++++++++ lesson_05/step_5.py | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 launch/step_5.launch.py create mode 100644 lesson_05/step_5.py diff --git a/launch/step_5.launch.py b/launch/step_5.launch.py new file mode 100644 index 0000000..0b3c8b8 --- /dev/null +++ b/launch/step_5.launch.py @@ -0,0 +1,25 @@ +from launch import LaunchDescription +from launch_ros.actions import Node + + +def generate_launch_description(): + return LaunchDescription([ + Node( + package='turtlesim', + namespace='', + executable='turtlesim_node', + name='sim' + ), + Node( + package='lesson_05', + namespace='', + executable='step_5', + name='square' + ), + Node( + package='rqt_plot', + namespace='', + executable='rqt_plot', + name='plot' + ) + ]) diff --git a/lesson_05/step_5.py b/lesson_05/step_5.py new file mode 100644 index 0000000..4f56c0c --- /dev/null +++ b/lesson_05/step_5.py @@ -0,0 +1,57 @@ +import rclpy +from rclpy.node import Node + +from geometry_msgs.msg import Twist +from turtlesim.msg import Pose +import math + + +def angle_diff(a1, a2): + a = a2 - a1 + return (a + math.pi) % (2 * math.pi) - math.pi + + +class RurMover(Node): + def __init__(self): + super().__init__('minimal_publisher') + self.publisher_twist = self.create_publisher(Twist, 'turtle1/cmd_vel', 10) + self.pose_subscriber = self.create_subscription(Pose, 'turtle1/pose', self.pose_callback, 10) + self.desired_heading = math.pi / 2 + self.SIDE_TIME = 5.0 + self.LINEAR_SPEED = 0.5 + self.timer = self.create_timer(self.SIDE_TIME, self.pub_timer_callback) + + def pose_callback(self, msg): + self.get_logger().info('Current heading: "%f"' % msg.theta) + self.get_logger().info('Setpoint: "%f"' % self.desired_heading) + msg_pub = Twist() + msg_pub.linear.x = self.LINEAR_SPEED + error = angle_diff(self.desired_heading, msg.theta) + if error < 0: + msg_pub.angular.z = math.radians(90.0) + self.get_logger().info('Turn left') + else: + msg_pub.angular.z = math.radians(-90.0) + self.get_logger().info('Turn right') + self.publisher_twist.publish(msg_pub) + + def pub_timer_callback(self): + self.desired_heading += math.pi / 2 + if self.desired_heading >= math.pi: + self.desired_heading -= 2 * math.pi + self.get_logger().info('New angle setpoint: "%f"' % self.desired_heading) + + +def main(args=None): + rclpy.init(args=args) + + mover = RurMover() + + rclpy.spin(mover) + + mover.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main() \ No newline at end of file -- GitLab