main content

calibrate ecms block for objective hybrid vehicle fuel economy assessment -凯发k8网页登录

when you compare fuel economy changes in a hybrid vehicle drive cycle, you must account for the amount of energy stored in the vehicle battery. otherwise, net battery charging or depletion during the drive cycle can artificially inflate or deflate the fuel economy estimate for the drive cycle. powertrain blockset allows you to simulate the fuel economy, performance, and emissions of hybrid electric vehicles with standard hybrid powertrain architectures p0, p1, p2, p3, and p4.

powertrain blockset uses an industry-standard equivalent consumption minimization strategy (ecms) to optimize the fuel economy of hybrid reference applications by optimally trading off internal combustion engine (ice) use versus electric motor use during a given drive-cycle.

an ecms block in the hybrid controller of each hybrid reference application optimizes trading off engine and motor use during the cycle. the ecms block has a parameter in its block mask called ecms_s that you can set so that the hybrid battery state of charge (soc) at the end of a given drive cycle is the same as the soc at the beginning of the drive cycle.

this script optimizes the ecms_s parameter automatically. the script runs a drive-cycle simulation, measures starting and ending soc, and repeats the process until the difference between starting and ending soc is minimized.

during the optimization process, an optimization plot indicates the progress of the optimizer toward net zero change in soc over the given driven cycle for the chosen vehicle configuration.

note that the optimization process may take more than one hour to complete. after optimization completes, the ecms_s parameter for net zero change in cycle soc is stored in the model workspace of the optimal control model reference.

ecms is in hev reference application controllers

ecms block helps achieve optimal fuel economy results

ecms block parameter ecms_s ensures starting and ending battery soc are equal

ecms calibration script

use this code to calibrate the ecms_s block parameter for net zero change in battery soc for any of the powertrain blockset hybrid reference applications p0, p1, p2, p3, and p4.

choose hev reference application and run optimization

% open a given reference application
pxname="p2"; % hev powertrain architecture configuration: p0,p1,p2,p3,p4
eval("autoblkhev" pxname "start"); %start a px reference application
socinit = 0.85; % initial soc, unit is in [0, 1]
ecms_s=calibrate_ecms_s(pxname,socinit); %run ecms_s optimization and return optimal ecms_s for net zero change in soc over the drive-cycle

elapsed time is 420.625345 seconds.
search converged. ecms_s parameter updated in model. 
ecms_s = 4.760604, socendtrg = 85.000000, soc_end = 85.413778 

execute ecms_s calibration function

