-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathmain.m
More file actions
106 lines (84 loc) · 3.14 KB
/
main.m
File metadata and controls
106 lines (84 loc) · 3.14 KB
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
clear; close all; clc;
%cfig = figure('Position', [10,10,1280,1080]);
cfig = figure(1);
% Lidar parameters
lidar = SetLidarParameters();
% Map parameters
borderSize = 1; % m
pixelSize = 0.2; % m
miniUpdated = false; %
miniUpdateDT = 0.1; % m
miniUpdateDR = deg2rad(5); % rad
% If the robot has moved 0.1 m or rotated 5 degree from last key scan,
% we would add a new key scan and update the map
% Scan matching parameters
fastResolution = [0.05; 0.05; deg2rad(0.5)]; % [m; m; rad]
bruteResolution = [0.01; 0.01; deg2rad(0.1)]; % not used
% Load lidar data
lidar_data = load('dataset/horizental_lidar.mat');
N = size(lidar_data.timestamps, 1);
% Create an empty map
map.points = [];
map.connections = [];
map.keyscans = [];
pose = [0; 0; 0];
path = pose;
% Here we go!!!!!!!!!!!!!!!!!!!!
for scanIdx = 1 : 1 : N
disp(['scan ', num2str(scanIdx)]);
% Get current scan [x1,y1; x2,y2; ...]
time = lidar_data.timestamps(scanIdx) * 1e-9;
scan = ReadAScan(lidar_data, scanIdx, lidar, 24);
% If it's the first scan, initiate
if scanIdx == 1
map = Initialize(map, pose, scan);
miniUpdated = true;
continue;
end
% ===== Matching current scan to local map ============
% 1. If we executed a mini update in last step, we shall update the
% local points map and local grid map (coarse)
if miniUpdated
localMap = ExtractLocalMap(map.points, pose, scan, borderSize);
gridMap1 = OccuGrid(localMap, pixelSize);
gridMap2 = OccuGrid(localMap, pixelSize/2);
end
% 2. Predict current pose using constant velocity motion model
if scanIdx > 2
pose_guess = pose + DiffPose(path(:,end-1), pose);
else
pose_guess = pose;
end
% 3. Fast matching
if miniUpdated
[pose, ~] = FastMatch(gridMap1, scan, pose_guess, fastResolution);
else
[pose, ~] = FastMatch(gridMap2, scan, pose_guess, fastResolution);
end
% 4. Refine the pose using smaller pixels
% gridMap = OccuGrid(localMap, pixelSize/2);
[pose, hits] = FastMatch(gridMap2, scan, pose, fastResolution/2);
%----------------------------------------------------------------------
% Execute a mini update, if the robot has moved a certain distance
dp = abs(DiffPose(map.keyscans(end).pose, pose));
if dp(1)>miniUpdateDT || dp(2)>miniUpdateDT || dp(3)>miniUpdateDR
miniUpdated = true;
[map, pose] = AddAKeyScan(map, gridMap2, scan, pose, hits,...
pixelSize, bruteResolution, 0.1, deg2rad(3));
else
miniUpdated = false;
end
path = [path, pose];
% ===== Loop Closing =========================================
% if miniUpdated
% if TryLoopOrNot(map)
% map.keyscans(end).loopTried = true;
% map = DetectLoopClosure(map, scan, hits, 4, pi/6, pixelSize);
% end
% end
%----------------------------------------------------------------------
% Plot
if mod(scanIdx, 30) == 0
PlotMap(cfig, map, path, scan, scanIdx);
end
end