|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <Eigen/Dense> |
| 4 | +#include <geometry_msgs/msg/wrench_stamped.hpp> |
3 | 5 | #include <rclcpp/rclcpp.hpp> |
4 | 6 | #include <std_msgs/msg/string.hpp> |
5 | | -#include <geometry_msgs/msg/wrench_stamped.hpp> |
6 | 7 | #include "PID_setup.hpp" |
7 | | -#include <Eigen/Dense> |
8 | 8 | #include "velocity_controller/utilities.hpp" |
9 | 9 |
|
10 | | - |
11 | | - |
12 | 10 | /*class Guidance_values{ |
13 | 11 | //Dataclass to store guidance values for LQR controller |
14 | 12 | public: |
15 | 13 | double surge=0.0; double pitch=0.0; double yaw=0.0; |
16 | | - double integral_surge=0.0; double integral_pitch=0.0; double integral_yaw=0.0; |
| 14 | + double integral_surge=0.0; double integral_pitch=0.0; double |
| 15 | +integral_yaw=0.0; |
17 | 16 | }; |
18 | 17 | */ |
19 | | -class LQRparameters{ |
20 | | - //Dataclass to store LQR parameters |
21 | | - public: |
22 | | - double q_surge=0.0; double q_pitch=0.0; double q_yaw=0.0; |
23 | | - double r_surge=0.0; double r_pitch=0.0; double r_yaw=0.0; |
24 | | - double i_surge=0.0; double i_pitch=0.0; double i_yaw=0.0; |
25 | | - double i_weight=0.0; double max_force=0.0; |
| 18 | +class LQRparameters { |
| 19 | + // Dataclass to store LQR parameters |
| 20 | + public: |
| 21 | + double q_surge = 0.0; |
| 22 | + double q_pitch = 0.0; |
| 23 | + double q_yaw = 0.0; |
| 24 | + double r_surge = 0.0; |
| 25 | + double r_pitch = 0.0; |
| 26 | + double r_yaw = 0.0; |
| 27 | + double i_surge = 0.0; |
| 28 | + double i_pitch = 0.0; |
| 29 | + double i_yaw = 0.0; |
| 30 | + double i_weight = 0.0; |
| 31 | + double max_force = 0.0; |
26 | 32 | }; |
27 | 33 |
|
28 | 34 | /*class angle{ |
29 | 35 | public: |
30 | 36 | double phit=0.0; |
31 | 37 | double thetat=0.0; |
32 | 38 | double psit=0.0; |
33 | | - |
| 39 | +
|
34 | 40 | };*/ |
35 | 41 |
|
36 | | -struct LQRsolveResult{ |
37 | | - Eigen::Matrix<double,3,6> K; |
38 | | - Eigen::Matrix<double,6,6> P; |
39 | | - int INFO=0; |
40 | | - LQRsolveResult(Eigen::Matrix<double,3,6> K,Eigen::Matrix<double,6,6> P, int INFO=0):K(K),P(P),INFO(INFO) {}; |
| 42 | +struct LQRsolveResult { |
| 43 | + Eigen::Matrix<double, 3, 6> K; |
| 44 | + Eigen::Matrix<double, 6, 6> P; |
| 45 | + int INFO = 0; |
| 46 | + LQRsolveResult(Eigen::Matrix<double, 3, 6> K, |
| 47 | + Eigen::Matrix<double, 6, 6> P, |
| 48 | + int INFO = 0) |
| 49 | + : K(K), P(P), INFO(INFO) {}; |
41 | 50 | }; |
42 | | -class LQRController{ |
43 | | - |
44 | | - public: |
45 | | - LQRController(LQRparameters params={0,0,0,0,0,0,0,0,0,0,0},Eigen::Matrix3d inertia_matrix=Eigen::Matrix3d::Identity()); |
46 | | - |
| 51 | +class LQRController { |
| 52 | + public: |
| 53 | + LQRController(LQRparameters params = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 54 | + Eigen::Matrix3d inertia_matrix = Eigen::Matrix3d::Identity()); |
47 | 55 |
|
48 | 56 | void set_params(LQRparameters params); |
49 | | - Eigen::Matrix3d calculate_coriolis_matrix(double pitchrate, double yaw_rate, double sway_vel, double heave_vel); |
| 57 | + Eigen::Matrix3d calculate_coriolis_matrix(double pitchrate, |
| 58 | + double yaw_rate, |
| 59 | + double sway_vel, |
| 60 | + double heave_vel); |
50 | 61 | void set_matrices(Eigen::Matrix3d inertia_matrix); |
51 | 62 | void update_augmented_matrices(Eigen::Matrix3d coriolis_matrix); |
52 | 63 |
|
53 | | - //angle quaternion_to_euler_angle(double w, double x, double y, double z); |
| 64 | + // angle quaternion_to_euler_angle(double w, double x, double y, double z); |
54 | 65 | double ssa(double angle); |
55 | 66 |
|
56 | | - std::tuple<double,double> saturate (double value, bool windup, double limit); |
57 | | - double anti_windup(double ki, double error, double integral_sum, bool windup); |
58 | | - Eigen::Vector<double,3> saturate_input(Eigen::Vector<double,3> u); |
59 | | - |
60 | | - Eigen::Vector<double,6> update_error(Guidance_data guidance_values, State states); |
61 | | - Eigen::Vector<double,3> calculate_lqr_u(State states, Guidance_data guidance_values); |
62 | | - |
63 | | - LQRsolveResult solve_k_p(const Eigen::Matrix<double,6,6> &A,const Eigen::Matrix<double,6,3> &B,const Eigen::Matrix<double,6,6> &Q, const Eigen::Matrix<double,3,3> &R); |
64 | | - |
65 | | - //Resets controller |
| 67 | + std::tuple<double, double> saturate(double value, |
| 68 | + bool windup, |
| 69 | + double limit); |
| 70 | + double anti_windup(double ki, |
| 71 | + double error, |
| 72 | + double integral_sum, |
| 73 | + bool windup); |
| 74 | + Eigen::Vector<double, 3> saturate_input(Eigen::Vector<double, 3> u); |
| 75 | + |
| 76 | + Eigen::Vector<double, 6> update_error(Guidance_data guidance_values, |
| 77 | + State states); |
| 78 | + Eigen::Vector<double, 3> calculate_lqr_u(State states, |
| 79 | + Guidance_data guidance_values); |
| 80 | + |
| 81 | + LQRsolveResult solve_k_p(const Eigen::Matrix<double, 6, 6>& A, |
| 82 | + const Eigen::Matrix<double, 6, 3>& B, |
| 83 | + const Eigen::Matrix<double, 6, 6>& Q, |
| 84 | + const Eigen::Matrix<double, 3, 3>& R); |
| 85 | + |
| 86 | + // Resets controller |
66 | 87 | void reset_controller(); |
67 | 88 |
|
68 | | - // VariablesEigen::Matrix3d vector_to_matrix3d(const std::vector<double> &other_matrix) |
69 | | - const double pi=3.14159265358979323846; |
70 | | - double integral_error_surge; double integral_error_pitch; double integral_error_yaw; |
71 | | - bool surge_windup; bool pitch_windup; bool yaw_windup; |
72 | | - double q_surge; double q_pitch; double q_yaw; |
73 | | - double r_surge; double r_pitch; double r_yaw; |
74 | | - double i_surge; double i_pitch; double i_yaw; |
75 | | - double i_weight; double max_force; |
| 89 | + // VariablesEigen::Matrix3d vector_to_matrix3d(const std::vector<double> |
| 90 | + // &other_matrix) |
| 91 | + const double pi = 3.14159265358979323846; |
| 92 | + double integral_error_surge; |
| 93 | + double integral_error_pitch; |
| 94 | + double integral_error_yaw; |
| 95 | + bool surge_windup; |
| 96 | + bool pitch_windup; |
| 97 | + bool yaw_windup; |
| 98 | + double q_surge; |
| 99 | + double q_pitch; |
| 100 | + double q_yaw; |
| 101 | + double r_surge; |
| 102 | + double r_pitch; |
| 103 | + double r_yaw; |
| 104 | + double i_surge; |
| 105 | + double i_pitch; |
| 106 | + double i_yaw; |
| 107 | + double i_weight; |
| 108 | + double max_force; |
76 | 109 |
|
77 | 110 | Eigen::Matrix3d inertia_matrix_inv; |
78 | | - Eigen::Matrix<double,6,6> state_weight_matrix; |
| 111 | + Eigen::Matrix<double, 6, 6> state_weight_matrix; |
79 | 112 | Eigen::Matrix3d input_weight_matrix; |
80 | | - Eigen::Matrix<double,6,6> augmented_system_matrix; |
81 | | - Eigen::Matrix<double,6,3> augmented_input_matrix; |
82 | | - |
83 | | - |
| 113 | + Eigen::Matrix<double, 6, 6> augmented_system_matrix; |
| 114 | + Eigen::Matrix<double, 6, 3> augmented_input_matrix; |
84 | 115 | }; |
85 | 116 |
|
86 | | -//Extra operations |
87 | | -Eigen::Matrix3d vector_to_matrix3d(const std::vector<double> &other_matrix); |
88 | | -std::vector<double> matrix3d_to_vector(const Eigen::Matrix3d &mat); |
89 | | -std::vector<std::vector<double>> matrix3d_to_vector2d(const Eigen::Matrix3d &mat); |
90 | | -Eigen::Matrix3d vector2d_to_matrix3d(const std::vector<std::vector<double>> &other_matrix); |
| 117 | +// Extra operations |
| 118 | +Eigen::Matrix3d vector_to_matrix3d(const std::vector<double>& other_matrix); |
| 119 | +std::vector<double> matrix3d_to_vector(const Eigen::Matrix3d& mat); |
| 120 | +std::vector<std::vector<double>> matrix3d_to_vector2d( |
| 121 | + const Eigen::Matrix3d& mat); |
| 122 | +Eigen::Matrix3d vector2d_to_matrix3d( |
| 123 | + const std::vector<std::vector<double>>& other_matrix); |
0 commit comments