function ecms_s=calibrate_ecms_s(pxname,socinit)
    % calibration_ecms_script
    battmdl = "batthev" pxname; % battery model - for soc sweep
    mdlname="hev" pxname "referenceapplication"; % hev model
    ctrlmdl="hev" pxname "optimalcontroller"; % controller model
    socendtrg = socinit*100;
    maxiter=4;
    
    % set up optimization progress plot window size and position
    x0=600;
    y0=1040;
    width=600;
    height=280;
    
    tic
    load_system(mdlname);
    load_system(ctrlmdl);
    load_system(battmdl);
    
    blk = mdlname   "/"   "drive cycle source";
    m = get_param(blk, 'maskobject');
    ecms_currentcycle = m.parameters(1,1).value; % current cycle setting.
    
    mdlwks = get_param(ctrlmdl,'modelworkspace');  % find model workspace
    % get current ecms_s value
    ecms_obj = getvariable(mdlwks,'ecms_s');
    if  isnumeric(ecms_obj); ecms_currentvalue = ecms_obj; end
    if ~isnumeric(ecms_obj); ecms_currentvalue = ecms_obj.value; end
    
    % extract initial value/name of ecms tuning parameter
    p = simulink.mask.get(ctrlmdl "/ecms");
    baseparamname=p.getparameter("ecms_s").value;
    battchrgmaxvalue_obj = getvariable(get_param(battmdl,'modelworkspace'),'battchargemax');
    if  isnumeric(battchrgmaxvalue_obj); battchrgmaxvalue = battchrgmaxvalue_obj; end
    if ~isnumeric(battchrgmaxvalue_obj); battchrgmaxvalue = battchrgmaxvalue_obj.value; end
    
    % set to a temp name
    set_param(ctrlmdl "/ecms",'ecms_s',"ecms_s_tune")
    save_system(mdlname,[],'savedirtyreferencedmodels','on');
    save_system(ctrlmdl,[],'savedirtyreferencedmodels','on');
    
    % enable soc recording
    x1_handles = get_param(mdlname "/visualization/rate transition1",'porthandles');
    x1 = x1_handles.outport(1);
    simulink.sdi.marksignalforstreaming(x1,'on');
    
    % arrays used to store ecms_s and soc_end values
    xc = zeros (3,1);
    yc = zeros (3,1);
    
    % plot showing the ecms_s and soc_end
    subplot (1,2,1);
    set(gcf,'position',[x0,y0,width,height])
    xlabel({'ecms\_s',' '})
    ylabel('dsoc')
    hold on;
    subplot (1,2,2);
    xlabel('ecms\_s')
    ylabel('soc')
    hold on;
    sgtitle([ecms_currentcycle ' drive-cycle soc balance']);
    % set model workspace variables
    in = modelsetvariable (mdlname, battchrgmaxvalue, socinit,battmdl, socendtrg, ctrlmdl);
    % get initial 3 ecms_s points and soc_end values
    [x, y, ecms_s, soc_end, solution_found] = dsoc_generate_starting_points (in, ecms_currentvalue, socendtrg);
    
    if ~solution_found
        
        for i = 1 : maxiter
           
            % solve second order equation to get z = ecms_s corresponds to y = socendtrg
            

