Matlab Simscape Electrical实例 2020-03-28~29

Refs:

  • Converters (Low power)
  • Applications
  • Specialized Power Systems
  • Simulation and Analysis
  • Device Characteristics Assessment

1. LC低通滤波

ee_filter_lowpass_ota: Low-Pass Filter Using Operational Transconductance Amplifiers

image
  • VSrc是controlled voltage source用来连接simulink模型和simscape模型。

  • simlog_ee_filter_lowpass_ota是存储到Workspace中log信号名,simlog_ee_filter_lowpass_ota.print可以快速查看每个信号名词,方便调用。模块Simscape Results Explorer可快速查看每个信号波形,但是变量名与调用的会有差异,如调用变量名没有空格,而是下划线

    Workspace中log信号名

绘制放大器电压Vo

ee_filter_lowpass_ota_plot1voltages

% Code to plot simulation results from ee_filter_lowpass_ota
%% Plot Description:
%
% The plot below shows the response of the filter to a brief voltage pulse.
% The result from the circuit is compared with the result from a transfer
% function which was specified using the desired frequency response
% behavior, and we see that the results match nearly perfectly.

% Copyright 2016-2019 The MathWorks, Inc.

% Generate new simulation results if they don't exist or if they need to be updated
if ~exist('simlog_ee_filter_lowpass_ota', 'var') || ...
        simlogNeedsUpdate(simlog_ee_filter_lowpass_ota, 'ee_filter_lowpass_ota') 
    sim('ee_filter_lowpass_ota')
    % Model StopFcn callback adds a timestamp to the Simscape simulation data log
end
% `~exist`是不存在, `...`是接下一行。这里目的是更新仿真。

% Reuse figure if it exists, else create new figure
if ~exist('h1_ee_filter_lowpass_ota', 'var') || ...
        ~isgraphics(h1_ee_filter_lowpass_ota, 'figure')
    h1_ee_filter_lowpass_ota = figure('Name', 'ee_filter_lowpass_ota'); % 图名为`ee_filter_lowpass_ota`
end
figure(h1_ee_filter_lowpass_ota)
clf(h1_ee_filter_lowpass_ota) % 清空当前图窗窗口

% Get simulation results
simlog_t = simlog_ee_filter_lowpass_ota.OTA2.Iout.v.series.time; % 取OTA2的时间
simlog_vOTA2 = simlog_ee_filter_lowpass_ota.OTA2.Iout.v.series.values('V'); 
% 取OTA2的输出端电压值,注意一定要写单位,如V、mV、kV
simlog_vi = simlog_ee_filter_lowpass_ota.VSrc.Controlled_Voltage_Source.v.series.values('V');

