-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcube_machine_GUI.m
473 lines (440 loc) · 10.4 KB
/
cube_machine_GUI.m
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
% GUI for Magic Cube Machine
% 双臂魔方机器的上位机
% Author: duzhentao
% Date: 2019-05-22
function cube_machine_GUI(~,~)
%% GUI Layout
figure('pos',[500 100 710 210],'menu','none','numbertitle','off','name','Cube Machine - DuZhentao');
%% 机械臂α和机械臂β,基于状态描述
uicontrol('pos',[200 10 80 40],'string','reset','callback',@call_reset,'fontsize',14);
uicontrol('pos',[420 10 80 40],'string','ready','callback',@call_ready,'fontsize',14);
uicontrol('pos',[30 90 40 30],'string','+90','callback',@call_A_P90,'fontsize',12);
uicontrol('pos',[70 90 40 30],'string','none','callback',@call_none,'fontsize',12);
uicontrol('pos',[30 130 40 30],'string','-90','callback',@call_A_N90,'fontsize',12);
uicontrol('pos',[70 130 40 30],'string','0','callback',@call_A_0,'fontsize',12);
uicontrol('pos',[30 170 40 30],'string','松','callback',@call_A_lose,'fontsize',12);
uicontrol('pos',[70 170 40 30],'string','夹','callback',@call_A_catch,'fontsize',12);
uicontrol('pos',[150 90 40 30],'string','+90','callback',@call_B_P90,'fontsize',12);
uicontrol('pos',[190 90 40 30],'string','none','callback',@call_none,'fontsize',12);
uicontrol('pos',[150 130 40 30],'string','-90','callback',@call_B_N90,'fontsize',12);
uicontrol('pos',[190 130 40 30],'string','0','callback',@call_B_0,'fontsize',12);
uicontrol('pos',[150 170 40 30],'string','松','callback',@call_B_lose,'fontsize',12);
uicontrol('pos',[190 170 40 30],'string','夹','callback',@call_B_catch,'fontsize',12);
uicontrol('Style','text','pos',[40 60 60 20],'string','机械臂α','fontsize',12);
uicontrol('Style','text','pos',[160 60 60 20],'string','机械臂β','fontsize',12);
uicontrol('Style','text','pos',[330 30 40 20],'string','串口','fontsize',12);
%% 串口模块
uicontrol('pos',[330 10 20 20],'string','开','callback',@start_serialport,'fontsize',8);
uicontrol('pos',[350 10 20 20],'string','关','callback',@close_serialport,'fontsize',8);
%% x轴和y轴,基于过程描述
uicontrol('pos',[260 170 40 30],'string','X+90','callback',@call_X_P90,'fontsize',11);
uicontrol('pos',[260 130 40 30],'string','X-90','callback',@call_X_N90,'fontsize',11);
uicontrol('pos',[260 90 40 30],'string','X180','callback',@call_X_180,'fontsize',11);
uicontrol('pos',[320 170 40 30],'string','Y+90','callback',@call_Y_P90,'fontsize',11);
uicontrol('pos',[320 130 40 30],'string','Y-90','callback',@call_Y_N90,'fontsize',11);
uicontrol('pos',[320 90 40 30],'string','Y180','callback',@call_Y_180,'fontsize',11);
uicontrol('Style','text','pos',[250 60 60 20],'string','X轴','fontsize',12);
uicontrol('Style','text','pos',[310 60 60 20],'string','Y轴','fontsize',12);
%% 'URFDLB'拧动按钮
uicontrol('pos',[390 170 40 30],'string','U','callback',@call_move_U,'fontsize',12);
uicontrol('pos',[390 130 40 30],'string','U''','callback',@call_move_Ui,'fontsize',12);
uicontrol('pos',[390 90 40 30],'string','U2','callback',@call_move_U2,'fontsize',12);
uicontrol('pos',[440 170 40 30],'string','R','callback',@call_move_R,'fontsize',12);
uicontrol('pos',[440 130 40 30],'string','R''','callback',@call_move_Ri,'fontsize',12);
uicontrol('pos',[440 90 40 30],'string','R2','callback',@call_move_R2,'fontsize',12);
uicontrol('pos',[490 170 40 30],'string','F','callback',@call_move_F,'fontsize',12);
uicontrol('pos',[490 130 40 30],'string','F''','callback',@call_move_Fi,'fontsize',12);
uicontrol('pos',[490 90 40 30],'string','F2','callback',@call_move_F2,'fontsize',12);
uicontrol('pos',[540 170 40 30],'string','D','callback',@call_move_D,'fontsize',12);
uicontrol('pos',[540 130 40 30],'string','D''','callback',@call_move_Di,'fontsize',12);
uicontrol('pos',[540 90 40 30],'string','D2','callback',@call_move_D2,'fontsize',12);
uicontrol('pos',[590 170 40 30],'string','L','callback',@call_move_L,'fontsize',12);
uicontrol('pos',[590 130 40 30],'string','L''','callback',@call_move_Li,'fontsize',12);
uicontrol('pos',[590 90 40 30],'string','L2','callback',@call_move_L2,'fontsize',12);
uicontrol('pos',[640 170 40 30],'string','B','callback',@call_move_B,'fontsize',12);
uicontrol('pos',[640 130 40 30],'string','B''','callback',@call_move_Bi,'fontsize',12);
uicontrol('pos',[640 90 40 30],'string','B2','callback',@call_move_B2,'fontsize',12);
uicontrol('Style','text','pos',[455 60 100 20],'string','URFDLB','fontsize',12);
uicontrol('Style','checkbox','pos',[555 60 120 20],'string','Capture','callback',@call_checkbox,'fontsize',12);
%% Serial Port
global scom_flag;
global scom;
global port;
scom_flag = false;
% 打开串口
function start_serialport(~,~)
serialinfo = instrhwinfo('serial');
port = serialinfo.SerialPorts;
if true == isempty(port)
msgbox('Serial Port is not found.');
return;
else
scom = serial(port,'BaudRate',9600);
scom.BytesAvailableFcnMode = 'byte';
scom.Timeout = 1;
scom.TimerPeriod = 2;
%scom.TimerFcn = @receive_data;
end
try
fopen(scom);
if true == strcmp(scom.Status,'open')
msgbox(strcat(port,' is opened.'));
scom_flag = true;
end
catch
msgbox('Can not open Serial Port.');
s = instrfind;
fclose(s);
delete(s);
end
end
% 关闭串口
function close_serialport(~,~)
if false == scom_flag
return;
else
fclose(scom);
delete(scom);
msgbox(strcat(port,' is closed.'));
end
end
end
% 接收串口数据
function [data] = receive_data(~,~)
global scom;
data = fread(scom,10,'uint8');
disp(data);
warning off;
end
% call_checkbox
function call_checkbox(hObject,~)
global value_checkbox
value_checkbox = get(hObject,'value');
end
%% 机械指令
% call_A_P90
function call_A_P90(~,~)
global scom;
fwrite(scom,'01001111');
fwrite(scom,13);
fwrite(scom,10);
end
% call_A_N90
function call_A_N90(~,~)
global scom;
fwrite(scom,'01111100');
fwrite(scom,13);
fwrite(scom,10);
end
% call_A_0
function call_A_0(~,~)
global scom;
fwrite(scom,'01110011');
fwrite(scom,13);
fwrite(scom,10);
end
% call_B_P90
function call_B_P90(~,~)
global scom;
fwrite(scom,'10001111');
fwrite(scom,13);
fwrite(scom,10);
end
% call_B_N90
function call_B_N90(~,~)
global scom;
fwrite(scom,'10111100');
% 13,10 为换行回车符
fwrite(scom,13);
fwrite(scom,10);
end
% call_B_0
function call_B_0(~,~)
global scom;
fwrite(scom,'10110011');
fwrite(scom,13);
fwrite(scom,10);
end
% call_A_catch
function call_A_catch(~,~)
global scom;
fwrite(scom,'01001100');
fwrite(scom,13);
fwrite(scom,10);
end
% call_A_lose
function call_A_lose(~,~)
global scom;
fwrite(scom,'01100001');
fwrite(scom,13);
fwrite(scom,10);
end
% call_B_catch
function call_B_catch(~,~)
global scom;
fwrite(scom,'10001100');
fwrite(scom,13);
fwrite(scom,10);
end
% call_B_lose
function call_B_lose(~,~)
global scom;
fwrite(scom,'10100001');
fwrite(scom,13);
fwrite(scom,10);
end
% call reset
function call_reset(~,~)
call_A_lose;
pause(0.1);
call_B_lose;
pause(0.1);
call_A_0;
pause(0.1);
call_B_0;
end
% call ready
function call_ready(~,~)
call_A_catch;
pause(0.1);
call_B_catch;
pause(0.1);
call_A_0;
pause(0.1);
call_B_0;
end
% call_X_P90
function call_X_P90(~,~)
call_B_lose;
pause(0.2);
call_A_P90;
pause(0.2);
call_B_catch;
pause(0.2);
call_A_lose;
pause(0.2);
call_A_0;
pause(0.2);
call_A_catch;
pause(0.2);
end
% call_X_N90
function call_X_N90(~,~)
call_X_P90;
call_X_P90;
call_X_P90;
end
% call_X_180
function call_X_180(~,~)
call_X_P90;
pause(0.2);
call_X_P90;
pause(0.2);
end
% call_Y_P90
function call_Y_P90(~,~)
call_A_lose;
pause(0.2);
call_B_P90;
pause(0.2);
call_A_catch;
pause(0.2);
call_B_lose;
pause(0.1);
call_B_0;
pause(0.3);
call_B_catch;
pause(0.2);
end
% call_Y_N90
function call_Y_N90(~,~)
call_A_lose;
pause(0.2);
call_B_N90;
pause(0.2);
call_A_catch;
pause(0.2);
call_B_lose;
pause(0.2);
call_B_0;
pause(0.2);
call_B_catch;
pause(0.2);
end
% call_Y_180
function call_Y_180(~,~)
call_Y_P90;
pause(0.2);
call_Y_P90;
pause(0.2);
end
% call_move_D
function call_move_D(~,~)
global value_checkbox
if 0 == value_checkbox
call_B_P90;
pause(0.4);
call_B_lose;
pause(0.4);
call_B_0;
pause(0.2);
call_B_catch;
pause(0.1);
else
call_Y_N90;
call_X_P90;
call_Y_P90;
end
end
% call_move_Di
function call_move_Di(~,~)
call_B_N90;
pause(0.4);
call_B_lose;
pause(0.2);
call_B_0;
pause(0.2);
call_B_catch;
pause(0.1);
end
% call_move_D2
function call_move_D2(~,~)
call_move_D;
call_move_D;
end
% call_move_U
function call_move_U(~,~)
global value_checkbox
if 0 == value_checkbox
call_X_180;
call_move_D;
call_X_180;
else
call_Y_P90;
call_X_P90;
call_Y_P90;
end
end
% call_move_Ui
function call_move_Ui(~,~)
call_X_180;
call_move_Di;
call_X_180;
end
% call_move_U2
function call_move_U2(~,~)
call_X_180;
call_move_D2;
call_X_180;
end
% call_move_R
function call_move_R(~,~)
global value_checkbox
if 0 == value_checkbox
call_X_P90;
call_move_D;
call_X_180;
call_X_P90;
else
call_X_N90;
call_Y_N90;
end
end
% call_move_Ri
function call_move_Ri(~,~)
call_X_P90;
call_move_Di;
call_X_180;
call_X_P90;
end
% call_move_R2
function call_move_R2(~,~)
call_X_P90;
call_move_D2;
call_X_180;
call_X_P90;
end
% call_move_L
function call_move_L(~,~)
global value_checkbox
if 0 == value_checkbox
call_X_180;
call_X_P90;
call_move_D;
call_X_P90;
else
call_Y_P90;
call_X_P90;
call_Y_N90;
call_X_P90;
end
end
% call_move_Li
function call_move_Li(~,~)
call_X_180;
call_X_P90;
call_move_Di;
call_X_P90;
end
% call_move_L2
function call_move_L2(~,~)
call_X_180;
call_X_P90;
call_move_D2;
call_X_P90;
end
% call_move_F
function call_move_F(~,~)
global value_checkbox
if 0 == value_checkbox
call_A_P90;
pause(0.4);
call_A_lose;
pause(0.2);
call_A_0;
pause(0.2);
call_A_catch;
pause(0.2);
else
call_X_P90;
end
end
% call_move_Fi
function call_move_Fi(~,~)
call_A_N90;
pause(0.4);
call_A_lose;
pause(0.2);
call_A_0;
pause(0.2);
call_A_catch;
pause(0.1);
end
% call_move_F2
function call_move_F2(~,~)
call_move_F;
call_move_F;
end
% call_move_B
function call_move_B(~,~)
global value_checkbox
if 0 == value_checkbox
call_Y_180;
call_move_F;
call_Y_180;
else
call_X_P90;
end
end
% call_move_Bi
function call_move_Bi(~,~)
call_Y_180;
call_move_Fi;
call_Y_180;
end
% call_move_B2
function call_move_B2(~,~)
call_Y_180;
call_move_F2;
call_Y_180;
end