function PU_System = mcb_SetPUSystem(motor,inverter)
% File          :   PU System Parameters File
% Description   :   Set base values for Per-Unit system for Motor Control Blockset

% Copyright 2020-2023 The MathWorks, Inc.
%%
% Get Per-Unit System parameters

PU_System.V_base   = (inverter.V_dc/sqrt(3));
PU_System.I_base   = inverter.ISenseMax;
if isfield(motor,'N_base')
    PU_System.N_base   = motor.N_base;
else
    inverter2.V_dc=max(inverter.V_dc,motor.I_rated*motor.Rs*sqrt(3));
    result=mcbPMSMSpeeds(motor,inverter2,"outputAll",0);
    PU_System.N_base=result(1);
end
PU_System.P_base   = (3/2)*PU_System.V_base*PU_System.I_base;
if ~isfield(motor,'motorType') || (isfield(motor,'motorType') && (strcmpi(motor.motorType,'ipmsm') || strcmpi(motor.motorType,'spmsm')))
    PU_System.T_base   = (3/2)*motor.p*motor.FluxPM*PU_System.I_base;
else
    motor2=motor;
    motor2.motorType='IPMSM';
    motor2.I_rated=PU_System.I_base;
    inverter2=inverter;
    inverter2.V_dc=max(inverter.V_dc,motor2.I_rated*motor2.Rs*sqrt(3));
    result=mcbPMSMSpeeds(motor2,inverter2,"outputAll",1);
    PU_System.T_base   = (3/2)*motor.p*(motor2.FluxPM*result(2,1)+(motor2.Ld-motor2.Lq)*result(1,1)*result(2,1));
    if PU_System.T_base==0 && PU_System.N_base>0
        PU_System.T_base=PU_System.P_base/(PU_System.N_base*pi/30);
    end
end