simlog_vdes = logsout_ee_filter_lowpass_ota.get('vd');
% 系统中用goto模块定义过的信号可以直接用`get`来获取

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_vOTA2, 'LineWidth', 2)
hold on
plot(simlog_vdes.Values.Time, simlog_vdes.Values.Data, 'LineStyle','--', 'LineWidth', 2)
hold off
grid on
title('Amplifier Voltage')
ylabel('Voltage (V)')
legend({'Circuit','Desired'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_vi,'LineWidth', 1)
grid on
title('Input Voltage')
ylabel('Voltage (V)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x') % 同步多个坐标区的范围

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_vOTA2 simlog_vi simlog_vdes

回路线性化并绘制伯德图

  • APPs内操作可以使用模块Model Linearizer,参考

识别电路伯德图并进行PID参数优化 >> Design Controller for Power Electronics Model Using Frequency Response Data

  1. QQ截图20200331115948.png
  2. Operating Point -> Snapshot指定某时间找出一个稳态工作点;
  3. 双击Snapshot并Initialize model,输出到Matlab Workspace;
  4. 给回路注入小信号得到频域响应;
  5. 绘制伯德图 。
  • 代码操作
    ee_filter_lowpass_ota_bodeplot

打开模块Model Explorer可以看到在Model Workspace中有一个变量w1,右键Find where used发现并没有在模型中使用,只是用来画伯德图时定义频率范围。导入变量到这里可以使用.m文件,如w2 = 6283.1853071795858;则可以将w2变量添加进来,或直接复制粘贴一个现有变量再更改名和值。

%% Linearize a Circuit to View Frequency Response
% This example script shows how you can view the small-signal frequency
% response of a Simscape(TM) model by using linearization. It uses example
% model ee_filter_lowpass_ota.
%
% An alternative and recommended way to linearize Simulink(R) and Simscape
% models is to use Simulink Control Design(TM). Simulink Control Design has
% tools that help you find operating points and returns a state-space model
% object that defines state names. If you have Simulink Control Design,
% open the model ee_filter_lowpass_ota. On the Apps tab, under Control
% Systems, click Model Linearizer. In the Model Linearizer, on the Linear
% Analysis tab, in the Linearize section, click Bode.

% Copyright 2016-2019 The MathWorks, Inc.

%% Open the model and set circuit parameters
open_system('ee_filter_lowpass_ota')

%% Linearize
% Input source is commented out only during linearization. Its output is not
% within the portion of the model being linearized, and it is commented out
% to avoid warnings that are irrelevant for this step.
set_param([bdroot '/Input'],'Commented','on');
[a, b, c, d] = linmod('ee_filter_lowpass_ota'); %`linmod`命令进行线性化
set_param([bdroot '/Input'],'Commented','off');
clear simlog_ee_filter_lowpass_ota

%% Bode plot
npts = 1200;
temp_hws = get_param(bdroot, 'modelworkspace'); % bdroot 返回当前系统的顶层模型 调取modelworkspace
temp_w1 = temp_hws.evalin('w1'); % 调取modelworkspace中变量w1的值
fr_low = (floor(log10(temp_w1/2/pi))-1);  % lower frequency limit floor - 朝负无穷大四舍五入  
fr_high = (ceil(log10(temp_w1/2/pi))+1);  % upper frequency limit ceil-朝正无穷大四舍五入
f = logspace(fr_low, fr_high, npts); % 这里fr_low=2,fr_high=4.在频率范围10^2~10^4内取npts=1200个点 
G = zeros(length(d), npts);
for i = 1:npts
    G(:,i) = c*(2*pi*1i*f(i)*eye(size(a))-a)^-1*b +d; % 传递函数,固定写法
end

% Reuse figure if it exists, else create new figure
if ~exist('h2_ee_filter_lowpass_ota', 'var') || ...
        ~isgraphics(h2_ee_filter_lowpass_ota, 'figure')
    h2_ee_filter_lowpass_ota = figure('Name', 'ee_filter_lowpass_ota');
end
figure(h2_ee_filter_lowpass_ota)
clf(h2_ee_filter_lowpass_ota)

simlog_handles(1) = subplot(2, 1, 1);
magline_h = semilogx(f, 20*log10(abs(G))); % semilogx - 半对数图
grid
ylabel('Magnitude (dB)')
title('Frequency Response (Low Pass Filter OTA)');
legend('Circuit','Desired');
simlog_handles(2) = subplot(2, 1, 2);
phsline_h = semilogx(f, 180/pi*unwrap(angle(G))); % unwrap - 平移相位角
% 展开向量 P 中的弧度相位角。每当连续相位角之间的跳跃大于或等于 π 弧度时,
% unwrap 就会通过增加 ±2π的整数倍来平移相位角,直到跳跃小于 π。
set([magline_h phsline_h], 'LineWidth', 2);
set([magline_h(2) phsline_h(2)],'LineStyle','--');
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
grid on

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear magline_h phsline_h a b c d G f npts i simlog_handles 
clear temp_w1 temp_hws fr_low fr_high

2. 三相整流

ee_composite_rectifier

物理模型到信号模型转换

3. Three-Phase Voltage-Sourced Converter (SPWM)

ee_voltage_sourced_converter_spwm
命令调用已经比较熟悉了。

右键converter观察其三相电流波形并获得变量名方波调用

可直接在左上角对坐标轴进行限制,并在新的Figure中打开

% Code to plot simulation results from ee_voltage_sourced_converter_spwm
%% Plot Description:
%
% The plot below shows the DC supply current, as well as the AC phase 
% currents in the Wye-Connected Load.

% Copyright 2014-2019 The MathWorks, Inc.

% Generate new simulation results if they don't exist or if they need to be updated
if ~exist('simlog_ee_voltage_sourced_converter_spwm', 'var') || ...
        simlogNeedsUpdate(simlog_ee_voltage_sourced_converter_spwm, 'ee_voltage_sourced_converter_spwm') 
    sim('ee_voltage_sourced_converter_spwm')
    % Model StopFcn callback adds a timestamp to the Simscape simulation data log
end

% Reuse figure if it exists, else create new figure
if ~exist('h1_ee_voltage_sourced_converter_spwm', 'var') || ...
        ~isgraphics(h1_ee_voltage_sourced_converter_spwm, 'figure')
    h1_ee_voltage_sourced_converter_spwm = figure('Name', 'ee_voltage_sourced_converter_spwm');
end
figure(h1_ee_voltage_sourced_converter_spwm)
clf(h1_ee_voltage_sourced_converter_spwm)

temp_colororder = get(gca,'defaultAxesColorOrder');

% Get simulation results
simlog_t = simlog_ee_voltage_sourced_converter_spwm.R.i.series.time;
simlog_R = simlog_ee_voltage_sourced_converter_spwm.R.i.series.values('A');
simlog_ia = simlog_ee_voltage_sourced_converter_spwm.Converter.phase_splitter.i_a.series.values('A');
simlog_ib = simlog_ee_voltage_sourced_converter_spwm.Converter.phase_splitter.i_b.series.values('A');
simlog_ic = simlog_ee_voltage_sourced_converter_spwm.Converter.phase_splitter.i_c.series.values('A');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_R, 'LineWidth', 1)
hold off
grid on
title('DC supply current')
ylabel('Current (A)')
xlim([0, simlog_t(end,1)]); % simlog_t(end,1)为最后一个数,也可以写simlog_t(end)

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_ia, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_ib, 'LineWidth', 1)
plot(simlog_t, simlog_ic, 'LineWidth', 1)
legend({'Phase A','Phase B','Phase C'},'Location','NorthEast');
grid on
title('Converter AC output currents')
ylabel('Current (A)')
xlabel('Time (s)')
xlim([0, simlog_t(end,1)]);

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_R
clear simlog_ia simlog_ib simlog_ic

