自动驾驶的模型预测控制

我们实施了一个模型预测控制来驱动赛道上的赛车。但是这一次我们没有交叉错误,我们必须自己计算。另外,在连接延迟之上的执行命令之间有100毫秒的延迟。

这篇文章从非常简单的P,PD和PID实现开始,到一个复杂的模型预测控制:

用Python实现的P,PD和PID控制器

模型预测控制在C ++中实现

用Python实现的P,PD和PID控制器 比例积分微分控制器(PID控制器或三项控制器)是广泛用于工业控制系统和各种其他需要连续调节控制的应用的控制回路反馈机构。一个PID控制器连续计算一个误差值e(t),作为所需设定值(SP)和测量的过程变量(PV)之间的差值,并根据比例,积分和微分项(表示为P,I和D)分别给他们的名字控制器。实际上,它会自动对控制功能应用准确和响应的校正。

P-控制器的实现

# -----------# User Instructions## Implement a P controller by running 100 iterations# of robot motion. The desired trajectory for the# robot is the x-axis. The steering angle should be set# by the parameter tau so that:## steering = -tau * crosstrack_error## You'll only need to modify the `run` function at the bottom.# ------------importrandomimportnumpyasnpimportmatplotlib.pyplotasplt# ------------------------------------------------## this is the Robot class#classRobot(object):def__init__(self, length=20.0):"""

Creates robot and initializes location/orientation to 0, 0, 0.

"""self.x =0.0self.y =0.0self.orientation =0.0self.length = length        self.steering_noise =0.0self.distance_noise =0.0self.steering_drift =0.0defset(self, x, y, orientation):"""

Sets a robot coordinate.

"""self.x = x        self.y = y        self.orientation = orientation % (2.0* np.pi)defset_noise(self, steering_noise, distance_noise):"""

Sets the noise parameters.

"""# makes it possible to change the noise parameters# this is often useful in particle filtersself.steering_noise = steering_noise        self.distance_noise = distance_noisedefset_steering_drift(self, drift):"""

Sets the systematical steering drift parameter

"""self.steering_drift = driftdefmove(self, steering, distance, tolerance=0.001, max_steering_angle=np.pi /4.0):"""

steering = front wheel steering angle, limited by max_steering_angle

distance = total distance driven, most be non-negative

"""ifsteering > max_steering_angle:            steering = max_steering_angleifsteering < -max_steering_angle:            steering = -max_steering_angleifdistance <0.0:            distance =0.0# apply noisesteering2 = random.gauss(steering, self.steering_noise)        distance2 = random.gauss(distance, self.distance_noise)# apply steering driftsteering2 += self.steering_drift# Execute motionturn = np.tan(steering2) * distance2 / self.lengthifabs(turn) < tolerance:# approximate by straight line motionself.x += distance2 * np.cos(self.orientation)            self.y += distance2 * np.sin(self.orientation)            self.orientation = (self.orientation + turn) % (2.0* np.pi)else:# approximate bicycle model for motionradius = distance2 / turn            cx = self.x - (np.sin(self.orientation) * radius)            cy = self.y + (np.cos(self.orientation) * radius)            self.orientation = (self.orientation + turn) % (2.0* np.pi)            self.x = cx + (np.sin(self.orientation) * radius)            self.y = cy - (np.cos(self.orientation) * radius)def__repr__(self):return'[x=%.5f y=%.5f orient=%.5f]'% (self.x, self.y, self.orientation)############## ADD / MODIFY CODE BELOW ##################### ------------------------------------------------------------------------## run - does a single control runrobot = Robot()robot.set(0,1,0)defrun(robot, tau, n=100, speed=1.0):x_trajectory = []    y_trajectory = []#TODO:your code heresteering =0.0foriinrange(n):        cte = robot.y        steer = -tau * cte        robot.move(steer, speed)        x_trajectory.append(robot.x)        y_trajectory.append(robot.y)returnx_trajectory, y_trajectory    x_trajectory, y_trajectory = run(robot,0.1)n = len(x_trajectory)fig, (ax1, ax2) = plt.subplots(2,1, figsize=(8,8))ax1.plot(x_trajectory, y_trajectory,'g', label='P controller')ax1.plot(x_trajectory, np.zeros(n),'r', label='reference')

PD控制器的实现

PD控制器与P控制器非常相似。增加了prev_cte分配给前一个CTE 的变量,以及diff_cte当前CTE和前一个CTE之间的差值。然后我们把它们和新的tau_d参数放在一起来计算新的转向值-tau_p * cte - tau_d * diff_cte。

# -----------# User Instructions## Implement a PD controller by running 100 iterations# of robot motion. The steering angle should be set# by the parameter tau_p and tau_d so that:## steering = -tau_p * CTE - tau_d * diff_CTE# where differential crosstrack error (diff_CTE)# is given by CTE(t) - CTE(t-1)### Only modify code at the bottom! Look for the TODO# ------------importrandomimportnumpyasnpimportmatplotlib.pyplotasplt# ------------------------------------------------## this is the Robot class#classRobot(object):def__init__(self, length=20.0):"""

