-
Notifications
You must be signed in to change notification settings - Fork 4
/
control_place2.py
68 lines (44 loc) · 1.41 KB
/
control_place2.py
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
import numpy as np
import control
import gym_CartPole_BT
from gym_CartPole_BT.systems.cartpend import cartpend_ss
A, B = cartpend_ss()
C = np.array([[1, 0, 0, 0], [0, 0, 1, 0]])
M = np.array([[1, 0, 0, 0], [0, 0, 1, 0]])
n = A.shape[0]
ny = C.shape[0]
nm = M.shape[0]
nu = B.shape[1]
# Check system is controllable
assert(np.linalg.matrix_rank(control.ctrb(A, B)) == 4)
tau = 0.05 # sampling rate
# Modal control
I = np.eye(n)
desired_poles = np.array([-1/10, -1/8])
# Desired eigenvalues
eigs = np.exp(-tau / desired_poles)
zero_nu = np.zeros([1, nu])
vpsi1 = np.block([[A - eigs[0] * I, B], [C[0, :], zero_nu], [C[1, :], zero_nu]])
vpsi2 = np.block([[A - eigs[1] * I, B], [C[0, :], zero_nu], [C[1, :], zero_nu]])
vpsi1 = null([A - eigs[1] * I B; C(1,:) zero_nu; C(2,:) zero_nu])
vpsi2 = null([A - eigs[1] * I B; C(1,:) zero_nu; C(3,:) zero_nu])
vpsi3 = null([A - eigs[2] * I B; C(2,:) zero_nu; C(3,:) zero_nu])
# Récupération des vecteur vi et psii
v1=vpsi1(1:n,1)
psi1=vpsi1(n+1:end,1)
v2=vpsi2(1:n,1)
psi2=vpsi2(n+1:end,1)
v3=vpsi3(1:n,1)
psi3=vpsi3(n+1:end,1)
# Formation des matrice Vv et PSI
Vv=[v1 v2 v3]
PSI=[psi1 psi2 psi3]
#%%%%%%%%%% CALCUL DE K %%%%%%%%%%
K=PSI/(M*Vv)
#%%%%%%%%%% CALCUL DE F %%%%%%%%%%
F=inv(C*inv(I-A-B*K*M)*B)
# If B and M are full rank, the number of poles that can
# be placed is
nv = max(nu, nm)
assert(np.linalg.matrix_rank(B) == nu)
assert(np.linalg.matrix_rank(M) == nm)