-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtutorial_simple_floating_boat.m
101 lines (90 loc) · 3.13 KB
/
tutorial_simple_floating_boat.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
close all
clear all
%% This example is designed for playing around with 8 types of reachability problem:
% which is determined by selecting the following options
% 'backward' vs 'forward'
% 'set' vs 'tube'
% and 'minimal' vs 'maximal' reachability.
% Note that the 'min'/'max' of the reachability problem does not necessarily correspond to uMode.
% In this example, setting uMode to 'max' corresponds to 'minimal reachability' in the backward problem.
% On the other hand, in the forward problem, 'max' uMode corresponds to the maximal reachability.
% For more details, please refer to Mitchell, "Comparing Forward and Backward Reachability
% as Tools for Safety Analysis".
% Finally, you can also add an obstacle to the problem, in this case, you will be solving a reach-avoid problem.
% The method for this is developed in Fisac et al., "Reach-Avoid Problems with Time-Varying Dynamics,
% Targets and Constraints". Set the below toggle to true to include the obstacle in the problem.
set_obstacle = false;
%% Set up grid.
grid_min = [-5; -5]; % Lower corner of computation domain
grid_max = [5; 5]; % Upper corner of computation domain
N = [101; 101]; % Number of grid points per dimension
g = createGrid(grid_min, grid_max, N);
%% Define target function.
data0 = shapeRectangleByCorners(g, [-1, -1], [1, 1]);
if set_obstacle
obstacle_function = shapeCylinder(g, [], [-1; -2;], 1);
HJIextraArgs.obstacleFunction = obstacle_function;
end
% set target function.
HJIextraArgs.targetFunction = data0;
% plot target function.
figure(1);
subplot(1, 2, 1);
visFuncIm(g, data0, 'r', 0.5);
hold on;
grid on;
axis equal;
subplot(1, 2, 2);
visSetIm(g, data0, 'r', 0);
hold on;
axis equal;
grid on;
xticks(-5:1:5);
%% time vector
t0 = 0;
tMax = 3;
dt = 0.02;
tau = t0:dt:tMax;
% 'min' or 'max'?
uMode = 'min';
schemeData.uMode = uMode;
% backward or forward?
schemeData.tMode = 'backward';
%Define dynamical system.
boat = SimpleFloatingBoat();
% Put grid and dynamic systems into schemeData
schemeData.grid = g;
schemeData.dynSys = boat;
% set accuracy
schemeData.accuracy = 'high';
%% Compute value function
HJIextraArgs.visualize.valueSet = 1;
HJIextraArgs.visualize.initialValueSet = 1;
HJIextraArgs.visualize.figNum = 2; %set figure number
HJIextraArgs.visualize.deleteLastPlot = true; %delete previous plot as you update
% For set computation, use 'none'. For tube computation, use 'minVWithL'
compMethod = 'minVWithL';
[data, tau2, ~] = ...
HJIPDE_solve(data0, tau, schemeData, compMethod, HJIextraArgs);
% Plot the final time target function.
figure(1);
subplot(1, 2, 1);
hold on;
visFuncIm(g, squeeze(data(:, :, end)), 'b', 0.5);
subplot(1, 2, 2);
hold on;
visSetIm(g, squeeze(data(:, :, end)), 'b', 0);
%% Compute optimal trajectory from some initial state
% set the initial state
xinit = [-2.0; -3];
boat.x = xinit; %set initial state of the dubins car
% set if control wants to min or max
TrajextraArgs.uMode = uMode;
% flip data time points so we start from the beginning of time
dataTraj = flip(data,3);
[traj, traj_tau] = ...
computeOptTraj(g, dataTraj, tau2, boat, TrajextraArgs);
% Showing trajectory.
figure(1);
subplot(1, 2, 2);
plot(traj(1, :), traj(2, :));