8 #include "hrpsys/util/Hrpsys.h" 14 template <std::
size_t dim>
17 Eigen::Matrix<double, dim, dim>
A;
18 Eigen::Matrix<double, dim, 1>
b;
19 Eigen::Matrix<double, 1, dim>
c;
20 Eigen::Matrix<double, dim, dim>
P;
21 Eigen::Matrix<double, 1, dim>
K;
26 riccati_equation() : A(Eigen::Matrix<double, dim, dim>::Zero()), b(Eigen::Matrix<double, dim, 1>::Zero()), c(Eigen::Matrix<double, 1, dim>::Zero()),
27 P(Eigen::Matrix<double, dim, dim>::Zero()), K(Eigen::Matrix<double, 1, dim>::Zero()),
28 A_minus_bKt(Eigen::Matrix<double, dim, dim>::Zero()), Q(0), R(0), R_btPb_inv(0) {};
29 riccati_equation(
const Eigen::Matrix<double, dim, dim>& _A,
const Eigen::Matrix<double, dim, 1>& _b,
30 const Eigen::Matrix<double, 1, dim>& _c,
const double _Q,
const double _R)
31 : A(_A), b(_b), c(_c), P(Eigen::Matrix<double, dim, dim>::Zero()), K(Eigen::Matrix<double, 1, dim>::Zero()), A_minus_bKt(Eigen::Matrix<double, dim, dim>::Zero()), Q(_Q), R(_R), R_btPb_inv(0) {};
34 Eigen::Matrix<double, dim, dim> prev_P;
35 for (
int i = 0;
i < 10000;
i++) {
36 R_btPb_inv = (1.0 / (R + (b.transpose() * P *
b)(0,0)));
37 Eigen::Matrix<double, dim, dim> tmp_pa(P * A);
38 K = R_btPb_inv * b.transpose() * tmp_pa;
39 prev_P = A.transpose() * tmp_pa + c.transpose() * Q * c - A.transpose() * P * b *
K;
40 if ((abs((P - prev_P).array()) < 5.0e-10).all()) {
41 A_minus_bKt = (A - b *
K).transpose();
50 template <std::
size_t dim>
55 Eigen::Matrix<double, 3, 3>
tcA;
56 Eigen::Matrix<double, 3, 1>
tcb;
57 Eigen::Matrix<double, 1, 3>
tcc;
58 Eigen::Matrix<double, 3, 2>
x_k;
59 Eigen::Matrix<double, 1, 2>
u_k;
61 std::deque<Eigen::Matrix<double, 2, 1> >
p;
62 std::deque<double>
pz;
63 std::deque< std::vector<hrp::Vector3> >
qdata;
66 virtual void calc_f() = 0;
67 virtual void calc_u() = 0;
68 virtual void calc_x_k() = 0;
70 const Eigen::Matrix<double, dim, 1>&
b,
71 const Eigen::Matrix<double, 1, dim>&
c,
72 const double q = 1.0,
const double r = 1.0e-6)
84 const hrp::Vector3& init_xk,
const double _gravitational_acceleration,
const double d = 1.6)
85 : riccati(), x_k(Eigen::Matrix<double, 3, 2>::Zero()), u_k(Eigen::Matrix<double, 1, 2>::Zero()), p(), pz(), qdata(),
86 zmp_z(0), cog_z(zc), delay(static_cast<size_t>(round(
d / dt))), ending_count(1+delay)
88 tcA << 1, dt, 0.5 * dt * dt,
91 tcb << 1 / 6.0 * dt * dt * dt,
94 tcc << 1.0, 0.0, -zc / _gravitational_acceleration;
95 x_k(0,0) = init_xk(0);
96 x_k(0,1) = init_xk(1);
104 virtual void update_x_k(
const hrp::Vector3& pr,
const std::vector<hrp::Vector3>& qdata);
111 update_x_k(pr, qdata.back());
137 Eigen::Matrix<double, 1, 2> _p(tcc * x_k);
144 ret[0] = p.front()(0);
145 ret[1] = p.front()(1);
150 _qdata = qdata.front();
152 bool is_doing () {
return p.size() >= 1 + delay; };
153 bool is_end () {
return ending_count <= 0 ; };
156 size_t num = p.size() - remain_length;
157 for (
size_t i = 0;
i < num;
i++) {
171 Eigen::Matrix<double, 2, 1> tmpv;
184 std::cerr <<
"(list ";
185 for (
size_t i = 0;
i < p.size();
i++) {
186 std::cerr <<
"#f(" << p[
i](0) <<
" " << p[
i](1) <<
") ";
188 std::cerr <<
")" << std::endl;
200 const hrp::Vector3& init_xk,
const double _gravitational_acceleration = DEFAULT_GRAVITATIONAL_ACCELERATION,
const double q = 1.0,
201 const double r = 1.0e-6,
const double d = 1.6)
204 init_riccati(tcA, tcb, tcc, q, r);
218 const hrp::Vector3& init_xk,
const double _gravitational_acceleration = DEFAULT_GRAVITATIONAL_ACCELERATION,
const double q = 1.0,
219 const double r = 1.0e-6,
const double d = 1.6)
220 :
preview_control_base<4>(dt, zc, init_xk, _gravitational_acceleration,
d), x_k_e(Eigen::Matrix<double, 4, 2>::Zero())
222 Eigen::Matrix<double, 4, 4>
A;
223 Eigen::Matrix<double, 4, 1>
b;
224 Eigen::Matrix<double, 1, 4>
c;
225 Eigen::Matrix<double, 1, 3> tmpca(tcc * tcA);
226 Eigen::Matrix<double, 1, 1> tmpcb(tcc * tcb);
227 A << 1.0, tmpca(0,0), tmpca(0,1), tmpca(0,2),
228 0.0, tcA(0,0), tcA(0,1), tcA(0,2),
229 0.0, tcA(1,0), tcA(1,1), tcA(1,2),
230 0.0, tcA(2,0), tcA(2,1), tcA(2,2);
236 x_k_e(0,0) = init_xk(0);
237 x_k_e(0,1) = init_xk(1);
238 init_riccati(A, b, c, q, r);
243 template <
class previw_T>
250 preview_dynamics_filter(
const double dt,
const double zc,
const hrp::Vector3& init_xk,
const double _gravitational_acceleration = DEFAULT_GRAVITATIONAL_ACCELERATION,
const double q = 1.0,
const double r = 1.0e-6,
const double d = 1.6)
251 : preview_controller(dt, zc, init_xk, _gravitational_acceleration, q, r,
d), finishedp(false) {};
257 preview_controller.update_x_k(pr, qdata);
258 flg = preview_controller.is_doing();
260 if ( !preview_controller.is_end() )
261 preview_controller.update_x_k();
262 flg = !preview_controller.is_end();
266 preview_controller.get_current_refzmp(p_ret.data());
267 preview_controller.get_refcog(x_ret.data());
268 preview_controller.get_current_qdata(qdata_ret);
274 preview_controller.remove_preview_queue(remain_length);
278 preview_controller.remove_preview_queue();
282 preview_controller.set_preview_queue(pr, qdata, idx);
286 return preview_controller.get_preview_queue_size();
290 preview_controller.print_all_queue();
293 void get_cart_zmp (
double* ret) { preview_controller.get_cart_zmp(ret);}
298 size_t get_delay () {
return preview_controller.get_delay(); };
299 double get_preview_f (
const size_t idx) {
return preview_controller.get_preview_f(idx); };
riccati_equation< dim > riccati
std::deque< std::vector< hrp::Vector3 > > qdata
double get_preview_f(const size_t idx)
Eigen::Matrix< double, 3, 1 > tcb
static const double DEFAULT_GRAVITATIONAL_ACCELERATION
Eigen::Matrix< double, 1, dim > c
extended_preview_control(const double dt, const double zc, const hrp::Vector3 &init_xk, const double _gravitational_acceleration=DEFAULT_GRAVITATIONAL_ACCELERATION, const double q=1.0, const double r=1.0e-6, const double d=1.6)
std::deque< Eigen::Matrix< double, 2, 1 > > p
void get_cart_zmp(double *ret)
size_t get_preview_queue_size()
void get_cart_zmp(double *ret)
Eigen::Matrix< double, dim, dim > A_minus_bKt
void remove_preview_queue(const size_t remain_length)
Eigen::Matrix< double, dim, dim > A
void remove_preview_queue()
virtual void update_x_k()
previw_T preview_controller
riccati_equation(const Eigen::Matrix< double, dim, dim > &_A, const Eigen::Matrix< double, dim, 1 > &_b, const Eigen::Matrix< double, 1, dim > &_c, const double _Q, const double _R)
void init_riccati(const Eigen::Matrix< double, dim, dim > &A, const Eigen::Matrix< double, dim, 1 > &b, const Eigen::Matrix< double, 1, dim > &c, const double q=1.0, const double r=1.0e-6)
void get_current_refzmp(double *ret)
void get_current_refzmp(double *ret)
void get_refcog_vel(double *ret)
Eigen::Matrix< double, dim, 1 > b
void remove_preview_queue(const size_t remain_length)
void get_refcog(double *ret)
Eigen::Matrix< double, 1, 2 > u_k
void get_current_qdata(std::vector< hrp::Vector3 > &_qdata)
void set_preview_queue(const hrp::Vector3 &pr, const std::vector< hrp::Vector3 > &qdata, const size_t idx)
void set_preview_queue(const hrp::Vector3 &pr, const std::vector< hrp::Vector3 > &q, const size_t idx)
void get_refcog_vel(double *ret)
void get_refcog_acc(double *ret)
preview_control_base(const double dt, const double zc, const hrp::Vector3 &init_xk, const double _gravitational_acceleration, const double d=1.6)
Eigen::Matrix< double, 4, 2 > x_k_e
void remove_preview_queue()
preview_dynamics_filter(const double dt, const double zc, const hrp::Vector3 &init_xk, const double _gravitational_acceleration=DEFAULT_GRAVITATIONAL_ACCELERATION, const double q=1.0, const double r=1.0e-6, const double d=1.6)
void get_refcog_acc(double *ret)
virtual ~preview_control()
virtual ~riccati_equation()
double get_preview_f(const size_t idx)
~preview_dynamics_filter()
Eigen::Matrix< double, 1, 3 > tcc
bool update(hrp::Vector3 &p_ret, hrp::Vector3 &x_ret, std::vector< hrp::Vector3 > &qdata_ret, const hrp::Vector3 &pr, const std::vector< hrp::Vector3 > &qdata, const bool updatep)
Eigen::Matrix< double, 3, 2 > x_k
virtual ~preview_control_base()
size_t get_preview_queue_size()
Eigen::Matrix< double, 3, 3 > tcA
virtual ~extended_preview_control()
preview_control(const double dt, const double zc, const hrp::Vector3 &init_xk, const double _gravitational_acceleration=DEFAULT_GRAVITATIONAL_ACCELERATION, const double q=1.0, const double r=1.0e-6, const double d=1.6)
preview_dynamics_filter()
Eigen::Matrix< double, 1, dim > K
Eigen::Matrix< double, dim, dim > P