-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathexample_inverted_pendulum.py
123 lines (96 loc) · 3.81 KB
/
example_inverted_pendulum.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
import numpy as np
import scipy.sparse as sparse
import time
import matplotlib.pyplot as plt
from pyMPC.mpc import MPCController
if __name__ == '__main__':
# Constants #
M = 0.5
m = 0.2
b = 0.1
ftheta = 0.1
l = 0.3
g = 9.81
Ts = 50e-3
Ac =np.array([[0, 1, 0, 0],
[0, -b/M, -(g*m)/M, (ftheta*m)/M],
[0, 0, 0, 1],
[0, b/(M*l), (M*g + g*m)/(M*l), -(M*ftheta + ftheta*m)/(M*l)]])
Bc = np.array([
[0.0],
[1.0/M],
[0.0],
[-1/(M*l)]
])
[nx, nu] = Bc.shape # number of states and number or inputs
# Simple forward euler discretization
Ad = np.eye(nx) + Ac*Ts
Bd = Bc*Ts
# Reference input and states
xref = np.array([0.3, 0.0, 0.0, 0.0]) # reference state
uref = np.array([0.0]) # reference input
uminus1 = np.array([0.0]) # input at time step negative one - used to penalize the first delta u at time instant 0. Could be the same as uref.
# Constraints
xmin = np.array([-1.0, -100, -100, -100])
xmax = np.array([0.3, 100.0, 100, 100])
umin = np.array([-20])
umax = np.array([20])
Dumin = np.array([-5])
Dumax = np.array([5])
# Objective function weights
Qx = sparse.diags([0.3, 0, 1.0, 0]) # Quadratic cost for states x0, x1, ..., x_N-1
QxN = sparse.diags([0.3, 0, 1.0, 0]) # Quadratic cost for xN
Qu = 0.0 * sparse.eye(1) # Quadratic cost for u0, u1, ...., u_N-1
QDu = 0.01 * sparse.eye(1) # Quadratic cost for Du0, Du1, ...., Du_N-1
# Initial state
phi0 = 15*2*np.pi/360
x0 = np.array([0, 0, phi0, 0]) # initial state
# Prediction horizon
Np = 20
K = MPCController(Ad,Bd,Np=Np, x0=x0,xref=xref,uminus1=uminus1,
Qx=Qx, QxN=QxN, Qu=Qu,QDu=QDu,
xmin=xmin,xmax=xmax,umin=umin,umax=umax,Dumin=Dumin,Dumax=Dumax,
eps_feas=1e3)
K.setup()
# Simulate in closed loop
[nx, nu] = Bd.shape # number of states and number or inputs
len_sim = 20 # simulation length (s)
nsim = int(len_sim/Ts) # simulation length(timesteps)
xsim = np.zeros((nsim,nx))
usim = np.zeros((nsim,nu))
tsim = np.arange(0,nsim)*Ts
time_start = time.time()
xstep = x0
uMPC = uminus1
for i in range(nsim):
xsim[i,:] = xstep
# MPC update and step. Could be in just one function call
K.update(xstep, uMPC) # update with measurement
uMPC = K.output() # MPC step (u_k value)
usim[i,:] = uMPC
# System simulation step
F = uMPC
v = xstep[1]
theta = xstep[2]
omega = xstep[3]
der = np.zeros(nx)
der[0] = v
der[1] = (m*l*np.sin(theta)*omega**2 -m*g*np.sin(theta)*np.cos(theta) + m*ftheta*np.cos(theta)*omega + F - b*v)/(M+m*(1-np.cos(theta)**2));
der[2] = omega
der[3] = ((M+m)*(g*np.sin(theta) - ftheta*omega) - m*l*omega**2*np.sin(theta)*np.cos(theta) -(F-b*v)*np.cos(theta))/(l*(M + m*(1-np.cos(theta)**2)) );
# Forward euler step
xstep = xstep + der*Ts
time_sim = time.time() - time_start
fig,axes = plt.subplots(3, 1, figsize=(10, 10))
axes[0].plot(tsim, xsim[:, 0], "k", label='p')
axes[0].plot(tsim, xref[0]*np.ones(np.shape(tsim)), "r--", label="p_ref")
axes[0].set_title("Position (m)")
axes[1].plot(tsim, xsim[:, 2]*360/2/np.pi, label="phi")
axes[1].plot(tsim, xref[2]*360/2/np.pi*np.ones(np.shape(tsim)), "r--", label="phi_ref")
axes[1].set_title("Angle (deg)")
axes[2].plot(tsim, usim[:, 0], label="u")
axes[2].plot(tsim, uref*np.ones(np.shape(tsim)), "r--", label="u_ref")
axes[2].set_title("Force (N)")
for ax in axes:
ax.grid(True)
ax.legend()