该例和上例都有自建block(模块i,iabc),关于如何创建模块并添加到库(library)中,参考:
>> 创建自定义库
>> 将库添加到 Library Browser 中


将例子中好的BLOCK添加到自建库中,方便使用:

  1. 创建库请参考上述连接,如果已存在库mylib需要修改时,先将库解锁:
    set_param('mylib','Lock','off');
  2. 对库进行更改,如修改快速检索名词时,选中指定模块输入set_param(gcb,'BlockKeywords',{"My Gain","Your Gain"})
  3. 先输入:set_param(gcs,'EnableLBRepository','on');再保存库,之后关闭;
  4. Simulink Library Browser中F5刷新库文件。
    我创建了单相和三相电流传感器自带示波器的模块

4. Boost变换器

ee_boost_converter_control

  • 模块Control里有模块RateTransition,处理以不同速率运行的模块之间的数据传输。常用于离散控制系统。
    Rate Transition 模块的行为取决于模块连接到的端口的采样时间

采样时间颜色和模块行为标签表明:
图中上方的 Rate Transition 模块充当从快速到慢速转移中的零阶保持器。
图中下方的 Rate Transition 模块充当从慢速到快速转移中的单位延迟器。

有关详细信息,请参阅处理速率转换 (Simulink Coder)。


  • 使用了Boost Converter宏模块,里面包含了每个开关上缓冲电路:Snubbers(串联电阻和电容),配合PWM Generator(Mask)使用。

5. Buck-Boost变换器

  • ControllerRelational Operator对两个输入进行比较运算,输出布尔值;
  • ControllerDC-DC Voltage ControllerDiscrete-time DC-DC voltage PI control with feedforward and optional integral anti-windup;
  • 该电路通过比较输入电压Vin和给定参考电压Vref来判断四开关变换器Buck-Boost Converter工作于Buck状态或Boost状态。

6. Buck Converter with Faults

ee_switching_power_supply_fault

这里例子中画图程序有些问题,因为参考信号Vref是constant,只有一个维度,与时间信号维度对应不上,所以参考信号图没有画出来。照着3-25的一个例子修改后正常。具体内容参考代码部分。

记录信号并在程序中调用:

  1. 勾选Log Data,或直接在信号线上右键记录所选信号
    记录信号
  2. Configuration Parameters中查看LoggingName,然后改变量会被输出到Matlab Workspace中。
    查找输出信号名
  3. logsout_ee_switching_power_supply_fault.get('Ref').Values.time调用该信号时间,logsout_ee_switching_power_supply_fault.get('Ref').Values.data调用该信号值,其中Values必须首字母大写,而time可以写作Timedata可以写作Data
