How do i write to the base workspace in simulink?

6 Ansichten (letzte 30 Tage)
Roy Hauge
Roy Hauge am 21 Apr. 2015
Kommentiert: Stephen23 am 28 Apr. 2015
I currently have:
BatteryParams = struct();
for idx = 1:numCells
%%Lookup Table Breakpoints
BatteryParams(idx).SOC_LUT = [0 0.1 0.25 0.5 0.75 0.9 1]';
BatteryParams(idx).Temperature_LUT = [5 20 40] + 273.15;
%%Em Branch Properties (OCV, Capacity)
% Battery capacity
BatteryParams(idx).Capacity_LUT = [
80.0000 79.5000 79.0000]; %Ampere*hours
From my GUI i want to change the "BatteryParams(idx).Capacity_LUT" in the workspace while running the simulation.
I have gotten a hint to these functions but otherwise I am unsure of how to proceed.
command = sprintf('BatteryParams(%d).Capacity_LUT = %f;', idx, value)
evalin('base', command);
I am guessing I have to use the "to workspace" block in simulink but other than what i have written here now i am kinda blank on the sollution to my problem.
Can anyone help me out ?
  2 Kommentare
Ajay Kumar P
Ajay Kumar P am 28 Apr. 2015
Please try using assignin('base','varibale name',value) function to create/update variable values in the base workspace.
Stephen23
Stephen23 am 28 Apr. 2015
Although global and assignin will work, they are also very difficult to debug and make code-checking almost impossible. It is reccomended to pass values as much as possible, which means they should be collected and passed as output arguments.
This page has more information on this topic:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Navaneeth Raman
Navaneeth Raman am 22 Apr. 2015
Hello,
Can you be clear on how you have your model setup and how the above code ties in within your system? Its not clear where your code fits in.
Thanks, Navaneeth
  1 Kommentar
