forked from wangting0310njtech/Multi-robot-SLAM-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimviewcb.m
More file actions
626 lines (556 loc) · 23.4 KB
/
simviewcb.m
File metadata and controls
626 lines (556 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
function simviewcb(action)
% Path - in SimPath
% ListStore = for list
% ActualStep - for future enhancements
% SimWindow(UserData) - 1=running, 0=not running
nameSim = 'MRSim - Multi-Robot Simulator v1.0';
switch action
case 'initialize'
case 'help'
open('help/helpMRSim.htm');
case 'about'
figAbout = figure;
set(figAbout,'MenuBar','none',...
'NumberTitle','off',...
'Name',['About ' nameSim],...
'RendererMode','manual',...
'Renderer','Painters',...
'Backingstore','off',...
'Position',[450 300 580 400],...
'Color',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'Tag','SimWindow','units','normalized',...
'Resize','off');
uicontrol('Parent',figAbout, ...
'Units','points', ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'FontWeight','bold',...
'HorizontalAlignment','left', ...
'Position',[10 260 250 15], ...
'ListboxTop',0, ...
'FontSize',12, ...
'String',nameSim, ...
'Style','text');
uicontrol('Parent',figAbout, ...
'Units','points', ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'HorizontalAlignment','left', ...
'Position',[10 240 400 15], ...
'ListboxTop',0, ...
'FontSize',10, ...
'String','Institute of Systems and Robotics, Mobile Robotics Laboratory', ...
'Style','text');
uicontrol('Parent',figAbout, ...
'Units','points', ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'HorizontalAlignment','left', ...
'Position',[10 225 400 15], ...
'ListboxTop',0, ...
'FontSize',10, ...
'String','Faculty of Sciences and Technology, University of Coimbra', ...
'Style','text');
uicontrol('Parent',figAbout, ...
'Units','points', ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'HorizontalAlignment','center', ...
'Position',[10 -50 420 100], ...
'ListboxTop',0, ...
'FontSize',7, ...
'String','© Copyright by Micael S. Couceiro, micaelcouceiro@isr.uc.pt, 2012. All Rights Reserved.', ...
'Style','text');
uicontrol('Parent',figAbout, ...
'Units','points', ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'HorizontalAlignment','left', ...
'Position',[10 -70 420 100], ...
'ListboxTop',0, ...
'FontSize',7, ...
'String','Extended from the Autonomous Mobile Robotics Toolbox SIMROBOT (SIMulated ROBOTs) developed at the Department of Control, Measurement and Instrumentation, Faculty of Electrical Engineering and Computer Science, Brno University of Technology, Czech Republic, 2001.', ...
'Style','text');
axHndl=gca;
set(axHndl, ...
'Position',[0.22 0.12 0.6 0.6], ...
'XDir','normal','YDir','normal', ...
'Drawmode','fast', ...
'Visible','on', ...
'Tag','Axes',...
'TickLength',[0 0],...
'XColor',[0.752941176470588 0.752941176470588 0.752941176470588],...
'YColor',[0.752941176470588 0.752941176470588 0.752941176470588]);
hold on;
A=imread('logo.bmp');
imshow(A);
drawnow;
case 'sim'
h = findobj('Tag','RunType');
set(h,'UserData','simulation')
h = findobj('Tag','SimWindow');
set(h,'UserData',1)
h = findobj('Tag','ActualStep'); % Must be here (due to continue option}
set(h,'UserData',0);
simviewcb run;
case 'rep'
h = findobj('Tag','RunType');
set(h,'UserData','replay');
h = findobj('Tag','SimWindow');
set(h,'UserData',1)
h = findobj('Tag','ActualStep'); % Must be here (continue option}
set(h,'UserData',0);
simviewcb run;
case 'run'
% ****** Menus ******
h = findobj('Type','uimenu');
set(h,'Enable','off')
h = findobj('Tag','WinRunMenu');
set(h,'Enable','on')
h = findobj('Tag','StopContMenu');
set(h,'Enable','on')
% *******************
h = findobj('Tag','ActualStep');
step = get(h,'UserData');
h = findobj('Tag','SimPath');
path = get(h,'UserData');
loadstr = load(path);
matrix = loadstr.matrix;
h = findobj('Tag','RunType');
type = get(h,'UserData');
h = findobj('Tag','StepsMenu');
no_steps = get(h,'UserData');
no_steps = no_steps{1};
h = findobj('Tag','DelayMenu');
delay = get(h,'UserData');
delay = delay{1};
h = findobj('Tag','ListStore');
list = get(h,'UserData');
% ********* Reset robots **********
for j = 1:length(list)
history = gethist(list(j));
deleteR(list(j))
list(j) = sethead(list(j),history(1,3));
list(j) = putrob(list(j),history(1,1:2),matrix);
list(j) = addinfo(list(j)); % Add context menu
list(j) = clearcf(list(j));
list(j) = setpower(list(j),1);
end
% *********************************
h = findobj('Tag','StepText');
switch type
case 'simulation'
h = findobj('Tag','Track');
if ~isempty(h)
delete(h);
drawnow;
end
for j = 1:length(list)
hist = gethist(list(j)); % Store first history step
list(j) = delhist(list(j));
list(j) = sethist(list(j),hist(1,:));
% We need to clear the buffer of the robots
list(j) = cleardata(list(j));
end
h = findobj('Tag','SimWindow');
title = get(h,'Name');
title = strrep(title,'(waiting ...)','(simulating ...)');
set(h,'Name',title)
clear functions; % We need to clear compiled algorithms
figHndl = findobj('Tag','SimWindow');
rand('seed',cputime); % random seed - this is necessary for stochastic algorithms
while (get(figHndl,'UserData')==1) && (step<1200)
step = step + 1;
h = findobj('Tag','StepText');
set(h,'String',int2str(step))
[list,matrix]=runR(list,matrix,step);
drawnow;
if (delay>0.1)
pause(delay);
end
h = findobj('Tag','StepsMenu');
no_steps = get(h,'UserData');
no_steps = no_steps{1};
end
h = findobj('Tag','ActualStep');
set(h,'UserData',step);
% This is here for the case of Steps limit condition = true
% (simviewcb stop is not executed in that case)
% ****** Menus ******
h = findobj('Type','uimenu');
set(h,'Enable','on')
h = findobj('Tag','StopContMenu');
set(h,'Enable','off')
h = findobj('Tag','SimWindow');
title = get(h,'Name');
if findstr(title,'simulating') % This assures unexpected states handling
title = strrep(title,'(simulating ...)','(waiting ...)');
else
title = strrep(title,'(replaying ...)','(waiting ...)');
end
set(h,'Name',title)
h = findobj('Tag','ListStore');
set(h,'UserData',list)
case 'replay'
h = findobj('Tag','Track');
if ~isempty(h)
delete(h);
drawnow;
end
h = findobj('Tag','RunType');
set(h,'UserData','replay');
h = findobj('Tag','SimWindow');
title = get(h,'Name');
title = strrep(title,'(waiting ...)','(replaying ...)');
set(h,'Name',title)
h = findobj('Tag','StepsMenu');
no_steps = get(h,'UserData');
no_steps = no_steps{1};
h = findobj('Tag','StepText');
figHndl = findobj('Tag','SimWindow');
true_steps = size(gethist(list(1)));
true_steps = true_steps(1);
while (get(figHndl,'UserData')==1) & (step<true_steps-1) & (step<no_steps)
step = step + 1;
set(h,'String',int2str(step))
tic
[list,matrix]=guireplay(list,matrix,step+1);
drawnow;
if (delay>0.1)
pause(delay);
end
end
h = findobj('Tag','ActualStep');
set(h,'UserData',step);
% This is here for the case of Steps limit condition = true
% (simviewcb stop is not executed in that case)
% ****** Menus ******
h = findobj('Type','uimenu');
set(h,'Enable','on')
h = findobj('Tag','StopContMenu');
set(h,'Enable','off')
h = findobj('Tag','SimWindow');
title = get(h,'Name');
if findstr(title,'simulating') % This assures unexpected states handling
title = strrep(title,'(simulating ...)','(waiting ...)');
else
title = strrep(title,'(replaying ...)','(waiting ...)');
end
set(h,'Name',title)
h = findobj('Tag','ListStore');
set(h,'UserData',list)
end
case 'stop'
% ****** Menus ******
h = findobj('Type','uimenu');
set(h,'Enable','on')
h = findobj('Tag','StopContMenu');
set(h,'Enable','off')
h = findobj('Tag','RunMenu');
set(h,'Enable','on')
h = findobj('Tag','ListStore');
list = get(h,'UserData');
s = size(gethist(list(1)));
if s(1) == 1
h = findobj('Tag','RunRepMenu');
set(h,'Enable','off')
end
% *******************
% ******* *******
h = findobj('Tag','RunType');
type = get(h,'UserData');
h = findobj('Tag','SimWindow');
title = get(h,'Name');
if findstr(title,'simulating') % This assures unexpected states handling
title = strrep(title,'(simulating ...)','(waiting ...)');
else
title = strrep(title,'(replaying ...)','(waiting ...)');
end
set(h,'Name',title)
h = findobj('Tag','SimWindow');
set(h,'UserData',0)
case 'tracks'
h = findobj('Tag','ListStore');
list = get(h,'UserData');
axHndl = findobj('Tag','SimAxes');
for j = 1:length(list)
h = line('XData',[],'YData',[],'Tag','Track');
set(h,'Parent',axHndl);
data = gethist(list(j));
xdata = data(:,1);
ydata = data(:,2);
set(h,'Color',getcol(list(j)));
set(h,'XData',xdata,'YData',ydata)
end
case 'save'
% Here could be some actions to be done before saving (incl. saving refusion)
simviewcb savefile
case 'savefile'
h = findobj('Tag','SimPath');
oldpath = get(h,'UserData');
loadstr = load(oldpath);
[path,name,ext] = fileparts(oldpath);
if strcmp(name,'untitled')
name = 'newsave';
end
[filename,pathname] = uiputfile([name ext],'Save replay');
if strcmp(filename,'untitled.mat')
h = msgbox('Error: Cannot save as ''untitled.mat''. Please select another name','Error','error');
return
end
if filename ~= 0 % if any file selected
h = findobj('Tag','StepsMenu');
no_steps = get(h,'UserData');
no_steps = no_steps{1};
path = [pathname filename];
h = findobj('Tag','ListStore');
list = get(h,'UserData'); % Get list
matrix = loadstr.matrix; % We need unmodified matrix
h = findobj('Tag','DelayMenu');
delay = get(h,'UserData'); % Get delay
delay = delay{1};
h = findobj('Tag','ActualStep');
step = get(h,'UserData');
s = size(gethist(list(1)));
if s(1) > 1 % Decide if saving simulation or replay
type = 'replay';
%if step > no_steps
% dispstr = strvcat('Warning!',['Replay contains ' num2str(step) ' steps.']);
% dispstr = strvcat(dispstr,['Steps limit is ', num2str(no_steps)]);
% dispstr = strvcat(dispstr,'Would you like to correct the limit?');
% dispstr = strvcat(dispstr,'Select ''Yes'' to set the limit equal to replay length');
% dispstr = strvcat(dispstr,'Select ''No'' to cut out the replay steps above limit');
% dispstr = strvcat(dispstr,'Select ''Cancel'' to abort saving');
% answer = questdlg(dispstr,'Question');
%end
else
type='simulation';
end
save(path,'list','matrix','no_steps','delay','type');
h = findobj('Tag','SimPath'); % Store the path
set(h,'UserData',path);
h = findobj('Tag','SimWindow');
title = get(h,'Name');
title = strrep(title,oldpath,path);
set(h,'Name',title)
end
% ***********************************************************
case 'load'
[filename,pathname] = uigetfile('*.mat','Open simulation/replay');
%%%%% ete remplacé par
% filename = '10map2RPSO.mat';
% pathname = './';
%%%%%
robotT = simrobot('',2,2,2,'',[1 1 0],1,10,10); % Create object
if filename ~= 0
% ****** Check the file structure ******
vars = who('-file',[pathname filename]);
load([pathname filename],'list')
if isempty(strmatch('list',vars)) | isempty(strmatch('no_steps',vars)) | isempty(strmatch('delay',vars)) | isempty(strmatch('matrix',vars)) | isempty(strmatch('type',vars)) | isempty(list)
dispstr = strvcat(['"' pathname filename '"'],' ');
dispstr = strvcat(dispstr,'Cannot open this file - invalid file structure!');
h = msgbox(dispstr,'Error','error');
return
end
% **************************************
h = findobj('Tag','SimPath');
set(h,'UserData',[pathname filename]);
simviewcb loadfile;
end
case 'loadfile'
% ****** Disable menus ******
h = findobj('Tag','TracksMenu');
set(h,'Enable','off')
h = findobj('Tag','RunRepMenu');
% set(h,'Enable','off')
% ***************************
% ******** New part ********
h = findobj('Tag','SimPath');
path = get(h,'UserData');
[pathname,filename,ext] = fileparts(path);
pathname = [pathname '\'];
filename = [filename ext];
% **************************
load([pathname filename]);
h = findobj('Tag','RunType');
set(h,'UserData',type);
h = findobj('Tag','SimWindow');
title = ['Simulation Window - [' filename ' - ' pathname filename ']' ' (waiting ...)'];
set(h,'Name',title)
% h = findobj('Tag','ListStore');
% list = get(h,'UserData');
%set(h,'UserData',type) % Store type of loaded file
LocalDraw(matrix,2);
for i = 1:length(list)
name = getnameR(list(i));
num = getnum(list(i));
power = getpower(list(i));
af = getaf(list(i));
color = getcol(list(i));
scale = getscale(list(i));
shape = getshape(list(i));
history = gethist(list(i));
head = history(1,3);
memory = readmem(list(i));
list1(i) = simrobot(name,num,head,power,af,color,scale,shape.xdata,shape.ydata);
list1(i) = putrob(list1(i),history(1,1:2),matrix);
list1(i) = addsenss(list1(i),getsens(list(i)));
list1(i) = sethist(list1(i),history);
list1(i) = addinfo(list1(i)); % Add context menu
list1(i) = writemem(list1(i),memory);
end
list = list1;
h = findobj('Tag','StepText');
set(h,'String','Step')
h = findobj('Tag','StepsMenu');
if iscell(no_steps)
no_steps = no_steps{1};
end
set(h,'Label',['Steps limit: ' num2str(no_steps)])
set(h,'Enable','on')
set(h,'UserData',no_steps)
h = findobj('Tag','DelayMenu');
set(h,'Enable','on')
if iscell(delay)
delay = delay{1};
end
set(h,'Label',['Step time: ' num2str(delay)])
set(h,'UserData',delay)
h = findobj('Tag','ListStore');
set(h,'UserData',list) % Store list
h = findobj('Tag','SimMatrix');
set(h,'UserData',matrix) % Store matrix
h = findobj('Tag','StepsMenu');
set(h,'UserData',no_steps)
h = findobj('Tag','DelayMenu');
set(h,'UserData',delay)
h = findobj('Tag','RunMenu');
set(h,'Enable','on')
h = findobj('Tag','WinRunMenu');
set(h,'Enable','on')
h = findobj('Tag','ParMenu');
set(h,'Enable','on')
h = findobj('Tag','ParamsMenu');
set(h,'Enable','on')
h = findobj('Tag','SaveMenu');
set(h,'Enable','on')
h = findobj('Tag','EditMenu');
set(h,'Enable','on')
h = findobj('Tag','TracksMenu');
set(h,'Enable','off')
if strcmp(type,'replay')
h = findobj('Tag','RunRepMenu');
set(h,'Enable','on')
h = findobj('Tag','TracksMenu');
set(h,'Enable','on')
end
% end
% ***********************************************************
case 'editor'
h = findobj('Tag','RunType');
type = get(h,'UserData');
h = findobj('Tag','ListStore');
list = get(h,'UserData');
for j = 1:length(list)
history = gethist(list(j));
list(j) = setpos(list(j),history(1,1:2));
list(j) = sethead(list(j),history(1,3));
list(j) = delhist(list(j));
list(j) = sethist(list(j),history(1,:));
end
h = findobj('Tag','SimPath');
path = get(h,'UserData');
loadstr = load(path);
matrix = loadstr.matrix;
h = findobj('Tag','DelayMenu');
delay = get(h,'UserData'); % Get delay
delay = delay{1};
h = findobj('Tag','StepsMenu');
no_steps = get(h,'UserData');
no_steps = no_steps{1};
type = 'simulation';
save(path,'list','matrix','no_steps','delay','type');
simedit;
close(gcbf);
h = findobj('Tag','EditPath');
set(h,'UserData',path)
simeditcb loadfile;
case 'steps'
h = findobj('Tag','StepsMenu');
% Because there ar two menus - context and toolbar - the cell array is returned
% by get() command, which couldn't be handled by inputdlg
def = get(h,'UserData');
def = def{1};
steps = inputdlg('Steps limit:','Number of steps',[1 14],{num2str(def)});
if ~isempty(steps)
if ~strcmp(steps{1},'Inf')
steps = str2num(steps{1});
if isempty(steps) | steps <= 0 | strcmpi(num2str(steps),'NaN')
h = msgbox('Please enter a valid number','Error','error');
waitfor(h)
simviewcb steps
return
else
steps = round(steps(1));
end
else
steps = Inf;
end
set(h,'UserData',steps)
set(h,'Label',['Steps limit: ' num2str(steps)])
h = findobj('Tag','RunRepMenu');
set(h,'Enable','off')
h = findobj('Tag','TracksMenu');
set(h,'Enable','off')
end
case 'delay'
h = findobj('Tag','DelayMenu');
% Because there ar two menus - context and toolbar - the cell array is returned
% by get() command, which couldn't be handled by inputdlg -> need to be fixed
def = get(h,'UserData');
def = def{1};
steps = inputdlg('Step time (in secs):','Minimal step length',[1 25],{num2str(def)});
if ~isempty(steps)
if ~strcmp(steps{1},'Inf')
steps = strrep(steps{1},',','.');
steps = str2num(steps);
if isempty(steps) | steps < 0 | strcmpi(num2str(steps),'NaN')
h = msgbox('Please enter a valid number','Error','error');
waitfor(h)
simviewcb delay
return
else
steps = steps(1);
end
else
steps = Inf;
end
set(h,'UserData',steps)
set(h,'Label',['Step time: ' num2str(steps)])
end
case 'close'
h = findobj('Tag','SimWindow');
delete(h)
end
%load(filename);
% Variables = matrix, delay, list, no_steps
function LocalDraw(matrix,size_of_marker);
[xmax, ymax] = size(matrix);
[x, y] = find(matrix);
figNumber = findobj('Tag','SimWindow');
figure(figNumber);
axHndl = findobj('Tag','SimAxes');
color = get(figNumber,'Color');
plotHndl = plot(x,y,'s', ...
'Color','black', ...
'MarkerFaceColor','black',...
'Tag','mapplot',...
'MarkerSize',size_of_marker,...
'Parent',axHndl);
axis equal
set(axHndl, ...
'XLim',[0 xmax+1],'YLim',[0 ymax+1], ...
'XDir','normal','YDir','normal', ...
'Drawmode','fast', ...
'Visible','on', ...
'NextPlot','replace', ...
'Tag','SimAxes',...
'TickLength',[0 0],...
'XColor',color,...
'YColor',color);
drawnow;