2010-05-07 62 views
2

我有4個文件,並且代碼'按預期工作'。爲什麼每次都重新初始化這個類?

我嘗試清理所有東西,將代碼放入函數等等,並且一切看起來都很好...並且它不起作用。有人可以解釋爲什麼MatLab是如此古怪......或者我只是愚蠢?

通常情況下,I型

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個函數。一個在機器人和一個在地標。

是每次創建的新實例,因爲它不斷爲此行打印相同的標題int機器人類 obj.heading = obj.heading + headingChange;

回答

1

替換爲您的定義:

classdef landmarks <handle 
classdef robots <handle 

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

+0

製造這兩個簡單的變化和現在:兩個輸入數組的 非單尺寸必須在==>的地標>在21 表觀= bsxfun(@minus,(landmarks.get.apparentPositions物鏡彼此 錯誤匹配.fixedPositions) 'currentPosition)'; Matlab似乎來自C++的古怪奇特。 – 2010-05-07 21:40:05

+0

我不知道爲什麼事情開始奏效,他們做到了。典型的Matlab ...或者我。 – 2010-05-07 22:28:53

1

我給你一個解釋。

默認情況下,當您在MATLAB對象上調用方法時,框架會創建對象的副本並調用此副本上的方法。這是一個完全不同於C++的範例。在C++中,當您調用對象的方法時,您正在調用該方法對該對象實例的引用。

所以,當你調用

​​

它創建機器人對象的新副本,並在此副本調用updatePosition。原始機器人的狀態保持不變。

替代使用手柄是這樣的代碼:

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

另一種方法是使從手柄澈類繼承。在這種情況下,每個複製操作都會複製對類的引用,而不是實例,這就是更新函數更改實際對象狀態的原因。