Project

General

Profile

Statistics
| Revision:

root / branches / 16299_s10 / matlab / evolveRobots.m @ 1818

History | View | Annotate | Download (4.58 KB)

1
% n robots
2
% Robot #1 is the queen
3
n = 13;
4
%for movie output use:
5
 dt = 0.041709;
6
%else:
7
%dt = .01;
8
tf = 5.5 %40;
9

    
10

    
11
%shape numbers:
12
%0 = nothing
13
%1 = circle
14
%2 = square
15
%3 = diamond
16
%4 = arrow
17
%5 = semicircles
18

    
19
numsteps = ceil(tf / dt) + 1;
20

    
21
%%% output options
22
makeMovie = false;
23
showPlots = true;
24
doFeedback = true;
25
shape = 1;
26

    
27
if shape == 1
28
   n = 10%100;
29
elseif shape < 4
30
        n = 9;
31
elseif shape == 4
32
    n = 9;
33
elseif shape == 5
34
    n = 20;
35
end
36

    
37
X = zeros(n,numsteps); %cm
38
Y = zeros(n,numsteps); %cm
39
R = zeros(n,numsteps);
40
Theta = zeros(n,numsteps); %rads
41
Phi = zeros(n,numsteps); %rads
42
V = zeros(n,numsteps); %0-255
43
W = zeros(n,numsteps); %0-255
44
 
45
idx = 2;
46

    
47
%W(1,1) = pi;
48
%X(2,1) = 2.3;
49
%Y(2,1) = 5.7;
50

    
51

    
52
desiredR = zeros(n,1);
53
desiredPhi = zeros(n,1);
54

    
55
if doFeedback == false
56
    % if we aren't doing feedback just pick random speeds for everyone so
57
    % we can see some open loop motion
58
    for i=1:n
59
        X(i,1) = rand*10 - 5;
60
        Y(i,1) = rand*10 - 5;
61
        V(i,:) = rand*10;
62
        W(i,:) = rand*2*pi;
63
    end
64
else
65

    
66
    if shape == 1
67
    %circle
68
    desiredR(:) = 20;
69
    desiredPhi = (0:(n-1))*(2*pi/(n-1));
70
    
71
    elseif shape == 2
72
    %square
73
    desiredR(2*(1:(n-1))+1) = 20;
74
    desiredR(2*(1:(n-1))) = 20*sqrt(2);
75
    desiredPhi = (0:(n-1))*pi/4;
76
    
77
    elseif shape == 3
78
    %diamond
79
    desiredR(2*(1:(n-1))) = 20;
80
    desiredR(2*(1:(n-1))+1) = 20*sqrt(2);
81
    desiredPhi = (0:(n-1))*pi/4;
82
     
83
    elseif shape == 4
84
    %aarow    
85
    desiredR(2:4) = 20;
86
    desiredR(5) = 13;
87
    desiredR(6) = 24;
88
    desiredR(7) = 36;
89
    desiredR(8) = 17;
90
    desiredR(9) = 17;
91
    desiredPhi(2) = 0;
92
    desiredPhi(3) = pi/2;
93
    desiredPhi(4) = 3*pi/2;
94
    desiredPhi(5:7) = pi;
95
    desiredPhi(8) = pi/4;
96
    desiredPhi(9) = 7*pi/4;
97
    
98
    elseif shape == 5
99
    %2 circles
100
    desiredR(1:(n/2)) = 20;
101
    desiredR((n/2)+1:n) = 40;
102
    desiredPhi(1:(n/2)) = (1:(n/2))*(2*pi/(n-1));
103
    desiredPhi((n/2)+1:n) =(1:(n/2))*(2*pi/(n-1))+pi/3;
104
   
105
    end
106
    
107
    
108
    V(1,end/2:3*end/4) = 1;
109
    %changing orientation of the queen
110
    W(1,floor(end/6):floor(end/6)+20) = pi/2;
111
    W(1,floor(end/3):floor(end/3)+20) = -1*pi/2;
112
    % start robots at random spots
