-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_3D_attitude.m
164 lines (97 loc) · 4.22 KB
/
plot_3D_attitude.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
clc
clear all
close all hidden
%% delete
delete('*.asv')
%% parameter
s = serial('COM3');
fopen( s);
Rx = 2.0; %% x軸切片 [m]
Ry = 1.0; %% y軸切片 [m]
Rz = 0.5; %% z軸切片 [m]
tild_mat = @( x)[ 0 -x(3) x(2) ;
x(3) 0 -x(1) ;
-x(2) x(1) 0 ];
L_mat = @( eps_vec)( [ -eps_vec(2:4) eps_vec(1)*eye(3)-tild_mat( eps_vec(2:4))] );
E_mat = @( eps_vec)( [ -eps_vec(2:4) eps_vec(1)*eye(3)+tild_mat( eps_vec(2:4))] );
A_mat = @( eps_vec)( E_mat( eps_vec)*L_mat(eps_vec).' ); %% 回転基本行列
%% 局所座標系データ
[ x_body, y_body, z_body] = ellipsoid( 0, 0, 0, Rx, Ry, Rz, 20);
r_body_T = [ x_body(:) y_body(:) z_body(:)];
R_G_vec_T = @( eps_vec)( r_body_T*A_mat( eps_vec).' );
%% plot
i_ax = 1;
h_fig(1) = figure(1);
set( h_fig(1), 'Position', [100 100 600 600], 'render', 'zbuffer')
h_ax(i_ax) = axes( 'Parent', h_fig(1), 'FontSize', 15);
eps_vec = [ 1 0 0 0].';
R_G_vec_T_v = R_G_vec_T( eps_vec);
X_G = reshape( R_G_vec_T_v(:,1), size( x_body, 1), []);
Y_G = reshape( R_G_vec_T_v(:,2), size( y_body, 1), []);
Z_G = reshape( R_G_vec_T_v(:,3), size( z_body, 1), []);
h_plot(1) = surf( h_ax(i_ax), X_G, Y_G, Z_G);
hold( h_ax(i_ax), 'on')
plot3( h_ax(i_ax), [-1 1]*3, [0 0], [0 0])
plot3( h_ax(i_ax), [0 0], [-1 1]*3, [0 0])
plot3( h_ax(i_ax), [0 0], [0 0], [-1 1]*2)
% light
view( h_ax(i_ax), [ 1 1 0.5])
xlabel( h_ax(i_ax), '{\itX} [m]')
ylabel( h_ax(i_ax), '{\itY} [m]')
zlabel( h_ax(i_ax), '{\itZ} [m]')
% box( h_ax(i_ax), 'on')
axis equal
% set( h_ax(i_ax), 'FontName', 'Times New Roman')
h_txt(1) = text( -1, -1, 1, [ 'Time = ', num2str( 0, '%0.2f'), ' [s]'],...
'FontSize', 12, 'FontName', 'Times New Roman', 'BackgroundColor', 'g');
ButtonHandle = uicontrol( h_fig(1), 'Style', 'PushButton', ...
'String', 'Stop loop', ...
'Callback', 'delete(gcbf)');
i_ax = i_ax + 1;
h_fig(2) = figure(2);
set( h_fig(2), 'Position', [700 100 600 600], 'render', 'zbuffer')
h_ax(i_ax) = axes( 'Parent', h_fig(2), 'FontSize', 15);
h_plot(2) = quiver3( h_ax(i_ax), 0, 0, 0, 1, 0, 0);
view( h_ax(i_ax), [ 0 0 1])
xlabel( h_ax(i_ax), '{\itm_x} [-]')
ylabel( h_ax(i_ax), '{\itm_y} [-]')
zlabel( h_ax(i_ax), '{\itm_z} [-]')
%% animation
while 1
data = fscanf( s);
%% "time q0 q1 q2 q3"
num_data = str2num( data);
if isempty( num_data) ~= 1 && length( num_data) == 8
time = num_data(1);
eps_vec = num_data(2:5).';
mag_vec = num_data(6:end).';
if mod( round( time*1000), 10) == 0
R_G_vec_T_v = R_G_vec_T( eps_vec);
X_G = reshape( R_G_vec_T_v(:,1), size( x_body, 1), []);
Y_G = reshape( R_G_vec_T_v(:,2), size( y_body, 1), []);
Z_G = reshape( R_G_vec_T_v(:,3), size( z_body, 1), []);
%% plot
%%[*] Attitude
set( h_plot(1), 'XData', X_G, 'YData', Y_G, 'ZData', Z_G)
xlim( h_ax(1), [-2 2])
ylim( h_ax(1), [-2 2])
zlim( h_ax(1), [-2 2])
set( h_txt(1), 'String', [ 'Time = ', num2str( time, '%0.2f'), ' [s]']);
%%[*] Magnetic field (Normalized)
set( h_plot(2), 'UData', mag_vec(1), 'VData', mag_vec(2), 'WData', mag_vec(3))
xlim( h_ax(2), [-2 2])
ylim( h_ax(2), [-2 2])
zlim( h_ax(2), [-2 2])
drawnow
end
end
if ~ishandle(ButtonHandle)
disp('Loop stopped by user');
break;
end
end
close all hidden
%% close serial port
fclose( s);
delete( s)
clear s