forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeBearingSensor.m
433 lines (380 loc) · 15.7 KB
/
RangeBearingSensor.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
%RangeBearingSensor Range and bearing sensor class
%
% A concrete subclass of the Sensor class that implements a range and bearing
% angle sensor that provides robot-centric measurements of landmark points in
% the world. To enable this it holds a references to a map of the world (LandmarkMap object)
% and a robot (Vehicle subclass object) that moves in SE(2).
%
% The sensor observes landmarks within its angular field of view between
% the minimum and maximum range.
%
% Methods::
%
% reading range/bearing observation of random landmark
% h range/bearing observation of specific landmark
% Hx Jacobian matrix with respect to vehicle pose dh/dx
% Hp Jacobian matrix with respect to landmark position dh/dp
% Hw Jacobian matrix with respect to noise dh/dw
%-
% g feature position given vehicle pose and observation
% Gx Jacobian matrix with respect to vehicle pose dg/dx
% Gz Jacobian matrix with respect to observation dg/dz
%
% Properties (read/write)::
% W measurement covariance matrix (2x2)
% interval valid measurements returned every interval'th call to reading()
% landmarklog time history of observed landmarks
%
% Reference::
%
% Robotics, Vision & Control, Chap 6,
% Peter Corke,
% Springer 2011
%
% See also Sensor, Vehicle, LandmarkMap, EKF.
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
classdef RangeBearingSensor < Sensor
properties
W % measurment covariance
r_range % range limits
theta_range % angle limits
randstream % random stream just for Sensors
landmarklog % time history of observed landmarks
end
properties (SetAccess = private)
count % number of reading()s
end
methods
function s = RangeBearingSensor(robot, map, varargin)
%RangeBearingSensor.RangeBearingSensor Range and bearing sensor constructor
%
% S = RangeBearingSensor(VEHICLE, MAP, OPTIONS) is an object
% representing a range and bearing angle sensor mounted on the Vehicle
% subclass object VEHICLE and observing an environment of known landmarks
% represented by the LandmarkMap object MAP. The sensor covariance is W
% (2x2) representing range and bearing covariance.
%
% The sensor has specified angular field of view and minimum and maximum
% range.
%
% Options::
% 'covar',W covariance matrix (2x2)
% 'range',xmax maximum range of sensor
% 'range',[xmin xmax] minimum and maximum range of sensor
% 'angle',TH angular field of view, from -TH to +TH
% 'angle',[THMIN THMAX] detection for angles betwen THMIN
% and THMAX
% 'skip',K return a valid reading on every K'th call
% 'fail',[TMIN TMAX] sensor simulates failure between
% timesteps TMIN and TMAX
% 'animate' animate sensor readings
%
% See also options for Sensor constructor.
%
% See also RangeBearingSensor.reading, Sensor.Sensor, Vehicle, LandmarkMap, EKF.
% call the superclass constructor
s = s@Sensor(robot, map, varargin{:});
s.randstream = RandStream.create('mt19937ar');
opt.range = [];
opt.angle = [];
opt.covar = zeros(2,2);
[opt,args] = tb_optparse(opt, varargin);
s.W = opt.covar;
if ~isempty(opt.range)
if length(opt.range) == 1
s.r_range = [0 opt.range];
elseif length(opt.range) == 2
s.r_range = opt.range;
end
end
if ~isempty(opt.angle)
if length(opt.angle) == 1
s.theta_range = [-opt.angle opt.angle];
elseif length(opt.angle) == 2
s.theta_range = opt.angle;
end
end
s.count = 0;
s.verbose = opt.verbose;
end
function init(s)
s.landmarklog = [];
end
function k = selectFeature(s)
k = s.randstream.randi(s.map.nlandmarks);
end
function [z,jf] = reading(s)
%RangeBearingSensor.reading Choose landmark and return observation
%
% [Z,K] = S.reading() is an observation of a random visible landmark where
% Z=[R,THETA] is the range and bearing with additive Gaussian noise of
% covariance W (property W). K is the index of the map feature that was
% observed.
%
% The landmark is chosen randomly from the set of all visible landmarks,
% those within the angular field of view and range limits. If no valid
% measurement, ie. no features within range, interval subsampling enabled
% or simulated failure the return is Z=[] and K=0.
%
% Notes::
% - Noise with covariance W (property W) is added to each row of Z.
% - If 'animate' option set then show a line from the vehicle to the
% landmark
% - If 'animate' option set and the angular and distance limits are set
% then display that region as a shaded polygon.
% - Implements sensor failure and subsampling if specified to constructor.
%
% See also RangeBearingSensor.h.
% TODO probably should return K=0 to indicated invalid
% model a sensor that emits readings every interval samples
s.count = s.count + 1;
% check conditions for NOT returning a value
z = [];
jf = 0;
% sample interval
if mod(s.count, s.interval) ~= 0
return;
end
% simulated failure
if ~isempty(s.fail) && (s.count >= s.fail(1)) && (s.count <= s.fail(2))
return;
end
% create a polygon to indicate the active sensing area based on range+angle limits
if s.animate && ~isempty(s.theta_range) && ~isempty(s.r_range)
h = findobj(gca, 'tag', 'sensor-area');
if isempty(h)
th=linspace(s.theta_range(1), s.theta_range(2), 20);
x = s.r_range(2) * cos(th);
y = s.r_range(2) * sin(th);
if s.r_range(1) > 0
th = flip(th);
x = [x s.r_range(1) * cos(th)];
y = [y s.r_range(1) * sin(th)];
else
x = [x 0];
y = [y 0];
end
% no sensor zone, create one
plot_poly([x; y], 'fillcolor', 'r', 'alpha', 0.1, 'edgecolor', 'none', 'animate', 'tag', 'sensor-area');
else
%hg = get(h, 'Parent');
plot_poly(h, s.robot.x);
end
end
if ~isempty(s.r_range) || ~isempty(s.theta_range)
% if range and bearing angle limits are in place look for
% any landmarks that match criteria
% get range/bearing to all landmarks, one per row
z = s.h(s.robot.x');
jf = 1:numcols(s.map.map);
if ~isempty(s.r_range)
% find all within range
k = find( z(:,1) >= s.r_range(1) & z(:,1) <= s.r_range(2) );
z = z(k,:);
jf = jf(k);
end
if ~isempty(s.theta_range)
% find all within angular range as well
k = find( z(:,2) >= s.theta_range(1) & z(:,2) <= s.theta_range(2) );
z = z(k,:);
jf = jf(k);
end
% deal with cases for 0 or > 1 features found
if isempty(z)
% no landmarks found
jf = 0;
elseif length(k) >= 1
% more than 1 in range, pick a random one
i = s.randstream.randi(length(k));
z = z(i,:);
jf = jf(i);
end
else
% randomly choose the feature
jf = s.selectFeature();
% compute the range and bearing from robot to feature
z = s.h(s.robot.x', jf);
end
if s.verbose
if isempty(z)
fprintf('Sensor:: no features\n');
else
fprintf('Sensor:: feature %d: %.1f %.1f\n', jf, z);
end
end
if s.animate
s.plot(jf);
end
z = z';
% add the reading to the landmark log
s.landmarklog = [s.landmarklog jf];
end
function z = h(s, xv, jf)
%RangeBearingSensor.h Landmark range and bearing
%
% Z = S.h(X, K) is a sensor observation (1x2), range and bearing, from vehicle at
% pose X (1x3) to the K'th landmark.
%
% Z = S.h(X, P) as above but compute range and bearing to a landmark at coordinate P.
%
% Z = s.h(X) as above but computes range and bearing to all
% map features. Z has one row per landmark.
%
% Notes::
% - Noise with covariance W (propertyW) is added to each row of Z.
% - Supports vectorized operation where XV (Nx3) and Z (Nx2).
% - The landmark is assumed visible, field of view and range liits are not
% applied.
%
% See also RangeBearingSensor.reading, RangeBearingSensor.Hx, RangeBearingSensor.Hw, RangeBearingSensor.Hp.
% get the landmarks, one per row
if nargin < 3
% s.h(XV)
xlm = s.map.map';
elseif length(jf) == 1
% s.h(XV, JF)
xlm = s.map.map(:,jf)';
else
% s.h(XV, XF)
xlm = jf(:)';
end
% Straightforward code:
%
% dx = xf(1) - xv(1); dy = xf(2) - xv(2);
%
% z = zeros(2,1);
% z(1) = sqrt(dx^2 + dy^2); % range measurement
% z(2) = atan2(dy, dx) - xv(3); % bearing measurement
%
% Vectorized code:
% compute range and bearing
dx = xlm(:,1) - xv(:,1); dy = xlm(:,2) - xv(:,2);
z = [sqrt(dx.^2 + dy.^2) angdiff(atan2(dy, dx), xv(:,3)) ]; % range & bearing measurement
% add noise with covariance W
z = z + s.randstream.randn(size(z)) * sqrtm(s.W) ;
end
function J = Hx(s, xv, jf)
%RangeBearingSensor.Hx Jacobian dh/dx
%
% J = S.Hx(X, K) returns the Jacobian dh/dx (2x3) at the vehicle
% state X (3x1) for map landmark K.
%
% J = S.Hx(X, P) as above but for a landmark at coordinate P.
%
% See also RangeBearingSensor.h.
if length(jf) == 1
xf = s.map.map(:,jf);
else
xf = jf;
end
if isempty(xv)
xv = s.robot.x;
end
Delta = xf - xv(1:2)';
r = norm(Delta);
J = [
-Delta(1)/r, -Delta(2)/r, 0
Delta(2)/(r^2), -Delta(1)/(r^2), -1
];
end
function J = Hp(s, xv, jf)
%RangeBearingSensor.Hp Jacobian dh/dp
%
% J = S.Hp(X, K) is the Jacobian dh/dp (2x2) at the vehicle
% state X (3x1) for map landmark K.
%
% J = S.Hp(X, P) as above but for a landmark at coordinate P (1x2).
%
% See also RangeBearingSensor.h.
if length(jf) == 1
xf = s.map.map(:,jf);
else
xf = jf;
end
Delta = xf - xv(1:2)';
r = norm(Delta);
J = [
Delta(1)/r, Delta(2)/r
-Delta(2)/(r^2), Delta(1)/(r^2)
];
end
function J = Hw(s, xv, jf)
%RangeBearingSensor.Hx Jacobian dh/dw
%
% J = S.Hw(X, K) is the Jacobian dh/dw (2x2) at the vehicle
% state X (3x1) for map landmark K.
%
% See also RangeBearingSensor.h.
J = eye(2,2);
end
function xf = g(s, xv, z)
%RangeBearingSensor.g Compute landmark location
%
% P = S.g(X, Z) is the world coordinate (2x1) of a feature given
% the observation Z (1x2) from a vehicle state with X (3x1).
%
% See also RangeBearingSensor.Gx, RangeBearingSensor.Gz.
range = z(1);
bearing = z(2) + xv(3); % bearing angle in vehicle frame
xf = [xv(1)+range*cos(bearing); xv(2)+range*sin(bearing)];
end
function J = Gx(s, xv, z)
%RangeBearingSensor.Gxv Jacobian dg/dx
%
% J = S.Gx(X, Z) is the Jacobian dg/dx (2x3) at the vehicle state X (3x1) for
% sensor observation Z (2x1).
%
% See also RangeBearingSensor.g.
theta = xv(3);
r = z(1);
bearing = z(2);
J = [
1, 0, -r*sin(theta + bearing);
0, 1, r*cos(theta + bearing)
];
end
function J = Gz(s, xv, z)
%RangeBearingSensor.Gz Jacobian dg/dz
%
% J = S.Gz(X, Z) is the Jacobian dg/dz (2x2) at the vehicle state X (3x1) for
% sensor observation Z (2x1).
%
% See also RangeBearingSensor.g.
theta = xv(3);
r = z(1);
bearing = z(2);
J = [
cos(theta + bearing), -r*sin(theta + bearing);
sin(theta + bearing), r*cos(theta + bearing)
];
end
function str = char(s)
str = char@Sensor(s);
str = char(str, ['W = ', mat2str(s.W, 3)]);
str = char(str, sprintf('interval %d samples', s.interval) );
if ~isempty(s.r_range)
str = char(str, sprintf('range: %g to %g', s.r_range) );
end
if ~isempty(s.theta_range)
str = char(str, sprintf('angle: %g to %g', s.theta_range) );
end
end
end % method
end % classdef