Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.57 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 = 4;
26

    
27
if shape == 1
28
    n = 65;
29
elseif shape < 4
30
    n = 65;
31
elseif shape == 4
32
    n = 65;
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
    desiredPhi = (0:(n-1))*(2*pi/(n-1))+pi/2;
74
    for index=2:(n-1)/4
75
        desiredR(index) = 20/cos(abs(desiredPhi(index)-3*pi/4));
76
    end
77
    for index=1+(n-1)/4:(n-1)/2
78
        desiredR(index) = 20/sin(abs(desiredPhi(index)-3*pi/4));
79
    end
80
    for index=1+(n-1)/2:3*(n-1)/4
81
        desiredR(index) = 20/sin(abs(desiredPhi(index)+3*pi/4));
82
    end
83
    for index=1+3*(n-1)/4:n-1
84
        desiredR(index) = abs(20/sin(abs(desiredPhi(index)-3*pi/4)));
85
    end
86
    desiredR(n) = 20*sqrt(2);
87
    desiredPhi = desiredPhi-pi/4;
88
    
89
    elseif shape == 3
90
    %diamond
91
    desiredPhi = (0:(n-1))*(2*pi/(n-1))+pi/2;
92
    for index=2:(n-1)/4
93
        desiredR(index) = 20/cos(abs(desiredPhi(index)-3*pi/4));
94
    end
95
    for index=1+(n-1)/4:(n-1)/2
96
        desiredR(index) = 20/sin(abs(desiredPhi(index)-3*pi/4));
97
    end
98
    for index=1+(n-1)/2:3*(n-1)/4
99
        desiredR(index) = 20/sin(abs(desiredPhi(index)+3*pi/4));
100
    end
101
    for index=1+3*(n-1)/4:n-1
102
        desiredR(index) = abs(20/sin(abs(desiredPhi(index)-3*pi/4)));
103
    end
104
    desiredR(n) = 20*sqrt(2);
105
     
106
    elseif shape == 4
107
    %arrow
108
    desiredPhi(2:(n-1)/4) = pi;
109
    desiredPhi(1+(n-1)/4:(n-1)/2) = 0;
110
    
111
    for index=1+(n-1)/2:n
112
        desiredPhi(index) = index*(2*pi/(n-1))+pi/2;
113
    end
114
    for index=2:(n-1)/4
115
        desiredR(index) = index*80*sqrt(2)/(n-1);
116
        desiredR(index+(n-1)/4) = desiredR(index);
117
    end
118
    for index=1+(n-1)/2:3*(n-1)/4
119
        desiredR(index) = 20/sin(abs(desiredPhi(index)+3*pi/4));
120
    end
121
    for index=1+3*(n-1)/4:n-1
122
        desiredR(index) = abs(20/sin(abs(desiredPhi(index)-3*pi/4)));
123
    end
124
    
125
    elseif shape == 5
126
    %2 circles
127
    desiredR(1:(n/2)) = 20;
128
    desiredR((n/2)+1:n) = 40;
129
    desiredPhi(1:(n/2)) = (1:(n/2))*(2*pi/(n-1));
130
    desiredPhi((n/2)+1:n) =(1:(n/2))*(2*pi/(n-1))+pi/3;
131
   
132
    end
133
    
134
    
135
    V(1,end/2:3*end/4) = 1;
136
    %changing orientation of the queen
137
    W(1,floor(end/6):floor(end/6)+20) = pi/2;
138
    W(1,floor(end/3):floor(end/3)+20) = -1*pi/2;
139
    % start robots at random spots
140
    %for i=2:n
141
    %     X(i,1) = rand*15 - 7.5;
142
    %     Y(i,1) = rand*15 - 7.5;
143
    %end
144

    
145

    
146
end
147

    
148
% V(2,:) = 5;
149
% V(3,:) = -10;
150
% V(4,:) = 5;
151
% 
152
% W(2,:) = pi;
153
% W(3,:) = 0;
154
% W(4,:) = -pi;
155

    
156

    
157
color = hsv(n);
158

    
159
encoderNoise = .01 * randn(2, n);
160

    
161

    
162
% The things that end in s are the sensor values
163
Xs = zeros(n,numsteps);
164
Ys = zeros(n,numsteps);
165
Thetas = zeros(n,numsteps);
166
Phis = zeros(n,numsteps);
167

    
168
% These are the desired values
169
XD = zeros(n,numsteps);
170
YD = zeros(n,numsteps);
171
ThetaD = zeros(n,numsteps);
172

    
173
% These state variables allow the models to maintain some state across calls
174
motorState = [];
175
sensorState = [];
176

    
177
%Running error count for integral control
178
XYError = zeros(n,numsteps);
179
ThetaError = zeros(n,numsteps);
180

    
181
f = figure;
182
set(f,'NextPlot','replacechildren');
183
winsize = get(f,'Position');
184
winsize(1:2) = [0 0];
185

    
186
mov = moviein(numsteps+1,f,winsize);
187
mov(:,1) = getframe(f,winsize);
188

    
189
% init
190
xoldSensor = 0;
191
yoldSensor = 0;
192
thetaoldSensor = 0;
193

    
194
% Run through each timestep
195
for t = 0:dt:tf
196
    
197
    
198
   
199
    % update the true positions using the motor model 
200
    [X(:,idx), Y(:,idx), Theta(:,idx),  Phi(:,idx), motorState, wheels] = ...
201
        motionModel(V(:,idx-1), W(:,idx-1), X(:,idx-1), Y(:,idx-1), Theta(:,idx-1), dt, motorState, n);
202
    %Phi
203
    
204
    % Update the sensor values using the sensor model
205
    [Xs(:,idx), Ys(:,idx), Thetas(:,idx), Phis(:,idx), sensorState, encoderNoise, xoldSensor, yoldSensor, thetaoldSensor] = ...
206
        sensorModel(X(:,idx), Y(:,idx), Theta(:,idx), Phi(:,idx), sensorState, n, encoderNoise, wheels, xoldSensor, yoldSensor, thetaoldSensor, dt);
207
    %Phis
208

    
209
    % visualize real position
210
    mov = visualizeRobots(f,n,X,Y,Theta,V,idx,color,mov,winsize);
211
        
212
    
213
    if doFeedback == true
214
        [XD(:,idx), YD(:,idx), ThetaD(:,idx)] = desiredPosition(Xs(:,idx),Ys(:,idx),Phis(:,idx),desiredR,desiredPhi);
215
    
216
%     ThetaD(:,idx)
217
    
218
        % don't compute for the queen
219
        [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));
220
    end
221
    
222
    disp(t);
223
    
224
    %pause %(dt);
225
    
226
    idx = idx + 1;
227
end
228

    
229
if showPlots
230
    figure;
231
    hold on;
232
    title('true X vs. sensed X');
233
    plot(1:idx-1, X(1,:),1:idx-1, Xs(1,:)) 
234
    plot(1:idx-1, X(2,:),1:idx-1, Xs(2,:)) 
235
end
236

    
237
if makeMovie
238
    disp('making movie...');
239
    movie2avi(mov,'movie.avi');
240
    disp('movie.avi created!');
241
end