# robot motion algorithm with classdef

12 ビュー (過去 30 日間)
aydin altun 2020 年 6 月 27 日

i am creating an algorithm for robot motion with classdef that tries to reach goal point through the obstacles , the codes i wrote consisted with two parts , when i removed first one there is no error but if i run them together , there are many errros mainly
Illegal use of reserved keyword "classdef".
%first part
a2=rot90(a);
imagesc(a2);
set(gca,'YDir','normal')
grid on
%second part
properties(Access=protected)
H % hit points
j % number of hit points
mline % line from starting position to goal
step % state, in step 1 or step 2 of algorithm
edge % edge list
k % edge index
end
methods
function bug = Bug2(varargin)
%Bug2.Bug2 Construct a Bug2 navigation object
%
% B = Bug2(MAP, OPTIONS) is a bug2 navigation object, and MAP is an occupancy grid,
% a representation of a planar world as a matrix whose elements are 0 (free
% space) or 1 (occupied).
% 'goal',G Specify the goal point (1x2)
% 'inflate',K Inflate all obstacles by K cells.
bug.H = [];
bug.j = 1;
bug.step = 1;
end
function pp = query(bug, start, goal, varargin)
%Bug2.query Find a path
%
% B.query(START, GOAL, OPTIONS) is the path (Nx2) from START (1x2) to GOAL
% (1x2). Row are the coordinates of successive points along the path. If
% either START or GOAL is [] the grid map is displayed and the user is
% prompted to select a point by clicking on the plot.
% - START and GOAL are given as X,Y coordinates in the grid map, not as
% MATLAB row and column coordinates.
% - START and GOAL are tested to ensure they lie in free space.
% - If the bug does a lot of back tracking it's hard to see the current
% position, use the 'current' option.
% - For the movie option if M contains an extension a movie file with that
% extension is created. Otherwise a folder will be created containing
% individual frames.
bug.start = []; bug.goal = [];
bug.checkquery(start, goal);
% compute the m-line
% create homogeneous representation of the line
% line*[x y 1]' = 0
bug.mline = homline(bug.start(1), bug.start(2), ...
bug.goal(1), bug.goal(2));
bug.mline = bug.mline / norm(bug.mline(1:2));
% iterate using the next() method until we reach the goal
robot = bug.start(:);
bug.step = 1;
path = bug.start(:);
% only return the path if required
if nargout > 0
pp = path';
end
end
function plot_mline(bug, ls)
% parameters of the M-line, direct from initial position to goal
% as a vector mline, such that [robot 1]*mline = 0
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
% handle the case that the line is vertical
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)
% implement the main state machine for bug2
n = [];
robot = robot(:);
% these are coordinates (x,y)
if bug.step == 1
% Step 1. Move along the M-line toward the goal
if colnorm(bug.goal - robot) == 0 % are we there yet?
return
end
% motion on line toward goal
d = bug.goal-robot;
if abs(d(1)) > abs(d(2))
% line slope less than 45 deg
dx = sign(d(1));
L = bug.mline;
y = -( (robot(1)+dx)*L(1) + L(3) ) / L(2);
dy = round(y - robot(2));
else
% line slope greater than 45 deg
dy = sign(d(2));
L = bug.mline;
x = -( (robot(2)+dy)*L(2) + L(3) ) / L(1);
dx = round(x - robot(1));
end
% detect if next step is an obstacle
if bug.isoccupied(robot + [dx; dy])
bug.message('(%d,%d) obstacle!', n);
bug.H(bug.j,:) = robot; % define hit point
bug.step = 2;
% get a list of all the points around the obstacle
bug.edge = edgelist(bug.occgridnav == 0, robot);
bug.k = 2; % skip the first edge point, we are already there
else
n = robot + [dx; dy];
end
end % step 1
if bug.step == 2
% Step 2. Move around the obstacle until we reach a point
% on the M-line closer than when we started.
if colnorm(bug.goal-robot) == 0 % are we there yet?
return
end
if bug.k <= numcols(bug.edge)
n = bug.edge(:,bug.k); % next edge point
else
% we are at the end of the list of edge points, we
% are back where we started. Step 2.c test.
error('RTB:bug2:noplan', 'robot is trapped')
return;
end
% are we on the M-line now ?
if abs( [robot' 1]*bug.mline') <= 0.5
bug.message('(%d,%d) moving along the M-line', n);
% are closer than when we encountered the obstacle?
if colnorm(robot-bug.goal) < colnorm(bug.H(bug.j,:)'-bug.goal)
% back to moving along the M-line
bug.j = bug.j + 1;
bug.step = 1;
return;
end
end
% no, keep going around
bug.message('(%d,%d) keep moving around obstacle', n)
bug.k = bug.k+1;
end % step 2
end % next
function plan(bug)
error('RTB:Bug2:badcall', 'This class has no plan method');
end
end % methods
end% classdef

サインインしてコメントする。

### 回答 (1 件)

Maadhav Akula 2020 年 7 月 2 日
"classdef" is a reserved keyword used to define classes in MATLAB.
Only blank lines and comments can precede classdef. Hence you are getting that error. You have to define the second part (or) your class definitions in a seperate file. You can learn more about classdef and classes in MATLAB by referring the following links:
Hope this helps!

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by