socendtrg=socinit=ax2 bx c,wherea,b,csatisfiesyi=axi2 bxi c,1i3.withx1=ecmss(1),y1=socend(ecmss(1)),x2=ecmss(2),y2=socend(ecmss(2)),x3=ecmss(3),y3=socend(ecmss(3)).

            [z] = second_order_roots (x, y, socendtrg);
            in = in.setvariable("ecms_s_tune", z);
            [soc_end, dsoc] =dsocsim_v1(in);
            subplot (1,2,1);
            plot(z,dsoc,'bs','markersize',15)
            grid on
            hold on
            subplot (1,2,2);
            plot(z,soc_end,'bs','markersize',15)
            grid on
            hold on
            
            if (abs(soc_end - socendtrg) <= 1) % check if a solution is found
                ecms_s = z;
                x(1) = ecms_s;
                y(1) = soc_end;
                solution_found = 1;
                break;
            end
            
            xc(1:3) = x (1:3); yc(1:3) = y (1:3); 
            x(1) = z; y(1) = soc_end; % since z and soc_end are new points, we use them and put into x(1), y(1)
            [yb, ii] = sort(yc,'ascend'); % sort the array for processing
            xb = xc(ii);
            
            % begin the process to pick other two points from the original 3 point
            % xb(1:3), yb(1:3)
            
            if (y(1) > socendtrg) % y(1)> socendtrg, we need to pick other points < socendtrg
                if ((yb(2) < socendtrg) && (socendtrg < yb(3)))  % yb(2) < socendtrg, pick yb(2)
                    x(2) = xb(2); y(2) = yb(2);
                    if (abs(yb(1)-socendtrg) < abs(yb(3)-socendtrg)) % pick last point from xb(1), and xb(3), according to shortest distance to socendtrg
                        x(3) = xb(1); y(3) = yb(1);
                    else
                        x(3) = xb(3); y(3) = yb(3);
                    end
                end
                if ((yb(1) < socendtrg) && (socendtrg < yb(2))) % yb(1) < socendtrg, pick yb(1)
                    x(2) = xb(1); y(2) = yb(1);
                    if (abs(yb(2)-socendtrg) < abs(yb(3)-socendtrg)) % pick last point from xb(2) and xb(3), according to shortest distance to socendtrg
                        x(3) = xb(2); y(3) = yb(2);
                    else
                        x(3) = xb(3); y(3) = yb(3);
                    end
                end
            end
            if (y(1) < socendtrg) % y(1)< socendtrg, we need to pick other points > socendtrg
                if ((yb(2) < socendtrg) && (socendtrg < yb(3))) % yb(3) > socendtrg, pick yb(3)
                    x(2) = xb(3); y(2) = yb(3);
                    if (abs(yb(1)-socendtrg) < abs(yb(2)-socendtrg)) % pick last point from xb(1) and xb(2), according to shortest distance to socendtrg
                        x(3) = xb(1); y(3) = yb(1);
                    else
                        x(3) = xb(2); y(3) = yb(2);
                    end
                end
                if ((yb(1) < socendtrg) && (socendtrg < yb(2))) % yb(2) > socendtrg, pick this point
                    x(2) = xb(2); y(2) = yb(2);
                    if (abs(yb(1)-socendtrg) < abs(yb(3)-socendtrg)) % pick last point from xb(1) and xb(3), according to shortest distance to socendtrg
                        x(3) = xb(1); y(3) = yb(1);
                    else
                        x(3) = xb(3); y(3) = yb(3);
                    end
                end
            end
        end
        
        y_abs = abs(y-socendtrg);
        [yb, ii] = sort(y_abs,'ascend');
        xb = x(ii);
        ecms_s = xb (1);
        
    end
    
    hold off;
    
    toc;
    
    if solution_found
        fprintf ('search converged. ecms_s parameter updated in model. \n');
        fprintf ('ecms_s = %f, socendtrg = %f, soc_end = %f \n',ecms_s, socendtrg, soc_end);
    else
        fprintf ('search failed to converge. an approximate ecms_s is updated in the model. \n');
        fprintf ('refer to the troubleshooting section of the example page for recommendations. \n');
    end
    
    ecms_s_tune = ecms_s;
    % reset model
    simulink.sdi.marksignalforstreaming(x1,'off');
    load_system(ctrlmdl)
    set_param(ctrlmdl "/ecms",'ecms_s',baseparamname)
    save_system(mdlname,[],'savedirtyreferencedmodels','on');
    
    % update model and sim
    hws = get_param(ctrlmdl, 'modelworkspace');% get the workspace
    hws.assignin('ecms_s',ecms_s);
    save_system(ctrlmdl,[],'savedirtyreferencedmodels','on');
    open_system(mdlname);
    load_system(battmdl);
    battchrgmaxvalue_obj = getvariable(get_param(battmdl,'modelworkspace'),'battchargemax');
    if  isnumeric(battchrgmaxvalue_obj); battchrgmaxvalue = battchrgmaxvalue_obj; end
    if ~isnumeric(battchrgmaxvalue_obj); battchrgmaxvalue = battchrgmaxvalue_obj.value; end
    in = in.setvariable('battcapinit', battchrgmaxvalue*socinit,'workspace',battmdl);
    in = in.setvariable('soctrgt', socendtrg,'workspace',ctrlmdl);
    in = in.setvariable('socmin', max(socendtrg-20,20.5),'workspace',ctrlmdl);
    in = in.setvariable('socmax', min(socendtrg 20,100),'workspace',ctrlmdl);
    set_param(ctrlmdl "/ecms",'ecms_s',"ecms_s");
    save_system(battmdl);
    save_system(ctrlmdl);
