-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_dynamics.py
More file actions
173 lines (146 loc) · 5.03 KB
/
system_dynamics.py
File metadata and controls
173 lines (146 loc) · 5.03 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import gym
import math
from scipy import signal
def discrete_model(env, measurements):
# linearized continuous model
J = 1/12*(env.masspole*2)**2
beta = env.masspole*env.masscart*env.length**2 + J*(env.masspole+env.masscart)
ac = -(env.masspole**2*env.length**2*env.gravity)/beta
bc = (J+env.masspole*env.length**2)/beta
cc = env.masspole*env.length*env.gravity*(env.masscart+env.masspole)/beta
dc = -env.masspole*env.length/beta
A_c = np.array([[0,1,0,0],
[0,0,ac,0],
[0,0,0,1],
[0,0,cc,0]])
B_c = np.array([[0],
[bc],
[0],
[dc]])
# C_c = np.array([[1,env.tau,0,0],
# [0,1,0,0],
# [0,0,0,1]])
# D_c = np.array([[0],
# [0],
# [0]])
# C_c = np.array([[1,0,0,0]])
# D_c = np.array([[0]])
if measurements is 2:
C_c = np.array([[1,0,0,0],
[0,0,0,1]])
D_c = np.array([[0],
[0]])
elif measurements is 1:
C_c = np.array([[1,0,0,0]])
D_c = np.array([[0]])
# discrete linearized model
sys = signal.StateSpace(A_c, B_c, C_c, D_c)
discrete_sys = sys.to_discrete(env.tau)
A = discrete_sys.A
B = discrete_sys.B
C = discrete_sys.C
D = discrete_sys.D
# print(A)
# print(B)
# print(C)
# while True:
# pass
return A, B, C, D
# linearized function around equilibrum state
# output is system dynamics
def linearized_model_control(env):
gamma = (4.0 / 3.0 - env.masspole / env.total_mass)
a = -env.gravity * env.masspole / (env.total_mass * gamma)
b = (1.0 / env.total_mass * (1 + env.masspole / (env.total_mass * gamma)))
c = env.gravity / (env.length * gamma)
d = -1.0 / (env.total_mass * env.length * gamma)
tau = env.tau
F = np.array([
[1, tau, 0, 0, 0],
[0, 1, tau * a, 0, tau * b],
[0, 0, 1, tau, 0],
[0, 0, tau * c, 1, tau * d],
])
return F
# linearized function around equilibrum state
# output is linearized state space model
def linearized_model_estimate(env, measurements):
gamma = (4.0 / 3.0 - env.masspole / env.total_mass)
a = -env.gravity * env.masspole / (env.total_mass * gamma)
b = (1.0 / env.total_mass * (1 + env.masspole / (env.total_mass * gamma)))
c = env.gravity / (env.length * gamma)
d = -1.0 / (env.total_mass * env.length * gamma)
tau = env.tau
A = np.array([
[1, tau, 0, 0],
[0, 1, tau * a, 0],
[0, 0, 1, tau],
[0, 0, tau * c, 1],
])
B = np.array([[ 0,
tau * b,
0,
tau * d
]]).T
if measurements is 1:
H = np.array([[1, 0, 0, 0]])
# H = np.array([[1, env.tau, 0, 0],
# [0, 1, 0, 0],
# [0, 0, 0, 1]])
elif measurements is 2:
H = np.array([[1, 0, 0, 0],
[0, 0, 0, 1]])
# print(A)
# print(B)
# print(H)
# while True:
# pass
return A, B, H
def UKF_model(state, dt, **kwargs):
env = kwargs["env"]
force = kwargs["u"]
x = state[0]
x_dot = state[1]
theta = state[2]
theta_dot = state[3]
costheta = math.cos(theta)
sintheta = math.sin(theta)
temp = (force + env.polemass_length * theta_dot * theta_dot * sintheta) / env.total_mass
thetaacc = (env.gravity * sintheta - costheta* temp) / (env.length * (4.0/3.0 - env.masspole * costheta * costheta / env.total_mass))
xacc = temp - env.polemass_length * thetaacc * costheta / env.total_mass
x = x + env.tau * x_dot
x_dot = x_dot + env.tau * xacc
theta = theta + env.tau * theta_dot
theta_dot = theta_dot + env.tau * thetaacc
state_next = np.array([x,x_dot,theta,theta_dot])
return state_next
def UKF_measurement(x):
# x = np.array([x]).T
# y = np.array([x[1,0]*0.02+x[0,0], x[1,0], x[3,0]])
y = np.array([x[0], x[3]])
# y = np.array([x[0]])
# print("measurement",y.shape)
return y
def non_linearized_model(env, state, u):
force = u
x = state[0]
x_dot = state[1]
theta = state[2]
theta_dot = state[3]
costheta = math.cos(theta)
sintheta = math.sin(theta)
temp = (force + env.polemass_length * theta_dot * theta_dot * sintheta) / env.total_mass
thetaacc = (env.gravity * sintheta - costheta* temp) / (env.length * (4.0/3.0 - env.masspole * costheta * costheta / env.total_mass))
xacc = temp - env.polemass_length * thetaacc * costheta / env.total_mass
x = x + env.tau * x_dot
x_dot = x_dot + env.tau * xacc
theta = theta + env.tau * theta_dot
theta_dot = theta_dot + env.tau * thetaacc
state_next = np.array([x,x_dot,theta,theta_dot])
return state_next
def measurement(x):
# y = np.array([x[0,0]+x[1,0]*0.02, x[1,0], x[3,0]])
# y = np.array([x[0,0]])
y = np.array([x[0,0], x[3,0]])
return y