-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCruiseControl.py
150 lines (109 loc) · 3.85 KB
/
CruiseControl.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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 30 17:10:05 2016
Cruise control model (elaborated from 'Feedback systems' by Astrom)
@author: manuelbaltieri
"""
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
dt = .02
T = 200
iterations = int(T/dt)
variables = 1
temp_orders = 2
# environment parameters
g = 9.81 # gravitational acceleration
theta = 4 # hill angle
C_r = .01 # rolling friction coefficient
C_d = .32 # drag coefficient
rho = 1.3 # air density
A = 2.4 # frontal area agent
# agent's parameters
m = 100 # car mass
T_m = 190 # maximum torque
omega_m = 420 # engine speed to reach T_m
alpha_n = 12 # = gear ration/wheel radius, a1 = 40, a2 = 25, a3 = 16, a4 = 12, a5 = 10
beta = .4
x = np.zeros((variables,temp_orders)) # hidden state (made observable to simplify things, y = x), x[0]=v x[1]=v_dot
y = np.zeros((variables,temp_orders)) # measured state
u = 0 # input
v = np.zeros((variables,temp_orders)) # z[0]: error, z[1]: integral of the error
n = np.exp(-3/2) * np.random.randn(iterations,temp_orders+1)
#n[:,1] *= np.sqrt(2)
k_p = 4
k_i = 7
k_i = np.exp(-4)
k_p = k_i/2
v_ref = 10
ext_input = np.zeros((1, iterations))
# history
y_history = np.zeros((iterations,variables,temp_orders))
x_history = np.zeros((iterations,variables,temp_orders))
u_history = np.zeros((iterations,1))
a_history = np.zeros((iterations,1))
v_history = np.zeros((iterations,variables,temp_orders))
v_ref_history = v_ref*np.ones((iterations))
# functions
def sigmoid(x):
return np.tanh(x)
return 1 / (1+np.exp(-x))
def force_gravitation(theta):
return m*g*np.sin(theta)
def force_friction(v):
return m*g*C_r*np.sign(v)
def force_drag(v):
return .5*rho*C_d*A*v**2
def force_disturbance(v, theta):
return force_gravitation(theta) + force_friction(v) + force_drag(v)
def force_drive(v,u):
return alpha_n*u*torque(v)
def torque(v):
return T_m*(1 - beta*(omega(v)/omega_m)**2)
def omega(v):
return alpha_n*v
a = 0
for i in range(iterations-1):
print(i)
# dn2 = - 1.0 * n[i, 1] + n[i, 2] / np.sqrt(dt)
# n[i+1, 1] = n[i, 1] + dt * dn2
# dn = - 1.0 * n[i, 0] + n[i, 1]
# n[i+1, 0] = n[i, 0] + dt * dn
# v[0,1] = y[0,0] - v_ref
# v[0,0] += dt*v[0,1]
#
# u = k_p*v[0,1] + k_i*v[0,0]
# ext_input[0,i] = .01*np.exp((i+iterations/2)*dt**1.3)
x[0,1] = (force_drive(x[0,0],-a) - force_disturbance(x[0,0],theta))/m
# if (i>iterations/4) and (i<iterations/2):
# x[0,1] = (force_drive(x[0,0],-u) - force_disturbance(x[0,0],theta))/m + n[0,i] + ext_input[0,i]
# else:
# x[0,1] = (force_drive(x[0,0],-u) - force_disturbance(x[0,0],theta))/m + n[0,i]
x[0,0] += dt*x[0,1]
y = x + n[i,:-1]
# v[0,1] = y[0,1] - 0.0
# v[0,0] = y[0,0] - v_ref
v[0,1] = x[0,1] + n[i, 1] / np.sqrt(dt) - 0.0
v[0,0] = x[0,0] + n[i, 0] / np.sqrt(dt) - v_ref
u += dt*(k_p*v[0,1] + k_i*v[0,0])
a = sigmoid(u)
# a = u
# save data
y_history[i,:,:] = y
x_history[i,:,:] = x
u_history[i] = u
a_history[i] = a
v_history[i,:,:] = v
plt.figure()
plt.plot(np.arange(0, T-dt, dt), u_history[:-1,0], 'b')
plt.title('Control')
plt.figure()
plt.plot(np.arange(0, T-dt, dt), y_history[:-1,0,0], 'b')
plt.title('Velocity')
plt.figure()
plt.plot(np.arange(0, T-dt, dt), y_history[:-1,0,1], 'b')
plt.title('Acceleration')
plt.figure()
plt.plot(np.arange(0, T-dt, dt), a_history[:-1], 'b')
plt.title('Action')
print(np.var(y_history[int(T/2/dt):-1,0,0]))