forked from EmbersArc/SuccessiveConvexificationFreeFinalTime
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdynamics_generation.py
305 lines (218 loc) · 8.75 KB
/
dynamics_generation.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from sympy import symbols, Matrix, Function
from time import time
import numpy as np
import sympy
class Dynamics(object):
''' The Dynamics class defines all dynamic functions symbolically.
It can be evaluted if "constants" is not None,
otherwise will generate a .py file with all the definitions
'''
def __init__(self, constants = None):
start = time()
# --------------------------------------------------------- setup
# Variable Symbols
m = symbols('m')
r = Matrix(symbols('r0 r1 r2')) # aka rI
v = Matrix(symbols('v0 v1 v2')) # aka vI
q = Matrix(symbols('q0 q1 q2 q3')) # aka qBI
w = Matrix(symbols('w0 w1 w2')) # aka wB
self.x = [
[m],
[r[0]], [r[1]], [r[2]],
[v[0]], [v[1]], [v[2]],
[q[0]], [q[1]], [q[2]], [q[3]],
[w[0]], [w[1]], [w[2]]
]
self.u = Matrix(symbols('u0 u1 u2', positive=True))
self.s = symbols('s', positive=True) # dtime/dtau
# Constants
if constants is not None:
generate_code = False
alpha = constants['alpha']
rTB = Matrix(constants['rTB'])
J = Matrix(constants['J'])
g = Matrix(constants['g'])
else:
alpha = symbols('alpha')
rTB = Matrix(symbols('rTB0 rTB1 rTB2'))
g = Matrix(symbols('gx gy gz'))
J = sympy.zeros(3,3)
J[0,0] = symbols('J00')
J[1,1] = symbols('J11')
J[2,2] = symbols('J22')
generate_code = True
# --------------------------------------------------------- dx / dt
# f(x) = first derivative of x with respect to t
self.f = sympy.zeros(14,1)
u_mag = sympy.sqrt(self.u[0]**2. + self.u[1]**2. + self.u[2]**2.)
self.f[0] = (-alpha) * u_mag # dm / dt = -alpha * ||u||
self.f[1:4,:] = (v) # dr/dt = velocity
# dv/dt = gravity + (pointed thrust)/mass
self.f[4:7,:] = (1./m) * self.cIB(q) * self.u + g
self.f[7:11,:] = (1./2.) * self.Om(w) * q
# J.T == J.inv() due to orthogonality
self.f[11:14,:] = J.pinv_solve(rTB.cross(self.u) - w.cross(J*w))
self.A = self.s * self.f.jacobian(self.x) # df/dx = (17b)
self.B = self.s * self.f.jacobian(self.u) # df/du = (17c)
print(' >> Dynamics class init took',time() - start,'sec')
# --------------------------------------------------------- code gen
if generate_code:
self.generate_functions()
@staticmethod
def Om(w, numpy=False):
w0, w1, w2 = w
if numpy:
Omega = np.zeros((4,4))
else:
Omega = sympy.zeros(4,4)
Omega[0,0] = 0
Omega[0,1] = -w0
Omega[0,2] = -w1
Omega[0,3] = -w2
Omega[1,0] = +w0
Omega[1,1] = 0
Omega[1,2] = +w2
Omega[1,3] = -w1
Omega[2,0] = +w1
Omega[2,1] = -w2
Omega[2,2] = 0
Omega[2,3] = +w0
Omega[3,0] = +w2
Omega[3,1] = +w1
Omega[3,2] = -w0
Omega[3,3] = 0
return Omega
@staticmethod
def cIB(q, numpy=False):
q0, q1, q2, q3 = q
if numpy:
cIB_m = np.zeros((3,3))
else:
cIB_m = sympy.zeros(3,3)
cIB_m[0,0] = 1-2*(q2**2 + q3**2)
cIB_m[0,1] = 2*(q1*q2 + q0*q3)
cIB_m[0,2] = 2*(q1*q3 - q0*q2)
cIB_m[1,0] = 2*(q1*q2 - q0*q3)
cIB_m[1,1] = 1-2*(q1**2 + q3**2)
cIB_m[1,2] = 2*(q2*q3 + q0*q1)
cIB_m[2,0] = 2*(q1*q3 + q0*q2)
cIB_m[2,1] = 2*(q2*q3 - q0*q1)
cIB_m[2,2] = 1-2*(q1**2 + q2**2)
return cIB_m
def get(self, name, xi, ui, si):
substitutions = []
for n, sym in enumerate(self.x):
substitutions.append( (sym[0], xi[n,0]) )
for n, sym in enumerate(self.u):
substitutions.append( (sym, ui[n]) )
substitutions.append((self.s, si))
function = getattr(self, name)
matrix = function.subs(substitutions)
return np.array(matrix).astype(np.float64)
def generate_functions(self):
''' Code generation of the symbolic dynamic functions '''
tab = ' ' # 4 spaces
functions = {'A':self.A, 'B':self.B, 'f':self.f}
with open('dynamics_functions.py','w') as f:
f.write('""" This was generated by dynamics_generation.py """' + '\n\n')
f.write('from numpy import sqrt' + '\n')
f.write('import numpy as np' + '\n\n')
f.write('class Dynamics:' + '\n\n')
set_parameters_string = tab
set_parameters_string += 'def set_parameters(self, parms):' + '\n'
set_parameters_string += tab
set_parameters_string += tab
set_parameters_string += 'for name, val in parms.items():' + '\n'
set_parameters_string += tab
set_parameters_string += tab
set_parameters_string += tab
set_parameters_string += 'setattr(self, name, val)'
f.write(set_parameters_string + '\n')
for name, matrix in functions.items():
if name != 'f':
f.write('\n')
f.write(tab)
f.write('def ' + name + '(self, x, u, s):' + '\n')
else:
f.write('\n')
f.write(tab)
f.write('def ' + name + '(self, x, u):' + '\n')
variables_unpacking = [
'm, r0, r1, r2, v0, v1, v2, q0, q1, q2, q3, w0, w1, w2 = x',
'u0, u1, u2 = u',
]
constants_unpacking = [
"J = self.J",
"alpha = self.alpha",
"gx, gy, gz = self.g_I",
"rTB0, rTB1, rTB2 = self.rTB",
]
for v in variables_unpacking:
f.write(tab + tab + v + '\n')
f.write('\n')
for c in constants_unpacking:
f.write(tab + tab + c + '\n')
f.write('\n' + tab)
if name != 'f':
f.write(tab + name + 'm')
f.write(' = np.zeros(' + str(matrix.shape) + ')\n')
else:
f.write(tab + name + 'm')
f.write(' = np.zeros((' + str(matrix.shape[0]) + ',))\n')
for n in range(matrix.shape[0]):
for m in range(matrix.shape[1]):
value = str(matrix[n,m])
if value != '0':
genline = tab
genline += name + 'm'
if name != 'f':
genline += '[' + str(n) + ', ' + str(m) +']='
genline += str(value)
else:
genline += '[' + str(n) + ']='
genline += str(value)
# J matrix should be indexed instead of unpacked
for i in range(3):
for j in range(3):
genline = genline.replace(
'J'+str(i)+str(j),
'J['+str(i)+','+str(j)+']',
)
f.write(tab + genline + '\n')
f.write(tab + tab + 'return ' + name + 'm' + '\n')
print('Done writing to file')
def runtime_tests():
d = Dynamics(constants)
start = time()
A = d.get('A', x, u, s)
B = d.get('B', x, u, s)
f = d.get('f', x, u, s)
print('took',time()-start,'sec to run A, B and f')
print('A',A.shape, A)
print('B',B.shape, B)
print('f',f.shape, f)
print('Ax', A*x)
print('Bu', B*u)
def function_tests():
import dynamics_functions as funk
f = funk.Dynamics()
f.set_parameters(constants)
# If these can be assigned, all is well in set_parameters
a = (f.alpha),(f.rTB),(f.g_I),(f.J)
# If this executes, all is well in the dynamics functions
x = np.random.random((14,1))
u = np.random.random((3,1))
s = 1
f.A(x,u,s)
f.B(x,u,s)
f.f(x,u)
print('Tests passed')
if __name__ == '__main__':
import numpy as np
constants = {}
constants['alpha'] = 0.1
constants['rTB'] = -1e-2 * np.array([1,0,0])
constants['J'] = 1e-2 * np.eye(3)
constants['g_I'] = np.array([ -1, 0, 0]) # inertial frame
d = Dynamics()
function_tests()