Creates robot and initializes location/orientation to 0, 0, 0.

"""self.x =0.0self.y =0.0self.orientation =0.0self.length = length        self.steering_noise =0.0self.distance_noise =0.0self.steering_drift =0.0defset(self, x, y, orientation):"""

Sets a robot coordinate.

"""self.x = x        self.y = y        self.orientation = orientation % (2.0* np.pi)defset_noise(self, steering_noise, distance_noise):"""

Sets the noise parameters.

"""# makes it possible to change the noise parameters# this is often useful in particle filtersself.steering_noise = steering_noise        self.distance_noise = distance_noisedefset_steering_drift(self, drift):"""

Sets the systematical steering drift parameter

"""self.steering_drift = driftdefmove(self, steering, distance, tolerance=0.001, max_steering_angle=np.pi /4.0):"""

steering = front wheel steering angle, limited by max_steering_angle

distance = total distance driven, most be non-negative

"""ifsteering > max_steering_angle:            steering = max_steering_angleifsteering < -max_steering_angle:            steering = -max_steering_angleifdistance <0.0:            distance =0.0# apply noisesteering2 = random.gauss(steering, self.steering_noise)        distance2 = random.gauss(distance, self.distance_noise)# apply steering driftsteering2 += self.steering_drift# Execute motionturn = np.tan(steering2) * distance2 / self.lengthifabs(turn) < tolerance:# approximate by straight line motionself.x += distance2 * np.cos(self.orientation)            self.y += distance2 * np.sin(self.orientation)            self.orientation = (self.orientation + turn) % (2.0* np.pi)else:# approximate bicycle model for motionradius = distance2 / turn            cx = self.x - (np.sin(self.orientation) * radius)            cy = self.y + (np.cos(self.orientation) * radius)            self.orientation = (self.orientation + turn) % (2.0* np.pi)            self.x = cx + (np.sin(self.orientation) * radius)            self.y = cy - (np.cos(self.orientation) * radius)def__repr__(self):return'[x=%.5f y=%.5f orient=%.5f]'% (self.x, self.y, self.orientation)############## ADD / MODIFY CODE BELOW ##################### ------------------------------------------------------------------------## run - does a single control run# previous P controllerdefrun_p(robot, tau, n=100, speed=1.0):x_trajectory = []    y_trajectory = []foriinrange(n):        cte = robot.y        steer = -tau * cte        robot.move(steer, speed)        x_trajectory.append(robot.x)        y_trajectory.append(robot.y)returnx_trajectory, y_trajectory    robot = Robot()robot.set(0,1,0)defrun(robot, tau_p, tau_d, n=100, speed=1.0):x_trajectory = []    y_trajectory = []#TODO:your code hereCTE_prev =0foriinrange(n):        CTE = robot.y        diff_CTE = CTE - CTE_prev        CTE_prev = CTE        steering = -tau_p * CTE - tau_d * diff_CTE        robot.move(steering, speed)        x_trajectory.append(robot.x)        y_trajectory.append(robot.y)returnx_trajectory, y_trajectory    x_trajectory, y_trajectory = run(robot,0.2,3.0)n = len(x_trajectory)fig, (ax1, ax2) = plt.subplots(2,1, figsize=(8,8))ax1.plot(x_trajectory, y_trajectory,'g', label='PD controller')ax1.plot(x_trajectory, np.zeros(n),'r', label='reference')

PID控制器的实现

随着积分项我们跟踪所有以前的CTE,最初我们设置int_cte为0,然后将当前cte项添加到计数int_cte += cte。最后,我们-tau_p * cte - tau_d * diff_cte - tau_i * int_cte用新tau_i参数更新转向值。

# -----------# User Instructions## Implement a P controller by running 100 iterations# of robot motion. The steering angle should be set# by the parameter tau so that:## steering = -tau_p * CTE - tau_d * diff_CTE - tau_i * int_CTE## where the integrated crosstrack error (int_CTE) is# the sum of all the previous crosstrack errors.# This term works to cancel out steering drift.## Only modify code at the bottom! Look for the TODO.# ------------importrandomimportnumpyasnpimportmatplotlib.pyplotasplt# ------------------------------------------------## this is the Robot class#classRobot(object):def__init__(self, length=20.0):"""