113
    %for i=2:n
114
    %     X(i,1) = rand*15 - 7.5;
115
    %     Y(i,1) = rand*15 - 7.5;
116
    %end
117

    
118

    
119
end
120

    
121
% V(2,:) = 5;
122
% V(3,:) = -10;
123
% V(4,:) = 5;
124
% 
125
% W(2,:) = pi;
126
% W(3,:) = 0;
127
% W(4,:) = -pi;
128

    
129

    
130
color = hsv(n);
131

    
132
encoderNoise = .01 * randn(2, n);
133

    
134

    
135
% The things that end in s are the sensor values
136
Xs = zeros(n,numsteps);
137
Ys = zeros(n,numsteps);
138
Thetas = zeros(n,numsteps);
139
Phis = zeros(n,numsteps);
140

    
141
% These are the desired values
142
XD = zeros(n,numsteps);
143
YD = zeros(n,numsteps);
144
ThetaD = zeros(n,numsteps);
145

    
146
% These state variables allow the models to maintain some state across calls
147
motorState = [];
148
sensorState = [];
149

    
150
%Running error count for integral control
151
XYError = zeros(n,numsteps);
152
ThetaError = zeros(n,numsteps);
153

    
154
f = figure;
155
set(f,'NextPlot','replacechildren');
156
winsize = get(f,'Position');
157
winsize(1:2) = [0 0];
158

    
159
mov = moviein(numsteps+1,f,winsize);
160
mov(:,1) = getframe(f,winsize);
161

    
162
% init
163
xoldSensor = 0;
164
yoldSensor = 0;
165
thetaoldSensor = 0;
166

    
167
% Run through each timestep
168
for t = 0:dt:tf
169
    
170
    
171
   
172
    % update the true positions using the motor model 
173
    [X(:,idx), Y(:,idx), Theta(:,idx),  Phi(:,idx), motorState, wheels] = ...
174
        motionModel(V(:,idx-1), W(:,idx-1), X(:,idx-1), Y(:,idx-1), Theta(:,idx-1), dt, motorState, n);
175
    %Phi
176
    
177
    % Update the sensor values using the sensor model
178
    [Xs(:,idx), Ys(:,idx), Thetas(:,idx), Phis(:,idx), sensorState, encoderNoise, xoldSensor, yoldSensor, thetaoldSensor] = ...
179
        sensorModel(X(:,idx), Y(:,idx), Theta(:,idx), Phi(:,idx), sensorState, n, encoderNoise, wheels, xoldSensor, yoldSensor, thetaoldSensor, dt);
180
    %Phis
181

    
182
    % visualize real position
183
    mov = visualizeRobots(f,n,X,Y,Theta,V,idx,color,mov,winsize);
184
        
185
    
186
    if doFeedback == true
187
        [XD(:,idx), YD(:,idx), ThetaD(:,idx)] = desiredPosition(Xs(:,idx),Ys(:,idx),Phis(:,idx),desiredR,desiredPhi);
188
    
189
%     ThetaD(:,idx)
190
    
191
        % don't compute for the queen
192
        [V(2:end,idx),W(2:end,idx)] = computeTrajectories(Xs(2:end,idx),Ys(2:end,idx),Thetas(2:end,idx),XD(2:end,idx),YD(2:end,idx),ThetaD(2:end,idx),XYError(2:end,idx),ThetaError(2:end,idx),V(2:end,idx-1),W(2:end,idx-1));
193
    end
194
    
195
    disp(t);
196
    
197
    %pause %(dt);
198
    
199
    idx = idx + 1;
200
end
201

    
202
if showPlots
203
    figure;
204
    hold on;
205
    title('true X vs. sensed X');
206
    plot(1:idx-1, X(1,:),1:idx-1, Xs(1,:)) 
207
    plot(1:idx-1, X(2,:),1:idx-1, Xs(2,:)) 
208
end
209

    
210
if makeMovie
211
    disp('making movie...');
212
    movie2avi(mov,'movie.avi');
213
    disp('movie.avi created!');
214
end