-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunchHammerhead.m
281 lines (227 loc) · 8.35 KB
/
launchHammerhead.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
% Top level script for setting up paths, setting constants,
% launching GUI, etc.
% clear all; close all;
%% Open the model
% Don't do this. Running the model on the robot after opening the GUI screws up the GUI.
% open('robulink.slx');
%% Paths
addpath('gui');
KINECT_FOLDER = 'kinect';
addpath(KINECT_FOLDER);
addpath([KINECT_FOLDER '/Mex']);
addpath([KINECT_FOLDER '/Config']);
addpath('localization');
addpath('path_following');
addpath('rover_functions');
addpath('terrain_assessment');
addpath('utils');
%% Global Variables
% Safety
global vAbsMax;
global omegaAbsMax;
% Kinect stuff
global context; global option;
global isContextDeleted;
global width; global height;
global rgb; global depth;
global U; global V;
global maxDepth;
% Rover localization
global T_rg;
global framePoints_k;
global calibStruct;
global isTrackingCalibrated;
global sampleList_k;
global lastPixVec;
global rgbObjectMask;
global localizeManuallyThisIter;
% Path planning and following
global waypoints_g; global pathLength;
global atGoal;
global v; global k1; global k2;
global goalThresh;
global maxPathLengthMultiple;
% Terrain
global terrain;
% Teleop mode settings
global enableTeleopMode;
global v_teleop; global omega_teleop;
%% Constants and initializations
% Safety
vAbsMax = 0.6;
omegaAbsMax = 4;
% Kinect stuff
isContextDeleted = true;
width = 640;
height = 480;
[U,V] = meshgrid(1:width, 1:height);
maxDepth = 10000; % mm
lostKinectTrackingCount = 0;
lostKinectTracking = false;
T_1s = NaN;
savedMapImages = false;
% Rover Localization
T_rg = NaN;
T_rg_prev = NaN;
T_rg_last_kinect = NaN;
T_rg_history = zeros(4,4);
lostKinectCount = 0;
rgbObjectMask = repmat(false(height,width), [1,1,3]);
localizeManuallyThisIter = false;
%Rolling average filter for the Transformation
FILTER_SIZE = 3;
T_rg_filter = zeros(4,4,FILTER_SIZE);
T_rg_kinect_flag = false;
T_rg_start_flag = false;
T_mk = NaN;
lastPixVec = NaN;
if exist('trackingCalibration.mat', 'file')% == 2
S = load('trackingCalibration.mat');
calibStruct = S.calibStruct;
isTrackingCalibrated = true;
else
isTrackingCalibrated = false;
end
% Path planning and following
atGoal = true;
v = 0.5;
k1 = 0.5; % lateral
k2 = 2; % heading
goalThresh = 0.3; % meters
maxPathLengthMultiple = 10;
distTraveled = 0; % meters
% Teleop mode setting
enableTeleopMode = false;
v_teleop = 0.5;
omega_teleop = 4;
% Sample found colours
[X, map] = imread('NO.bmp');
noSampleCData = ind2rgb(X,map);
[X,map] = imread('YES.bmp');
foundSampleCData = ind2rgb(X,map);
%% Launch GUI
h = hammerheadGUI;
gui_data = guidata(h);
% Keyboard control
set(h,'KeyPressFcn',@KeyPressFcn,'KeyReleaseFcn',@KeyReleaseFcn);
%% Main loop
rto_detectSample = get_param('robulink/Detect Sample Filter','RunTimeObject');
rto_lightSensor = get_param('robulink/Light Sensor','RunTimeObject');
rto_batteryLevel = get_param('robulink/Battery','RunTimeObject');
rto_clearance = get_param('robulink/Ultrasonic Sensor','RunTimeObject');
rto_odometry = get_param('robulink/Wheel Odometry','RunTimeObject');
rto_odometryState = get_param('robulink/Display State','RunTimeObject');
%Reset the co-ordinate frame
resetFlag = str2num(get_param('robulink/resetFlag','Value'));
set_param('robulink/resetFlag','Value', num2str(~resetFlag));
%Debug
dstate_list = [];
while ishandle(h)
% tic
% Kinect data streams
[rgb, depth] = getKinectData(context, option);
if isfield(terrain, 'T_kg')
[depth,~] = fillMissingDepthWithGroundPlane(context, depth, U, V, terrain.m, terrain.n, terrain.p);
if ~savedMapImages
mappingData.rgb = rgb;
mappingData.depth = depth;
mappingData.realWorldCoords_k = mxNiConvertProjectiveToRealWorld(context, depth)/1000;
save('mappingData.mat','mappingData');
savedMapImages = true;
end
end
set(gui_data.kinectRGB_image,'CData',rgb);
set(gui_data.kinectDepth_image,'CData',depth);
% Detect the samples
if ~isempty(rto_lightSensor)
set(gui_data.txt_sampleDetection, 'String', num2str(rto_lightSensor.OutputPort(1).Data));
else
set(gui_data.txt_sampleDetection, 'String', 'n/a');
end
if ~isempty(rto_detectSample) && rto_detectSample.OutputPort(1).Data
set(gui_data.overSample_image, 'CData', foundSampleCData);
overSample = true;
else
overSample = false;
set(gui_data.overSample_image, 'CData', noSampleCData);
end
%Health and safety
if ~isempty(rto_batteryLevel)
set(gui_data.txt_batteryLevel, 'String', [num2str(rto_batteryLevel.OutputPort(1).Data) ' mV']);
end
if ~isempty(rto_odometry)
set(gui_data.txt_velocity, 'String', [num2str(norm([rto_odometry.OutputPort(1).Data, rto_odometry.OutputPort(2).Data]),2) ' m/s']);
set(gui_data.txt_omega, 'String', [num2str(rto_odometry.OutputPort(3).Data,2) ' deg/s']);
end
if ~isempty(rto_clearance)
set(gui_data.txt_clearance, 'String', num2str(rto_clearance.OutputPort(1).Data));
end
% Note: teleop doesn't operate smoothly if all this stuff is going on,
% so just disable it if we're teleopping since we don't need it anyway
if true%~enableTeleopMode
% Localization etc.
if isTrackingCalibrated
if localizeManuallyThisIter
[redCentroid, blueCentroid, redVec_k, blueVec_k] = localizeManually(context, rgb, depth);
localizeManuallyThisIter = false;
else
[redCentroid, blueCentroid, redVec_k,blueVec_k] = localizeRover(context, rgb, depth, calibStruct, lastPixVec);
end
if ~isnan(redCentroid)
displayLocalization(gui_data.kinectRGB, redCentroid, blueCentroid);
lastPixVec = redCentroid - blueCentroid;
end
if isfield(terrain, 'T_gk')
T_rg_prev = T_rg;
if ~isnan(redCentroid(1))
T_rg = localizeInTerrain(redVec_k,blueVec_k, terrain.T_gk);
T_rg_last_kinect = T_rg; %Used for keeping track of the last T_rg that came from the kinect
lostKinectCount = 0;
resetFlag = str2num(get_param('robulink/resetFlag','Value'));
set_param('robulink/resetFlag','Value', num2str(~resetFlag));
elseif ~isempty(rto_odometryState)
dx = rto_odometryState.OutputPort(1).Data;
dy = rto_odometryState.OutputPort(2).Data;
dtheta = rto_odometryState.OutputPort(3).Data;
dstate = [dx dy dtheta]';
if sum(dstate) > 0
disp(dstate);
end
lostKinectCount =lostKinectCount+1;
%Do not consider the first iteration where kinect
%data is lost
if lostKinectCount > 3
T_rg = localizeWithWheelOdom(dx,dy,dtheta, T_rg_last_kinect);
end
else
T_rg = NaN;
end
%T_rg_history(:,:,end+1) = inv(T_rg);
% Path following
if ~atGoal && ~overSample
set_param('robulink/resetFlag','Value', '0');
[atGoal, distTraveled] = followPathIteration(T_rg, T_rg_prev, waypoints_g, atGoal, distTraveled);
disp(['Distance Traveled: ' num2str(distTraveled) ' / ' num2str(pathLength)]);
%atGoal = atGoal || distTraveled >= maxPathLengthMultiple * pathLength;
rovPos = T_rg \ [0 0 0 1]';
if atGoal
distTraveled
disp('SUCCESS: Path completed.');
brake();
end
elseif overSample
brake();
atGoal = true;
disp('SUCCESS: Path completed.');
disp('FOUND SAMPLE!');
else
distTraveled = 0;
end
end
end
else
atGoal = true;
end
drawnow;
pause(0.001);
end