为什么这个类每次都要重新初始化?

发布于 2024-09-01 02:37:55 字数 6160 浏览 6 评论 0原文

我有 4 个文件,代码按预期“工作”。

我尝试清理所有内容,将代码放入函数中,等等......一切看起来都很好......但它不起作用。有人可以解释一下为什么 MatLab 如此古怪......还是我只是愚蠢?

通常情况下,我打字

terminator = simulation(100,20,0,0,0,1);
terminator.animate();

,它应该会生成一张树木地图,其中终结者在森林中走动。一切都以他的视角旋转。

当我将其分解为函数时......一切都停止工作。

我实际上只更改了几行代码,如注释中所示。

有效的代码:

classdef simulation
properties
    landmarks
    robot
end

methods
    function obj = simulation(mapSize, trees, x,y,heading,velocity)
        obj.landmarks = landmarks(mapSize, trees);
        obj.robot = robot(x,y,heading,velocity);
    end
    function animate(obj)
        %Setup Plots
        fig=figure;
        xlabel('meters'), ylabel('meters')
        set(fig, 'name', 'Phil''s AWESOME 80''s Robot Simulator')
        xymax = obj.landmarks.mapSize*3;
        xymin = -(obj.landmarks.mapSize*3);
        l=scatter([0],[0],'bo');
        axis([xymin xymax xymin xymax]);
        obj.landmarks.apparentPositions
        %Simulation Loop  THIS WAS ORGANIZED
        for n = 1:720,
            %Calculate and Set Heading/Location
            obj.robot.headingChange = navigate(n);

            %Update Position
            obj.robot.heading = obj.robot.heading + obj.robot.headingChange;
            obj.landmarks.heading = obj.robot.heading;
            y = cosd(obj.robot.heading);
            x = sind(obj.robot.heading);     
            obj.robot.x = obj.robot.x + (x*obj.robot.velocity);
            obj.robot.y = obj.robot.y + (y*obj.robot.velocity);
            obj.landmarks.x = obj.robot.x;
            obj.landmarks.y = obj.robot.y;

            %Animate
            set(l,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
            rectangle('Position',[-2,-2,4,4]);
            drawnow
        end
    end
end
end

-----------
classdef landmarks
properties
    fixedPositions  %# positions in a fixed coordinate system. [ x, y ]
    mapSize = 10;  %Map Size.  Value is side of square
    x=0;
    y=0;
    heading=0;
    headingChange=0;
end
properties (Dependent)
    apparentPositions
end
methods
    function obj = landmarks(mapSize, numberOfTrees)
        obj.mapSize = mapSize;
        obj.fixedPositions = obj.mapSize * rand([numberOfTrees, 2]) .* sign(rand([numberOfTrees, 2]) - 0.5);
    end
    function apparent = get.apparentPositions(obj)
        %-STILL ROTATES AROUND ORIGINAL ORIGIN
        currentPosition = [obj.x ; obj.y];
        apparent = bsxfun(@minus,(obj.fixedPositions)',currentPosition)';
        apparent = ([cosd(obj.heading)  -sind(obj.heading) ; sind(obj.heading)  cosd(obj.heading)] * (apparent)')';
    end
end
end

----------
classdef robot

properties
    x
    y
    heading
    velocity
    headingChange
end

methods
    function obj = robot(x,y,heading,velocity)
        obj.x = x;
        obj.y = y;
        obj.heading = heading;
        obj.velocity = velocity;
    end
end
end

----------
function headingChange = navigate(n)
%steeringChange = 5 * rand(1) * sign(rand(1) - 0.5); Most chaotic shit
%Draw an S
if n <270
    headingChange=1;
elseif n<540
    headingChange=-1;
elseif n<720
    headingChange=1;
else
    headingChange=1;
end
end

无效的代码...

classdef simulation
properties
    landmarks
    robot
end

methods
    function obj = simulation(mapSize, trees, x,y,heading,velocity)
        obj.landmarks = landmarks(mapSize, trees);
        obj.robot = robot(x,y,heading,velocity);
    end
    function animate(obj)
        %Setup Plots
        fig=figure;
        xlabel('meters'), ylabel('meters')
        set(fig, 'name', 'Phil''s AWESOME 80''s Robot Simulator')
        xymax = obj.landmarks.mapSize*3;
        xymin = -(obj.landmarks.mapSize*3);
        l=scatter([0],[0],'bo');
        axis([xymin xymax xymin xymax]);
        obj.landmarks.apparentPositions
        %Simulation Loop
        for n = 1:720,
            %Calculate and Set Heading/Location

            %Update Position
            headingChange = navigate(n); 
            obj.robot.updatePosition(headingChange); 
            obj.landmarks.updatePerspective(obj.robot.heading, obj.robot.x, obj.robot.y);

            %Animate
            set(l,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
            rectangle('Position',[-2,-2,4,4]);
            drawnow
        end
    end
end
end

-----------------
classdef landmarks
properties
    fixedPositions;  %# positions in a fixed coordinate system. [ x, y ]
    mapSize;  %Map Size.  Value is side of square
    x;
    y;
    heading;
    headingChange;
end
properties (Dependent)
    apparentPositions
end
methods
    function obj = createLandmarks(mapSize, numberOfTrees)
        obj.mapSize = mapSize;
        obj.fixedPositions = obj.mapSize * rand([numberOfTrees, 2]) .* sign(rand([numberOfTrees, 2]) - 0.5);
    end
    function apparent = get.apparentPositions(obj)
        %-STILL ROTATES AROUND ORIGINAL ORIGIN
        currentPosition = [obj.x ; obj.y];
        apparent = bsxfun(@minus,(obj.fixedPositions)',currentPosition)';
        apparent = ([cosd(obj.heading)  -sind(obj.heading) ; sind(obj.heading)  cosd(obj.heading)] * (apparent)')';
    end
    function updatePerspective(obj,tempHeading,tempX,tempY)
        obj.heading = tempHeading;
        obj.x = tempX;
        obj.y = tempY;
    end
end
end

-----------------
classdef robot

properties
    x
    y
    heading
    velocity
end

methods
    function obj = robot(x,y,heading,velocity)
        obj.x = x;
        obj.y = y;
        obj.heading = heading;
        obj.velocity = velocity;
    end
    function updatePosition(obj,headingChange)
        obj.heading = obj.heading + headingChange;
        tempy = cosd(obj.heading);
        tempx = sind(obj.heading);     
        obj.x = obj.x + (tempx*obj.velocity);
        obj.y = obj.y + (tempy*obj.velocity);  
    end
end
end

导航功能是相同的...

我将不胜感激任何关于为什么事情不起作用的帮助。

我所做的就是从注释:%Simulation Loop THIS WAS ORGANIZED 下获取第一部分的代码,并将其分成 2 个函数。一种是机器人,另一种是地标。

每次都会创建一个新实例,因为它不断地在机器人类中打印该行的相同标题 obj.标题 = obj.标题 + 标题更改;

I have 4 files and the code 'works' as expected.

I try to clean everything up, place code into functions, etc... and everything looks fine... and it doesn't work. Can somebody please explain why MatLab is so quirky... or am I just stupid?

Normally, I type

terminator = simulation(100,20,0,0,0,1);
terminator.animate();

and it should produce a map of trees with the terminator walking around in a forest. Everything rotates to his perspective.

When I break it into functions... everything ceases to work.

I really only changed a few lines of code, shown in comments.

Code that works:

classdef simulation
properties
    landmarks
    robot
end

methods
    function obj = simulation(mapSize, trees, x,y,heading,velocity)
        obj.landmarks = landmarks(mapSize, trees);
        obj.robot = robot(x,y,heading,velocity);
    end
    function animate(obj)
        %Setup Plots
        fig=figure;
        xlabel('meters'), ylabel('meters')
        set(fig, 'name', 'Phil''s AWESOME 80''s Robot Simulator')
        xymax = obj.landmarks.mapSize*3;
        xymin = -(obj.landmarks.mapSize*3);
        l=scatter([0],[0],'bo');
        axis([xymin xymax xymin xymax]);
        obj.landmarks.apparentPositions
        %Simulation Loop  THIS WAS ORGANIZED
        for n = 1:720,
            %Calculate and Set Heading/Location
            obj.robot.headingChange = navigate(n);

            %Update Position
            obj.robot.heading = obj.robot.heading + obj.robot.headingChange;
            obj.landmarks.heading = obj.robot.heading;
            y = cosd(obj.robot.heading);
            x = sind(obj.robot.heading);     
            obj.robot.x = obj.robot.x + (x*obj.robot.velocity);
            obj.robot.y = obj.robot.y + (y*obj.robot.velocity);
            obj.landmarks.x = obj.robot.x;
            obj.landmarks.y = obj.robot.y;

            %Animate
            set(l,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
            rectangle('Position',[-2,-2,4,4]);
            drawnow
        end
    end
end
end

-----------
classdef landmarks
properties
    fixedPositions  %# positions in a fixed coordinate system. [ x, y ]
    mapSize = 10;  %Map Size.  Value is side of square
    x=0;
    y=0;
    heading=0;
    headingChange=0;
end
properties (Dependent)
    apparentPositions
end
methods
    function obj = landmarks(mapSize, numberOfTrees)
        obj.mapSize = mapSize;
        obj.fixedPositions = obj.mapSize * rand([numberOfTrees, 2]) .* sign(rand([numberOfTrees, 2]) - 0.5);
    end
    function apparent = get.apparentPositions(obj)
        %-STILL ROTATES AROUND ORIGINAL ORIGIN
        currentPosition = [obj.x ; obj.y];
        apparent = bsxfun(@minus,(obj.fixedPositions)',currentPosition)';
        apparent = ([cosd(obj.heading)  -sind(obj.heading) ; sind(obj.heading)  cosd(obj.heading)] * (apparent)')';
    end
end
end

----------
classdef robot

properties
    x
    y
    heading
    velocity
    headingChange
end

methods
    function obj = robot(x,y,heading,velocity)
        obj.x = x;
        obj.y = y;
        obj.heading = heading;
        obj.velocity = velocity;
    end
end
end

----------
function headingChange = navigate(n)
%steeringChange = 5 * rand(1) * sign(rand(1) - 0.5); Most chaotic shit
%Draw an S
if n <270
    headingChange=1;
elseif n<540
    headingChange=-1;
elseif n<720
    headingChange=1;
else
    headingChange=1;
end
end

Code that does not work...

classdef simulation
properties
    landmarks
    robot
end

methods
    function obj = simulation(mapSize, trees, x,y,heading,velocity)
        obj.landmarks = landmarks(mapSize, trees);
        obj.robot = robot(x,y,heading,velocity);
    end
    function animate(obj)
        %Setup Plots
        fig=figure;
        xlabel('meters'), ylabel('meters')
        set(fig, 'name', 'Phil''s AWESOME 80''s Robot Simulator')
        xymax = obj.landmarks.mapSize*3;
        xymin = -(obj.landmarks.mapSize*3);
        l=scatter([0],[0],'bo');
        axis([xymin xymax xymin xymax]);
        obj.landmarks.apparentPositions
        %Simulation Loop
        for n = 1:720,
            %Calculate and Set Heading/Location

            %Update Position
            headingChange = navigate(n); 
            obj.robot.updatePosition(headingChange); 
            obj.landmarks.updatePerspective(obj.robot.heading, obj.robot.x, obj.robot.y);

            %Animate
            set(l,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
            rectangle('Position',[-2,-2,4,4]);
            drawnow
        end
    end
end
end

-----------------
classdef landmarks
properties
    fixedPositions;  %# positions in a fixed coordinate system. [ x, y ]
    mapSize;  %Map Size.  Value is side of square
    x;
    y;
    heading;
    headingChange;
end
properties (Dependent)
    apparentPositions
end
methods
    function obj = createLandmarks(mapSize, numberOfTrees)
        obj.mapSize = mapSize;
        obj.fixedPositions = obj.mapSize * rand([numberOfTrees, 2]) .* sign(rand([numberOfTrees, 2]) - 0.5);
    end
    function apparent = get.apparentPositions(obj)
        %-STILL ROTATES AROUND ORIGINAL ORIGIN
        currentPosition = [obj.x ; obj.y];
        apparent = bsxfun(@minus,(obj.fixedPositions)',currentPosition)';
        apparent = ([cosd(obj.heading)  -sind(obj.heading) ; sind(obj.heading)  cosd(obj.heading)] * (apparent)')';
    end
    function updatePerspective(obj,tempHeading,tempX,tempY)
        obj.heading = tempHeading;
        obj.x = tempX;
        obj.y = tempY;
    end
end
end

-----------------
classdef robot

properties
    x
    y
    heading
    velocity
end

methods
    function obj = robot(x,y,heading,velocity)
        obj.x = x;
        obj.y = y;
        obj.heading = heading;
        obj.velocity = velocity;
    end
    function updatePosition(obj,headingChange)
        obj.heading = obj.heading + headingChange;
        tempy = cosd(obj.heading);
        tempx = sind(obj.heading);     
        obj.x = obj.x + (tempx*obj.velocity);
        obj.y = obj.y + (tempy*obj.velocity);  
    end
end
end

The navigate function is the same...

I would appreciate any help as to why things aren't working.

All I did was take the code from the first section from under comment: %Simulation Loop THIS WAS ORGANIZED and break it into 2 functions. One in robot and one in landmarks.

Is a new instance created every time because it's constantly printing the same heading for this line int he robot class
obj.heading = obj.heading + headingChange;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-09-08 02:37:55

将您的定义替换为:

classdef landmarks <handle 
classdef robots <handle

然后查看: http:// /www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html

Replace your definitions with:

classdef landmarks <handle 
classdef robots <handle

Then have a look at: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html

故乡的云 2024-09-08 02:37:55

我会给你一个解释。

默认情况下,当您调用 MATLAB 对象上的方法时,框架会创建该对象的副本并调用该副本上的方法。这是与 C++ 完全不同的范例。在 C++ 中,当您调用对象的方法时,您是在对该对象实例的引用上调用该方法。

因此,当您调用

obj.robot.updatePosition(headingChange);

It 时,它会创建机器人对象的新副本,并在此副本上调用 updatePosition。原始机器人的状态保持不变。

使用句柄的另一种方法是编写如下代码:

obj.robot = obj.robot.updatePosition(headingChange);

另一种方法是使 che 类继承自句柄。在这种情况下,每个复制操作都会复制对类的引用,而不是实例,这就是更新函数更改实际对象状态的原因。

I'll give you an explanation.

By default, when you call a method on a MATLAB object, the framework creates a copy of the object and calls the method on this copy. This is a completely different paradigm to C++. In C++, when you call an object's method, you are calling the method on a reference to that object instance.

So, when you call

obj.robot.updatePosition(headingChange);

It is creating a new copy of the robot object, and calling updatePosition on this copy. The state of the original robot remains unchanged.

An alternative to using handles is to code like this:

obj.robot = obj.robot.updatePosition(headingChange);

The other approach is to make che class inherit from handle. In this case, every copy operation copies a reference to the class, not an instance, which is why the update functions change the actual objects state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文