Skip to content
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
20e276a
更新代码
183899 Dec 1, 2025
91bf847
Merge branch 'OpenHUTB:main' into main
183899 Dec 1, 2025
08bccf0
Merge branch 'OpenHUTB:main' into main
183899 Dec 8, 2025
4a2ccc8
将原模块名更改为更加详细的名字
183899 Dec 8, 2025
da1e375
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
1678aeb
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
2b3e4b2
更新了控制器功能,增添了速度显示效果
183899 Dec 15, 2025
10f7798
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
1f64807
补充并在原有的 drawer.py 中添加了 draw_point 方法
183899 Dec 16, 2025
b21c1f2
Merge branch 'OpenHUTB:main' into main
183899 Dec 16, 2025
cebf093
Merge branch 'OpenHUTB:main' into main
183899 Dec 18, 2025
417561d
添加新的转弯感知模块,修改main函数实现转弯时自主检测刹车
183899 Dec 18, 2025
ebbb588
Merge branch 'OpenHUTB:main' into main
183899 Dec 19, 2025
532b4ab
新增预测功能,提前预测转弯效果
183899 Dec 19, 2025
85832ac
更新了REDME.py文件
183899 Dec 20, 2025
05fd4e2
Merge branch 'OpenHUTB:main' into main
183899 Dec 20, 2025
26c0d0f
Merge branch 'OpenHUTB:main' into main
183899 Dec 21, 2025
94c1049
修改了config.py,解决了原先小车转弯半径过小导致的撞墙情况。
183899 Dec 21, 2025
3c2716d
Merge branch 'OpenHUTB:main' into main
183899 Dec 21, 2025
c9d9d49
新增测评功能,将车辆状态实时显示。
183899 Dec 21, 2025
94c1f2e
Merge branch 'OpenHUTB:main' into main
183899 Dec 22, 2025
57d483a
修改了函数代码,添加了航点导航功能
183899 Dec 22, 2025
9074aff
Merge branch 'OpenHUTB:main' into main
183899 Dec 22, 2025
17d53a4
根据现有代码修改REDME文件。
183899 Dec 22, 2025
77a83e0
Merge branch 'OpenHUTB:main' into main
183899 Dec 22, 2025
ba176f7
Merge branch 'OpenHUTB:main' into main
183899 Dec 24, 2025
485447c
修改函数代码,增添评价功能
183899 Dec 24, 2025
ee43661
Merge branch 'OpenHUTB:main' into main
183899 Dec 25, 2025
e8f5637
根据现有代码更新REDME文件
183899 Dec 25, 2025
fa4e0fe
Merge branch 'OpenHUTB:main' into main
183899 Dec 28, 2025
fc5bd77
新增一个独立的MPC代码,实现更详细的控制
183899 Dec 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/unmannedcar_MPC/mpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import math
import numpy as np
import utils
from scipy.optimize import minimize


class MPC:
HORIZON = 10
L = 2.5

def __init__(self, drawer, ego):
self.drawer = drawer
self.ego = ego

def run_step(self, path, speed_m_s, dt):
# convert path to body frame
path = utils.to_body_frame(self.ego, path)

# fit the path with a polynomial
poly = np.polyfit(path[:, 0], path[:, 1], 3)

# generate lane center locations
self.speed_dt = speed_m_s * dt

x_arr = np.arange(1, self.HORIZON + 1) * self.speed_dt
self.locs = np.vstack((x_arr, np.polyval(poly, x_arr))).T

# mpc
bounds = np.full((self.HORIZON, 2), (-0.3, 0.3))
init_steer_arr = np.full(self.HORIZON, 0)
solution = minimize(self.objective, init_steer_arr, (), method='SLSQP', bounds=bounds, tol=1e-4)
eval_states = self.evaluate_states(solution.x)

# draw lines
self.drawer.draw_camera_lines((255, 255, 255), utils.to_global_frame(self.ego, self.locs), 1)
self.drawer.draw_camera_lines((255, 0, 0), utils.to_global_frame(self.ego, np.array(eval_states)[:, 0:2]), 1)

return solution.x[0]

def mpc_model(self, state, steering):
x_t = state[0]
y_t = state[1]
psi_t = state[2]

x_t_1 = x_t + self.speed_dt * np.cos(psi_t)
y_t_1 = y_t + self.speed_dt * np.sin(psi_t)
psi_t_1 = psi_t + self.speed_dt * np.tan(steering) / self.L

return [x_t_1, y_t_1, psi_t_1]

def evaluate_states(self, steer_arr):
states = []
state = [0, 0, 0]
for steer in steer_arr:
state = self.mpc_model(state, steer)
states.append(state)
return states

def objective(self, steer_arr, *args):
final_state = self.evaluate_states(steer_arr)[-1]
final_loc = self.locs[-1]
return math.sqrt((final_state[0] - final_loc[0]) ** 2 + (final_state[1] - final_loc[1]) ** 2)
Loading