-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyesClosedInvarianceKernel3D.m
212 lines (175 loc) · 6.7 KB
/
eyesClosedInvarianceKernel3D.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
%% Generate Eyes-closed invariance kernel
clear; clc; close all;
save_file = true;
timestamp = now;
compTraj = true;
% Setup Grids
x_min = -10;
y_min = -15;
theta_min = -pi;
x_max = 20;
y_max = 15;
theta_max = pi;
N_x = 71;
N_y = 71;
N_theta = 81;
grid_pursuer_min = [x_min; y_min; theta_min]; % Lower corner of computation domain
grid_pursuer_max = [x_max; y_max; theta_max]; % Upper corner of computation domain
grid_evader_min = [x_min; y_min; theta_min];
grid_evader_max = [x_max; y_max; theta_max];
N_pursuer = [N_x; N_y; N_theta]; % Number of grid points per dimension
pdDims_pursuer = 3; % 3rd dimension is periodic
N_evader = [N_x; N_y; N_theta];
pdDims_evader = 3;
g_pursuer = createGrid(grid_pursuer_min, grid_pursuer_max, N_pursuer, pdDims_pursuer);
g_evader = createGrid(grid_evader_min, grid_evader_max, N_evader, pdDims_evader);
%% Generate Initial Set of Pursuer
R = 0.75; % Uncertainty in initial position ((x-x0)^2 + (y-y0)^2 <= R)
W = 0.6; % Uncertainty in initial heading (theta0 +/- W/2)
x0 = [0; 0; 0];
% data0 = shapeCylinder(grid,ignoreDims,center,radius)
cylinder0 = shapeCylinder(g_pursuer, 3, x0, R);
box0 = shapeRectangleByCenter(g_pursuer, x0, [Inf; Inf; W]);
initialPursuerState = shapeIntersection(box0, cylinder0);
%% Time vector for game
t0 = 0;
tMax = 5.0;
dt = 0.10;
tau = t0:dt:tMax;
%% Pursuer params
speed = [0, 3];
wMax = 0.75;
uModePursuer = 'max';
minWithPursuer = 'none';
dCarPursuer = DubinsCarVControl(x0, wMax, speed); % Puruser system
schemeDataPursuer.grid = g_pursuer;
schemeDataPursuer.dynSys = dCarPursuer;
schemeDataPursuer.accuracy = 'high'; %set accuracy
schemeDataPursuer.uMode = uModePursuer;
schemeDataPursuer.tMode = 'forward';
HJIextraArgsPursuer.visualize = true; %show plot
HJIextraArgsPursuer.fig_num = 1; %set figure number
HJIextraArgsPursuer.deleteLastPlot = true; %delete previous plot as you update
%[data, tau, extraOuts] = ...f
% HJIPDE_solve(data0, tau, schemeData, minWith, extraArgs)
[forward_reach_set, ~, ~] = ...
HJIPDE_solve(initialPursuerState, tau, schemeDataPursuer, minWithPursuer, HJIextraArgsPursuer);
%% Project and Extrude Forward Reach Set Into Heading and Velocity Dims
radius = 2.0;
extruded_frs = projectExtrudeSet(forward_reach_set, g_pursuer, g_evader, 3, 3, radius);
% extruded_frs = projectExtrudeSet(forward_reach_set, g_pursuer, 3, 3);
%% Modify set to allow for collisions at very low ego velocity.
% low_vel = 0.25; % Velocity at which colision is acceptable
% high_velocity_region = shapeHyperplane(g_evader,[0,0,-1,0]',[0,0,low_vel,0]');
% negative_velocity_region = shapeHyperplane(g_evader, [0,0,1,0]', [0,0,-0.2,0]');
%
% for t = 1:length(tau)
% extruded_frs(:,:,:,:,t) = shapeUnion(shapeIntersection(extruded_frs(:,:,:,:,t), high_velocity_region), negative_velocity_region);
% end
% extruded_frs(:,:,:,:,end) = shapeUnion(high_velocity_region, negative_velocity_region);
%% Evader Parameters
avoidSets = flip(extruded_frs,4);
terminalAvoidSet = avoidSets(:,:,:,1);
x0 = [0;0;0];
% input bounds
speed = [0, 4.0];
wMax = 1.0;
% control trying to min or max value function?
uModeEvader = 'max';
minWithEvader = 'none';
dCarEvader = DubinsCarVControl(x0, wMax, speed); %do dStep3 here
% Put grid and dynamic systems into schemeData
schemeDataEvader.grid = g_evader;
schemeDataEvader.dynSys = dCarEvader;
schemeDataEvader.accuracy = 'high'; %set accuracy
schemeDataEvader.uMode = uModeEvader;
HJIextraArgsEvader.targets = avoidSets;
HJIextraArgsEvader.visualize = true; %show plot
HJIextraArgsEvader.fig_num = 2; %set figure number
HJIextraArgsEvader.deleteLastPlot = true; %delete previous plot as you update
HJIextraArgsEvader.visualizeLevelSet = true;
% HJIextraArgs.visualizeObstaclesFunction = true;
%[data, tau, extraOuts] = ...
% HJIPDE_solve(data0, tau, schemeData, minWith, extraArgs)
[eyes_closed_invariance_kernel, ~, ~] = ...
HJIPDE_solve(terminalAvoidSet, ...
tau, ...
schemeDataEvader, ...
minWithEvader, ...
HJIextraArgsEvader);
%% Compute optimal trajectory from some initial state
if true
%set the initial state
xinit = [5, 0, -pi];
%check if this initial state is in the BRS/BRT
%value = eval_u(g, data, x)
value = eval_u(g_evader,eyes_closed_invariance_kernel(:,:,:,end),xinit);
if value >= 0 %if initial state is in BES/BET
% find optimal trajectory
dCarEvader.x = xinit; %set initial state of the dubins car
TrajextraArgs.uMode = uModeEvader; %set if control wants to min or max
TrajextraArgs.visualize = true; %show plot
TrajextraArgs.fig_num = 3; %figure number
%we want to see the first two dimensions (x and y)
TrajextraArgs.projDim = [1 1 0];
%flip data time points so we start from the beginning of time
dataTraj = flip(eyes_closed_invariance_kernel,4);
for t = 1:length(tau)
t
derivs{t} = computeGradients(g_evader, dataTraj(:,:,:,t));
end
else
error(['Initial state is not in the BRS/BRT! It have a value of ' num2str(value,2)])
end
end
%%
if compTraj && value >= 0
xinit = [2.5,-1.0,pi/2];
dCarEvader.x = xinit;
% [traj, traj_tau] = ...
% computeOptTraj(g, data, tau, dynSys, extraArgs)
[traj, traj_tau] = ...
computeOptTraj(g_evader, dataTraj, tau, dCarEvader, TrajextraArgs, derivs, flip(avoidSets,4));
end
% %% Process data for saving
% derivs_array = zeros([size(eyes_closed_invariance_kernel),3]);
% for t = 1:length(derivs)
% for d = 1:3
% derivs_array(:,:,:,t,d) = derivs{t}{d};
% end
% end
%
% eyes_closed_invariance_kernel_orig = eyes_closed_invariance_kernel;
% eyes_closed_invariance_kernel_wrapped = cat(4, ...
% eyes_closed_invariance_kernel, ...
% eyes_closed_invariance_kernel(:,:,:,1,:));
%
% derivs_array = cat(4, derivs_array, derivs_array(:,:,:,1,:,:));
%
% eyes_closed_invariance_kernel_wrapped = flip(eyes_closed_invariance_kernel_wrapped, 5);
%
% g_evader_wrapped = g_evader;
% g_evader_wrapped.vs{4} = cat(1, g_evader.vs{4}, g_evader.vs{4}(end) + g_evader.dx(4));
%
% g_vs_x = g_evader_wrapped.vs{1};
% g_vs_y = g_evader_wrapped.vs{2};
% g_vs_v = g_evader_wrapped.vs{3};
% g_vs_theta = g_evader_wrapped.vs{4};
%% Save data
if false
save('eyesclosedV1.mat',...
'eyes_closed_invariance_kernel_wrapped',...
'derivs_array',...
'g_vs_x',...
'g_vs_y',...
'g_vs_v',...
'g_vs_theta',...
'accel',...
'rMin',...
'wMax',...
'tau',...
'timestamp',...
'-v7.3')
end
%% Validate lookup
% out = eval_u(g_evader_wrapped, eyes_closed_invariance_kernel_wrapped(:,:,:,:,1), [2.12,-2.21,0.32,2.06])