-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontrol.py
162 lines (137 loc) · 5.97 KB
/
control.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
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
from scipy.optimize import minimize
import copy
class Car_Dynamics:
def __init__(self,
x_0,
y_0,
v_0,
psi_0,
# length,
dt,
car_length=8,
car_width=4,
wheel_length=1.5,
wheel_width=0.7,
# [0,0] is car's center
wheel_positions=np.array(
[[2.0, 1.0], [2.0, -1.0], [-2.0, 1.0], [-2.0, -1.0]])
):
self.dt = dt # sampling time
self.L = wheel_positions[0][0] - \
wheel_positions[2][0] # while_base
self.x = x_0
self.y = y_0
self.v = v_0
self.psi = psi_0
self.state = np.array([[self.x, self.y, self.v, self.psi]]).T
self.car_length = car_length
self.car_width = car_width
self.wheel_length = wheel_length
self.wheel_width = wheel_width
self.wheel_positions = wheel_positions
self.d_front = car_length / 2 - wheel_positions[0][0]
self.d_rear = car_length / 2 - (-wheel_positions[2][0])
self.a = car_length - self.d_rear - self.d_front
self.d_l = car_width / 2 - wheel_positions[0][1]
self.d_r = car_width / 2 - (-wheel_positions[1][1])
self.b = (car_width - self.d_l - self.d_r) / 2
self.steer_max = np.deg2rad(40)
def move(self, accelerate, delta):
x_dot = self.v * np.cos(self.psi)
y_dot = self.v * np.sin(self.psi)
v_dot = accelerate
psi_dot = self.v * np.tan(delta) / self.L
return np.array([[x_dot, y_dot, v_dot, psi_dot]]).T
def update_state(self, state_dot):
# self.u_k = command
# self.z_k = state
self.state = self.state + self.dt * state_dot
self.x = self.state[0, 0]
self.y = self.state[1, 0]
self.v = self.state[2, 0]
self.psi = self.state[3, 0]
class MPC_Controller:
def __init__(self):
self.horiz = None
self.R = np.diag([0.01, 0.01]) # input cost matrix
self.Rd = np.diag([0.01, 1.0]) # input difference cost matrix
self.Q = np.diag([1.0, 1.0]) # state cost matrix
self.Qf = self.Q # state final matrix
def mpc_cost(self, u_k, my_car, points):
mpc_car = copy.copy(my_car)
u_k = u_k.reshape(self.horiz, 2).T
z_k = np.zeros((2, self.horiz + 1))
desired_state = points.T
cost = 0.0
for i in range(self.horiz):
state_dot = mpc_car.move(u_k[0, i], u_k[1, i])
mpc_car.update_state(state_dot)
z_k[:, i] = [mpc_car.x, mpc_car.y]
cost += np.sum(self.R @ (u_k[:, i]**2))
cost += np.sum(self.Q @ ((desired_state[:, i] - z_k[:, i])**2))
if i < (self.horiz - 1):
cost += np.sum(self.Rd @ ((u_k[:, i + 1] - u_k[:, i])**2))
return cost
def optimize(self, my_car, points):
self.horiz = points.shape[0]
bnd = [(-5, 5), (np.deg2rad(-60), np.deg2rad(60))] * self.horiz
result = minimize(
self.mpc_cost, args=(
my_car, points), x0=np.zeros(
(2 * self.horiz)), method='SLSQP', bounds=bnd)
return result.x[0], result.x[1]
##########################################################################
class Linear_MPC_Controller:
def __init__(self):
self.horiz = None
self.R = np.diag([0.01, 0.01]) # input cost matrix
self.Rd = np.diag([0.01, 1.0]) # input difference cost matrix
self.Q = np.diag([1.0, 1.0]) # state cost matrix
self.Qf = self.Q # state final matrix
self.dt = 0.2
self.L = 4
def make_model(self, v, psi, delta):
# matrices
# 4*4
A = np.array([[1, 0, self.dt*np.cos(psi) , -self.dt*v*np.sin(psi)],
[0, 1, self.dt*np.sin(psi) , self.dt*v*np.cos(psi) ],
[0, 0, 1 , 0 ],
[0, 0, self.dt*np.tan(delta)/self.L, 1 ]])
# 4*2
B = np.array([[0 , 0 ],
[0 , 0 ],
[self.dt, 0 ],
[0 , self.dt*v/(self.L*np.cos(delta)**2)]])
# 4*1
C = np.array([[self.dt*v* np.sin(psi)*psi ],
[-self.dt*v*np.cos(psi)*psi ],
[0 ],
[-self.dt*v*delta/(self.L*np.cos(delta)**2)]])
return A, B, C
def mpc_cost(self, u_k, my_car, points):
u_k = u_k.reshape(self.horiz, 2).T
z_k = np.zeros((2, self.horiz + 1))
desired_state = points.T
cost = 0.0
old_state = np.array(
[my_car.x, my_car.y, my_car.v, my_car.psi]).reshape(4, 1)
for i in range(self.horiz):
delta = u_k[1, i]
A, B, C = self.make_model(my_car.v, my_car.psi, delta)
new_state = A @ old_state + B @ u_k + C
z_k[:, i] = [new_state[0, 0], new_state[1, 0]]
cost += np.sum(self.R @ (u_k[:, i]**2))
cost += np.sum(self.Q @ ((desired_state[:, i] - z_k[:, i])**2))
if i < (self.horiz - 1):
cost += np.sum(self.Rd @ ((u_k[:, i + 1] - u_k[:, i])**2))
old_state = new_state
return cost
def optimize(self, my_car, points):
self.horiz = points.shape[0]
bnd = [(-5, 5), (np.deg2rad(-60), np.deg2rad(60))] * self.horiz
result = minimize(
self.mpc_cost, args=(
my_car, points), x0=np.zeros(
(2 * self.horiz)), method='SLSQP', bounds=bnd)
return result.x[0], result.x[1]