load("hw3007.mat");
a2=rot90(a);
imagesc(a2);
set(gca,'YDir','normal')
grid on
classdef Bug2 < Navigation
properties(Access=protected)
H
j
mline
step
edge
k
end
methods
function bug = Bug2(varargin)
bug = bug@Navigation(varargin{:});
bug.H = [];
bug.j = 1;
bug.step = 1;
end
function pp = query(bug, start, goal, varargin)
bug.start = []; bug.goal = [];
bug.checkquery(start, goal);
bug.mline = homline(bug.start(1), bug.start(2), ...
bug.goal(1), bug.goal(2));
bug.mline = bug.mline / norm(bug.mline(1:2));
robot = bug.start(:);
bug.step = 1;
path = bug.start(:);
if nargout > 0
pp = path';
end
end
function plot_mline(bug, ls)
if nargin < 2
ls = 'k--';
end
dims = axis;
xmin = dims(1); xmax = dims(2);
ymin = dims(3); ymax = dims(4);
hold on
if bug.mline(2) == 0
plot([start(1) start(1)], [ymin ymax], 'k--');
else
x = [xmin xmax]';
y = -[x [1;1]] * [bug.mline(1); bug.mline(3)] / bug.mline(2);
plot(x, y, ls);
end
end
function n = next(bug, robot)
n = [];
robot = robot(:);
if bug.step == 1
if colnorm(bug.goal - robot) == 0
return
end
d = bug.goal-robot;
if abs(d(1)) > abs(d(2))
dx = sign(d(1));
L = bug.mline;
y = -( (robot(1)+dx)*L(1) + L(3) ) / L(2);
dy = round(y - robot(2));
else
dy = sign(d(2));
L = bug.mline;
x = -( (robot(2)+dy)*L(2) + L(3) ) / L(1);
dx = round(x - robot(1));
end
if bug.isoccupied(robot + [dx; dy])
bug.message('(%d,%d) obstacle!', n);
bug.H(bug.j,:) = robot;
bug.step = 2;
bug.edge = edgelist(bug.occgridnav == 0, robot);
bug.k = 2;
else
n = robot + [dx; dy];
end
end
if bug.step == 2
if colnorm(bug.goal-robot) == 0
return
end
if bug.k <= numcols(bug.edge)
n = bug.edge(:,bug.k);
else
error('RTB:bug2:noplan', 'robot is trapped')
return;
end
if abs( [robot' 1]*bug.mline') <= 0.5
bug.message('(%d,%d) moving along the M-line', n);
if colnorm(robot-bug.goal) < colnorm(bug.H(bug.j,:)'-bug.goal)
bug.j = bug.j + 1;
bug.step = 1;
return;
end
end
bug.message('(%d,%d) keep moving around obstacle', n)
bug.k = bug.k+1;
end
end
function plan(bug)
error('RTB:Bug2:badcall', 'This class has no plan method');
end
end
end