root / trunk / code / behaviors / formation_control / push_pull / vectorSizeSpecs.m @ 1512
History | View | Annotate | Download (1.63 KB)
1 |
% vectorSizeSpecs - Script used to help calculate the max scalar value by |
---|---|
2 |
% which to multiply the BOM unit vectors (thus increasing the |
3 |
% precision of the scaled vectors) such that in the worst case, the |
4 |
% total net sum will not overflow the data type used to represent |
5 |
% the net vector components. |
6 |
% |
7 |
% Author: John Sexton, Colony Project, CMU Robotics Club |
8 |
|
9 |
|
10 |
% Parameters |
11 |
maxIntensity = 255; |
12 |
dataBits = 16; |
13 |
scalar = 25; |
14 |
|
15 |
n = 0:8; |
16 |
angle = n * 2 * pi / 16; |
17 |
|
18 |
vector_components = sin(angle); |
19 |
max_net_component = sum(vector_components); |
20 |
|
21 |
% Formula for max_net_component (mnc), obtained by summing |
22 |
% values from the unit circle at angles of 0 : pi/8 : pi |
23 |
% in quadrants I and II |
24 |
mnc = 1; %sin(pi/2) |
25 |
mnc = mnc + (2* (1/2)*sqrt(2+sqrt(2))); %sin(3pi/8) and sin(5pi/8) |
26 |
mnc = mnc + (2* sqrt(2)/2); %sin(pi/4) and sin(3pi/4) |
27 |
mnc = mnc + (2* (1/2)*sqrt(2-sqrt(2))); %sin(pi/8) and sin(7pi/8) |
28 |
|
29 |
|
30 |
calc_scalar = (2^(dataBits-1)-1) / (max_net_component * maxIntensity); |
31 |
% calc_scalar = 25.560 for 16 bits => use 25 as scalar for int data type |
32 |
|
33 |
fprintf('With %d data bits, calculated scalar value: %.3f\n\n', dataBits, calc_scalar); |
34 |
|
35 |
|
36 |
% Check worst case |
37 |
scaled_vector_components = scalar * vector_components; |
38 |
|
39 |
worst_case = floor(scaled_vector_components) * maxIntensity; |
40 |
worst_sum = sum(worst_case); |
41 |
fprintf('With scalar %d, max worst case sum: %d\n', scalar, round(worst_sum)); |
42 |
fprintf('Max number: 2^(%d-1) - 1 = %d\n', dataBits, (2^(dataBits-1)-1)); |
43 |
|
44 |
|
45 |
% Calculate the x and y component arrays which should be used in the |
46 |
% BOM Vector Component Tables in push_pull.c |
47 |
N = 0:15; |
48 |
x_comp = floor(scalar * cos(2 * pi / 16 * N)) |
49 |
y_comp = floor(scalar * sin(2 * pi / 16 * N)) |