|
Revision 528, 1.5 KB
(checked in by atorf, 4 years ago)
|
|
|
| Line | |
|---|
| 1 | |
|---|
| 2 | %% Set up Matlab |
|---|
| 3 | COM_CloseNXT all |
|---|
| 4 | close all |
|---|
| 5 | clear all |
|---|
| 6 | format compact |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | %% Set up ports |
|---|
| 10 | |
|---|
| 11 | port = SENSOR_1; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | %% Get USB handle |
|---|
| 16 | myNXT = COM_OpenNXT; |
|---|
| 17 | COM_SetDefaultNXT(myNXT); |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | %% Open Sensor |
|---|
| 21 | |
|---|
| 22 | OpenAccelerator(port); |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | %% Main loop |
|---|
| 26 | |
|---|
| 27 | figure |
|---|
| 28 | hold on |
|---|
| 29 | |
|---|
| 30 | % tic |
|---|
| 31 | % count = 0; |
|---|
| 32 | pos = [0; 0]; |
|---|
| 33 | speed = [0; 0]; |
|---|
| 34 | |
|---|
| 35 | count = 100; |
|---|
| 36 | noise = zeros(3, 1); |
|---|
| 37 | for j = 1 : count |
|---|
| 38 | tmp = GetAccelerator(port); |
|---|
| 39 | noise = noise + tmp; |
|---|
| 40 | end%for |
|---|
| 41 | |
|---|
| 42 | noise = noise ./ count; |
|---|
| 43 | %noise |
|---|
| 44 | |
|---|
| 45 | j = 0; |
|---|
| 46 | tic |
|---|
| 47 | while (true) |
|---|
| 48 | j = j + 1; |
|---|
| 49 | |
|---|
| 50 | % get accel |
|---|
| 51 | acc = GetAccelerator(port); |
|---|
| 52 | |
|---|
| 53 | % get time period |
|---|
| 54 | t = toc; |
|---|
| 55 | tic % start again right away |
|---|
| 56 | |
|---|
| 57 | % substract noise |
|---|
| 58 | acc = acc - noise; |
|---|
| 59 | |
|---|
| 60 | % throw away z component |
|---|
| 61 | acc = acc(1:2); |
|---|
| 62 | |
|---|
| 63 | % invert x and y |
|---|
| 64 | acc = flipud(acc); |
|---|
| 65 | % invert sign of y |
|---|
| 66 | acc(2) = -acc(2); |
|---|
| 67 | |
|---|
| 68 | % integrate for speed |
|---|
| 69 | speed = speed + acc .* t; |
|---|
| 70 | |
|---|
| 71 | % integrate for displacement |
|---|
| 72 | % "classical" |
|---|
| 73 | %pos = pos + speed .* t; |
|---|
| 74 | % "physics-like" |
|---|
| 75 | pos = pos + speed .* t + 0.5 .* acc .* t^2; |
|---|
| 76 | |
|---|
| 77 | if mod(j, 30) == 0 |
|---|
| 78 | % plot |
|---|
| 79 | clf |
|---|
| 80 | %plot(acc(1), acc(2), '.k') |
|---|
| 81 | %plot(speed(1), speed(2), '.k') |
|---|
| 82 | plot(pos(1), pos(2), '.k') |
|---|
| 83 | limit = 100; |
|---|
| 84 | axis([-limit limit -limit limit]) |
|---|
| 85 | drawnow |
|---|
| 86 | pause(0.001) |
|---|
| 87 | end%if |
|---|
| 88 | |
|---|
| 89 | % maybe show FPS |
|---|
| 90 | if mod(j, 50) == 0 |
|---|
| 91 | disp(sprintf('Something like %.1f FPS', 1 / t)) |
|---|
| 92 | end%if |
|---|
| 93 | |
|---|
| 94 | end%while |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | COM_CloseNXT(myNXT) |
|---|