Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.62 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 = 20 %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 = 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
    %HACK
59
    V(2,:) = 2;
60
    for i=3:n
61
        X(i,1) = rand*10 - 5;
62
        Y(i,1) = rand*10 - 5;
63
        V(i,:) = rand*10;
64
        W(i,:) = rand*2*pi;
65
    end
66
else
67

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

    
147

    
148
end
149

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

    
158

    
159
color = hsv(n);
160

    
161
encoderNoise = 3 * randn(2, n);
162

    
163

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

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

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

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

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

    
188
mov = moviein(numsteps+1,f,winsize);
189
mov(:,1) = getframe(f,winsize);
190

    
191
% init
192
xoldSensor = 0;
193
yoldSensor = 0;
194
thetaoldSensor = 0;
195
noise = 0;
196

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

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

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

    
240
if makeMovie
241
    disp('making movie...');
242
    movie2avi(mov,'movie.avi');
243
    disp('movie.avi created!');
244
end