Plot Performance

This cell demonstrates the controllers setpoint tracking performance using the parameters found from running the genetic algorithm below
% Initialize Blackbox system
close all
blackBox
temp = get(0,'showHiddenHandles');
set(0,'showHiddenHandles','on');
hfig = gcf;
handles = guidata(hfig);
set(handles.radioFile, 'Value', 1);
set(handles.inputFile, 'String', 'stepInput');
 
 
% Configure start time, end time, step size, refine output
endTime = '60'; %100
stepSize = '0.1';%'0.5'
set(handles.axisStart, 'String', '0');
set(handles.axisEnd, 'String', endTime);
set(handles.stepSize, 'String', stepSize);
set(handles.refineOutput, 'String', '1');
 
 
% Assign PID Coefficients
assignin('base','Kp',-9.839119e-02);
assignin('base','Ki',-3.393445e-01);
assignin('base','Kd',3.752320e-03);
assignin('base','tau_f',0.3);
 
 
%Run SIM
blackBox('run_Callback',handles.run,[],handles);
ans = 'starting'
ans = 'Done Runnin''
 
sim_t = handles.axes2.Children.XData;
sim_y = handles.axes2.Children.YData;
% sim_r = interpolate_sim_r(handles.axes1.Children.YData,sim_y);
% [rise_time, overshoot] = GetMetrics(sim_t,sim_y,sim_r)
sim_r = handles.axes1.Children.YData;
close all
figure(10)
plot(sim_t, arrayfun(@(t) stepInput(t),sim_t),sim_t, sim_y)
ylabel("output")
xlabel("time")
title("System Setpoint tracking with PID control")
 
set(0,'showHiddenHandles',temp);
 
 

Genetic Algorithm Run

This cell runs the genetic algorithm on the black box system and returns the optimal tuning parameters
% Initialize Blackbox system
close all
blackBox
temp = get(0,'showHiddenHandles');
set(0,'showHiddenHandles','on');
hfig = gcf;
handles = guidata(hfig);
 
% Set Input File Name
set(handles.radioFile, 'Value', 1);
set(handles.inputFile, 'String', 'stepInput');
 
 
% Configure start time, end time, step size, refine output
endTime = '100';
stepSize = '0.5';
set(handles.axisStart, 'String', '0');
set(handles.axisEnd, 'String', endTime);
set(handles.stepSize, 'String', stepSize);
set(handles.refineOutput, 'String', '1');
 
 
% Define the boundaries for the PID parameters
fun = @(P) PIDFitness(P);
lower_bound =[-0.05 -0.7 -0.01 0];
upper_bound = [0.15 0.5 0.01 1];
 
 
% Run the genetic algorithm
global cur_min cur_best fileID;
cur_min = 1e100;
cur_best = [-1 -1 -1 -1];
options = optimoptions('ga','FunctionTolerance',100,'Display','final')
[bestE, MSE, exitflag,output,population,scores] = ga(fun, 4,[],[],[],[],lower_bound,upper_bound,[],options)

Helper Functions

function mse = PIDFitness(P)
% Fitness Function for the genetic Algoirthm
global cur_min;
global cur_best;
global fileID;
break_itr = 0;
 
X = fprintf('Kp: %d, Ki: %d, Kd: %d, tau_f: %d',P(1),P(2),P(3),P(4));
disp(X);
 
assignin('base','Kp',P(1));
assignin('base','Ki',P(2));
assignin('base','Kd',P(3));
assignin('base','tau_f',P(4));
hfig = findobj( 'Type', 'Figure', 'Name', 'blackBox' );
handles = guidata(hfig);
try
break_itr = break_itr + 1;
if break_itr > 5
mse = 1e100;
end
 
blackBox('run_Callback',handles.run,[],handles);
handles = guidata(hfig);
sim_y = handles.axes2.Children.YData;
sim_r = interpolate_sim_r(handles.axes1.Children.YData,sim_y);
 
temp = trapz((sim_y-sim_r).^2);
 
if temp < cur_min
cur_min = temp;
cur_best = [P(1) P(2) P(3) P(4)];
end
 
fprintf(fileID,'%f, %f, %f, %f, %f,',P(1),P(2),P(3),P(4),temp);
disp(cur_min);
disp(cur_best);
mse = temp;
 
catch
 
break_itr = break_itr + 1;
if break_itr > 5
mse=1e100;
end
fprintf(fileID,'%f, %f, %f, %f, %f,',P(1),P(2),P(3),P(4),1e100);
display("Failed")
mse=1e100;
end
 
end
 
function [rise_time, overshoot] = GetMetrics(sim_t,sim_y,sim_r)
%Given a simulation output, return the rise_time and overshoot
t=4;
setpoint = sim_r(end);
 
not_crossed_setpoint = 1;
for c= 1:length(sim_y)
if sim_y(c) > setpoint
rise_time = sim_t(c)-4;
not_crossed_setpoint = 0;
break
end
end
if not_crossed_setpoint
rise_time = 1000;
end
if max(sim_y) < setpoint
overshoot = 1000;
else
overshoot = max(sim_y) - setpoint;
end
end
 
 
 
function new_sim_r = interpolate_sim_r(sim_r,sim_y)
% Interpolate the simulation values to use the same time steps
[step_val, step_loc] = max(sim_r);
sim_r_scaled = ones(1,length(sim_y));
new_step_loc = fix((step_loc/length(sim_r))*length(sim_r_scaled));
sim_r_scaled(1,1:new_step_loc) = min(sim_r);
sim_r_scaled(1,new_step_loc:length(sim_r_scaled)) = step_val;
new_sim_r = sim_r_scaled;
end