% Code to plot simulation results from ee_switching_power_supply_fault
%% Plot Description:
%
% The plot below shows the output voltage as compared to the reference
% voltage.  It also shows the control signal that adjusts the duty cycle of
% the PWM signals applied to the MOSFET gates. As the faults occur, the
% effect can be seen on the control signal and the voltage supplied by the
% converter.

% Copyright 2016-2019 The MathWorks, Inc.

% Generate new simulation results if they don't exist or if they need to be updated
if ~exist('simlog_ee_switching_power_supply_fault', 'var') || ...
        simlogNeedsUpdate(simlog_ee_switching_power_supply_fault, 'ee_switching_power_supply_fault') 
    sim('ee_switching_power_supply_fault')
    % Model StopFcn callback adds a timestamp to the Simscape simulation data log
end

% Reuse figure if it exists, else create new figure
if ~exist('h1_ee_switching_power_supply_fault', 'var') || ...
        ~isgraphics(h1_ee_switching_power_supply_fault, 'figure')
    h1_ee_switching_power_supply_fault = figure('Name', 'ee_switching_power_supply_fault');
end
figure(h1_ee_switching_power_supply_fault)
clf(h1_ee_switching_power_supply_fault)

% Get simulation results
simlog_t = simlog_ee_switching_power_supply_fault.R_Load.v.series.time;
simlog_vout = simlog_ee_switching_power_supply_fault.R_Load.v.series.values('V');
simlog_vCtrl = simlog_ee_switching_power_supply_fault.Driver.Voltage_Source_SL.Controlled_Voltage_Source.v.series.values('V');
simlog_vref = logsout_ee_switching_power_supply_fault.get('Ref');

% Plot results
% Plot gate-source voltages
simlog_handles(1) = subplot(2,1,1);
if length(simlog_vref.Values.data) <= 1 % .Data==.data, .Time==.time
    plot(simlog_t*1000,simlog_vref.Values.data*ones(length(simlog_t),1),'k--','LineWidth',1)
% 因为这里信号VRef是固定值,所以必须为其乘以时间长度向量,否则无法画出图形
else
    plot(simlog_vref.Values.time*1000,simlog_vref.Values.data,'k--','LineWidth',1)
end
hold on
plot(simlog_t*1000,simlog_vout,'LineWidth',1); % *1000是为了把默认时间单位s换算为ms
hold off
ylabel('Voltage (V)')
legend('Ref','Output')
grid on
title('Output and Reference Voltage')

% Plot drain-source currents
simlog_handles(2) = subplot(2,1,2);
plot(simlog_t*1000,simlog_vCtrl,'LineWidth',1);
grid on
ylabel('Voltage (V)');
xlabel('Time (ms)')
title('Control Voltage');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_vout simlog_vCtrl simlog_vref
  • R、L、C元件都可以在双击打开的属性框Faults中设置某一时间点,假设发生错误时,元件值突变,如电阻变为无穷大(开路)或电容容量只有默认值的一半等。
    还可以为R设置热噪声,具体再探索。

7. Four-Quadrant Chopper Control

ee_four_quadrant_chopper_control

The Four-Quadrant Chopper block represents a four-quadrant controlled chopper for converting a fixed DC input to a variable DC output. The block contains two bridge arms. Each bridge arm each has two switching devices. Options for the type of switching devices are:


The equivalent circuit and the operation for the block

负载为电感。开关S1、S4和S2、S3互补。
当输入为正压时,开关S1和S4动作,若打开,则输出正电压、正电流;若关断,D2、D4续流,负载为负电压、正电流;
当输入为负压时,开关S2和S3动作,若打开,则输出负电压、负电流;若关断,D2、D4续流,负载为正电压、负电流;


Pulse Generator脉宽是用采样时间和采样数决定的。

要通过保留输入信号的真实值,将信号从一种数据类型转换为另一种数据类型,请选择默认值 “Real World Value (RWV)”。模块会解释由输入和输出的定标施加的限制,并尝试生成具有相等真实值的输出。

8. First and Fourth Quadrant Chopper Control

ee_quadrant14_chopper_control
观察输出与给定、控制器输出变化情况。

当电流上升时,占空比开到最大0.99,当输出超过给定后,控制逐渐减小,产生一定超调后跟随给定。

当0.25s负载由正压变为负压时,开关由S1/S4动作变为S2/S3动作,两组开关占空比时互补的,在交换过程中产生了负载电压尖峰。

程序ee_quadrant14_chopper_control_plot1current有些问题,改正后:

% Code to plot simulation results from ee_quadrant14_chopper_control
%% Plot Description:
%
% The plot below shows the requested and measured current for the
% test and the output voltage in the circuit.

