forked from GPrathap/motion_planning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lqr_controller_todo.h
69 lines (57 loc) · 2.02 KB
/
lqr_controller_todo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef LQR_CONTROLLER
#define LQR_CONTROLLER
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <Eigen/Dense>
#include <unsupported/Eigen/Polynomials>
#include <cmath>
#include <vector>
#include <complex>
#include <cfenv>
#include "motion_model.h"
template<typename robot_motion_model>
class LQRController {
private:
const double kp;
const double allowed_error;
int N = 100;
const int n_x = robot_motion_model::N_X;
const int n_u = robot_motion_model::N_U;
Eigen::MatrixXd Q_int;
public:
LQRController(const double kp_=1, const double allowed_error_=1e-2):kp(kp_)
, allowed_error(allowed_error_){
Q_int = Eigen::MatrixXd::Identity(n_x, n_x);
}
Eigen::MatrixXd calculateDARE(Eigen::MatrixXd& A, Eigen::MatrixXd& B
, Eigen::MatrixXd& Q, Eigen::MatrixXd& R){
Eigen::MatrixXd P = Q_int;
Eigen::MatrixXd P_;
for(int i=0; i<N; i++){
// TODO calculate DARE, see the lectures
// P_ =
double max_val = (P_-P).cwiseAbs().maxCoeff();
if(max_val < allowed_error){
break;
}
P = P_;
}
return P_;
}
double lqrControl(robot_state state, robot_state ref_state
, Eigen::MatrixXd& A, Eigen::MatrixXd& B, Eigen::MatrixXd& Q, Eigen::MatrixXd& R){
Eigen::VectorXd x_diff = state - ref_state;
Eigen::MatrixXd P = calculateDARE(A, B, Q, R);
Eigen::MatrixXd K;
// TODO estimate the K matrix, see the lectures
auto u = K*x_diff;
return u(1);
}
double pControl(const double desired, const double current){
return kp*(desired-current);
}
~LQRController() = default;
};
typedef LQRController<HagenRobot> LQRControl;
#endif //LQR_CONTROLLER