Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

创建简单模型

您可以使用 Simulink® 来对系统建模,然后仿真该系统的动态行为。本教程中创建简单模型所使用的基本方法也适用于创建更复杂的模型。此示例对简化的汽车运动进行仿真。当踩下油门踏板时,汽车通常处于行进状态。松开踏板后,汽车怠速并停下来。

Simulink 模块是定义模块输入和模块输出之间数学关系的模型元素。要创建这个简单模型,您需要四个 Simulink 模块。

模块名称模块目的模型目的
Pulse Generator为模型生成输入信号表示加速踏板
Gain将输入信号乘以常量值计算踩下加速踏板后如何影响汽车的加速度
Second-Order Integrator将输入信号积分两次根据加速度计算汽车位置
Outport指定一个信号作为模型的输出指定汽车位置做为模型的输出

A Simulink model with Pulse Generator, Gain, Second-Order Integrator block, and two Outport blocks.

此模型的仿真过程是将一个简短的脉冲信号积分两次,形成一个斜坡。结果显示在一个示波器窗口中。输入脉冲表示是否踩下油门踏板 - 1 表示踩下,0 表示未踩下。输出斜坡表示与起点之间的距离增加。

打开新模型

使用 Simulink 编辑器构建模型。

  1. 启动 MATLAB®。从 MATLAB 工具条中,点击 Simulink

    The Simulink start page has two tabs, New and Examples, from which you can open a new template or an example.

  2. 点击空白模型模板。

    Simulink 编辑器打开。

    为了避免遮蔽,Simulink 编辑器检查路径中加载的模型和文件,并使用下一个可用名称 untitleduntitled1untitled2 等创建一个模型。

    Empty Simulink Editor

  3. 仿真选项卡中,选择保存 > 另存为。在文件名文本框中,输入您的模型的名称。例如,simple_model。点击保存。模型使用文件扩展名 .slx 进行保存。

打开 Simulink库浏览器

Simulink 在库浏览器中提供了一系列按功能分类的模块库。下面是大多数工作流常用的一些模块库:

  • Continuous - 表示具有连续状态的系统的模块

  • Discrete - 表示具有离散状态的系统的模块

  • Math Operations - 实现代数和逻辑方程的模块

  • Sinks - 存储并显示所连接信号的模块

  • Sources - 生成模型的驱动信号值的模块

要打开库浏览器,请在 Simulink 工具条的仿真标签页上,点击库浏览器

Library Browser

要浏览模块库,请在库树中展开一个类别,然后展开一个功能区。

要搜索所有可用的模块库,请输入搜索词。

例如,查找 Pulse Generator 模块。在搜索框中,输入 pulse,然后按 Enter 键。软件在库中搜索名称或描述中包含 pulse 的模块,然后在库浏览器的搜索结果标签页上显示这些模块。

提示

您可以通过点击库选项卡返回浏览库树。

The Simulink Library Browser displays the results for the search term pulse, which include Pulse Generator and Continuous Pulse Generator blocks.

获取模块的详细信息。在搜索结果选项卡上,右键点击 Pulse Generator 模块,然后选择 Pulse Generator 模块的帮助。帮助浏览器随即打开并显示该模块的参考页。

模块通常有几个参数。您可以通过双击该模块来访问所有模块参数。

将模块添加到模型

要开始构建模型,请浏览库并添加模块。

  1. Sources 库中,将 Pulse Generator 模块拖到 Simulink 编辑器中。您的模型中将出现 Pulse Generator 模块的副本,还有一个文本框用于输入振幅参数的值。输入 1

    The Pulse Generator block with a value of 1 entered in the text box prompt to specify the Amplitude parameter.

    参数值在整个仿真过程中保持不变。

  2. 使用相同的方法将这些模块添加到模型中。

    模块参数
    GainSimulink/Math Operations

    增益: 2

    Second-Order IntegratorSimulink/Continuous

    初始条件: 0

    OutportSimulink/Sinks

    端口号: 1

    复制现有 Outport 模块,然后使用键盘快捷方式将其粘贴到另一个点,从而添加第二个 Outport 模块。

    您的模型现在已经包含您需要的模块。

  3. 通过点击并拖动每个模块来排列模块。要调整模块大小,请拖动一个角。

    Arranged blocks from left to right.

连接模块

通过在输出端口和输入端口之间创建线条来连接模块。

  1. 点击 Pulse Generator 模块右侧的输出端口。

    该输出端口和所有适合连接的输入端口由蓝色向前符号 port hint symbol 指示。

    Five unconnected blocks include, from left to right, a Pulse Generator block, a Gain block, a Second Order Integrator block, and two Outport blocks. The output port of the Pulse Generator block and the input ports of the other blocks display blue chevron port hint symbols.

  2. 指向 port hint symbol 以查看连接提示。

    A connection cue is displayed between the Pulse Generator block and the Gain block.

    点击提示,用一个线条和一个指示信号流动方向的箭头连接这些模块。

    An arrow represents the connection between the Pulse Generator block and the Gain block.

  3. Gain block 的输出端口连接到 Second-Order Integrator 模块的输入端口。

  4. Second-Order Integrator 模块的两个输出连接到两个 Outport 模块。

  5. 保存模型。在仿真选项卡中,点击保存

    All the blocks are connected.

