% Luan Nguyen and Taylor Johnson
% Computer Science and Engineering
% University of Texas at Arlington
% http://verivital.uta.edu/
%
% initialization and writing spaceex output
%
% OPTIONS: select the converter type by changing the mode variable
%
% usage: first, execute this file, matrices.m
% this generates SpaceEx input files and sets up parameters for simulation
% using the Simulink/StateFlow model dc2dc.slx / dc2dc.mdl

clear all; % warning, all results cleared

option_extrema = 0; % component variation; only supports buck converter for now

% converter types
MODE_BUCK = 1;
MODE_BOOST = 2;
MODE_BUCKBOOST = 3;

% selected converter to model
mode = MODE_BUCKBOOST;

% source voltage noise for stateflow simulation
Vs_noise = 0.1;

% initial current and voltage
i0 = 6;
v0 = 17.5;

switch mode
    case MODE_BUCK
        C = 25e-6; % capacitor value

        L = 50e-6; % inductor

        R = 1; % resistor

        Ac_nom = [0, -(1/L); (1/C), -(1/(R*C))];
        Bc_nom = [(1/L); 0];

        Ao_nom = [0, -(1/L); (1/C), -(1/(R*C))];
        Bo_nom = [0; 0];
        
        % switching period
        T = 0.00001;
        
        % input (source) voltage
        Vs = 12;
        % desired output voltage
        Vref = 5;
        % duty cycle
        D = Vref / Vs;
        
        % max simulation time (spaceex uses shorter time by default)
        Tmax = T*2500;
        
        % output filename
        outname = 'buck';
    case MODE_BOOST
        C = 25e-6;

        L = 50e-6;

        R = 1;

        Ac_nom = [0, 0; 0, -(1/(R*C))];
        Bc_nom = [(1/L); 0];

        Ao_nom = [0, -(1/L); (1/C), -(1/(R*C))];
        Bo_nom = [(1/L); 0];
        
        % switching period
        T = 0.00001;
        Tmax = T*500;
        
        % input (source) voltage
        Vs = 20;
        % desired output voltage
        Vref = 30;
        % duty cycle
        % Vs = Vo(1 - D), so Vs / Vo = 1 - D, Vs / Vo - 1 = -D, D = 1 - Vs / Vo, D = (Vo - Vs) / Vo
        D = (Vref - Vs) / Vref;
        
        outname = 'boost';
    case MODE_BUCKBOOST
        C = 6e-5;

        L = 4e-5;

        R = 4;

        Ac_nom = [0, 0; 0, -(1/(R*C))];
        Bc_nom = [(1/L); 0];

        Ao_nom = [0, -(1/L); (1/C), -(1/(R*C))];
        Bo_nom = [0; 0];
        
        % switching period
        T = 4e-6;
        Tmax = T*1000;
        
        % input (source) voltage
        Vs = 20;
        % desired output voltage
        Vref = 15;
        % duty cycle (boost if D > .5, buck if D < .5)
        % Vo = Vs D / (1 - D)
        % Vo / Vs = D / (1 - D)
        % (Vo / Vs) * (1 - D) = D
        % Vo/Vs - DVo/Vs = D
        % Vo/Vs = D(1 + Vo/Vs)
        % Vo/Vs / (1 + Vo/Vs) = D
        %D = Vref/Vs / (1 + Vref/Vs);
        D = Vref/(Vref + Vs);
        
        outname = 'buckboost';
    otherwise
        % error
        'Error'
end



if option_extrema
    sys = matrices_extrema(R,L,C);
else
    sys(1).Ac = Ac_nom;
    sys(1).Bc = Bc_nom;
    sys(1).Ao = Ao_nom;
    sys(1).Bo = Bo_nom;
    
    % set parameters used in stateflow simulation
    a00c = sys(1).Ac(1,1);
    a01c = sys(1).Ac(1,2);
    a10c = sys(1).Ac(2,1);
    a11c = sys(1).Ac(2,2);
    
    b0c = sys(1).Bc(1);
    b1c = sys(1).Bc(2);
    
    a00o = sys(1).Ao(1,1);
    a01o = sys(1).Ao(1,2);
    a10o = sys(1).Ao(2,1);
    a11o = sys(1).Ao(2,2);
    
    b0o = sys(1).Bo(1);
    b1o = sys(1).Bo(2);
end

% write spaceex file
write_spaceex(outname, 'buckboost', sys, length(sys), Tmax, T, D, Vs, i0, v0);