-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlaneVisualizer.m
232 lines (203 loc) · 6.58 KB
/
PlaneVisualizer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
classdef PlaneVisualizer < Visualizer
% Implements the draw function for the MURI Plane
% Takes in a Forest of Trees if trees are to be drawn with the
% plane/trajectory
properties
angleRep = 'euler';
forest = [];
end
methods
function obj = PlaneVisualizer(coordinateFrame, forest,angleRep)
obj = obj@Visualizer(coordinateFrame);
obj.playback_speed = 1;
obj.display_dt = 0;
if nargin>0
obj.forest = forest;
end
if (nargin > 2)
obj.angleRep = angleRep;
end
end
function draw(obj,t,x,u)
u = zeros(5,1);
persistent hFig;
if (isempty(hFig))
hFig = sfigure(25);
set(hFig,'DoubleBuffer', 'on');
%set(gca,'YDir', 'reverse');
set(gca,'ZDir', 'reverse');
end
%set(gca,'YDir', 'reverse');
set(gca,'ZDir', 'reverse');
%Airplane definition
hull_scale=0.04;
air_hull=[1 3; -1 3; -1 1; -6 1; -6 -1; -1 -1; -0.8 -4.8; -3 -5; -3 -6;...
3 -6; 3 -5; 0.8 -4.8; 1 -1; 6 -1; 6 1; 1 1; 1 3];
air_hull=hull_scale*air_hull;
%x_coll = [air_hull(:,1),air_hull(:,2),zeros(size(air_hull,1),1)];
x_coll = [air_hull(:,2),air_hull(:,1),zeros(size(air_hull,1),1)];
% x(3) = -x(3);%flips the altitude to the Global frame we use
roll = x(4);
pitch = x(5);
yaw = x(6);
Rotx = [1 0 0; 0 cos(roll) sin(roll); 0 -sin(roll) cos(roll)];
Roty = [cos(pitch) 0 -sin(pitch); 0 1 0; sin(pitch) 0 cos(pitch)];
Rotz = [cos(yaw) sin(yaw) 0; -sin(yaw) cos(yaw) 0; 0 0 1];
GtoB = Rotx*Roty*Rotz;
% Size of the arena.
%Important that it is 8 wide in the y-direction
X_LOW = -2 + x(1);
X_HIGH = 6 + x(1);
Y_LOW = -4 + x(2);
Y_HIGH = 4 + x(2);
Z_LOW = -4 + x(3);
Z_HIGH = 4 + x(3);
if x(2)<-3
Yoff = -3;
elseif x(2)>3
Yoff = 3;
else
Yoff = x(2);
end
if x(3)<-3
Zoff = -3;
elseif x(3)>3
Zoff = 3;
else
Zoff = x(3);
end
Y_LOW = Y_LOW - Yoff;
Y_HIGH = Y_HIGH - Yoff;
Z_LOW = Z_LOW - Zoff;
Z_HIGH = Z_HIGH - Zoff;
curr_window = [X_LOW,X_HIGH,Y_LOW,Y_HIGH,Z_LOW,Z_HIGH];
axis_size = curr_window;
%Velocity Bar:
velz = [Z_HIGH, Z_HIGH, -.2*x(7) + Z_HIGH,-.2*x(7) + Z_HIGH];
velx = X_LOW*ones(1,4);
vely = [Y_LOW, Y_LOW+1, Y_LOW+1, Y_LOW];
%P (roll angular rate) bar
Px = X_LOW*ones(1,4);
Py = [Y_LOW+1, Y_LOW+2, Y_LOW+2, Y_LOW+1];
Pz = [Z_HIGH-1, Z_HIGH-1, -.2*x(10) + Z_HIGH-1,-.2*x(10) + Z_HIGH-1];
if (abs(x(10))>2*pi)
Pcolor = 'red';
else
Pcolor = 'green';
end
%P (pitch angular rate) bar
Qx = X_LOW*ones(1,4);
Qy = [Y_LOW+2, Y_LOW+3, Y_LOW+3, Y_LOW+2];
Qz = [Z_HIGH-1, Z_HIGH-1, -.2*x(11) + Z_HIGH-1,-.2*x(11) + Z_HIGH-1];
if (abs(x(11))>2*pi)
Qcolor = 'red';
else
Qcolor = 'green';
end
%R (yaw angular rate) bar
Rx = X_LOW*ones(1,4);
Ry = [Y_LOW+3, Y_LOW+4, Y_LOW+4, Y_LOW+3];
Rz = [Z_HIGH-1, Z_HIGH-1, -.2*x(12) + Z_HIGH-1,-.2*x(12) + Z_HIGH-1];
if (abs(x(12))>2*pi)
Rcolor = 'red';
else
Rcolor = 'green';
end
%Thrust bar
tx = X_LOW*ones(1,4);
ty = [Y_LOW+4, Y_LOW+5, Y_LOW+5, Y_LOW+4];
tz = [Z_HIGH, Z_HIGH, -2*u(1)/255 + Z_HIGH,-2*u(1)/255 + Z_HIGH];
if (u(1)>150)
tcolor = 'red';
else
tcolor = 'blue';
end
%elev input bar
elevx = X_LOW*ones(1,4);
elevy = [Y_LOW+5, Y_LOW+6, Y_LOW+6, Y_LOW+5];
elevz = [Z_HIGH-1, Z_HIGH-1, -u(2)/20 + Z_HIGH-1,-u(2)/20 + Z_HIGH-1];
if (abs(x(12))>2*pi)
elevcolor = 'red';
else
elevcolor = 'blue';
end
rudx = X_LOW*ones(1,4);
rudy = [Y_LOW+6, Y_LOW+7, Y_LOW+7, Y_LOW+6];
rudz = [Z_HIGH-1, Z_HIGH-1, -u(3)/20 + Z_HIGH-1,-u(3)/20 + Z_HIGH-1];
if (abs(x(12))>2*pi)
rudcolor = 'red';
else
rudcolor = 'blue';
end
ailx = X_LOW*ones(1,4);
aily = [Y_LOW+7, Y_LOW+8, Y_LOW+8, Y_LOW+7];
ailz = [Z_HIGH-1, Z_HIGH-1, -u(4)/20 + Z_HIGH-1,-u(4)/20 + Z_HIGH-1];
if (abs(x(12))>2*pi)
ailcolor = 'red';
else
ailcolor = 'blue';
end
%Plot the airplane
if(~isempty(x))
plane_color=[0, 0, 0];
[x_points,y_points,z_points]=plane_coll_points(x,x_coll,obj.angleRep);
fill3(x_points+x(1),y_points+x(2),z_points+x(3),plane_color);
%velocity bar
end
hold on
% Draw the forest
% obj.forest.draw();
% %Draws the state visualizer bars
% fill3(velx, vely, velz, 'green');
% fill3(Px, Py, Pz, Pcolor);
% fill3(Qx, Qy, Qz, Qcolor);
% fill3(Rx, Ry, Rz, Rcolor);
% fill3(tx, ty, tz, tcolor);
% %Draws input visualizer bars
% fill3(elevx, elevy, elevz, elevcolor);
% fill3(rudx, rudy, rudz, rudcolor);
% fill3(ailx, aily, ailz, ailcolor);
hold off
title(strcat('t = ', num2str(t,'%.5f')));
axis(axis_size);
set(gca,'DataAspectRatio',[1 1 1]);
grid on;
ylabel('Vel,P,Q,R,thr,elev,rud,ail')
%set(gca,'YDir', 'reverse');
%set(gca,'ZDir', 'reverse');
drawnow
%xlabel('x');
%ylabel('z');
%zlabel('y');
end
end
end
function [x_out,y_out,z_out]=plane_coll_points(x,x_coll,angleRep)
if strcmp(angleRep,'euler')
% Make sure size of x is correct
% if (length(x) ~= 2*6)
% error('x should contain 12 elements: x,y,z,yaw,pitch,roll');
% end
%keyboard;
angs = [-x(4);-x(5);-x(6)];
%q = SpinCalc('EA321toQ',(180/pi)*angs', 0.001, 1);
%points = qrot3d(x_coll,[q(end),q(1:3)]);
%points = qrot3d(x_coll,q);
M = SpinCalc('EA123toDCM',(180/pi)*angs',0.001,1);
%keyboard;
points = M*x_coll';
points = points';
elseif strcmp(angleRep,'quaternion')
% Make sure size of x is correct
if (length(x) ~= 2*7)
error('x should contain 14 elements: x,y,z,quaternion');
end
q = [x(4),x(5),x(6),x(7)]./(norm([x(4),x(5),x(6),x(7)]));
points = qrot3d(x_coll,q);
else
error('This angle representation is not supported');
end
x_out = points(:,1);
y_out = points(:,2);
z_out = points(:,3);
end