forked from HJReachability/helperOC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial.m
130 lines (105 loc) · 3.84 KB
/
tutorial.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
function tutorial_test()
% 1. Run Backward Reachable Set (BRS) with a goal
% uMode = 'min' <-- goal
% minWith = 'none' <-- Set (not tube)
% compTraj = false <-- no trajectory
% 2. Run BRS with goal, then optimal trajectory
% uMode = 'min' <-- goal
% minWith = 'none' <-- Set (not tube)
% compTraj = true <-- compute optimal trajectory
% 3. Run Backward Reachable Tube (BRT) with a goal, then optimal trajectory
% uMode = 'min' <-- goal
% minWith = 'zero' <-- Tube (not set)
% compTraj = true <-- compute optimal trajectory
% 4. Add disturbance
% dStep1: define a dMax (dMax = [.25, .25, 0];)
% dStep2: define a dMode (opposite of uMode)
% dStep3: input dMax when creating your DubinsCar
% dStep4: add dMode to schemeData
% 5. Change to an avoid BRT rather than a goal BRT
% uMode = 'max' <-- avoid
% dMode = 'min' <-- opposite of uMode
% minWith = 'zero' <-- Tube (not set)
% compTraj = false <-- no trajectory
% 6. Change to a Forward Reachable Tube (FRT)
% add schemeData.tMode = 'forward'
% note: now having uMode = 'max' essentially says "see how far I can
% reach"
% 7. Add obstacles
% add the following code:
% obstacles = shapeCylinder(g, 3, [-1.5; 1.5; 0], 0.75);
% HJIextraArgs.obstacles = obstacles;
%% Should we compute the trajectory?
compTraj = true;
%% Grid
grid_min = [-5; -5; -pi]; % Lower corner of computation domain
grid_max = [5; 5; pi]; % Upper corner of computation domain
N = [41; 41; 41]; % Number of grid points per dimension
pdDims = 3; % 3rd dimension is periodic
g = createGrid(grid_min, grid_max, N, pdDims);
% Use "g = createGrid(grid_min, grid_max, N);" if there are no periodic
% state space dimensions
%% target set
R = 1;
% data0 = shapeCylinder(grid,ignoreDims,center,radius)
data0 = shapeCylinder(g, 3, [0; 0; 0], R);
% also try shapeRectangleByCorners, shapeSphere, etc.
%% time vector
t0 = 0;
tMax = 2;
dt = 0.05;
tau = t0:dt:tMax;
%% problem parameters
% input bounds
speed = 1;
wMax = 1;
% do dStep1 here
% control trying to min or max value function?
uMode = 'min';
% do dStep2 here
%% Pack problem parameters
% Define dynamic system
% obj = DubinsCar(x, wMax, speed, dMax)
dCar = DubinsCar([0, 0, 0], wMax, speed); %do dStep3 here
% Put grid and dynamic systems into schemeData
schemeData.grid = g;
schemeData.dynSys = dCar;
schemeData.accuracy = 'high'; %set accuracy
schemeData.uMode = uMode;
%do dStep4 here
%% If you have obstacles, compute them here
%% Compute value function
HJIextraArgs.visualize = true; %show plot
HJIextraArgs.fig_num = 1; %set figure number
HJIextraArgs.deleteLastPlot = true; %delete previous plot as you update
%[data, tau, extraOuts] = ...
% HJIPDE_solve(data0, tau, schemeData, minWith, extraArgs)
[data, tau2, ~] = ...
HJIPDE_solve(data0, tau, schemeData, 'none', HJIextraArgs);
%% Compute optimal trajectory from some initial state
if compTraj
pause
%set the initial state
xinit = [2, 1, -pi];
%check if this initial state is in the BRS/BRT
%value = eval_u(g, data, x)
value = eval_u(g,data(:,:,:,end),xinit);
if value <= 0 %if initial state is in BRS/BRT
% find optimal trajectory
dCar.x = xinit; %set initial state of the dubins car
TrajextraArgs.uMode = uMode; %set if control wants to min or max
TrajextraArgs.visualize = true; %show plot
TrajextraArgs.fig_num = 2; %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(data,4);
% [traj, traj_tau] = ...
% computeOptTraj(g, data, tau, dynSys, extraArgs)
[traj, traj_tau] = ...
computeOptTraj(g, dataTraj, tau2, dCar, TrajextraArgs);
else
error(['Initial state is not in the BRS/BRT! It have a value of ' num2str(value,2)])
end
end
end