【玩具】ANU Matlab Toy-Robot Movement Simulation

给朋友做的,当是放松了。。。本人真心不会matlab,下面的代码不代表个人水平,纯属堆砌。

这个程序模拟了一些机器人在一个方形空间中的移动轨迹。

clc;
clear all;
close all;

%--------------------Input parameters------------------
%R=input('Number of Robots: ');
%L=input('Length of the Room (m): ');
%N=input('Number of Steps: ');
%S=input('Maximum Size of each Step (cm):');
%typeN=input('Type of the North Wall (Reflecting(0)/Absorbing(1)): ');
%typeS=input('Type of the South Wall (Reflecting(0)/Absorbing(1)): ');
%typeW=input('Type of the West Wall (Reflecting(0)/Absorbing(1)): ');
%typeE=input('Type of the East Wall (Reflecting(0)/Absorbing(1)): ');

%-----------random----------------
%R = round(unifrnd(3,10,1,1));
%L = round(unifrnd(1,10,1,1));
%N = round(unifrnd(10,100,1,1));
%S = round(unifrnd(10,100,1,1));

%---------pre_assignment------------------
R = 8;
L = 1;
N = 10;
S = 50;
typeN = 0;
typeS = 0;
typeW = 0;
typeE = 0;


%B=zeros(N+1,3);
B=[];
B(1,:)=[0 0 0];
save result B;
M=N;
for i=1:R
    N = M;
    t = 1;
    position=[0 0 0];
while N>0
    B(M-N+1,:)=position;
    save result B;
    step = rand(1,1)*S;
    d = floor(rand(1,1)*4);
    [nextposition,N] = newposition(d,step,position,L,typeN,typeS,typeW,typeE,N);
    position=nextposition;
    N=N-1;
    t=t+1;
   %disp(position);
end
 B(t,:)=position;
 disp(B);
 plot3(B(:,1),B(:,2),B(:,3),'*-','color',[mod(floor((i-1)/4),2),mod(floor((i-1)/2),2),mod((i-1),2)]);
 hold on;
 title('Position of the Robots');
 text(0,0,0,'Origin');
 text(0,50*L,0,'North');
 text(0,-50*L,0,'South');
 text(50*L,0,0,'West');
 text(-50*L,0,0,'East');
 axis([-50*L 50*L -50*L 50*L 0 M]);
 grid on
end
function [ nextposition ,N] = newposition( d,step,position,L,typeN,typeS,typeW,typeE,N )
    %disp(position);
    if d==0
        nextposition = position + [0 step 1];
            if nextposition(2)>50*L
                if typeN==0
                    new=100*L-nextposition(2);
                    nextposition(2)=new;
                else 
                    nextposition(2)=50*L;
                    N=0;
                end
            end
            
    elseif d==1
        nextposition = position + [0 -step 1];
         if nextposition(2)<-50*L
             if typeS==0
                new=-100*L-nextposition(2);
                nextposition(2)=new;
             else
                nextposition(2)=- 50*L;
                N=0;
             end
        end 
    elseif d==2
        nextposition = position + [step 0 1];
        if nextposition(1)>50*L
            if typeE==0
                new=100*L-nextposition(1);
                nextposition(1)=new;  
            else
                nextposition(1)=50*L;
                N=0;
            end
        end
    else
        nextposition = position + [-step 0 1];
         if nextposition(1)<-50*L
             if typeW==0
                new=-100*L-nextposition(1);
                nextposition(1)=new;
             else
                 nextposition(1)=-50*L;
                 N=0;
             end
         end
    end
end

posted @ 2013-05-23 17:48  花考拉  阅读(319)  评论(0编辑  收藏  举报