% ss_pp_design.m
% state-space pole-placement regulator design for pole balancer

format compact
clc    % clear command window
clear  % clear MatLab memory

% system parameters
disp ('system parameters:');
M = 1;  m = 0.1;  l = 0.5;  g = 9.81;
a = 2/3*l*(M + m) - m*l/2
b = g*(M + m)

% state-space model
A = [0 1; b/a 0]
B = [0; -1/a]
C = [1 0]
D = [0]

% define desired closed-loop poles
P1 = [-4 -4]    % critical damping with Ts = 4*tau = 1 sec
P2 = [(-4 + 4j) (-4 - 4j)]       % underdamped with zeta = 0.707

% Use Ackerman's forumla to find required state gain vectors
K1 = acker(A, B, P1)
K2 = acker(A, B, P2)

% Find closed-loop system state-space model
clss1 = ss ((A - B*K1), B, C, D);
clss2 = ss ((A - B*K2), B, C, D);

% Simulate IC response (with IC = 0.1 radians = 5.7 deg)
initial (clss1, [0.1; 0])
hold on
initial (clss2, [0.1; 0])
hold off
legend ('critical damping', 'underdamped')

% pause for the user and remove figures
display ('Hit Enter to continue');
pause
close all
