-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawCar.m
executable file
·156 lines (127 loc) · 4.57 KB
/
drawCar.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
function drawCar(state, P)
% process inputs to function
x = state(1); % inertial x position (cm)
y = state(2); % inertial y position (cm)
theta = state(3); % heading angle (rad)
vel = state(4); % forward velocity (cm/sec)
theta_dot = state(5); % turn rate (rad/sec)
t = state(6); % time (s)
% define persistent variables
persistent car_handle; % figure handle for car
persistent lidar_handle; %figure for P.lidar
persistent Vertices
persistent Faces
persistent facecolors
% first time function is called, initialize plot and persistent vars
if t==0
figure(1), clf
[Vertices,Faces,facecolors] = defineCarBody(P);
car_handle = drawBody(Vertices,Faces,facecolors,...
x, y, theta,...
[]);
title('Rover Course')
xlabel('x (cm)')
ylabel('y (cm)')
axis([x-300,x+300,y-300,y+300]);
grid on
% at every other time step, redraw quadrotor and target
else
drawBody(Vertices,Faces,facecolors,...
x, y, theta,...
car_handle);
% move axes with car
set(car_handle.Parent, 'XLim',[x-300,x+300])
set(car_handle.Parent, 'YLim',[y-300,y+300])
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function handle = drawBody(V,F,colors,...
x, y, theta,...
handle)
V = rotate(V, theta); % rotate rigid body
V = translate(V, x, y); % translate after rotation
if isempty(handle)
handle = patch('Vertices', V', 'Faces', F,...
'FaceVertexCData',colors,...
'FaceColor','flat');
%hold on;
%P.lidar_handle = rectangle('Position', P.lidar, 'Curvature', [1 1]);
else
set(handle,'Vertices',V','Faces',F);
%set(P.lidar_handle, 'Position', P.lidar);
drawnow
end
end
%%%%%%%%%%%%%%%%%%%%%%%
function pts=rotate(pts,theta)
% define rotation matrix (right handed)
R = [...
cos(theta), -sin(theta);...
sin(theta), cos(theta)];
% rotate vertices
pts = R*pts;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% translate vertices by x, y
function pts = translate(pts,x, y)
pts = pts + repmat([x;y],1,size(pts,2));
end
% end translate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define aircraft vertices and faces
function [V,F,colors] = defineCarBody(P)
% parameters for drawing aircraft
% scale = 20 %only for scale drawing purposes.
scale = 1;
% P.chasis_width = 18;
% P.chasis_forward = 20;
% P.chasis_backward = -2;
% P.wheel_base = 21;
% P.wheel_width = 2.5;
% P.wheel_radius = 4.5;
% P.lidar_forward = 16*scale;
% P.lidar_radius = 3.5*scale;
%define points
b_lf = [P.chasis_forward P.chasis_width/2]';
b_rf = [P.chasis_forward -P.chasis_width/2]';
b_lr = [P.chasis_backward P.chasis_width/2]';
b_rr = [P.chasis_backward -P.chasis_width/2]';
lw_lf = [P.wheel_radius P.wheel_width/2+P.wheel_base/2]';
lw_rf = [P.wheel_radius -P.wheel_width/2+P.wheel_base/2]';
lw_rr = [-P.wheel_radius -P.wheel_width/2+P.wheel_base/2]';
lw_lr = [-P.wheel_radius P.wheel_width/2+P.wheel_base/2]';
rw_lf = [P.wheel_radius P.wheel_width/2-P.wheel_base/2]';
rw_rf = [P.wheel_radius -P.wheel_width/2-P.wheel_base/2]';
rw_rr = [-P.wheel_radius -P.wheel_width/2-P.wheel_base/2]';
rw_lr = [-P.wheel_radius P.wheel_width/2-P.wheel_base/2]';
li_lf = [P.lidar_forward+P.lidar_radius P.lidar_radius]';
li_rf = [P.lidar_forward+P.lidar_radius -P.lidar_radius]';
li_rr = [P.lidar_forward-P.lidar_radius -P.lidar_radius]';
li_lr = [P.lidar_forward-P.lidar_radius P.lidar_radius]';
%define faces
body = [b_lf, b_rf, b_rr, b_lr];
left_wheel = [lw_lf, lw_rf, lw_rr, lw_lr];
right_wheel = [rw_lf, rw_rf, rw_rr, rw_lr];
lidar = [li_lf, li_rf, li_rr, li_lr]; %square here can make a circle
% colors
red = [1, 0, 0];
green = [0, 1, 0];
blue = [0, 0, 1];
yellow = [1,1,0];
magenta = [0, 1, 1];
black = [0, 0, 0];
V = [body, left_wheel, right_wheel, lidar];
F = [...
1, 2, 3, 4;... %body
5, 6, 7, 8;... %left P.wheel
9, 10, 11, 12;... %right P.wheel
13, 14, 15, 16;... %P.lidar square
];
colors = [...
blue;... % body
black;... % left P.wheel
black;... % right P.wheel
black;... %P.lidar
];
V = scale*V; % rescale vertices
end