% Angel L. Carrion
% AIAA Landing Gear Design
%
% [upsw_angle_deg, toff_angle_deg, tov_angle_deg] = ...
% lgearfunction(x_nosegear, x_maingear, y_maingear,x_cg_aft, ...
% x_cg_forward,z_cg_high,W_to,h_fus,l_cargo)
%
% upsw_angle_deg = angle between bottom of main wheel and lowest...
% tail part (end of cargo bay)
% toff_angle_deg = angle between bottom of main wheel and aft cg
% tov_angle_deg = turnover angle


function [upsw_angle_deg, toff_angle_deg, tov_angle_deg, pctwt_nose] = lgearfunction(x_nosegear,...
   x_maingear,y_maingear,x_cg_aft, x_cg_forward,z_cg_high)


l_cargo = 40;					% Length of the nose plus length of the cargo bay (ft)
h_fus = 9.5;					% Maximum height of fuselage (ft)
z_ground2fus = 2;				% distance from ground to bottom of fuselage, ft

% for turnover angle (feet)
l_half_noseaxle = 0.8; % dist from middle of nose strut to middle ...
							  %of nose wheel (half axle length)

x_nose2main = x_maingear - x_nosegear;   % dist from nose strut to main strut
x_cg_aft2nose = x_cg_aft - x_nosegear;  % dist betw aft CG and nose strut
x_cg_aft2main = x_nose2main - x_cg_aft2nose;  % dist betw aft CG and main strut
x_fwdcg2nose = x_cg_forward - x_nosegear;   % dist betw fwd CG and nose strut
z_gr2cg = z_cg_high-1;

% angle between aft cg and bottom of main wheel (take-off angle):
toff_angle_deg = atan((x_maingear - x_cg_aft)/z_cg_high) * 180/pi;

% %%%%%%%%%%%%%%%%%%
% Turnover angle 
alpha = atan((y_maingear - l_half_noseaxle)/x_nose2main) * 180/pi;
x = l_half_noseaxle/tan(alpha*pi/180);
% turnover angle should be less than 63 degrees (currey, p37)
tov_angle_deg = atan(z_gr2cg/((x_cg_aft2nose + x)*sin(alpha*pi/180))) * 180/pi;
% end turnover angle
% %%%%%%%%%%%%%%%%%%

% %%%%%%%%%%%%%%%%%%
% Uwspeep clearance angle (angle between bottom of main gear and tail, ~12-15 deg):
upsw_angle_deg = atan(2/(l_cargo - x_maingear)) * 180/pi;
% end uwspeep clearance angle 
% %%%%%%%%%%%%%%%%%%

pctwt_nose = 100*(x_maingear - x_cg_aft)/(x_maingear - x_nosegear);


return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  The following stuff is NOT necessary for the OPTIMIZER:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% for shock stroke and other dimensions:
teff  = 0.47;  % tire efficiency
seff  = 0.8;   % stut efficiency
Nreact = 3;    % reaction factor (Currey, p 79)
g = 9.8*3.28;  % gravity, ft/sec^2
tdeflM = 0.33; % main tire deflection, ft
vsinkM = 15;   % main sink speed, ft/sec  (Currey, p 78)
vsinkN = 10;   % nose sink speed, ft/sec  (Currey, p 78)
pstat = 1500;  % shock pressure at static deflection, psi

%  for tire selection
ntires = 2;          % # of tires per strut
safety_fact = 1.07;  % safety factor (Currey, p. 31)

%  for Retraction Bars:
coef_frict = 0.95;    % Maximum coeff of friction, (Currey, p132)
FOS = 1.5;            % Design factor of safety
E_titanium = 16e6;    % Titanium Young's Modulus , psi
Fty_titanium = 122e3; % Titanium Tensile yield strength, psi
rho_titanium = 0.16;  % Titanium density, lbs/in^3
% *******************************************************************


% getting positions, all in feet:
x_nose2main = x_maingear - x_nosegear;   % dist from nose strut to main strut
x_cg_aft2nose = x_cg_aft - x_nosegear;  % dist betw aft CG and nose strut
x_cg_aft2main = x_nose2main - x_cg_aft2nose;  % dist betw aft CG and main strut
x_fwdcg2nose = x_cg_forward - x_nosegear;   % dist betw fwd CG and nose strut


