From 2356dba95e196ca40974b7f20813eb094786108e Mon Sep 17 00:00:00 2001 From: vkolesnikov Date: Sat, 26 Oct 2024 22:43:07 +0300 Subject: [PATCH 1/3] Added turtle p_controller script --- lesson_05/turtle_P.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lesson_05/turtle_P.py diff --git a/lesson_05/turtle_P.py b/lesson_05/turtle_P.py new file mode 100644 index 0000000..5f87df4 --- /dev/null +++ b/lesson_05/turtle_P.py @@ -0,0 +1,58 @@ +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 = a1-a2 + 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 = 0 + self.SIDE_TIME = 5.0 + self.LINEAR_SPEED = 0.5 + self.KP = 10 + 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) + + error = angle_diff(self.desired_heading, msg.theta) + msg_pub = Twist() + msg_pub.linear.x = self.LINEAR_SPEED + msg_pub.angular.z = self.controller_p(error) + self.publisher_twist.publish(msg_pub) + + + def controller_p(self, error): + + return self.KP * error + + def pub_timer_callback(self): + self.desired_heading += math.pi / 2 + 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 From 2d590ad35bdcdc7e8d473fd07c9b435bc02a51b3 Mon Sep 17 00:00:00 2001 From: vkolesnikov Date: Sat, 26 Oct 2024 22:47:41 +0300 Subject: [PATCH 2/3] Added launch-file --- launch/turtle_P.launch.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 launch/turtle_P.launch.py diff --git a/launch/turtle_P.launch.py b/launch/turtle_P.launch.py new file mode 100644 index 0000000..56115cb --- /dev/null +++ b/launch/turtle_P.launch.py @@ -0,0 +1,24 @@ +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='turtle_P', + name='square' + ), + Node( + package='rqt_plot', + namespace='', + executable='rqt_plot', + name='plot' + ) + ]) -- GitLab From 9e3827e4b0ba9c1cc0781a98257822d086aed2be Mon Sep 17 00:00:00 2001 From: vkolesnikov Date: Sat, 26 Oct 2024 22:57:35 +0300 Subject: [PATCH 3/3] Added support for the p_controller file --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 0e83277..38f72d3 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ setup( 'step_2 = lesson_05.step_2:main', 'step_3 = lesson_05.step_3:main', 'step_4 = lesson_05.step_4:main', + 'turtle_P = lesson_05.turtle_P:main', ], }, ) -- GitLab