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..a761d824e280d23172975e8e233eb700c9a7c2ae 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..3d2a7c33f50727d7f7f64103819c8f723867edb1 --- /dev/null +++ b/lesson_06/step_1.py @@ -0,0 +1,90 @@ +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/output', self.PID_output_callback, 10) + self.PID_state = self.create_publisher(Float64, '/pid/state', 10) + + self.SIDE_TIME = 10 + self.LINEAR_SPEED = 1.0 + self.desired_heading = -math.pi / 2 + self.PID_angular = 0.0 + self.timer = self.create_timer(self.SIDE_TIME, self.pub_timer_callback) + self.pose_history = [] + + msgSetPoint = Float64() + msgSetPoint.data = self.desired_heading + self.PID_setpoint.publish(msgSetPoint) + + def PID_output_callback(self, msg): + self.PID_angular = msg.data + + 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) + + msgStatePose = Float64() + msgStatePose.data = heading + self.PID_state.publish(msgStatePose) + + msg_pub = Twist() + msg_pub.linear.x = self.LINEAR_SPEED + msg_pub.angular.z = self.PID_angular + self.publisher_twist.publish(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.MultiThreadedExecutor() + 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() \ No newline at end of file diff --git a/setup.py b/setup.py index 1d4258459be8c7a7075d3e94d7f1b8fbea97ea06..7d2de4cc3ec13259379728c8b34a38529a8f9eca 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,8 @@ package_name = 'lesson_06' setup( name=package_name, version='0.0.0', - packages=find_packages(exclude=['test']), + packages=find_packages(where='src'), + package_dir={'': 'src'}, data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), @@ -29,9 +30,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', ], }, )