-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGyroscope_Visualization.m
97 lines (88 loc) · 2.04 KB
/
Gyroscope_Visualization.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%% Initialization
close all;
clear;
clc;
%% Create serial object for Arduino
baudrate = 115200;
s = serial('COM3','BaudRate',baudrate); % change the COM Port number as needed
s.ReadAsyncMode = 'manual';
set(s,'InputBufferSize',100);
pause(2);
%% Connect the serial port to Arduino
try
fopen(s);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
%% Prepare Figures
Fig = figure('Position',[0 40 900 700],'ToolBar','none');
Ax(1) = axes('Position',[.05 .75 0.90 .20]);
grid;
hold on;
H = zeros(1,3);
for k = 1:3
H(k) = plot(0,0);
end
Ax(2) = axes('Position',[.15 0.05 .6 .6],'CameraPosition',[10 -10 10]);
hold on;
axis([-1 1 -1 1 -1 1]);
%% Read and plot the data from Arduino
Tmax = 60;
Ts = 0.02;
i = 1;
ata = 0;
t = 0;
tic % Start timer
T(i)=0;
FLAG_CASTING = false;
CubH = [];
Angles = zeros(1,3);
Flag_Initializing = true;
%%
while(Flag_Initializing)
while(strcmp(s.TransferStatus,'read'))
pause(0.01);
end
readasync(s);
sms = fscanf(s);
if ~strcmp(sms(1:3),'ypr')
fprintf(sms)
else
Flag_Initializing = false;
end
end
%%
while T(end) <= 2000
T(end+1)=T(end)+1;
sms='a';
idx = [];
Angles = 0;
while isempty(idx) || numel(Angles)~=3
sms = fscanf(s);
idx = find(sms=='r');
if ~isempty(idx)
idx = idx(end)+1;
Angles = sscanf(sms(idx:end),'%f %f %f');
end
end
Yaw = Angles(3);
Pitch = Angles(2);
Roll = Angles(1);
k = 1;
vY = get(H(k),'YData');vX = get(H(k),'XData');
set(H(k),'YData',[vY,Angles(k)]);set(H(k),'XData',[vX,T(end)]);
k = 2;
vY = get(H(k),'YData');vX = get(H(k),'XData');
set(H(k),'YData',[vY,Angles(k)]);set(H(k),'XData',[vX,T(end)]);
k = 3;
vY = get(H(k),'YData');vX = get(H(k),'XData');
set(H(k),'YData',[vY,Angles(k)]);set(H(k),'XData',[vX,T(end)]);
CubH = Plot_Cube(deg2rad(-Yaw),deg2rad(Pitch),deg2rad(Roll),Ax(2),CubH);
drawnow;
end
%%
fclose(s); % Close Connection
clear;
close all;
clc;