This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPCMain.py
More file actions
218 lines (197 loc) · 7.12 KB
/
MPCMain.py
File metadata and controls
218 lines (197 loc) · 7.12 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
###
import math as mat
import numpy as np
from numpy.linalg import norm
##Cost function
class Order:
def __init__(self):
self.X = int(0)
self.Y = int(1)
self.Vx = int(2)
self.Vy = int(3)
self.Theta = int(4)
self.Throttle = int(0)
self.Steering = int(1)
class MPC(Order):
def __init__(
self, Rmax, Initial_Position, Optimal_path, Path_center, Weights, Time_Delta,
):
super().__init__()
self.Vehicle_Total_Length = 1.535
self.Vehicle_Rear_Length = 0.7675
self.Horizon = 2
self.slip_angle = np.array([])
self.Control_Command = np.array([])
self.Rotational_Speed = np.array([])
self.Time_Delta = Time_Delta
self.Target_Path = np.array([])
self.Orthogonal_error = np.array([])
self.Optimal_path = Optimal_path
self.State = Initial_Position
self.Predicted_State = np.empty([5, self.Horizon])
self.Tanget_Angle = Optimal_path
self.Weights = Weights
self.Path_center = Path_center
self.Rmax = Rmax
self.Cost = 0
@property
def State(self):
return self.__State
@property
def Tanget_Angle(self):
return self.__Tanget_Angle
@property
def Target_Path(self):
return self.__Target_Path
@State.setter
def State(self, State):
self.__State = State
@Target_Path.setter
def Target_Path(self, Target_Path):
self.__Target_Path = Target_Path
@Tanget_Angle.setter
def Tanget_Angle(self, Predicted_path):
Tanget_Angle = np.zeros([self.Horizon, 1])
Tanget_Angle[0] = mat.atan2(
Predicted_path[0, 1] - self.State[self.Y],
Predicted_path[0, 0] - self.State[self.X],
)
for i in range(1, self.Horizon):
Tanget_Angle[i] = mat.pi - mat.atan2(
Predicted_path[1, i] - Predicted_path[1, i - 1],
Predicted_path[0, i] - Predicted_path[0, i - 1],
)
self.__Tanget_Angle = Tanget_Angle
def Kinematic_Model(self, Control_Command):
# Calculate the absolute velocity
V_abs = norm(self.State[[self.Vx, self.Vy]])
# Calculate The slip angle maybe take it from state!!!
self.Slip_angle = mat.atan2(
mat.tan(Control_Command[self.Steering]) * self.Vehicle_Rear_Length,
self.Vehicle_Total_Length,
)
# Calculate the rotational speed maybe take it from state!!!
self.Rotational_Speed = (
V_abs
* mat.cos(self.Slip_angle)
* mat.tan(Control_Command[self.Steering])
/ self.Vehicle_Total_Length
)
# Calculate the process advancement
advancement = np.array(
[
[
self.Time_Delta * self.State[self.Vx]
+ (self.Time_Delta ** 2)
/ 2
* mat.cos(self.State[self.Theta] + self.Slip_angle)
* Control_Command[self.Throttle]
],
[
self.Time_Delta * self.State[self.Vy]
+ (self.Time_Delta ** 2)
/ 2
* mat.sin(self.State[self.Theta] + self.Slip_angle)
* Control_Command[self.Throttle]
],
[
self.Time_Delta
* mat.cos(self.State[self.Steering] + self.Slip_angle)
* Control_Command[self.Throttle]
],
[
self.Time_Delta
* mat.sin(self.State[self.Theta] + self.Slip_angle)
* Control_Command[self.Throttle]
],
[self.Time_Delta * self.Rotational_Speed],
],
dtype="float",
)
# Update the state
self.State = self.State + advancement
def Calculate_Target(self, Control_Command):
Ec = 0.0
El = 0.0
Du = 0.0
self.Control_Command = Control_Command.reshape(
[int(len(Control_Command) / 2), 2]
)
for i in range(self.Horizon):
# TODO: change angle calculation to polynomial dervitive from future point
Ec += self.Weights[0] * (
mat.sin(self.Tanget_Angle[i])
* (self.Predicted_State[self.X, i] - self.Optimal_path[self.X, i])
- mat.cos(self.Tanget_Angle[i])
* (self.Predicted_State[self.Y, i] - self.Optimal_path[self.Y, i])
)
El += self.Weights[1] * (
-mat.cos(self.Tanget_Angle[i])
* (self.Predicted_State[self.X, i] - self.Optimal_path[self.X, i])
- mat.sin(self.Tanget_Angle[i])
* (self.Predicted_State[self.Y, i] - self.Optimal_path[self.Y, i])
)
Du += self.Weights[4] * (
self.Control_Command[i + 1, 0] - self.Control_Command[i, 0]
) + self.Weights[5] * (
self.Control_Command[i + 1, 1] - self.Control_Command[i, 1]
)
self.Cost = (
Ec ** 2
+ El ** 2
+ Du ** 2
+ self.Weights[3]
* np.dot(self.Control_Command[:, 0], self.Control_Command[:, 0])
+ self.Weights[4]
* np.dot(self.Control_Command[:, 0], self.Control_Command[:, 1])
)
return self.Cost
def Constraint(self, Control_Command):
self.Control_Command = Control_Command.reshape(
[int(len(Control_Command) / 2), 2]
)
self.State = np.zeros([5, 1])
for i in range(self.Horizon):
self.Kinematic_Model(self.Control_Command[i, :])
self.Predicted_State[:, i : i + 1] = self.State
for i in range(self.Horizon):
if (
((self.Predicted_State[self.X, i] - self.Path_center[self.X, i]) ** 2)
+ (self.Predicted_State[self.Y, i] - self.Path_center[self.Y, i]) ** 2
) <= self.Rmax ** 2:
return 0
return 1
def optimize(self, Initial_state):
# TODO: call engine for optimize. Initial_state from state estimation return optimal control vector.
pass
##RunningFunciton
# Rmax = 0.5
# Initial_Position = np.zeros([5, 1])
# # Optimal_path = ......!!
# # For first running we will check Path_center = Optimal_path
# Optimal_path = np.array([[5, 10, 15, 20], [5, 10, 15, 20]])
# Path_center = Optimal_path
# Weights = np.ones([6, 1])
# Time_Delta = 1
# ##Genetic initialization
# Number_of_Candidate = 10
# # control efforts
# Val_max = np.array([4, mat.pi / 4, 4, mat.pi / 4, 4, mat.pi / 4])
# val_min = np.array([-2, -mat.pi / 4, -2, -mat.pi / 4, -2, -mat.pi / 4])
# resulotion = 0.01
# # control arguments gas and steering
# contorlarguments = 6
# K = DNA(100, np.array([4, 4]), np.array([-2, -2]), 0.1, 2)
# K.Calculate_NumberofBits()
# K.initial_Parent_List()
# K.Initialize_Population()
##perpendicular distance from the poly
##newton optimization
##optimal path function
##constraints
## initial condition
##kinmatic model
##track bounds
##saturation constraints
##dynamic model
##slip constrints