2

I am currently working on a simulink simulator and right now I am trying to customize a simscape block so I can get a parameter as an input rather than a fixed value.

I have added the Capacity input but i dunno how to propagate this over to the C_Table correctly since it needs to be a 1x3 vector when generating the library block. Anyone able to help me with this?

Code of the whole block:

 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
        T = {293.15,'K'} % T:right
        Capacity = {[0 0 0], 'A*hr'} % Cap:right
    end

    outputs
        C = {31,'A*hr'} %C:left
        SOC = {1,'1'}   %SOC:left
    end

    parameters (Size=variable)
        C_Table = {[1 1 1], 'A*hr'} % Capacity values at specified temperature breakpoints
        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(value(C_Table,'A*hr')<=0)   
            pm_error('simscape:GreaterThanZero','Capacity values at specified temperature breakpoints');
        end
        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

        % Perform the capacity table lookup
        C == tablelookup(Temp_Table,C_Table,T,...
            interpolation=linear,extrapolation=nearest)

        % 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
Cœur
  • 37,241
  • 25
  • 195
  • 267
RoyHau
  • 21
  • 3

2 Answers2

0

My suggestion would be to delete this line

 C_Table = {[1 1 1], 'A*hr'} % Capacity values at specified temperature breakpoints

and replace any instance of C_Table by Capacity.

am304
  • 13,758
  • 2
  • 22
  • 40
  • I have tried as you suggested but i get errors on the function setup : %check parameter values and %perform the capacity table lookup. i have also tried removing both of them but it still leaves me with errors. – RoyHau Apr 24 '15 at 07:36
  • Caused by: Error using MyBatteryBlocks.Em_tableMod>setup (line 43) Undefined function 'value' for input arguments of type 'NetworkEngine.InputCompileData'. – RoyHau Apr 24 '15 at 08:35
  • when i remove those lines i get: Caused by: Error using MyBatteryBlocks.Em_tableMod>equations (line 78) Wrong variability is given to the function argument. – RoyHau Apr 24 '15 at 08:36
  • i testet the same input on the similar block that is without temperature and i get it to work but the problem i have there is that i cant adjust the discharge characteristic as i want to – RoyHau Apr 24 '15 at 08:38
  • It looks like the parameters of the look-up table have to be defined as variable-size parameters, you can't use physical inputs to parameterise your look-up table. So it looks like you can't really do what you want, the capacity values have to be constant parameters, they can't be signals changing with time. Sorry. – am304 Apr 24 '15 at 09:46
  • I actually figured out a solution for the problem: i Deleted the whole C_Table and the %perform the capacity table lookup means that C wont be varying with the temp but the change is so small it wont matter i think. Then i set C as an input instead of output and change that instead. – RoyHau Apr 24 '15 at 10:16
  • Now i need to figure out a way to do the same with Qinit but when trying the same as before leaves me with: Invalid type 'NetworkEngine.InputCompileData' used for parameter value. – RoyHau Apr 24 '15 at 10:17
  • I assume you have defined `Qinit` as an input. You then need to delete the corresponding parameter definition, and probably worth trying removing the lines in the `setup` function that check the sign. – am304 Apr 24 '15 at 13:04
  • Ahh i did that i i managed to create the library block but when i am trying to run the simulation i get this fault message: * ['ssc_lithium_battery_1CellMultiplied/Battery of 1 Cell with Multiplier1/Lithium Cell 1/Em_tableMod']: Function 'Qe' is not supported. – RoyHau Apr 27 '15 at 07:30
  • Error using MyBatteryFailureBlocks.Em_tableMod>setup (line 57) Invalid type 'NetworkEngine.InputCompileData' used for parameter value. This fault i am getting then trying to manipulate the Qe = Qinit either by making a new input stating Qinit-xx or changing Qinit to input port – RoyHau Apr 27 '15 at 07:59
  • Well, yes. If `Qinit` is now a signal instead of a parameter, you can't initialise `Qe` with it. Initial conditions by their very nature can't vary with time, they have to be constant parameters, not time-varying signals. – am304 Apr 27 '15 at 08:27
  • That takes me back to the first problem I tried to solve, that is changing the value from the GUI to my simulator workspace. The thing is that the whole simulation will be compiled to a .dll file and run from a non matlab GUI so faar everything seems to be working except this litle thing. I dont have to change the value during simulation though. it will be enough if i can change it when stopping it – RoyHau Apr 27 '15 at 10:45
0

Here is what I did to manipulate the battery capacity, setting c as an input instead of output. Removing all instances of C_table and capacity table lookup at the bottom to prevent two instances of setting c. This effectively prevents the calculation of capacity changes due to temperature but i find it to only be a minor inconvenience to me at this point.

Here is the functioning block 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 = {1,'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
RoyHau
  • 21
  • 3