Roy Hauge
Roy Hauge am 28 Apr. 2015
The complete file for the various values are:
% Initialization file for demo ssc_lithium_battery_80Cells.mdl.
%
% Demo based on model from publication: T. Huria, M. Ceraolo, J. Gazzarri,
% R. Jackey. "High Fidelity Electrical Model with Thermal Dependence for
% Characterization and Simulation of High Power Lithium Battery Cells,"
% IEEE International Electric Vehicle Conference, March 2012
%
% Copyright 2012 The MathWorks, Inc.
% Number of series cells
numCells = 96;
%%Thermal Properties
% Cell dimensions and sizes
cell_thickness = 0.0084; %m
cell_width = 0.215; %m
cell_height = 0.220; %m
% Cell surface area
cell_area = (...
cell_thickness * cell_width +...
cell_thickness * cell_height +...
cell_width * cell_height); %m^2
% Cell volume
cell_volume = cell_thickness * cell_width * cell_height; %m^3
% Convective heat transfer coefficient
% For natural convection this number should be in the range of 5 to 25
h_conv = 5; %W/m^2/K Cell-to-cell
h_conv_end = 10; %W/m^2/K End cells to ambient
% String1
Init = 0; % SOC adjustment
% String2
Init1 = 0; % SOC adjustment
%%Populate Lookup Tables
BatteryParams = struct();
for idx = 1:numCells
%%Lookup Table Breakpoints
BatteryParams(idx).SOC_LUT = [0 0.1 0.25 0.5 0.75 0.9 1]';
BatteryParams(idx).Temperature_LUT = [5 20 40] + 273.15;
BatteryParams(idx).SOC_LUT1 = [0 0.1 0.25 0.5 0.75 0.9 1]';
BatteryParams(idx).Temperature_LUT1 = [5 20 40] + 273.15;
%%Em Branch Properties (OCV, Capacity)
% Battery capacity
BatteryParams(idx).Capacity_LUT = [
80.0000 79.5000 79.0000]; %Ampere*hours
BatteryParams(idx).Capacity_LUT1 = [
80.0000 79.5000 79.0000]; %Ampere*hours
% Em open-circuit voltage vs SOC rows and T columns
BatteryParams(idx).Em_LUT = [
2.0000 2.9474 3.0000
3.0526 3.1055 3.1579
3.2105 3.2632 3.3158
3.3684 3.4211 3.4737
3.5263 3.5789 3.6315
3.6842 3.7368 3.7895
3.9421 4.0947 4.1473]; %Volts
BatteryParams(idx).Em_LUT1 = [
2.0000 2.9474 3.0000
3.0526 3.1055 3.1579
3.2105 3.2632 3.3158
3.3684 3.4211 3.4737
3.5263 3.5789 3.6315
3.6842 3.7368 3.7895
3.9421 4.0947 4.1473]; %Volts
%%Terminal Resistance Properties
% R0 resistance vs SOC rows and T columns
BatteryParams(idx).R0_LUT = [
0.0117 0.0085 0.0090
0.0110 0.0085 0.0090
0.0114 0.0087 0.0092
0.0107 0.0082 0.0088
0.0107 0.0083 0.0091
0.0113 0.0085 0.0089
0.0116 0.0085 0.0089]; %Ohms
BatteryParams(idx).R0_LUT1 = [
0.0117 0.0085 0.0090
0.0110 0.0085 0.0090
0.0114 0.0087 0.0092
0.0107 0.0082 0.0088
0.0107 0.0083 0.0091
0.0113 0.0085 0.0089
0.0116 0.0085 0.0089]; %Ohms
%%RC Branch 1 Properties
% R1 Resistance vs SOC rows and T columns
BatteryParams(idx).R1_LUT = [
0.0109 0.0029 0.0013
0.0069 0.0024 0.0012
0.0047 0.0026 0.0013
0.0034 0.0016 0.0010
0.0033 0.0023 0.0014
0.0033 0.0018 0.0011
0.0028 0.0017 0.0011]; %Ohms
BatteryParams(idx).R1_LUT1 = [
0.0109 0.0029 0.0013
0.0069 0.0024 0.0012
0.0047 0.0026 0.0013
0.0034 0.0016 0.0010
0.0033 0.0023 0.0014
0.0033 0.0018 0.0011
0.0028 0.0017 0.0011]; %Ohms
% C1 Capacitance vs SOC rows and T columns
BatteryParams(idx).C1_LUT = [
1913.6 12447 30609
4625.7 18872 32995
23306 40764 47535
10736 18721 26325
18036 33630 48274
12251 18360 26839
9022.9 23394 30606]; %Farads
BatteryParams(idx).C1_LUT1 = [
1913.6 12447 30609
4625.7 18872 32995
23306 40764 47535
10736 18721 26325
18036 33630 48274
12251 18360 26839
9022.9 23394 30606]; %Farads
% Cell mass
BatteryParams(idx).cell_mass = 1; %kg
% Volumetric heat capacity
% assumes uniform heat capacity throughout the cell
% ref: J. Electrochemical Society 158 (8) A955-A969 (2011) pA962
BatteryParams(idx).cell_rho_Cp = 2.04E6; %J/m3/K
% Specific Heat
BatteryParams(idx).cell_Cp_heat = BatteryParams(idx).cell_rho_Cp * cell_volume; %J/kg/K
%%Initial Conditions
% Charge deficit
% BatteryParams(idx).Qe_init = 15.6845; %Ampere*hours
% Ambient Temperature
BatteryParams(idx).T_init = 20 + 273.15; %K
BatteryParams(idx).T_init1 = 20 + 273.15; %K
% Initial charge deficit
BatteryParams(idx).Qe_init = 0; %Ampere*hours
BatteryParams(idx).Qe_init1 = 0; %Ampere*hours
%%Cell Variation
% Tweak resistances and capacitances
BatteryParams(idx).R0_LUT = BatteryParams(idx).R0_LUT * (1 + .1*(rand-0.5));
BatteryParams(idx).R1_LUT = BatteryParams(idx).R1_LUT * (1 + .1*(rand-0.5));
BatteryParams(idx).C1_LUT = BatteryParams(idx).C1_LUT * (1 + .1*(rand-0.5));
BatteryParams(idx).R0_LUT1 = BatteryParams(idx).R0_LUT1 * (1 + .1*(rand-0.5));
BatteryParams(idx).R1_LUT1 = BatteryParams(idx).R1_LUT1 * (1 + .1*(rand-0.5));
BatteryParams(idx).C1_LUT1 = BatteryParams(idx).C1_LUT1 * (1 + .1*(rand-0.5));
This is the complete file i am currently working with.
% Tweak initial charge deficit
BatteryParams(idx).Qe_init = 5 * rand; %Ampere*hours
BatteryParams(idx).Qe_init1 = 5 * rand; %Ampere*hours
end
The block where I implement the variables I wish to change has the following code:
component Em_tableMod
% Em_tableMod
% This block implements the cell's main branch voltage source, and determines
% values for capacity (C) and state of charge (SOC). The defining equations
% depend on cell temperature, T.
% Copyright 2012-2013 The MathWorks, Inc.
nodes
p = foundation.electrical.electrical; % +:left
n = foundation.electrical.electrical; % -:right
end
inputs
C = {79.5,'A*hr'} % Cap:right
T = {293.15,'K'} % T:right
end
outputs
SOC = {1,'1'} %SOC:left
end
parameters (Size=variable)
Em_Table = {3.8*ones(5,3),'V'} % Matrix of voltage values, Em(SOC,T)
SOC_Table = {[0;0.1;0.5;0.9;1],'1'} % State of charge (SOC) breakpoints
Temp_Table = {[273.15 293.15 313.15],'K'} % Temperature (T) breakpoints
end
parameters
Qinit = {0,'A*hr'} % Initial charge deficit
end
variables(Access=private)
i = { 0, 'A' }; % Current
v = { 0, 'V' }; % Voltage
Qe = {0,'A*hr'}; % Charge deficit
end
function setup
% Check parameter values
if any(any(value(Em_Table,'V')<=0))
pm_error('simscape:GreaterThanZero','Matrix of voltage values, Em(SOC,T)');
end
if any(value(SOC_Table,'1')<0)
pm_error('simscape:GreaterThanOrEqualToZero','State of charge (SOC) breakpoints');
end
if any(value(Temp_Table,'K')<0)
pm_error('simscape:GreaterThanOrEqualToZero','Temperature (T) breakpoints');
end
if value(Qinit,'A*hr')<0
pm_error('simscape:GreaterThanOrEqualToZero','Initial charge deficit');
end
% Set initial charge deficit
Qe = Qinit;
end
branches
i : p.i -> n.i;
end
equations
v == p.v - n.v;
% Charge deficit calculation, preventing SOC>1
if Qe<0 && i>0
Qe.der == 0;
else
Qe.der == -i;
end
% SOC Equation
SOC == 1 - Qe/C;
% Electrical equation by table lookup
v == tablelookup(SOC_Table,Temp_Table,Em_Table,SOC,T,...
interpolation=linear,extrapolation=nearest)
end
end
I have modified the block so I can adjust the capacity directly as an input as i asked for but I still need a way to adjust the SOC to a certain level during simulation which is basically the same problem. To workspace doesnt work since it doesnt implement the changes to workspace live.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by