【发布时间】:2014-01-28 04:30:01
【问题描述】:
我必须为机器人编写代码以拾取类型 1 或类型 2 的螺栓,然后将它们放入对应于类型 1 或类型 2 的孔中。
我遇到的问题是 Excel 电子表格中的数据设置方式无法更改它的顺序。例如,孔类型向量将有 1 1 1 2 2 2,但螺栓类型向量将是 1 2 1 2 1 2。问题是如果孔类型为 1,螺栓类型为 2,则程序结束并不会将螺栓放入孔中。我需要帮助编写它,所以即使螺栓类型为 1,孔类型为 2,它仍会将其放置在孔 1 中。
clc
clear variables
close all
Data=xlsread('Sample_Data'); %read in data from excel spread sheet
[n_rows,n_cols]=size(Data); %obtains the number of rows and columns from data
file_no=initialize('Robot_Project.txt');
x_hole=Data(1:6,1); % creates vector for hole x_coordinates
y_hole=Data(1:6,2); % creates vector for hole y_coordinates
bolt_type=Data(7:12,3); % creates vector seperating bolts into type 1 or 2
hole_type=Data(1:6,3); % creates vector seperating holes into type 1 or 2
for i=1:n_rows/2
if bolt_type(i) == 1 && hole_type(i) == 1;
pickup(file_no) % pickup function
moveto(file_no,x_hole(i),y_hole(i)) %moves
putdown(file_no) % putdown function
elseif bolt_type(i) == 2 && hole_type(i) == 1;
pickup(file_no)
moveto(file_no,x_hole(i),y_hole(i))
putdown(file_no)
end
end
【问题讨论】:
-
当然这只是您的
if声明的另一个扩展,就像您的elseif bolt_type(i) == 2 && hole_type(i) == 1一样?也就是bolt 1有type 2,bolt 2有type 2的情况加行?
标签: matlab if-statement for-loop