end
function in = modelsetvariable (mdlname, battchrgmaxvalue, socinit, battmdl, socendtrg, ctrlmdl) 
in = simulink.simulationinput(mdlname);
in = in.setvariable('battcapinit', battchrgmaxvalue*socinit,'workspace',battmdl);
in = in.setvariable('soctrgt', socendtrg,'workspace',ctrlmdl);
in = in.setvariable('socmin', max(socendtrg-20,20.5),'workspace',ctrlmdl); 
in = in.setvariable('socmax', min(socendtrg 20,100),'workspace',ctrlmdl);
end
%% 
function [x_out, y_out, ecms_s, soc_end, solution_found] = dsoc_generate_starting_points (in, ecms_currentvalue, socendtrg)
x_out = zeros (3,1);
y_out = zeros (3,1);
x = zeros (4,1);
y = zeros (4,1);
solution_found = false;
ecms_s = ecms_currentvalue;
alpha = 0.8;
tol = 1;
x1 = ecms_currentvalue;  % use current ecms_s value as the starting point
in = in.setvariable("ecms_s_tune", x1);
[soc_end,  dsoc] =dsocsim_v1(in);  %evaluate x1 results
y1 = soc_end;
subplot (1,2,1);
plot(x1,dsoc,'bs','markersize',15)
grid on
hold on
subplot (1,2,2);
plot(x1,y1,'bs','markersize',15)
grid on
hold on
if (abs(y1 - socendtrg) <= tol)  % check if a solution found
    ecms_s  = x1;
    soc_end = y1;
    solution_found = true;
end
if ((y1 > socendtrg) && ~solution_found)
    x2 = x1 - ecms_s_dis_up (x1, y1, socendtrg); % use a decreased ecms_s value as x2
    in = in.setvariable("ecms_s_tune", x2);
    [soc_end, dsoc] =dsocsim_v1(in);
    y2 = soc_end;
    subplot (1,2,1);
    plot(x2,dsoc,'bs','markersize',15)
    grid on
    hold on
    subplot (1,2,2);
    plot(x2,y2,'bs','markersize',15)
    grid on
    hold on
    if (abs(y2 - socendtrg) <= tol)  % check if a solution found
        ecms_s  = x2;
        soc_end = y2;
        solution_found = true;
    end
    if ((y2 > socendtrg) && ~solution_found) % y2 is still too high
        y3_try = socendtrg - 2; % set a lower soc end trial to it will be easier to get y2 < soc_endtrg
        x3 = x1   alpha*(y3_try - y1)*(x2-x1)/(y2-y1); % use x1, y1, x2, y2 to fit a linear function

x=x1 αy-y1y2-y1×(x2-x1) such that x=x1aty=y1,andx=αx2 (1-α)x1aty=y2

        dx = ecms_s_dis_up (x2, y2, socendtrg); % get a pre-defined decrease value
        x3 = min (x3, x2 -   dx);  % upper limit for x3
        x3 = max (x3, x2 - 2*dx);  % lower limit for x3
        in = in.setvariable("ecms_s_tune", x3); % set the x3 value as ecms_s
        [soc_end,  dsoc] =dsocsim_v1(in);       % evaluate
        y3 = soc_end;
        subplot (1,2,1);
        plot(x3,dsoc,'bs','markersize',15)
        grid on
        hold on
        subplot (1,2,2);
        plot(x3,y3,'bs','markersize',15)
        grid on
        hold on
        if (abs(y3 - socendtrg) <= tol) % check if a solution is found
        ecms_s  = x3;
        soc_end = y3;
        solution_found = true;
        end
        if ~solution_found
            x_out(1) = x1; x_out(2) = x2; x_out(3) = x3; y_out(1) = y1; y_out(2) = y2; y_out(3) = y3; % output 3 points
        end
    end
    if ~solution_found
        if ((y2 > socendtrg) && (y3 >= socendtrg)) % if y3 is still greater than socendtrg, lower it again
            for it_again = 1 : 10
                [x(4)] = second_order_roots (x_out, y_out, socendtrg - 2);  % decrease again
                dx = ecms_s_dis_up (x_out (3), y_out (3), socendtrg);   % standard decrease
                x(4) = max (x(4), x_out (3) -   dx);             % make it higher than the standard increase
                x(4) = min (x(4), x_out (3) - 2*dx);             % limit the increase to 2 times the standard
                in = in.setvariable("ecms_s_tune", x(4));
                [soc_end, ~] = dsocsim_v1(in);
                ecms_s = x(4);
                y(4) = soc_end;
                x_out (1) = x_out (2);   y_out (1) = y_out (2);
                x_out (2) = x_out (3);   y_out (2) = y_out (3);
                x_out (3) = x(4);        y_out (3) = soc_end;
                if (abs(y(4) - socendtrg) <= tol); break; end
                if (soc_end <= socendtrg); break; end
            end
            if (abs(y(4) - socendtrg) <= tol)
                ecms_s  = x(4);
                soc_end = y(4);
                solution_found = true;
            end
        end
    end
    if ((y2 < socendtrg) && ~solution_found) % y2 is lower than socendtrg while y1 is greater than socendtrg
        x_min = min (x1, x2); x_max = max (x1, x2); y_min = min(y1, y2); y_max = max (y1, y2); % sort the min and max
        w_min = abs (y_min - socendtrg) / (abs(y_min - socendtrg)   abs(y_max - socendtrg)); % weighting factor
        x3 = (1-w_min) * x_min   w_min * x_max; % x3 is a weighted interpolation between x1 and x2
        in = in.setvariable("ecms_s_tune", x3);
        [soc_end,  dsoc] =dsocsim_v1(in);
        y3 = soc_end;
        
        subplot (1,2,1);
        plot(x3,dsoc,'bs','markersize',15)
        grid on
        hold on
        subplot (1,2,2);
        plot(x3,y3,'bs','markersize',15)
        grid on
        hold on
        if (abs(y3 - socendtrg) <= tol)
        ecms_s  = x3;
        soc_end = y3;
        solution_found = true;
        end
    end
    if ~solution_found
        x_out(1) = x1; x_out(2) = x2; x_out(3) = x3; y_out(1) = y1; y_out(2) = y2; y_out(3) = y3; % output 3 points
    end
