-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
62 lines (49 loc) · 1.23 KB
/
run.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
# STEPC
# A fast and extensible linear system and controller simulator
# for evaluation of predictive controllers
#
# Jon Sowman 2014 <[email protected]>
import numpy as np
import subprocess
from stepc.linsystem import LinSystem
from stepc.simulator import Simulator
from stepc.controller import PIDController
from stepc.solver import ForwardsEulerSolver
git_version = subprocess.check_output(["git", "describe", "--dirty",
"--always"])
print "+++++ STEPC (Version %s) +++++" % git_version.strip()
# Create the system
sys = LinSystem(2, 1, 1)
# Mass on a spring
# Mass
m = .5
# Damping const
b = .2
# Spring const
k = 1
sys.A[0, 0] = 0
sys.A[0, 1] = 1
sys.A[1, 0] = -k/m
sys.A[1, 1] = -b/m
sys.B[0, 0] = 0
sys.B[1, 0] = 1/m
sys.C[0, 0] = 1
sys.C[0, 1] = 0
# Now a simple controller
pid = PIDController()
pid.set_kp(2)
# Target state is [0,0]
pid.set_target(np.array([0]))
# Make a simulator and set parms
solver = ForwardsEulerSolver()
sim = Simulator()
sim.set_solver(solver)
sim.set_timestep(0.001)
sim.set_endtime(10)
# Initial condition
x0 = np.array([[-1], [0]])
# Go for it
t_all, x_all, u_all = sim.simulate(sys, x0, pid)
x = x_all[:, -1]
# Results
print "Final state is [%.6f, %.5f]" % (x[0], x[1])