root/branches/atorf/NewSensorDemos/AcceleratorPositionTracking2D.m @ 528

Revision 528, 1.5 KB (checked in by atorf, 4 years ago)
Line 
1
2%% Set up Matlab
3COM_CloseNXT all
4close all
5clear all
6format compact
7
8
9%% Set up ports
10
11port  = SENSOR_1;
12
13
14
15%% Get USB handle
16myNXT = COM_OpenNXT;
17COM_SetDefaultNXT(myNXT);
18
19
20%% Open Sensor
21
22OpenAccelerator(port);
23
24
25%% Main loop
26
27figure
28hold on
29
30% tic
31% count = 0;
32pos = [0; 0];
33speed = [0; 0];
34
35count = 100;
36noise = zeros(3, 1);
37for j = 1 : count
38    tmp = GetAccelerator(port);
39    noise = noise + tmp;
40end%for
41
42noise = noise ./ count;
43%noise
44
45j = 0;
46tic
47while (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
94end%while
95
96
97COM_CloseNXT(myNXT)
Note: See TracBrowser for help on using the browser.