end
if ((y1 < socendtrg) && ~solution_found) % y1 < socendtrg
    x2 = x1   ecms_s_dis_up (x1, y1, socendtrg); % use a higher ecms_s value as x2
    in = in.setvariable("ecms_s_tune", x2);
    [soc_end, dsoc] =dsocsim_v1(in);
    y2 = soc_end;
    subplot (1,2,1);
    plot(x2,dsoc,'bs','markersize',15)
    grid on
    
    subplot (1,2,2);
    plot(x2,y2,'bs','markersize',15)
    grid on
    hold on
    if (abs(y2 - socendtrg) <= tol)
        ecms_s  = x2;
        soc_end = y2;
        solution_found = true;
    end
    if ((y2 < socendtrg) && ~solution_found) % x2 is not high enough
        y3_try = socendtrg   2;                    % set a higher soc target
         

x=x1 αy-y1y2-y1(x2-x1),x=x1,aty=y1,x=αx2 (1-α)x1aty=y2,

        x3 = x1   alpha*(y3_try - y1)*(x2-x1)/(y2-y1); % use x1, y1, x2, y2 as a linear prdeiction
        dx = ecms_s_dis_up (x2, y2, socendtrg);    % standard increase
        x3 = max (x3, x2     dx);                  % lower limit for x3
        x3 = min (x3, x2   2*dx);                  % upper limit for x3
        in = in.setvariable("ecms_s_tune", x3);
        [soc_end, dsoc] =dsocsim_v1(in);
        y3 = soc_end;
        subplot (1,2,1);
        plot(x3,dsoc,'bs','markersize',15)
        grid on
        subplot (1,2,2);
        plot(x3,y3,'bs','markersize',15)
        grid on
        
        if (abs(y3 - socendtrg) <= tol)
            ecms_s  = x3;
            soc_end = y3;
            solution_found = true;
        end
        
        if ~solution_found
            x_out(1) = x1; x_out(2) = x2; x_out(3) = x3; y_out(1) = y1; y_out(2) = y2; y_out(3) = y3; % output 3 points
        end
    end
    if ~solution_found
        if ((y2 < socendtrg) && (y3 <= socendtrg))  % y3 is still not high enough
            for it_again = 1 : 10
                [x(4)] = second_order_roots (x_out, y_out, socendtrg   2);  % increase again
                dx = ecms_s_dis_up (x_out (3), y_out (3), socendtrg);   % standard increase
                x(4) = max (x(4), x_out (3)     dx);             % make it higher than the standard increase
                x(4) = min (x(4), x_out (3)   2*dx);             % limit the increase to 2 times the standard
                in = in.setvariable("ecms_s_tune", x(4));
                [soc_end, dsoc] = dsocsim_v1(in);
                ecms_s = x(4);
                y(4) = soc_end;
                x_out (1) = x_out (2);   y_out (1) = y_out (2);
                x_out (2) = x_out (3);   y_out (2) = y_out (3);
                x_out (3) = x(4);        y_out (3) = soc_end;
                if (soc_end >= socendtrg); break; end
                if (abs(y(4) - socendtrg) <= tol); break; end
            end
            subplot (1,2,1);
            plot(x(4),dsoc,'bs','markersize',15)
            grid on
            subplot (1,2,2);
            plot(x(4),y(4),'bs','markersize',15)
            grid on
            if (abs(y(4) - socendtrg) <= tol)
                ecms_s  = x(4);
                soc_end = y(4);
                solution_found = true;
            end
        end
    end
    if ((y2 > socendtrg) && ~solution_found)  % y2 is good
        x_min = min (x1, x2); x_max = max (x1, x2); y_min = min(y1, y2); y_max = max (y1, y2); % get min and max for x1, x2, y1, y2
        w_min = abs (y_min - socendtrg) / (abs(y_min - socendtrg)   abs(y_max - socendtrg));   % weighted factor
        x3= (1-w_min) * x_min   w_min * x_max;  % get weighted interpolation between x1, and x2
        in = in.setvariable("ecms_s_tune", x3);
        [soc_end, dsoc] =dsocsim_v1(in);
        y3 = soc_end;
        subplot (1,2,1);
        plot(x3,dsoc,'bs','markersize',15)
        grid on
        subplot (1,2,2);
        plot(x3,y3,'bs','markersize',15)
        grid on
        if (abs(y3 - socendtrg) <= tol)
            ecms_s  = x3;
            soc_end = y3;
            solution_found = true;
        end
    end
    if ~solution_found
        x_out(1) = x1; x_out(2) = x2; x_out(3) = x3; y_out(1) = y1; y_out(2) = y2; y_out(3) = y3; % output three points
    end
