% DARM Open Loop Simulato

% constants
Larm = 4000;
c = 299792458;
fsr = c/2/Larm;
ITMT = 0.014;
fc = fsr * ITMT / (4*pi); % wrong

f = logspace(-2, 4, 300);
w = 2*pi*f;
fname = '../chans/C1LSC.txt';

% The Total Digital Compensator
%DARM_FILT = Lm1*Lm2*Lm3*Lm4*Lm5;
DARM_FILT = onlinefilter(fname, 'LSC_DARM', 1,2,4,6,9, 'analogSYS');





% Gain Knob
DARM_GAIN = -1/1000;


% Pendulum
Q = 10;

w0 = 2*pi*1; % Angular frequency

pendTF = tf(w0^2,[1 w0/Q w0^2]);


% Arm Cavity
OpticalGain = 1e6;   % Watts/ meter
fc = 8 * fc;         % RSE moves the pole up
armTF = tf(2*pi*fc, [1 2*pi*fc]);
armTF = armTF * OpticalGain;

% Photodiode gain + response
Responsivity = 0.71;  % Amperes / Watt

Zrf = 200 * 10; % passive transimpedance + pre-amp w/ G = 10

RFPD = Zrf;  % neglecting the RF resonance response


% LSC Anti-Alias filter from D13XXX
%[z,p,k] = cheby1(4, 1, 2*pi*7500, 's');
%AAfilt = zpk(z,p,k);

%[z,p,k] = cheby1(4, 1, 2*pi*7500, 's');
%AIfilt = zpk(z,p,k);
AAfilt = onlinefilter(fname, 'LSC_AS55_Q', 5,'analogSYS');
AIfilt = onlinefilter(fname, 'LSC_AS55_Q', 6,'analogSYS');


% ADC Time Delay
adc_delay = 50e-6;

% DAC Time Delay
dac_delay = 50e-6;

time_delay = adc_delay + dac_delay;

ADCfac = 2^16 / 40;  % 16-bit ADC; 40 Vpp range

DACfac = 20 / 2^18;  % 18-bit ADC; 20 Vpp range

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Setup for locally damped pendulum	   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Chebychev bypass (0 means filter in)
bypass = 0;


% damping gain
kd = 1.2;  %  Q ~ 8

% Local Damping Filters
[z,p,k] = cheby1(10,1,75.4,'s');
p2 = p(1:2);
k2 = abs(p(1))*abs(p(2));
p8 = p(3:10);
k8 = abs(p(3))*abs(p(4))*abs(p(5))*abs(p(6))*abs(p(7))*...
    abs(p(8))*abs(p(9))*abs(p(10));


ch2 = zpk(z,p2,k2);
ch8 = zpk(z,p8,k8);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Makes the State-Space object from the .mdl
[A,B,C,D] = linmod('darm_02_L');
warning off
darmsys = ss(A,B,C,D);



