% Initialize Blackbox system
temp = get(0,'showHiddenHandles');
set(0,'showHiddenHandles','on');
set(handles.radioFile, 'Value', 1);
set(handles.inputFile, 'String', 'stepInput');
% Configure start time, end time, step size, refine output
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);
blackBox('run_Callback',handles.run,[],handles);
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;
plot(sim_t, arrayfun(@(t) stepInput(t),sim_t),sim_t, sim_y)
title("System Setpoint tracking with PID control")
set(0,'showHiddenHandles',temp);
% Initialize Blackbox system
temp = get(0,'showHiddenHandles');
set(0,'showHiddenHandles','on');
set(handles.radioFile, 'Value', 1);
set(handles.inputFile, 'String', 'stepInput');
% Configure start time, end time, step size, refine output
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_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)
function mse = PIDFitness(P)
% Fitness Function for the genetic Algoirthm
X = fprintf('Kp: %d, Ki: %d, Kd: %d, tau_f: %d',P(1),P(2),P(3),P(4));
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' );
break_itr = break_itr + 1;
blackBox('run_Callback',handles.run,[],handles);
sim_y = handles.axes2.Children.YData;
sim_r = interpolate_sim_r(handles.axes1.Children.YData,sim_y);
temp = trapz((sim_y-sim_r).^2);
cur_best = [P(1) P(2) P(3) P(4)];
fprintf(fileID,'%f, %f, %f, %f, %f,',P(1),P(2),P(3),P(4),temp);
break_itr = break_itr + 1;
fprintf(fileID,'%f, %f, %f, %f, %f,',P(1),P(2),P(3),P(4),1e100);
function [rise_time, overshoot] = GetMetrics(sim_t,sim_y,sim_r)
%Given a simulation output, return the rise_time and overshoot
not_crossed_setpoint = 1;
not_crossed_setpoint = 0;
overshoot = max(sim_y) - setpoint;
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;