calssdef varible are overwriten using classdef function

1 Ansicht (letzte 30 Tage)
eden meirovich
eden meirovich am 24 Dez. 2020
Kommentiert: Steven Lord am 25 Dez. 2020
hello
i have a classdef that i created. i want to create difrent varible in that class.
lets say rob 1 and rob 2.
when i create rob2 (after creating rob1),rbo1 it's overwriten by rob2 ( so they are the same)
i could i fix that? i rather not change the classdef code.
thank u!
classdef cRobot < handle
properties
% pose
x;
y;
theta;
% noise
forward_noise = 0;
turn_noise = 0;
sense_noise_range = 0;
sense_noise_bearing = 0;
end
methods
function obj = cRobot % method constructor
% place robot with a random pose
obj.x = rand*cWorld.world_size;
obj.y = rand*cWorld.world_size;
obj.theta = rand*2*pi;
end
% sets the new pose
function obj = set(obj,new_x,new_y, new_orientation)
if (new_x < 0 | new_x >= cWorld.world_size)
error('X coordinate out of bound');
end
if (new_y < 0 | new_y >= cWorld.world_size)
error('Y coordinate out of bound');
end
if (new_orientation < 0 | new_orientation >= 2 * pi);
error('Orientation must be in [0,2pi]');
end
obj.x = new_x;
obj.y = new_y;
obj.theta = new_orientation;
end
% prints the pose of the robot to the Matlab prompt
function print(obj)
format long
display(['[x= ',num2str(obj.x), ' y=', num2str(obj.y), ' heading=', num2str(obj.theta),']']);
end
% plots the pose of the robot in the world
function plot(obj,mycolor,style)
if(nargin == 1)
mycolor = 'b'; % default
style = 'robot'
end
if(nargin == 2)
style = 'robot'; % default
end
hold on;
% different plot styles for the robot
switch style
case 'robot'
% size of robot
phi = linspace(0,2*pi,101);
r = 1;
% plot robot body
plot(obj.x + r*cos(phi),obj.y + r*sin(phi),'Color',mycolor);
hold on;
% plot heading direction
plot([obj.x, obj.x + r*cos(obj.theta)],[obj.y, obj.y + r*sin(obj.theta)],'Color',mycolor);
axis equal;
case 'particle'
% plots robot position but ignores heading
plot(obj.x,obj.y,'.','Color',mycolor,'MarkerSize',10);
otherwise
disp('unknown style');
end
xlim([0,cWorld.world_size]);
ylim([0,cWorld.world_size]);
end
% sets new noise parameters
function obj = set_noise(obj, new_forward_noise, new_turn_noise, new_sense_noise_range, new_sense_noise_bearing)
obj.forward_noise = new_forward_noise;
obj.turn_noise = new_turn_noise;
obj.sense_noise_range = new_sense_noise_range;
obj.sense_noise_bearing = new_sense_noise_bearing;
end
%move function
function obj = move(obj,u1,u2)
u1 = u1 + normrnd(0,obj.turn_noise);
u2 = u2 + normrnd(0,obj.forward_noise);
obj.x = obj.x + u2*cos(obj.theta+u1);
obj.y = obj.y + u2*sin(obj.theta+u1);
obj.theta =obj.theta + u1;
end
function mess = sense(obj)
for i =1:6
mess(i,1) = sqrt((cWorld.landmarks(i,1)-obj.x)^2+(cWorld.landmarks(i,2)-obj.y)^2) + normrnd(0,obj.sense_noise_range);
mess(i,2) = atan2(cWorld.landmarks(i,2)-obj.y,cWorld.landmarks(i,1)-obj.x) - obj.theta + normrnd(0,obj.sense_noise_bearing);
end
end
function p = measurement_probability(mess)
end
end
end
  1 Kommentar
Steven Lord
Steven Lord am 25 Dez. 2020
Can you show us the code you run to create rob1 and rob2 as instances of your cRobot class? I suspect I know what you're doing to see the behavior you describe, but seeing the code will help me be certain.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by