% Output lookup for virtual archery
% All distances are in feet
fname= 'ArcheryOut.h';
fid= fopen(fname,'w');

% Set the title of the header and the definition protection
fprintf(fid,'/*********************************\r');
fprintf(fid,'   Archery Output Lookup Table\r');
fprintf(fid,'   Written by Michael Ross\r');
fprintf(fid,'*********************************/\r');
fprintf(fid,'#ifndef ARCHERYOUT_H\r#define ARCHERYOUT_H\r');

% Constants
s= 5*2/(11*8192);   % Sensitivity (Volts/LSB)
g= 32.174;  % Gravity
d= 20;      % Distance to target
v= 180;     % Arrow speed
N= -450:450;  % Inputs
r= 10;      % Target radius
c= 31;       % Correction factor - allows for a "bull's eye" state

a= g*N*s/.05;    % Measured x acceleration
% Output height
y= -g^3*d^2./(2*v^2*(g^2 - a.^2)) + d*a./(sqrt(g^2 - a.^2));

Out= round(100- 70*y/r)-c;
start= find(Out>=200,1,'last');
finish= find(Out<=0,1,'first');
fprintf(fid,'#define LOWVALID %d\r',start-451);
fprintf(fid,'#define HIGHVALID %d\r',finish-451);


% Target has a 2-ft diameter.  There are 4 concentric sections, with the
% center section being half the size of the others
fprintf(fid,'flash char archout[%d]={\r',finish-start-1);
for i= start+1:finish-2
    fprintf(fid,' %d,',Out(i));
    if mod(i,16)==0
        fprintf(fid,'\r');
    end
end
fprintf(fid,' %d};\r',Out(finish-1));

fprintf(fid,'#endif\r');
fclose(fid);
	