Creates robot and initializes location/orientation to 0, 0, 0.

"""self.x =0.0self.y =0.0self.orientation =0.0self.length = length        self.steering_noise =0.0self.distance_noise =0.0self.steering_drift =0.0defset(self, x, y, orientation):"""

Sets a robot coordinate.

"""self.x = x        self.y = y        self.orientation = orientation % (2.0* np.pi)defset_noise(self, steering_noise, distance_noise):"""

Sets the noise parameters.

"""# makes it possible to change the noise parameters# this is often useful in particle filtersself.steering_noise = steering_noise        self.distance_noise = distance_noisedefset_steering_drift(self, drift):"""

Sets the systematical steering drift parameter

"""self.steering_drift = driftdefmove(self, steering, distance, tolerance=0.001, max_steering_angle=np.pi /4.0):"""

steering = front wheel steering angle, limited by max_steering_angle

distance = total distance driven, most be non-negative

"""ifsteering > max_steering_angle:            steering = max_steering_angleifsteering < -max_steering_angle:            steering = -max_steering_angleifdistance <0.0:            distance =0.0# apply noisesteering2 = random.gauss(steering, self.steering_noise)        distance2 = random.gauss(distance, self.distance_noise)# apply steering driftsteering2 += self.steering_drift# Execute motionturn = np.tan(steering2) * distance2 / self.lengthifabs(turn) < tolerance:# approximate by straight line motionself.x += distance2 * np.cos(self.orientation)            self.y += distance2 * np.sin(self.orientation)            self.orientation = (self.orientation + turn) % (2.0* np.pi)else:# approximate bicycle model for motionradius = distance2 / turn            cx = self.x - (np.sin(self.orientation) * radius)            cy = self.y + (np.cos(self.orientation) * radius)            self.orientation = (self.orientation + turn) % (2.0* np.pi)            self.x = cx + (np.sin(self.orientation) * radius)            self.y = cy - (np.cos(self.orientation) * radius)def__repr__(self):return'[x=%.5f y=%.5f orient=%.5f]'% (self.x, self.y, self.orientation)############## ADD / MODIFY CODE BELOW ##################### ------------------------------------------------------------------------## run - does a single control runrobot = Robot()robot.set(0,1,0)defrun(robot, tau_p, tau_d, tau_i, n=100, speed=1.0):x_trajectory = []    y_trajectory = []#TODO:your code hereCTE_prev =0int_CTE =0foriinrange(n):        CTE = robot.y        diff_CTE = CTE - CTE_prev        CTE_prev = CTE        int_CTE += CTE        steering = -tau_p * CTE - tau_d * diff_CTE - tau_i * int_CTE        robot.move(steering, speed)        x_trajectory.append(robot.x)        y_trajectory.append(robot.y)returnx_trajectory, y_trajectoryx_trajectory, y_trajectory = run(robot,0.2,3.0,0.004)n = len(x_trajectory)fig, (ax1, ax2) = plt.subplots(2,1, figsize=(8,8))ax1.plot(x_trajectory, y_trajectory,'g', label='PID controller')ax1.plot(x_trajectory, np.zeros(n),'r', label='reference')

这似乎并不令人印象深刻。PID似乎比PD控制器差。I期的目的是弥补偏差,而目前的机器人没有偏见。

模型预测控制重构了追踪轨迹作为优化问题的任务。优化问题的解决方案是最佳的轨迹。模型预测控制涉及模拟不同的执行器输入,预测最终的轨迹并以最小的成本选择该轨迹。当前状态和参考轨迹是已知的。在每个时间步骤中,致动器输入被优化以便最小化预测轨迹的成本。一旦找到最低成本的轨迹,执行第一组致动器命令,其余部分被丢弃,并且在计算新的最佳轨迹时在每个时间步重复。

成本函数的一个好的开始是想想我们想要最小化的错误。例如,测量从车道中心的偏移量,其中车道中心可以称为参考或期望的状态。同样,如果目标是在两个地点之间移动,我们想惩罚缓慢或停止的汽车。另外,我们希望汽车尽可能平稳地改变车道。

doublecost =0;for(intt =0; t < N; t++) {    cost +=pow(cte[t],2);    cost +=pow(epsi[t],2);}

模型预测控制使用优化器来查找控制输入并最小化成本函数。我们实际上执行第一组控制输入,使车辆进入新的状态,然后重复这个过程。

在下图中,通过执行第一步(第一个控制输入)将汽车移动到下一个黄点。之后,包括新点在内产生一个全新的轨迹。

首先,我们设置模型预测控制回路所需的一切。这包括通过选择N和dt来定义轨迹的持续时间T。接下来,我们定义车辆模型和限制,如实际限制。最后,我们定义成本函数。

随着模型设置完成,我们开始状态反馈回路。首先,我们将当前状态传递给模型预测控制器。

接下来,调用优化求解器。求解器使用初始状态,模型约束和成本函数来返回使成本函数最小化的控制输入向量。

使用的求解器是IPOPT。

我们首先将第一个控制输入应用于车辆并重复循环。

在真实的汽车中,执行命令不会立即执行 - 命令在系统中传播时会有延迟。实际的延迟可能在100毫秒左右。

这是一个叫做“等待时间”的问题,对于某些控制器(比如PID控制器)来说,这是一个很难克服的挑战。但是模型预测控制器可以很好地适应,因为我们可以在系统中对这个延迟进行建模。

PID控制器将计算相对于当前状态的误差,但是当车辆处于未来(并且可能不同)状态时将执行启动。这有时会导致不稳定。

PID控制器可以尝试基于未来的误差来计算控制输入,但是如果没有车辆模型,则这不太可能是准确的。

致动器动力学是导致等待时间的一个因素 例如,从命令转向角度到实际达到角度的时间间隔。这可以很容易地通过一个简单的动态系统建模,并纳入车辆模型。一种方法是使用从当前状态开始的车辆模型在等待时间期间进行模拟。从模拟得到的状态是MPC的新的初始状态。

因此,与PID控制器相比,MPC可以更有效地处理延迟,通过明确考虑延迟。

完整的源代码可以在公众号里留言找到。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容