diff --git a/config/control-config.yaml b/config/control-config.yaml index 80bbe66d87876352396a2d3238f2f8625038b722..c7ff5b8aaf3dd7d43a75e3e027bc2413a82815b4 100644 --- a/config/control-config.yaml +++ b/config/control-config.yaml @@ -4,8 +4,8 @@ angular: true clamp: 15.0 inverted: false - kd: 0.9 - ki: 0.0 - kp: 0.04 + kd: 1.0 + ki: 0.1 + kp: 0.05 start_type_description_service: true use_sim_time: false diff --git a/launch/step_1.launch.py b/launch/step_1.launch.py index 2908c5e31e5e82e8b15f94ba0f50ad109b5e311f..cc31152af50f21027a705163d307c74434b0cb3d 100644 --- a/launch/step_1.launch.py +++ b/launch/step_1.launch.py @@ -14,13 +14,15 @@ def generate_launch_description(): package='lesson_06', namespace='waterstrider', executable='step_1', - name='controller' + name='controller', + output = 'screen' ), Node( package='lesson_06', namespace='waterstrider', executable='pid_node', name='pid', + output = 'screen' parameters=[config_file] ) ]) diff --git a/lesson_06/step_1.py b/lesson_06/step_1.py new file mode 100644 index 0000000000000000000000000000000000000000..a3dedf6cfc8fe855358f55e980f3b57763b5015f --- /dev/null +++ b/lesson_06/step_1.py @@ -0,0 +1,84 @@ +import rclpy +from pid_node import PID +from rclpy.node import Node +from std_msgs.msg import Float64 +from geometry_msgs.msg import Twist +from geometry_msgs.msg import PoseStamped +from tf_transformations import euler_from_quaternion +import math + +def get_yaw_from_quaternion(q): + return euler_from_quaternion([q.x, q.y, q.z, q.w])[2] + +class RurMover(Node): + def __init__(self): + super().__init__('controller') + self.publisher_twist = self.create_publisher(Twist, '/waterstrider/twist_command', 10) + self.pose_subscriber = self.create_subscription(PoseStamped, '/waterstrider/ground_truth_to_tf_waterstrider/pose', self.pose_callback, 10) + self.PID_setpoint = self.create_publisher(Float64, 'pid/setpoint', 10) + self.PID_output = self.create_subscription(Float64, 'pid/ouyput', self.PID_output_callback, 10) + self.PID_state = self.create_publisher(Float64, 'pid/state', 10) + self.SIDE_TIME = 10.0 + self.LINEAR_SPEED = 1.0 + self.desired_heading = - math.pi / 2 + self.timer = self.create_timer(self.SIDE_TIME, self.pub_timer_callback) + self.pose_history = [] + + msgSetPoint = Float64() + msgSetPoint.date = self.desired_heading + self.PID_setpoint.publish(msgSetPoint) + + def PID_output_callback(self, msg): + self.PID_angular = msg.date + + def pose_callback(self, msg): + heading = get_yaw_from_quaternion(msg.pose.orientation) + self.get_logger().info('Current heading: "%f"' % heading) + self.get_logger().info('Setpoint: "%f"' % self.desired_heading) + + msg_pub = Twist() + msg_pub.linear.x = self.LINEAR_SPEED + msg_pub.angular.z = self.PID_angular + self.publisher_twist.publisher(msg_pub) + + self.pose_history.append(msg.pose) + if len(self.pose_history) >= 2: + cog = self.calculate_cog() + self.get_logger().info('cog: "%f"' % cog) + + def calculate_cog(self): + P1 = self.pose_history[-2] + P2 = self.pose_history[-1] + dx = P2.position.x - P1.position.x + dy = P2.position.y - P1.position.y + return math.atan2(dy, dx) + + 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) + + msgSetPoint = Float64() + msgSetpoint.data = self.desired_heading + self.PID_setpoint.publish(msgSetPoint) + +def main(args=None): + rclpy.init(args=args) + + mover = RurMover() + PID_node = PID() + + executor = rclpy.executors.MultiThreadeExecutor() + executor.add_node(PID_node) + executor.add_node(mover) + + try: + executor.spin() + finally: + executor.shutdown() + PID_node.destroy_node() + mover.destroy_node() + rclpy.shutdown() + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index 1d4258459be8c7a7075d3e94d7f1b8fbea97ea06..71a8c9f9d9fe101772af65d072d72df38849860c 100644 --- a/setup.py +++ b/setup.py @@ -29,9 +29,7 @@ setup( tests_require=['pytest'], entry_points={ 'console_scripts': [ - 'step_0 = lesson_06.step_0:main', 'step_1 = lesson_06.step_1:main', - 'pid_node = lesson_06.pid_node:main', ], }, )