%  getting strut loads:
maxstatmain = W_to*(x_nose2main-x_cg_aft2main)/(2*x_nose2main); % Max static load on main struts
maxstatnose = W_to*(x_nose2main-x_fwdcg2nose)/x_nose2main;      % Max static load on nose strut
minstatnose = W_to*(x_nose2main-x_cg_aft2nose)/x_nose2main;     % Min static load on nose strut
noseloadperc = minstatnose/W_to * 100;                          % Min Nose load to W_to ratio 
maxbraknose = maxstatnose + 10*z_gr2cg*W_to/(32.2*x_nose2main);    % Max braking load on nose


% **********************************************************************
% Tire selection:
max_t_M = maxstatmain * safety_fact / ntires; % maximum main tire load
max_t_N = maxbraknose * safety_fact / ntires; % maximum nose tire load

% choose appropriate tires using table in Currey, p 32:
%   main:  (type VII, rated for 13,000 lbs)
t_diam_M = 25.5;   % Main tire diameter, in
t_width_M = 6.85;  % Main tire width, in
t_weight_M = 35.5; % Main tire weight, lbs
%   nose:  (type VII, rated for 9300 lbs)
t_diam_N = 17.8;   % Nose tire diameter, in
t_width_N = 5.6;   % Nose tire width, in
t_weight_N = 13.7; % Nose tire weight, lbs


% **********************************************************************
% For lateral location of main gear (all dims in feet) and turnover angle 
alpha = atan((y_maingear - l_half_noseaxle)/x_nose2main) * 180/pi;
x = l_half_noseaxle/tan(alpha*pi/180);

% turnover angle should be less than 63 degrees (currey, p37)
tov_angle = atan(z_gr2cg/((x_cg_aft2nose + x)*sin(alpha*pi/180))) * 180/pi;


% **********************************************************************
% For MAIN shock strut dimensions, Oleo-Pneumatic shock
%  piston area and diameter
pareaM = maxstatmain/pstat;   % piston area at static deflection, in^2
pdiamM = 2 * sqrt(pareaM/pi); % piston diam at static deflection, in

%  main shock stroke is rounded to next integer to give it a safety margin
minstrokeM = 12 * (vsinkM^2/(2*g*Nreact) - teff*tdeflM)/seff;  % inches
strokeM = ceil(minstrokeM);
strokeincrM = strokeM-minstrokeM;  % main safety increase (<= 1 inch)

%  total length (extended):
permolM = 2.75 * pdiamM;          % main minimum permissible overlap (Currey, p.111)
smaxlengthM = permolM + strokeM;  % main maximum shock length (extended)


% Dimensions of Main retraction bars (look at kinematics sketch):
theta_M = 45;         % degrees
l1_M = (permolM * 1.1 + t_diam_M)/2;  % 1.1 factor to account for joints
l2_M = l1_M;

l_bar1_M = l1_M + l2_M - permolM;          % main bar 1 length
l_bar2_M = l2_M/sin(theta_M * pi/180); % main bar 2 length

V_load_M = maxstatmain  * FOS;     % vertical load on truss
H_load_M = V_load_M * coef_frict;  % horizontal load on truss

load_bar1_M = V_load_M + H_load_M/cos(theta_M * pi/180);
load_bar2_M = H_load_M/sin(theta_M * pi/180);

d1_M = (4*l_bar1_M^2*load_bar1_M/(pi^3 * E_titanium))^0.25 * 2;	%inches
d2_M = (4*l_bar2_M^2*load_bar2_M/(pi^3 * E_titanium))^0.25 * 2;	%inches

% end MAIN shock strut dimensions
% **********************************************************************


% **********************************************************************
% For NOSE shock strut dimensions, Oleo-Pneumatic shock
pareaN = maxstatnose/pstat;   % nose piston area at static deflection, in^2
pdiamN = 2 * sqrt(pareaN/pi); % nose piston diam at static deflection, in

%  stroke:
tdeflN = 0.48 * t_diam_N/2 /12;  % nose tire deflection, ft (48% of tire radius Currey, p 33)
%  nose shock stroke is rounded to next integer to give it a safety margin
minstrokeN = 12 * (vsinkN^2/(2*g*Nreact) - teff*tdeflN)/seff;  % inches
strokeN = ceil(minstrokeN);
strokeincrN = strokeN-minstrokeN; % nose safety increase (<= 1 inch)

