%File: design2_step.m
%Revised November 30, 2006 by Marco Vagani
echo on
%------------------------------------------------%
% This script computes and displays the closed   %
% loop step response of a 2nd order torsional    %
% mass spring damper system for the range of     %
% parameter values J, b, k.                      % 
% You will be prompted to enter your             %
% proportional and derivative gain values Kp     %
% and Kd                                         %
%------------------------------------------------%
echo off

%Get Student Name:
student_name = input('Enter your last name, student number = ','s');
if isempty(student_name); student_name='NoName'; end

%Get controller gains Kp, and Kd
Kp = input('Enter the proportional gain, Kp: ');
if isempty(Kp); Kp=0.2; end
Kd = input('Enter the derivative gain, Kd: ');
if isempty(Kd); Kd=0.002; end
tdelay = input('Enter the time delay constant, tdelay: ');
if isempty(tdelay); tdelay=0.006; end
kh = input('Enter the hardware gain value, kh (Ask your TA): ');
if isempty(kh); kh=0.24; end

%The range of parameter values
J(1)=0.008;  J(2)=0.006;  J(3)=0.01;
b(1)=0.05;   b(2)=0.02;   b(3)=0.08;
k(1)=1.6;    k(2)=1.4;    k(3)=1.8;
ke=25;

for i=1:3
    for j=1:3
        for l=1:3
%Computing the open loop transfer function for various combinations of
%parameter values
g=tf([ke*kh*Kd ke*kh*Kp],conv([tdelay 1],[J(i) b(j) k(l)]));
%Computing the closed loop transfer function
h=feedback(g,1);
%Plotting the step resonse of the closed loop system
step(h);
%Ensuring all plots remain in the same figure
hold on
        end
    end 
end
title([student_name, ': Torsional system step response, kp = [', num2str(Kp), '],kd = [', num2str(Kd),']']);
xlabel('Time');
ylabel('System response to Unit Step Input');
