root/branches/atorf/MotorTests/AnalyzeEmbeddedMotorBehaviour.m @ 542

Revision 542, 8.2 KB (checked in by atorf, 4 years ago)
  • Added more NXT-G test programs
  • More NXC testing…
Line 
1
2%% prepare
3COM_CloseNXT all
4close all
5clear all
6format compact
7
8
9%% open NXT
10h = COM_OpenNXT('bluetooth.ini', 'check');
11COM_SetDefaultNXT(h);
12
13
14%% settings
15port = MOTOR_B;
16measuringTime = 3; % seconds
17%programName = 'test';
18%programName = 'TestRampMotorCo';
19%programName = 'ManualMotorRamp';
20%programName = 'NXTG_Wait1100_M';
21programName = 'NXTG_MovePwr100';
22%programName = 'NXTG_MotorPwr60';
23
24
25%% precall functions to fill matlab cache
26tic;
27tmp = toc;
28tmp = NXT_GetOutputState(port);
29
30
31
32
33%% initialize vars
34runState    = zeros(measuringTime * 1000, 1);
35mode        = runState;
36regMode     = runState;
37tachoCount  = runState;
38tachoLimit  = runState;
39power       = runState;
40time        = runState;
41speed       = runState;
42
43
44%% start embedded program
45NXT_StopProgram();
46pause(1);
47NXT_StartProgram(programName);
48pause(1)
49
50
51
52
53%% record stuff...
54j = 0;
55tic;
56while(toc < measuringTime)
57    j = j + 1;
58   
59    tmp = NXT_GetOutputState(port);
60   
61    time(j)         = toc;
62    runState(j)     = tmp.RunStateByte;
63    mode(j)         = tmp.Mode;
64    regMode(j)      = tmp.RegModeByte;
65    tachoCount(j)   = tmp.TachoCount;
66    tachoLimit(j)   = tmp.TachoLimit;
67    power(j)        = tmp.Power;
68   
69   
70end%while
71
72
73%% cut off
74time        = time(1:j);
75runState    = runState(1:j);
76mode        = mode(1:j);
77regMode     = regMode(1:j);
78tachoCount  = tachoCount(1:j);
79tachoLimit  = tachoLimit(1:j);
80power       = power(1:j);
81speed       = speed(1:j);
82speed1      = speed;
83speed2      = speed;
84speed3      = speed;
85accel       = speed;
86
87%% smooth out tachoCount
88tachoCount = moving_average(tachoCount, 4);
89
90
91%% calculate real speed, simple method
92% central difference, only accurate to order of h (i.e. delta t)
93% also very sensitive to noise
94speed0 = [0; diff(tachoCount) ./ diff(time)];
95
96%% calculate real speed
97for j = 2 : length(tachoCount) - 1
98    % see http://numericalmethods.eng.usf.edu/mws/gen/02dif/mws_gen_dif_ppt_discrete.pdf
99    % sheet 18
100   
101    % 3 points for lagrangian interpolation
102    t0 = time(j-1);
103    t1 = time(j);
104    t2 = time(j+1);
105    % we evaluate at t1...
106    t = t1;
107   
108    speed(j) =             ((2*t - (t1 + t2)) / ((t0 - t1) * (t0 - t2))) * tachoCount(j-1);
109    speed(j) = speed(j) +  ((2*t - (t0 + t2)) / ((t1 - t0) * (t1 - t2))) * tachoCount(j);
110    speed(j) = speed(j) +  ((2*t - (t0 + t1)) / ((t2 - t0) * (t2 - t1))) * tachoCount(j+1);
111   
112end%for
113speed1 = speed;
114
115%% calculate real speed, alternative method...
116for j = 3 : length(tachoCount) - 2
117    % use central differences aka the 5-point-stencil
118    % average spacing (the best we can do)
119    h = (time(j+2) - time(j-2)) / 5;
120    speed2(j) = (tachoCount(j-2) - 8*tachoCount(j-1) + 8*tachoCount(j+1) - tachoCount(j+2)) / (12*h);
121end%for
122
123%% final algo, this time 9-point method...
124coeffs = [3; -32; 168; -672; 0; 672; -168; 32; -3];
125for j = 5 : length(tachoCount) - 4
126    % use central differences aka the 9-point-stencil
127    % average spacing (the best we can do)
128    h = (time(j+4) - time(j-4)) / 9;
129    for k = 1 : 9
130        speed3(j) = speed3(j) + coeffs(k) * tachoCount(j + k-5);
131    end%for
132    speed3(j) = speed3(j) / (840*h);
133           
134end%for
135
136
137% I finally decided to take speed3
138n = 4;
139speed = moving_average(speed3, n);
140
141
142%% get acceleration
143coeffs = [3; -32; 168; -672; 0; 672; -168; 32; -3];
144for j = 5 : length(tachoCount) - 4
145    % use central differences aka the 9-point-stencil
146    % average spacing (the best we can do)
147    h = (time(j+4) - time(j-4)) / 9;
148    accel(j) = 0;
149    for k = 1 : 9
150        accel(j) = accel(j) + coeffs(k) * speed(j + k-5);
151    end%for
152    accel(j) = accel(j) / (840*h);
153           
154end%for
155
156
157%% plot position, speeds
158
159
160figure('Name', 'Speed vs. Time')
161hold all
162h1 = plot(time, tachoCount, '.-');
163h2 = plot(time, power * 10, '.-');
164
165% n = 4;
166% plot(time, moving_average(speed0, n) .* 0.5, '-');
167% plot(time, moving_average(speed1 , n) .* 0.5, '-');
168% plot(time, moving_average(speed2, n) .* 0.5, '-');
169% plot(time, moving_average(speed3, n) .* 0.5, '-');
170
171h3 = plot(time, speed .* 0.5, '-');
172h4 = plot(time, moving_average(accel, 10) .* 0.05, '-');
173
174%plot(time, runState * 3, '-')
175%plot(time, mode * 3, '-')
176
177offset = true;
178for j = 2 : length(mode)
179    if mode(j) ~= mode(j-1)
180        x = time(j);
181        y = -350;
182        offset = ~offset;
183        if offset y = y + 50; end
184       
185        [isMOTORON isBRAKE isREGULATED] = byte2outputmode(mode(j));
186        if isMOTORON;   name = 'ON '; else name = 'OFF ';  end
187        if isBRAKE;     name = [name 'BRK '];     end
188        if isREGULATED; name = [name 'REG ']; end
189       
190        c = [191/255 191/255 0];
191        text(time(j), y, name, 'Color', c);
192        %set(h5, 'EdgeColor', 'black')
193        h5 = line([x; x], [0; y * 1.1], 'Color', c);
194    end%if
195end%for
196
197offset = true;
198for j = 2 : length(runState)
199    if runState(j) ~= runState(j-1)
200        x = time(j);
201        y = -200;
202        offset = ~offset;
203        if offset y = y + 50; end
204       
205        c = [191/255 0 191/255];
206        text(time(j), y, byte2runstate(runState(j)), 'Color', c);
207        %set(h6, 'EdgeColor', 'black')
208        h6 = line([x; x], [0; y * 1.1], 'Color', c);
209    end%if
210end%for
211
212
213
214h6b = plot(time, tachoLimit, 'g--');
215
216h7 = plot([0; measuringTime], [0; 0], '-k');
217h8 = plot([0; measuringTime], [1000; 1000], '-k');
218
219
220
221%legend('TachCount', 'Power * 10', 'Simple Diff', 'Lagrangian 3-point-poly', '5-point-stencil', '9-point-stencil', 'Start: 0', 'Target: 1000')
222legend([h1 h2 h3 h4 h5 h6 h6b h7 h8], 'TachoCount', 'Power * 10', 'Speed (avg.) / 2', 'Acceleration (avg.) / 20', 'MODE', 'RUNSTATE', 'TachoLimit', 'Start: 0', 'Target: 1000')
223
224axis([0 measuringTime -500 1100])
225xlabel('Time [s]')
226ylabel('TachoCount [degrees]')
227
228text(measuringTime, -570, '(C) Institute of Imaging & Computer Vision, RWTH Aachen University', 'HorizontalAlignment', 'right');
229text(measuringTime, -610, 'http://www.mindstorms.rwth-aachen.de', 'HorizontalAlignment', 'right');
230
231
232
233
234%% Plot everything vs position!
235figure('Name', 'Speed vs. Position')
236hold all
237
238
239tmp = find(tachoCount);
240first = tmp(1);
241[forget last] = max(tachoCount);
242
243
244h1 = plot([tachoCount(first); tachoCount(last)], [0; 0], '-');
245
246h2 = plot(tachoCount(first:last), power(first:last) * 10, '.-');
247
248h3 = plot(tachoCount(first:last), speed(first:last) .* 0.5, '-');
249h4 = plot(tachoCount(first:last), moving_average(accel(first:last), 10) .* 0.05, '-');
250
251
252%plot(time, runState * 3, '-')
253%plot(time, mode * 3, '-')
254
255offset = true;
256for j = 2 : length(mode)
257    if mode(j) ~= mode(j-1)
258        x = tachoCount(j);
259        y = -350;
260        offset = ~offset;
261        if offset, y = y + 50; end
262       
263        [isMOTORON isBRAKE isREGULATED] = byte2outputmode(mode(j));
264        if isMOTORON;   name = 'ON '; else name = 'OFF ';  end
265        if isBRAKE;     name = [name 'BRK '];     end
266        if isREGULATED; name = [name 'REG ']; end
267       
268        c = [191/255 191/255 0];
269        text(tachoCount(j), y, name, 'Color', c);
270        %set(h5, 'EdgeColor', 'black')
271        h5 = line([x; x], [0; y * 1.1], 'Color', c);
272    end%if
273end%for
274
275offset = true;
276for j = 2 : length(runState)
277    if runState(j) ~= runState(j-1)
278        x = tachoCount(j);
279        y = -200;
280        offset = ~offset;
281        if offset, y = y + 50; end
282       
283        c = [191/255 0 191/255];
284        text(tachoCount(j), y, byte2runstate(runState(j)), 'Color', c);
285        %set(h6, 'EdgeColor', 'black')
286        h6 = line([x; x], [0; y * 1.1], 'Color', c);
287    end%if
288end%for
289
290
291
292%legend('TachCount', 'Power * 10', 'Simple Diff', 'Lagrangian 3-point-poly', '5-point-stencil', '9-point-stencil', 'Start: 0', 'Target: 1000')
293legend([h1 h2 h3 h4 h5 h6], '0-line (x-axis)', 'Power * 10', 'Speed (avg.) / 2', 'Acceleration (avg.) / 20', 'MODE', 'RUNSTATE')
294
295axis([tachoCount(first) tachoCount(last) -500 1100])
296xlabel('TachoCount [degrees]')
297ylabel('Speed / Acceleration / Power')
298
299text(tachoCount(last), -570, '(C) Institute of Imaging & Computer Vision, RWTH Aachen University', 'HorizontalAlignment', 'right');
300text(tachoCount(last), -610, 'http://www.mindstorms.rwth-aachen.de', 'HorizontalAlignment', 'right');
Note: See TracBrowser for help on using the browser.