% vectorSizeSpecs - Script used to help calculate the max scalar value by
% 		which to multiply the BOM unit vectors (thus increasing the
%		precision of the scaled vectors) such that in the worst case, the
%		total net sum will not overflow the data type used to represent
%		the net vector components.
%
%	Author: John Sexton, Colony Project, CMU Robotics Club


% Parameters
maxIntensity = 255;
dataBits = 16;
scalar = 25;

n = 0:8;
angle = n * 2 * pi / 16;

vector_components = sin(angle);
max_net_component = sum(vector_components);

% Formula for max_net_component (mnc), obtained by summing
% values from the unit circle at angles of 0 : pi/8 : pi
% in quadrants I and II
mnc = 1;									%sin(pi/2)
mnc = mnc + (2*  (1/2)*sqrt(2+sqrt(2)));	%sin(3pi/8) and sin(5pi/8)
mnc = mnc + (2*  sqrt(2)/2);				%sin(pi/4) and sin(3pi/4)
mnc = mnc + (2*  (1/2)*sqrt(2-sqrt(2)));	%sin(pi/8) and sin(7pi/8)


calc_scalar = (2^(dataBits-1)-1) / (max_net_component * maxIntensity);
% calc_scalar = 25.560 for 16 bits => use 25 as scalar for int data type

fprintf('With %d data bits, calculated scalar value: %.3f\n\n', dataBits, calc_scalar);


% Check worst case
scaled_vector_components = scalar * vector_components;

worst_case = fix(scaled_vector_components) * maxIntensity;
worst_sum = sum(worst_case);
fprintf('With scalar %d, max worst case sum: %d\n', scalar, round(worst_sum));
fprintf('Max number: 2^(%d-1) - 1 = %d\n', dataBits, (2^(dataBits-1)-1));


% Calculate the x and y component arrays which should be used in the
% BOM Vector Component Tables in push_pull.c
N = 0:15;
x_comp = fix(scalar * cos(2 * pi / 16 * N))
y_comp = fix(scalar * sin(2 * pi / 16 * N))