添加信号查看器

要查看仿真结果,请将第一个输出连接到一个 Signal Viewer

点击信号。在仿真选项卡中的准备下,点击添加查看器。选择 Scope。信号上会出现查看器图标,并打开一个示波器窗口。

A viewer icon appears on the signal between the Second-Order Integrator block and Outport block for output 1.

您可以随时通过双击该图标打开示波器。

运行仿真

指定仿真的停止时间。然后,对模型进行仿真。

  1. 仿真选项卡上,设置仿真停止时间。在 Simulink 工具条的仿真选项卡上,在停止时间字段中输入值。

    Simulation stop time is displayed on the Simulation tab of the toolstrip

    默认停止时间 10.0 适合此模型。此时间值没有单位。Simulink 中的时间单位取决于方程的构造方式。此示例对简化的汽车运动进行 10 秒的仿真 - 其他模型的时间单位可以是毫秒或年。

  2. 要运行仿真,请点击运行

仿真开始运行并在波形查看器中生成输出。

A scope viewer shows the output of the simulation.

细化模型

更改模块参数

此示例使用现有模型 moving_car.slx,并基于此运动模型对接近传感器建模。

在这种情况下,数字传感器用于测量汽车与 10 米(30 英尺)外的障碍物之间的距离。模型基于下列条件来输出传感器的测量值和汽车的位置值:

  • 汽车在到达障碍物时会紧急刹车。

  • 在现实世界中,传感器对距离的测量不够精确,从而导致随机数值误差。

  • 数字传感器以固定时间间隔运行。

要开始,请打开 moving_car 模型。在 MATLAB 命令行中,输入:

open_system('moving_car.slx');

您首先需要对在汽车位置到达 10 时的紧急刹车进行建模。Integrator, Second-Order 模块有用于此目的的参数。

  1. 双击 Integrator, Second-Order 模块。将出现“模块参数”对话框。

  2. 选择 x 限制,然后为 x 上限输入 10。参数的背景色发生变化以指示模型存在未应用的修改。点击确定应用更改并关闭对话框。

添加新模块和连接

添加一个用来测量与障碍物之间距离的传感器。

  1. 修改模型。根据需要展开模型窗口,以容纳新模块。

    • 求实际距离。要想求出障碍物位置和车辆位置之间的距离,需要从 Math Operations 库中添加 Subtract 模块。还要从 Sources 库中添加 Constant 模块来为障碍物的位置设置常量值 10

    • 对真实传感器中常见的不完美测量进行建模。使用 Sources 库中的 Band-Limited White Noise 模块产生噪声。将噪声功率参数设置为 0.001。通过使用 Math Operations 库中的 Add 模块将噪声添加到测量中。

    • 对每 0.1 秒触发一次的数字传感器进行建模。在 Simulink 中,以给定时间间隔对信号进行采样需要一个样本和保持器。从 Discrete 库中添加 Zero-Order Hold 模块。将该模块添加到模型后,将采样时间参数更改为 0.1

    • 添加另一个 Outport,用来连接传感器输出。保留端口号参数的默认值。

  2. 连接新模块。Second-Order Integrator 模块的输出已连接到另一个端口。要在该信号中创建分支,请左键点击该信号以突出显示可供连接的端口,然后点击适当的端口。

    With the signal between the Second-Order Integrator block and the Outport block selected, the software suggests connecting the signal line to the minus input port of the Subtract block.

为信号添加注释

将信号名称添加到模型中。

  1. 双击信号并键入信号名称。

    Highlighted signal is called pedal position

  2. 要完成输入,请点击文本框外部。

  3. 重复上述步骤以添加下图中所示的名称。

    Model with named signals. The signal between Pulse Generator block and the gain block is called pedal position. The signal between the Gain block and the Integrator, Second-Order block is called acceleration. The signal between two Subtract blocks is called actual distance. The signal between the Zero-Order Hold block and the Outport block is called measured distance.

比较多个信号

actual distance 信号与 measured distance 信号进行比较。

  1. 创建 Scope Viewer 并将其连接到 actual distance 信号。右键点击信号并选择创建并连接查看器 > Simulink > Scope。信号的名称显示在查看器标题中。

  2. measured distance 信号添加到同一个查看器中。右键点击信号,然后选择连接到查看器 > Scope1。确保您连接到在上一步中创建的查看器。

    Window after right clicking a signal. Scope 1 is selected.

  3. 对模型进行仿真。波形查看器显示两个信号:actual distance(黄色)和 measured distance(蓝色)。

    The scope viewer shows the actual distance and the measured distance values.

  4. 放大图形以观察噪声和采样的影响。点击缩放 。左键点击并拖动鼠标框住您想放大查看的区域。

    A rectangular box in the scope viewer window shows the region selected to zoom.

    您可以根据需要重复此操作来观察细节。

    The zoomed region in the scope viewer

该图显示,测量值可偏离实际值达 0.3 米之多。这些信息在设计安全功能(如碰撞预警)时非常有用。

另请参阅

模块

相关主题