forked from zgh551/SlidingModeControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidingModeControlWithIntegral.py
More file actions
172 lines (142 loc) · 4.7 KB
/
SlidingModeControlWithIntegral.py
File metadata and controls
172 lines (142 loc) · 4.7 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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 19 20:20:31 2019
@author: zhuguohua
"""
# 使用控制率的微分来平滑控制输出,减少抖振
import numpy as np
import control as ct
import matplotlib.pyplot as plt
c = 1.5
c_bar = 10.0
rho = 30.0
x1_ref = 0
x2_ref = 0
u_ref = 0
x1_0 = 1
x2_0 = -2
u_0 = 0
def f(x1,x2,t):
return np.sin(2*t)
###############################################################################
# Mass Modle
###############################################################################
# System state: x1,x2,u
# System input: v
# System output: x1,x2,u
# System parameters: none
def mass_update(t, x, u, params):
# Return the derivative of the state
return np.array([
x[1], # dx1 = x2
x[2] + f(x[0],x[1],t), # dx2 = u + f(x1,x2,t)
u[0] # du = v
])
def mass_output(t, x, u, params):
return x # return x1, x2 , u (full state)
unit_mass = ct.NonlinearIOSystem(
mass_update, mass_output, states=3, name='unit_mass',
inputs=('v'),
outputs=('x1', 'x2', 'u'))
###############################################################################
# Control
###############################################################################
# s = u + (c + c_bar)x2 c*c_bar*x1 + f(x1,x2,t)
# System state: none
# System input: x1,x2,u
# System output: v
# System parameters: none
def control_output(t, x, u, params):
temp_s = u[2] + (c + c_bar)*u[1] + c*c_bar*u[0] + f(u[0],u[1],t)
# temp_s = c*u[0] + u[1]
return np.array([c*c_bar*u[1] + (c + c_bar)*u[2] + rho*np.sign(temp_s)])
# Define the controller as an input/output system
controller = ct.NonlinearIOSystem(
None, control_output, name='controller', # static system
inputs=('x1', 'x2', 'u'), # system inputs
outputs=('v') # system outputs
)
###############################################################################
# Target
###############################################################################
def target_output(t, x, u, params):
x1_ref, x2_ref ,u_ref = u
return np.array([x1_ref,x2_ref,u_ref])
# Define the trajectory generator as an input/output system
target = ct.NonlinearIOSystem(
None, target_output, name='target',
inputs=('x1_ref', 'x2_ref', 'u_ref'),
outputs=('x1_r', 'x2_r', 'u_r'))
###############################################################################
# System Connect
###############################################################################
fastest = ct.InterconnectedSystem(
# List of subsystems
(target,controller, unit_mass), name='fastest',
# Interconnections between subsystems
connections=(
('controller.x1','target.x1_r','-unit_mass.x1'),
('controller.x2','target.x2_r','-unit_mass.x2'),
('controller.u' ,'target.u_r' ,'-unit_mass.u'),
('unit_mass.v', 'controller.v')
),
# System inputs
inplist=['target.x1_ref', 'target.x2_ref', 'target.u_ref'],
inputs=['x1_ref', 'x2_ref', 'u_ref'],
# System outputs
outlist=['unit_mass.x1', 'unit_mass.x2', 'unit_mass.u','controller.v'],
outputs=['x1', 'x2', 'u','v']
)
###############################################################################
# Input Output Response
###############################################################################
# time of response
T = np.linspace(0, 8, 1000)
# the response
tout, yout = ct.input_output_response(fastest, T, [x1_ref*np.ones(len(T)),x2_ref*np.ones(len(T)),u_ref*np.ones(len(T))],X0=[x1_0,x2_0,u_0])
s = []
for i in range(len(tout)):
s.append(yout[2][i] + (c + c_bar)*yout[1][i] + c*c_bar*yout[0][i] + f(yout[0][i],yout[1][i],tout[i]))
sigma = []
sigma_d = []
for i in range(len(tout)):
sigma.append(yout[1][i] + c*yout[0][i])
sigma_d.append(yout[2][i] + c*yout[1][i] + f(yout[0][i],yout[1][i],tout[i]))
plt.figure()
plt.grid()
plt.title("Sliding Variable")
plt.xlabel("Time[s]")
plt.plot(tout,c*yout[0]+yout[1],label='sigma')
plt.plot(tout,s,label='s')
plt.legend()
plt.figure()
plt.grid()
plt.title("Asymptotic convergence for f(x,v,t)=sin(2t)")
plt.xlabel("Time(s)")
plt.plot(tout,yout[0],label='distance(m)')
plt.plot(tout,yout[1],label='velocity(m/s)')
plt.legend()
plt.title('unit mass modle(with disturbance[sin(2t)])')
plt.figure()
plt.grid()
plt.title("Phase portrait")
plt.xlabel("x1")
plt.ylabel("x2")
plt.plot(yout[0],yout[1])
plt.figure()
plt.grid()
plt.title("Phase portrait")
plt.xlabel("sigma")
plt.ylabel("sigma_d")
plt.plot(sigma,sigma_d)
plt.figure()
plt.grid()
plt.title("Sliding mode control[v]")
plt.plot(tout,yout[3])
#plt.plot(tout,v)
plt.figure()
plt.grid()
plt.title("Sliding mode control[u]")
plt.xlabel("Time[s]")
plt.plot(tout,yout[2])
plt.show()