Следующий пример показывает линейный резистор с дополнительным тепловым портом. Компонент использует условные разделы, чтобы реализовать управляющую логику. Разделы annotations в рамках условных переходов выборочно представляют или скрывают соответствующие порты, параметры и переменные на основе значения параметра управления. Два варианта блока имеют различное количество портов, и поэтому значок авторского блока также изменяется соответственно.
component CondResistor
% Linear Resistor with Optional Thermal Port
% If "Model thermal effects" is set to "Off", the block represents a
% linear resistor. The voltage-current (V-I) relationship is V=I*R,
% where R is the constant resistance in ohms.
%
% If "Model thermal effects" is set to "On", the block represents a
% resistor with a thermal port. The resistance at temperature T1 is given by
% R(T) = R0*(1+alpha(T1-T0)), where R0 is the Nominal resistance at the
% Reference temperature T0, and alpha is the Temperature coefficient.
nodes
p = foundation.electrical.electrical; % +:left
n = foundation.electrical.electrical; % -:right
H = foundation.thermal.thermal; % H:left
end
parameters
thermal_effects = simscape.enum.onoff.off; % Model thermal effects
end
parameters(ExternalAccess=none)
R = { 1, 'Ohm' }; % Nominal resistance
T0 = {300,'K'}; % Reference temperature
alpha = {50e-6,'1/K'}; % Temperature coefficient
tc = {10,'s'}; % Thermal time constant
K_d = {1e-3,'W/K'}; % Dissipation factor
end
variables(ExternalAccess=none)
i = { 0, 'A' }; % Current
v = { 0, 'V' }; % Voltage
T1 = {value = {300,'K'}, priority = priority.high}; % Temperature
end
branches
i : p.i -> n.i;
end
equations
v == p.v - n.v;
end
if thermal_effects == simscape.enum.onoff.off
annotations
% Show non-thermal settings
Icon = 'custom_resistor.png';
[R, i, v] : ExternalAccess=modify;
% Hide thermal node
H : ExternalAccess=none;
end
connections
connect(H, *); % Connect hidden thermal node to reference
end
equations
R*i == v;
T1 == T0; % Temperature is constant
end
else
annotations
% Show thermal settings
Icon = 'custom_resistor_thermal.png';
[T1, T0, alpha, tc, K_d, H] : ExternalAccess=modify;
end
% Add heat flow + thermal equations
variables(Access=private)
Q = { 0, 'J/s' }; % Heat flow
end
branches
Q : H.Q -> *
end
equations
T1 == H.T;
let
mc = tc*K_d; % mc in Q = m*c*dT
% Calculate R(T), protecting against negative values
Rdem = R*(1+alpha*(T1-T0));
R_T = if Rdem > 0, Rdem else {0,'Ohm'} end;
in
R_T*i == v; % Electrical equation
mc * T1.der == Q + R_T*i*i; % Thermal equation
end
end
end
end
Компонент первоначально объявляет все дополнительные параметры и переменные с набором атрибута ExternalAccess к none, и затем представляет их выборочно при помощи условных разделов annotations. Противоположный метод, сокрытия неподходящих участников, также допустим, но этот подход более легко масштабируем, когда у вас есть несколько настроек компонента.
Если параметр управления, Model thermal effects, устанавливается на Off, блок представляет линейный резистор. Единственными представленными параметрами блоков является Nominal resistance, вкладка Variables позволяет вам поставить цели для Current и Voltage, и значок блока имеет два порта, + и -.



Если параметр Model thermal effects устанавливается на On, блок представляет резистор с тепловым портом с температурно-зависимым сопротивлением. Параметры блоков, переменные, порты и значки авторского блока изменяются соответственно.


