主要是针对无人驾驶车辆模型预测控制第四章联合仿真的代码修改和调整。
Simulink
模型注解:
首先是simulink
模型的搭建框图:
代码:S-Function中MY_MPCController3的代码
Simulink
与S-Function
:
两者的关系:
S
函数使Simulink
的功能大大扩充,除matlab
外,用户还可以用其他语言(C/C++/FORTRAN/Ada
)编写实现算法,很强大的同时也对使用者提出了较高的要求。
Simulink
仿真过程:
Simulnk
仿真分为两步:初始化、仿真循环。
仿真是由求解器控制的,求解器主要作用是:计算模块输出、更新模块离散状态、计算连续状态。求解器传递给系统的信息包括:时间、输入和当前状态。
系统的作用:计算模块的输出、更新状态、计算状态导数,然后将这些信息传递给求解器。
求解器和系统之间的信息传递是通过不同标志位flag
来控制的。
S-函数
控制流:
一般而言,S-Function
一般是先flag=0
初始化,然后4-3-2
循环,flag=1,9
一般是用不上的。
S-Function
中相应的代码注解:
算法核心输出函数mdlOutput
的注解:
已知轨迹,已知车辆的模型,下面的这些量都可以计算倒推得到。
其中关于$\xi$的含义可以参考:
从对控制量的推导可以看到,控制量等于参考值+上一时刻的偏差值+增量
最后,计算输出:
对于跟踪轨迹需要注意的地方:
对于程序中参考的0.104
的解释:即当车辆要跟踪一个25m
的圆的时候,前轮偏角的参考值可以计算得到恒为:0.104rad/s
最后,可以用matlab
画出相应的轨迹:
命令为:workspace
(调出工作区中的变量)plot(x_coordinate.data,y_coordinate.data)
(画出相应的轨迹)
参考链接:
Matlab中S-函数的编写
【控制】模型预测控制 MPC 08.04 总结 修正 代码解析 CarSim Simulink联合仿真
函数代码