% Copyright 2017-2019 The MathWorks, Inc.

% Generate new simulation results if they don't exist or if they need to be updated
if ~exist('simlog_ee_quadrant14_chopper_control', 'var') || ...
        simlogNeedsUpdate(simlog_ee_quadrant14_chopper_control, 'ee_quadrant14_chopper_control') 
    sim('ee_quadrant14_chopper_control')
    % Model StopFcn callback adds a timestamp to the Simscape simulation data log
end

% Reuse figure if it exists, else create new figure
if ~exist('h1_ee_quadrant14_chopper_control', 'var') || ...
        ~isgraphics(h1_ee_quadrant14_chopper_control, 'figure')
    h1_ee_quadrant14_chopper_control = figure('Name', 'ee_quadrant14_chopper_control');
end
figure(h1_ee_quadrant14_chopper_control)
clf(h1_ee_quadrant14_chopper_control)

% Get simulation results
simlog_t = simlog_ee_quadrant14_chopper_control.Two_Quadrant_Chopper.p2.v.series.time;
simlog_vo = simlog_ee_quadrant14_chopper_control.Sensing_v.Voltage_Sensor.V.series.values('V');
simlog_iRef = logsout_ee_quadrant14_chopper_control.get('current_request');
simlog_io = simlog_ee_quadrant14_chopper_control.Sensing_i.Current_Sensor.I.series.values('A');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_io, 'LineWidth', 1)
hold on
if length(simlog_iRef.Values.Data) <=1
    plot(simlog_t,simlog_iRef.Values.Data*ones(length(simlog_t),1),'LineWidth',1)
else
    plot(simlog_iRef.Values.Time, simlog_iRef.Values.Data, 'LineWidth', 1)
end
hold off
grid on
title('Output current')
ylabel('Current (A)')
legend({'Measured','Reference'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_vo, 'LineWidth', 1)
grid on
title('Output voltage')
ylabel('Voltage (V)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles temp_colororder
clear simlog_iRef simlog_io simlog_vo

9. First and Second Quadrant Chopper Control

ee_quadrant12_chopper_control

Scope 0.2s给定参考电流由正变为负,由于负载电感作用,到0.27s附近负载上电流才变为负电流。电流下降阶段负载电压为0,直到电流在负向稳定后负载电压才为正。因此整个电路工作于1/2象限。

10. DC-DC Converter Model Fidelity Comparison

ee_dcdc_converter_fidelity

比较不同开关建模方式区别。理想开关采样时间10us;平均开关采样时间50us,开关电流和电压波形变为阶梯状,不再光滑;平均模型无法反映开关电压电流峰值,要注意峰值电压电流是否超出SOA。

添加超链接
模型空白处双击键入文字/公式(Tex)等,右键ClickFcn可以添加URL或Matlab文件,如edit XXX.m等。

添加超链接

11. Push-Pull Buck Converter in Continuous Conduction Mode

open_system('ee_push_pull_converter_ccm.slx');

  • 各种执行命令都可以用代码来完成,例如:
  • Open Model
    open_system('ee_push_pull_converter_ccm.slx');
  • Run the Simulation
    sim('ee_push_pull_converter_ccm.slx');
  • View Simulation Results
    open_system('ee_push_pull_converter_ccm/Scopes/Circuit Scope');
    open_system('ee_push_pull_converter_ccm/Scopes/PI Controller Scope');
  • View logged Simscape™ data using the Simscape Results Explorer, enter:
    sscexplore(simlog_ee_push_pull_converter_ccm);
  • 示波器输入线双击编辑名词,会显示在波形对应标题上;
  • 该电路负载分为固定负载和一个cyclic load循环载荷(注入500Hz的脉冲电流),在负载电流波动的情况下还能保持输出电压稳定;
  • 该电路通过定义负载电流纹波系数和电压纹波系数来选取滤波电感和电容参数;
控制器内部

PI前后信号
  • 不连续工作模式 >> Push-Pull Buck Converter in Discontinuous Conduction Mode

12. Inverting Topology Buck-Boost Converter Control

ee_inverting_buck_boost_converter_control

离散系统输出波形记得加Rate Transition,代码画的输出波形不是真实的波形,因为没有加零阶保持器。
  • 点击模块上方三个点选择comment out(注释掉)则可以禁用该模块,模块两边断路。

你可能感兴趣的:(Matlab Simscape Electrical实例 2020-03-28~29)