-
Notifications
You must be signed in to change notification settings - Fork 0
/
simController.py
102 lines (72 loc) · 2.64 KB
/
simController.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
# !/usr/bin/env python
import numpy as np
from controller.base import base
from structures import structure
class simController(object):
def __init__(self, theSolver, theLaw):
self.solver = theSolver
self.cLaw = theLaw
def setController(self, theLaw):
self.cLaw = theLaw
def initialize(self, tspan, x0):
self.solver.initialize(tspan, x0)
if (isinstance(self.cLaw, base)):
(u0, rc) = self.cLaw.compute()
else:
(u0, rc) = self.cLaw()
self.u = np.zeros((u0.shape[0], self.solver.t.size))
def initializeByStruct(self, tspan, istate):
self.initialize(tspan=tspan, x0=istate.x)
def reset(self):
self.solver.reset()
if isinstance(self.cLaw, base):
u0 = self.cLaw.compute()
else:
u0 = self.cLaw()
self.u = np.zeros_like(self.u)
def preStep(self):
pass
def postStep(self):
pass
def advance(self, varargin):
self.preStep()
if isinstance(self.cLaw, base):
(self.uc, rc) = self.cLaw.compute(self.solver.tc, self.solver.xc)
else:
(self.uc, rc) = self.cLaw(self.solver.tc, self.solver.xc)
self.u[:,self.solver.ci:self.solver.ci+1] = self.uc
self.solver.advance( varargin=self.uc)
self.postStep()
#print(str(self.solver.ci) + ", " + str(self.solver.xc.transpose())+ ", " + str(self.uc))
#print(self.solver.xc.transpose())
#print(self.uc)
def simulate(self, tspan=None, x0=None, varargin=None):
if x0 is not None and tspan is not None:
self.initialize(tspan=tspan, x0=x0)
else:
if self.solver.state != self.solver.INITIALIZED:
print("ERROR!")
else:
if tspan is not None:
self.initialize(tspan=tspan, x0=self.solver.xc)
while self.solver.state != self.solver.DONE:
self.advance(varargin)
if isinstance(self.cLaw, base):
(self.uc, rc) = self.cLaw.compute(self.solver.tc, self.solver.xc)
else:
(self.uc, rc) = self.cLaw(self.solver.tc, self.solver.xc)
self.u[:, self.solver.ci:self.solver.ci+1] = self.uc
t = self.solver.t
x = self.solver.x
u = self.u
sol = structure(t=t,x=x,u=u)
return sol
def getState(self):
sol = structure(t=self.solver.tc, x=self.solver.xc, u=self.uc)
return sol
@staticmethod
def linearControlSys(A, B):
def LinearCEOM(t, x, u):
dx = np.matmul(A,x) + np.matmul(B,u)
return dx
return LinearCEOM