end
end
function [soc_end, dsoc]=dsocsim_v1(in)
    evalc('out=sim(in)'); %simulate suppressing build info
    if isempty(out.errormessage)
        soc=out.logsout.getelement('battery soc').values.data;
        dsoc=soc(end)-soc(1);
        soc_end = soc(end);
    else
        dsoc=nan;
    end
    
end
function dx = ecms_s_dis_up (ecms_s, y, soc_target)
% function computes standard dx = increase/decrease in ecms_s
% the dx is proportional to the distance between soc_end (y) and soc_target
% also, dx is proportional to ecms_s, a, y, ff can be adjusted
ff = 0.05;
a  = 0.015;
c = 3.5;
b = ecms_s / c;   % ecms_s effect
if (ecms_s < c-0.1); b = 1; end
dx = a   b * abs(y-soc_target) * ff;
end
function [z] = second_order_roots (x, y, y_tar)

solve the equationytar=az2 bz c,withyi=axi2 bxi c,for1i3.

d1 = y(1) / (x(1) - x(2)) / (x(1) - x(3));
d2 = y(2) / (x(2) - x(1)) / (x(2) - x(3));
d3 = y(3) / (x(3) - x(1)) / (x(3) - x(2));
aa = (d1   d2   d3);
bb = - (d1 * (x(2)   x(3))   d2 * (x(1)   x(3))   d3 * (x(1)   x(2)));
cc = d1 * x(2) * x(3)   d2 * x(1) * x(3)   d3 * x(1) * x(2) - y_tar;
z1 = (-bb   sqrt (bb*bb - 4 * aa * cc)) / 2 / aa;
z = real(z1);
end

凯发官网入口首页 copyright 2020-2022, the mathworks, inc.

see also

网站地图