%  total length (extended):
permolN = 2.75 * pdiamN;          % nose minimum permissible overlap (Currey, p.111)
smaxlengthN = permolN + strokeN;  % nose maximum shock length (extended)

% Dimensions of Nose retraction bars (look at kinematics sketch):
theta_N = 45;         % degrees
l1_N = (permolN * 1.1 + t_diam_N)/2;  % 1.1 factor to account for joints
l2_N = l1_N;

l_bar1_N = l1_N + l2_N - permolN;          % nose bar 1 length
l_bar2_N = l2_N/sin(theta_N * pi/180);     % nose bar 2 length

V_load_N = maxbraknose  * FOS;             % vertical load on truss
H_load_N = V_load_N * coef_frict;          % horizontal load on truss

load_bar1_N = V_load_N + H_load_N/cos(theta_N * pi/180);
load_bar2_N = H_load_N/sin(theta_N * pi/180);

d1_N = (4*l_bar1_N^2*load_bar1_N/(pi^3 * E_titanium))^0.25 * 2;	%inches
d2_N = (4*l_bar2_N^2*load_bar2_N/(pi^3 * E_titanium))^0.25 * 2;	%inches

% end NOSE shock strut dimensions
% **********************************************************************


fprintf('Positions and loads on struts (lbs): \n\n')
fprintf(' Take-off weight (lbs):                       %8i\n',W_to)
fprintf(' X-position of nose gear (ft):                  %8.2f\n',x_nosegear)
fprintf(' X-position of main gear (ft):                  %8.2f\n',x_maingear)
fprintf(' Y-position of main strut (ft):                 %8.2f\n\n',y_maingear)
fprintf(' Maximum static load on main strut (per strut): %8.2f\n',maxstatmain)
fprintf(' Maximum static load on nose strut:             %8.2f\n',maxstatnose)
fprintf(' Minimum static load on nose strut:             %8.2f\n',minstatnose)
fprintf(' Ratio between W_to and min nose load (percent): %7.2f\n',noseloadperc)
fprintf(' Maximum load on nose strut during braking:     %8.2f\n',maxbraknose)

fprintf('\n\nTires: \n\n')
fprintf(' Main tire maximum load (lbs): %25.2f\n',max_t_M)
fprintf(' Main tire diameter (in): %30.2f\n', t_diam_M)
fprintf(' Main tire width (in): %33.2f\n\n', t_width_M)
fprintf(' Nose tire maximum load (lbs): %25.2f\n',max_t_N)
fprintf(' Nose tire diameter (in): %30.2f\n', t_diam_N)
fprintf(' Nose tire width (in): %33.2f\n', t_width_N)

fprintf('\n\nImportant angles:\n\n')
fprintf(' Turnover angle, (< 63 deg): %27.2f\n',tov_angle);
fprintf(' Upsweep clearance angle, (~12 deg): %19.2f\n',upsw_angle_deg);
fprintf(' CG-main gear angle, (13-15 deg):    %19.2f\n',toff_angle_deg);

fprintf('\n\nShock dimensions (all in inches):\n\n')
fprintf('  Main strut diameter: %33.2f\n', pdiamM)
fprintf('  Main strut stroke:   %33.2f\n', strokeM)
fprintf('  Main strut stroke margin: %28.2f\n', strokeincrM)
fprintf('  Main strut length:   %33.2f\n\n', smaxlengthM)
fprintf('  Nose strut diameter: %33.2f\n', pdiamN)
fprintf('  Nose strut stroke:   %33.2f\n', strokeN)
fprintf('  Nose strut stroke margin: %28.2f\n', strokeincrN)
fprintf('  Nose strut length:   %33.2f\n', smaxlengthN)

fprintf('\n\nRetraction Mechanism bars:\n\n');
fprintf('  Main bar1 load (lbs): %32.2f\n',load_bar1_M);
fprintf('  Main bar2 load (lbs): %32.2f\n',load_bar2_M);
fprintf('  Nose bar1 load (lbs): %32.2f\n',load_bar1_N);
fprintf('  Nose bar2 load (lbs): %32.2f\n',load_bar2_N);
fprintf('  Main bar1 diam (in): %33.2f\n',d1_M);
fprintf('  Main bar2 diam (in): %33.2f\n',d2_M);
fprintf('  Nose bar1 diam (in): %33.2f\n',d1_N);
fprintf('  Nose bar2 diam (in): %33.2f\n',d2_N);
