ARCS6 AR6-REV.24062600
読み取り中…
検索中…
一致する文字列を見つけられません
ArcsControl.hh
[詳解]
1
8//
9// Copyright (C) 2011-2022 Yokokura, Yuki
10// This program is free software;
11// you can redistribute it and/or modify it under the terms of the FreeBSD License.
12// For details, see the License.txt file.
13
14#ifndef ARCSCONTROL
15#define ARCSCONTROL
16
17#include <cassert>
18#include <tuple>
19#include "Matrix.hh"
20
21// ARCS組込み用マクロ
22#ifdef ARCS_IN
23 // ARCSに組み込まれる場合
24 #include "ARCSassert.hh"
25 #include "ARCSeventlog.hh"
26#else
27 // ARCSに組み込まれない場合
28 #define arcs_assert(a) (assert(a))
29 #define PassedLog()
30 #define EventLog(a)
31 #define EventLogVar(a)
32#endif
33
34namespace ARCS { // ARCS名前空間
37 public:
44 template<size_t NN, size_t MM>
45 static constexpr void Lyapunov(const Matrix<NN,MM,double>& A, const Matrix<NN,MM,double>& Q, Matrix<NN,MM,double>& X){
46 static_assert(NN == MM); // 正方行列のみ対応
47 constexpr auto I = Matrix<NN,NN>::eye(); // 単位行列
48 X = Matrix<1,NN*MM>::template vecinv<NN,MM>( -inv( Kronecker(I,A) + Kronecker(A,I) )*vec(Q) ); // A*X + X*A^T + Q = 0 をXについて解く
49 }
50
57 template<size_t NN, size_t MM>
60 Lyapunov(A, Q, X);
61 return X;
62 }
63
71 template<size_t NA, size_t MA, size_t NB>
72 static constexpr void GramianCtrl(const Matrix<NA,MA>& A, const Matrix<NB,MA>& b, Matrix<NA,MA>& Wc){
73 Lyapunov(A, b*tp(b), Wc);
74 }
75
83 template<size_t NA, size_t MA, size_t NB>
84 static constexpr Matrix<NA,MA> GramianCtrl(const Matrix<NA,MA>& A, const Matrix<NB,MA>& b){
86 GramianCtrl(A, b, Wc);
87 return Wc;
88 }
89
97 template<size_t NA, size_t MA, size_t MC>
98 static constexpr void GramianObsrv(const Matrix<NA,MA>& A, const Matrix<NA,MC>& c, Matrix<NA,MA>& Wo){
99 Lyapunov(tp(A), tp(c)*c, Wo);
100 }
101
109 template<size_t NA, size_t MA, size_t MC>
110 static constexpr Matrix<NA,MA> GramianObsrv(const Matrix<NA,MA>& A, const Matrix<NA,MC>& c){
111 Matrix<NA,MA> Wo;
112 GramianObsrv(A, c, Wo);
113 return Wo;
114 }
115
127 template<size_t NA, size_t MA, size_t NB, size_t MC>
128 static constexpr void BalanceReal(const Matrix<NA,MA>& A, const Matrix<NB,MA>& b, const Matrix<NA,MC>& c, Matrix<NA,MA>& Ah, Matrix<NB,MA>& bh, Matrix<NA,MC>& ch){
129 static_assert(NA == MA); // A行列は正方行列
130
131 // 1. グラミアンズの計算
132 const Matrix<NA,MA> Wc = GramianCtrl(A, b); // 可制御グラミアン
133 const Matrix<NA,MA> Wo = GramianObsrv(A, c); // 可観測グラミアン
134
135 // 2. 1をコレスキー分解
136 Matrix<NA,MA> Lc, Lo;
137 Cholesky(Wc, Lc);
138 Cholesky(Wo, Lo);
139
140 // 3. 2を特異値分解する
141 const auto [U, S, V] = SVD( tp(Lo)*Lc );
142
143 // 4. 3から変換行列を計算する
144 const Matrix<NA,MA> T = inv(sqrte(S))*tp(U)*tp(Lo);
145 const Matrix<NA,MA> Ti = Lc*V*inv(sqrte(S));
146
147 // 5. 4を使って状態空間モデルを変換する
148 Ah = T*A*Ti;
149 bh = T*b;
150 ch = c*Ti;
151
152 // 下記はデバッグ用
153 //PrintMat(T*Wc*tp(T) - tp(Ti)*Wo*Ti); // 零行列になればOK
154 //PrintMat(T*Ti); // 単位行列になればOK
155 }
156
166 template<size_t NA, size_t MA, size_t NB, size_t MC>
167 static constexpr std::tuple<Matrix<NA,MA>, Matrix<NB,MA>, Matrix<NA,MC>> BalanceReal(const Matrix<NA,MA>& A, const Matrix<NB,MA>& b, const Matrix<NA,MC>& c){
168 Matrix<NA,MA> Ah;
169 Matrix<NB,MA> bh;
170 Matrix<NA,MC> ch;
171 BalanceReal(A, b, c, Ah, bh, ch);
172 return {Ah, bh, ch};
173 }
174
185 template <size_t N, size_t M>
186 static constexpr void Discretize(
187 const Matrix<N,N>& Ac, const Matrix<M,N>& Bc, Matrix<N,N>& Ad, Matrix<M,N>& Bd, const double Ts, const size_t Npade = 13, const size_t Nint = 10000
188 ){
189 Ad = expm(Ac*Ts, Npade); // A行列の離散化
190 Bd = integral_expm(Ac, Ts, Nint, Npade)*Bc; // B行列の離散化
191 }
192
202 template <size_t N, size_t M>
203 static constexpr std::tuple<Matrix<N,N>, Matrix<M,N>> Discretize(
204 const Matrix<N,N>& Ac, const Matrix<M,N>& Bc, const double Ts, const size_t Npade = 13, const size_t Nint = 10000
205 ){
206 Matrix<N,N> Ad;
207 Matrix<M,N> Bd;
208 Discretize(Ac, Bc, Ad, Bd, Ts, Npade, Nint);
209 return {Ad, Bd};
210 }
211
212 private:
213 ArcsControl() = delete;
214 ArcsControl(ArcsControl&& r) = delete;
215 ~ArcsControl() = delete;
216 ArcsControl(const ArcsControl&) = delete;
217 const ArcsControl& operator=(const ArcsControl&) = delete;
218};
219}
220
221#endif
222
ARCS イベントログクラス
ARCS用ASSERTクラス
行列/ベクトル計算クラス(テンプレート版)
ARCS-Control 制御理論クラス
Definition ArcsControl.hh:36
static constexpr void BalanceReal(const Matrix< NA, MA > &A, const Matrix< NB, MA > &b, const Matrix< NA, MC > &c, Matrix< NA, MA > &Ah, Matrix< NB, MA > &bh, Matrix< NA, MC > &ch)
状態空間モデルを平衡化する関数(引数で返す版)
Definition ArcsControl.hh:128
static constexpr Matrix< NN, MM, double > Lyapunov(const Matrix< NN, MM, double > &A, const Matrix< NN, MM, double > &Q)
連続リアプノフ方程式 A*X + X*A^T + Q = 0 の解Xを求める関数(実数版, 戻り値で返す版)
Definition ArcsControl.hh:58
static constexpr void Discretize(const Matrix< N, N > &Ac, const Matrix< M, N > &Bc, Matrix< N, N > &Ad, Matrix< M, N > &Bd, const double Ts, const size_t Npade=13, const size_t Nint=10000)
連続系状態空間モデルを離散化する関数(引数で返す版)
Definition ArcsControl.hh:186
static constexpr void GramianObsrv(const Matrix< NA, MA > &A, const Matrix< NA, MC > &c, Matrix< NA, MA > &Wo)
可観測グラミアンを計算する関数(引数で返す版)
Definition ArcsControl.hh:98
static constexpr std::tuple< Matrix< NA, MA >, Matrix< NB, MA >, Matrix< NA, MC > > BalanceReal(const Matrix< NA, MA > &A, const Matrix< NB, MA > &b, const Matrix< NA, MC > &c)
状態空間モデルを平衡化する関数(タプルで返す版)
Definition ArcsControl.hh:167
static constexpr Matrix< NA, MA > GramianObsrv(const Matrix< NA, MA > &A, const Matrix< NA, MC > &c)
可観測グラミアンを計算する関数(戻り値で返す版)
Definition ArcsControl.hh:110
static constexpr Matrix< NA, MA > GramianCtrl(const Matrix< NA, MA > &A, const Matrix< NB, MA > &b)
可制御グラミアンを計算する関数(戻り値で返す版)
Definition ArcsControl.hh:84
static constexpr void GramianCtrl(const Matrix< NA, MA > &A, const Matrix< NB, MA > &b, Matrix< NA, MA > &Wc)
可制御グラミアンを計算する関数(引数で返す版)
Definition ArcsControl.hh:72
static constexpr std::tuple< Matrix< N, N >, Matrix< M, N > > Discretize(const Matrix< N, N > &Ac, const Matrix< M, N > &Bc, const double Ts, const size_t Npade=13, const size_t Nint=10000)
連続系状態空間モデルを離散化する関数(タプルで返す版)
Definition ArcsControl.hh:203
static constexpr void Lyapunov(const Matrix< NN, MM, double > &A, const Matrix< NN, MM, double > &Q, Matrix< NN, MM, double > &X)
連続リアプノフ方程式 A*X + X*A^T + Q = 0 の解Xを求める関数(実数版, 引数で返す版)
Definition ArcsControl.hh:45
行列/ベクトル計算クラス(テンプレート版)
Definition Matrix.hh:44
static constexpr Matrix< NN, NN, TT > eye(void)
n行n列の単位行列を返す関数 (identのエイリアス)
Definition Matrix.hh:678