-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
275 lines (226 loc) · 9.14 KB
/
main.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
% Copyright(c) 2020, Filmon Yacob.
% Distributed under the MIT License
%%% Predicts part quality considering locating surfaces and machining error%%%
clear; clc; close all;
include_namespace_dq
% The pos of 5 locating surfaces and tool deviations
locatingSurfs = getLocatingSurfs();
f = [1:3; 4:6; 7:9; 10:12; 13:15]';
toolDeviations = [0, -0.01, 0.02, -0.03, 0.04];
ParallelismS1s = [];
ParallelismS8s = [];
for e = 1:5 % 5 experiments
disp(strcat('Experiment ', num2str(e)))
% get fixture and workpiece info
fixDatums = locatingSurfs(:, f(:, e)');
v8WorkPiece = getWorkpiece2();
% Run operations
%%
R = zeros(8, 1); R(1) = 1;
v8WorkPiece = operationStn1(v8WorkPiece, fixDatums, R, toolDeviations(e));
v8WorkPiece = operationStn2(v8WorkPiece, fixDatums, R, toolDeviations(e));
% final check primary
%%
tol = 10^-6;
err = getProjectedTestPointsPlucker2(fixDatums(:, 1), [0, 0, 0], fixDatums(2:4, 1)) - ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 3), [0, 0, 0], fixDatums(2:4, 1));
% Move to insection station and conduct measurement
%%
v8WorkPiece = moveToInspectionStation(v8WorkPiece, 3, 7);
S1 = [getProjectedTestPointsPlucker2(v8WorkPiece(:, 2), [0, 0, 120], [0, 0, 1]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 2), [0, 200, 120], [0, 0, 1]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 2), [200, 0, 120], [0, 0, 1]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 2), [200, 200, 120], [0, 0, 1])];
ParallelismS1s = [ParallelismS1s; round(max(S1(:, 3))-min(S1(:, 3)), 3)];
S8_1 = [getProjectedTestPointsPlucker2(v8WorkPiece(:, 9), [40, 160, 100], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 9), [40, 160, 120], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 9), [160, 160, 100], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 9), [160, 160, 120], [0, 1, 0])];
S8_2 = [getProjectedTestPointsPlucker2(v8WorkPiece(:, 5), [40, 160, 100], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 5), [40, 160, 120], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 5), [160, 160, 100], [0, 1, 0]); ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, 5), [160, 160, 120], [0, 1, 0])];
S8_1_2 = abs(S8_1-S8_2);
ParallelismS8s = [ParallelismS8s; round(max(S8_1_2(:, 2))-min(S8_1_2(:, 2)), 3)];
end
%
% All predicted parallelism values
Parallelisms = [ParallelismS1s, ParallelismS8s]
%
%
%%
function v8WorkPiece = operationStn1(v8WorkPiece, fixDatums, RMach, ToolDeviation)
disp('Running Station 1 ...')
include_namespace_dq
% assemble to fixture
stn = 1;
v8WorkPiece = assemblePrimSecTert(v8WorkPiece, fixDatums, stn);
% machining error
s1CuttingPlane = vec8(k_+E_*120);
TMach = vec8(1-E_*0.5*ToolDeviation*k_);
RT = DQmult(TMach, RMach);
% replace with the machined surf
v8WorkPiece(:, 3) = moveFeaturesBy(s1CuttingPlane, RT);
end
%%
function v8WorkPiece = operationStn2(v8WorkPiece, fixDatums, RMach, ToolDeviation)
disp('Running Station 2 ...')
include_namespace_dq
% rotate first
v8WorkPiece = RotatePart180DegXaxis(fixDatums, v8WorkPiece, 2);
v8WorkPiece = flipNormalsWorkpiece(v8WorkPiece);
% Assemble to fixture
stn = 2;
v8WorkPiece = assemblePrimSecTert(v8WorkPiece, fixDatums, stn);
% Nominal cutting planes
s2CuttingPlaneH = vec8(k_+E_*100);
s2CuttingPlaneV1 = vec8(j_+E_*160);
% machining error
TMach = vec8(1-E_*0.5*ToolDeviation*k_);
TMachV = vec8(1-E_*0.5*ToolDeviation*j_);
RT = DQmult(TMach, RMach);
RTV = DQmult(TMachV, RMach);
v8WorkPiece(:, 2) = moveFeaturesBy(s2CuttingPlaneH, RT);
v8WorkPiece(:, 9) = moveFeaturesBy(s2CuttingPlaneV1, RTV);
end
%%
%
function v8WorkPiece = assemblePrimSecTert(v8WorkPiece, fixDatums, stn)
% Assembles to primary, secondary, and tertiary locating surfaces
include_namespace_dq
tol = 10^-6;
% get intersection lines and Direction based on fixture orientation
ULPS = checkAndFlipNormalDir(normalize(cross(fixDatums(2:4, 1), fixDatums(2:4, 2))));
ULPT = checkAndFlipNormalDir(normalize(cross(fixDatums(2:4, 1), fixDatums(2:4, 3))));
assert(ULPS(1) > 0)
assert(ULPT(2) > 0)
% Datum
switch stn
case 1
pDatum = 1;
sDatum = 5;
case 2
pDatum = 3;
sDatum = 7;
end
% Assembly of primary feature
%%
[R, errD] = getErrTransformationParas(fixDatums(:, 1), v8WorkPiece(:, pDatum), [], [], 3);
T = vec8(1-E_*errD/2*fixDatums(2:4, 1));
RT = DQmult(T, R);
v8WorkPiece = moveFeaturesBy(v8WorkPiece, RT);
err = getProjectedTestPointsPlucker2(fixDatums(:, 1), [0, 0, 0], fixDatums(2:4, 1)) - ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, pDatum), [0, 0, 0], fixDatums(2:4, 1));
assert(norm(err) < tol);
disp('Passed primary check')
% Assembly of secondary feature
%%
[pts45T, pts45B] = getPointsOnSecTerDatumShortFix(v8WorkPiece, fixDatums, sDatum, 's'); %Project on Fixture and rotate
[p4Bfix, p5Bfix, p4Tfix, p5Tfix] = projectOntoSecondaryFix(pts45B, pts45T, fixDatums, ULPT);
% Rotate part first to make it parallel with secondary locator
p4Tp5Tvec = pts45T(1, :) - pts45T(2, :); % vector connecting both points
p4Tp5TvecFix = p4Tfix' - p5Tfix';
[R, errD] = getErrTransformationParas(normalize(p4Tp5TvecFix), normalize(p4Tp5Tvec), [], [], 3);
v8WorkPiece = moveFeaturesBy(v8WorkPiece, R);
% check primary again
err = getProjectedTestPointsPlucker2(fixDatums(:, 1), [0, 0, 0], fixDatums(2:4, 1)) - ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, pDatum), [0, 0, 0], fixDatums(2:4, 1));
assert(norm(err) < tol);
% check for equal distance from locators after rotation
%%
[pts45T, pts45B] = getPointsOnSecTerDatumShortFix(v8WorkPiece, fixDatums, sDatum, 's');
[p4Bfix, p5Bfix, p4Tfix, p5Tfix, t4B, t5B, t4T, t5T] = ...
projectOntoSecondaryFix(pts45B, pts45T, fixDatums, ULPT);
errT = [p4Tfix'; p5Tfix'] - pts45T(1:2, :);
errB = [p4Bfix'; p5Bfix'] - pts45B(1:2, :);
% assert(errT(1, 2) - errT(2, 2) < tol); % checks equal distance
% take the shortest of the two pairs
if abs(errT(1, 2)) < abs(errB(2, 2))
err = errT(1, :);
disp('Contact at the top')
else
err = errB(1, :);
disp('Contact at the bottom')
end
T = vec8(1-E_*err/2);
v8WorkPiece = moveFeaturesBy(v8WorkPiece, T);
% check primary
err = getProjectedTestPointsPlucker2(fixDatums(:, 1), [0, 0, 0], fixDatums(2:4, 1)) - ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, pDatum), [0, 0, 0], fixDatums(2:4, 1));
assert(norm(err) < tol);
% check secondary
[pts45T, pts45B] = getPointsOnSecTerDatumShortFix(v8WorkPiece, fixDatums, sDatum, 's');
errT = [p4Tfix'; p5Tfix'] - pts45T;
errB = [p4Bfix'; p5Bfix'] - pts45B;
try
errt = errT(1, :);
assert(norm(errT(1, :)) < tol)
catch
errb = errB(1, :);
assert(norm(errB(1, :)) < tol)
end
disp('Passed secondary check')
% Assembly of tertiary feature
%%
tDatum = 6;
[pts45Tt, pts45Bt] = getPointsOnSecTerDatumShortFix(v8WorkPiece, fixDatums, tDatum, 't');
vTFeature = [pts45Tt; pts45Bt];
R = zeros(8, 1); R(1) = 1;
err = projOnToFixtureGetErr(fixDatums(2:5, 3), vTFeature, ULPS);
T = vec8(1-E_*err*ULPS/2);
v8WorkPiece = moveFeaturesBy(v8WorkPiece, T);
% check contact with primary again
err = getProjectedTestPointsPlucker2(fixDatums(:, 1), [0, 0, 0], fixDatums(2:4, 1)) - ...
getProjectedTestPointsPlucker2(v8WorkPiece(:, pDatum), [0, 0, 0], fixDatums(2:4, 1));
assert(norm(err) < tol);
%
disp('Passed tertiary check')
end
%%
function err = projOnToFixtureGetErr(fixPlane, vFeature, dir)
iPts = [];
dists = [];
for v = 1:size(vFeature, 1)
L = Plucker.pointdir(vFeature(v, :), dir);
[p, t] = L.intersect_plane2(fixPlane); % intersection to Fix
if ~isempty(p)
dist = norm([p' - vFeature(v, :)]);
iPts = [iPts; dist, p'];
end
end
% get the shortest distance from four vertices
err = min(abs(iPts(:, 1)));
shortestDist = iPts(find(min(abs(iPts(:, 1))) == abs(iPts(:, 1))), 2:4);
shortestDistNorm = norm(shortestDist);
end
%%
function [pts45T, pts45B] = getPointsOnSecTerDatumShortFix(v8WorkPiece, fixDatums, vDatum, dtm)
include_namespace_dq
% two vertical lines (% vDatum vertical datum)
if dtm == 's'
LS5 = Plucker.planes(v8WorkPiece(2:5, 6), v8WorkPiece(2:5, vDatum));
LS7 = Plucker.planes(v8WorkPiece(2:5, 8), v8WorkPiece(2:5, vDatum));
else dtm == 't';
LS5 = Plucker.planes(v8WorkPiece(2:5, 5), v8WorkPiece(2:5, vDatum));
LS7 = Plucker.planes(v8WorkPiece(2:5, 7), v8WorkPiece(2:5, vDatum));
end
% Get a plane parallel to the primary passing through height 75 mm
plane75 = vec8(getDQPlane(fixDatums(2:4, 1), [0, 0, 75]));
p4T = LS5.intersect_plane3(plane75(2:5))';
p5T = LS7.intersect_plane3(plane75(2:5))';
p4B = LS5.intersect_plane3(fixDatums(2:5, 1))';
p5B = LS7.intersect_plane3(fixDatums(2:5, 1))';
pts45T = [p4T; p5T];
pts45B = [p4B; p5B];
end
%%
function[p4Bfix, p5Bfix, p4Tfix, p5Tfix, t4B, t5B, t4T, t5T] = projectOntoSecondaryFix(pts45B, pts45T, fixDatums, ULPT)
L4B = Plucker.pointdir(pts45B(1, :), ULPT);
L5B = Plucker.pointdir(pts45B(2, :), ULPT);
L4T = Plucker.pointdir(pts45T(1, :), ULPT);
L5T = Plucker.pointdir(pts45T(2, :), ULPT);
[p4Bfix, t4B] = L4B.intersect_plane2(fixDatums(2:5, 2));
[p5Bfix, t5B] = L5B.intersect_plane2(fixDatums(2:5, 2));
[p4Tfix, t4T] = L4T.intersect_plane2(fixDatums(2:5, 2));
[p5Tfix, t5T] = L5T.intersect_plane2(fixDatums(2:5, 2));
end