Issue in my class?

1 visualización (últimos 30 días)
pachu
pachu el 23 de Nov. de 2014
Comentada: pachu el 24 de Nov. de 2014
I am getting a very strange error when I try to initialize a class that I've written. properties of my class:
properties(GetAccess= 'private', SetAccess= 'private')
% Values require get and set methods to view or manipulate
% Sensor variables
odomDist; % Distance traveled since last check from odometry
% Format: double
odomAng; % Angle turned since last check from odometry
% Format: double
noise; % Contains noise data for sensors used
% Format: structure, fieldnames are sensor names and
% field values are sensor noise [mean standard_dev]
comDelay; % Communication delay of commands to robot
% Format: double
% Robot state variables
posAbs; % Position in absolute coordinates
% Format: vector of doubles, [x y]
velAbs; % Velocity in absolute coordinates
% Format: vector of doubles, [x y]
thAbs; % Yaw angle relative to positive x-axis
% Format: double (-pi < thAbs <= pi)
wAbs; % Angular velocity
% Format: double (positive counter-clockwise)
velInt; % Forward velocity intended by motor commands
% Format: double
wInt; % Angular velocity intended by motor commands
% Format: double
dataHist; % Time history of position and function calls for output
% Format: cell array, columns
% [time x y th fcn_called arg_val]
% time - Double, time relative to start of autonomous code
% x - Double, x-coordinate of center of robot
% y - Double, y-coordinate of center of robot
% th - Double, angle of robot relative to pos x-axis
% fcn_called - String or cell array of strings,
% name(s) of the function(s) called in that time step
% arg_val - Vector of doubles or cell array of vectors,
% value(s) for the input OR output argument(s);
% the robot object argument is ignored
% Environment variables
timeElap; % Time of the start of autonomous code execution
% Format: unsigned 64 bit integer
% time syntax - to be used with toc function
handlesGUI; % Handles to all GUI objects
% Format: structure containing handle numbers
% See SimulatorGUI.fig and SimulatorGUI.m for more
% format information on the contents of the structure
XY;
Ramp_Center;
Ramp_Entrance;
Ramp_Exit;
Target;
%positions of the sensors:
IR1;
IR2;
IR3;
IR4;
IR5;
%positions of the four corners;
p1;
p2;
p3;
p4;
body_array = zeros(4,4);
%sensor value array
sensor_vals = [0 0 0 0];
%wall array to calculate the the sensor hits:
wall_array = zeros(size(XY,1), 4);
%array of lines representing the IR sensors:
IR_array = zeros(5, 4);
%STATE VARIABLES;
%the current state
CURRENT_STATE;
%the next state
NEXT_STATE;
%collision
COLLIDED;
%distance to move
dist;
%theta to turn
thetadelta;
end
My constructor:
function obj= Rover8(varargin)
% obj = RedRover
% Creates instance of the user-defined class Create Robot and
% initializes all properties. Note that if RedRover is not
% called by SimulatorGUI (with handlesGUI argument), full
% functionality impossible.
%
% obj = RedRover(handlesGUI)
% Format of function call from SimulatorGUI. Passes along structure
% of handles for the GUI to allow full functionality
%
% Input:
% handlesGUI - Structure of handles to GUI objects
% e.g. handlesGUI.push_adv - handle for advance button
%
% Output:
% obj - Instance of class RedRover with all fields initialized
% Deal with input argument
% Will be handles to SimulatorGUI if called by simulator
if ~isempty(varargin) && isstruct(varargin{1})
obj.handlesGUI= varargin{1};
else
obj.handlesGUI= [];
end
% Assign properties
obj.timeElap= [];
obj.dataHist= {};
obj.noise= struct; % Empty structure to store noise data
obj.comDelay= 0; % Default instantaneous communication
obj.posAbs= [0 0]; % Initial position
obj.velAbs= [0 0]; % Assume robot is stationary to start
obj.thAbs= 0;
obj.wAbs= 0;
obj.velInt= 0;
obj.wInt= 0;
obj.CURRENT_STATE = 0;
obj.NEXT_STATE = 0;
obj.COLLIDED = false;
%set the positions of the sensors:
updateSensorPos(obj);
%set map vals
end
However, whenever I call this constructor from my i get the error: Undefined function or variable 'XY'.
Clearly XY is in the class so why am I getting this error? Any help is appreciated greatly.
  1 comentario
Guillaume
Guillaume el 23 de Nov. de 2014
Can you attach your class m file, assuming it's just one file?
It would be easier to figure out the problem that way.

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 23 de Nov. de 2014
pachu - yes, you have included a property named XY but then you have another property that references it in the property list
wall_array = zeros(size(XY,1), 4);
The above line of code seems more like something that you would want in your constructor once you have defined what XY is (a string, scalar, matrix, etc.). If you were just to replace the above line of code with
wall_array;
and then in the constructor do
% initialize XY to something
obj.XY = ...;
% initialize wall_array
obj.wall_array = zeros(size(obj.XY,1),4);
you should then be able to create an instance of your class.
  1 comentario
pachu
pachu el 24 de Nov. de 2014
Oh my god, I can't believe I just glanced over that so many times -.-. I guess it truly does help to get a fresh perspective haha. Anyway, this makes perfect sense, I just can't believe I missed it for so long. Thank you so much!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Sample Class Implementations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by