-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2state.py
555 lines (412 loc) · 17.3 KB
/
v2state.py
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
import logging
import ini
from calibstate import CalibState, Stages
import v2calculations
from v2routines import V2_10, V2_50, PROBE_DIA, SPINDLE_BALL_DIA_10, SPINDLE_BALL_DIA_50, APPROX_FIXTURE_BALL_HOME, FIXTURE_BALL_DIA, APPROX_COR
from metrology import Feature, angle_between_ccw_2d, intersectLines, angle_between, projectPointToPlane, projectDirectionToPlane
import numpy as np
logger = logging.getLogger(__name__)
def getHomeOffsetDx(state, interpreter):
"""
Our current X home offset may have changed since the PROBE_SPINDLE_POS stage
so look up what it was, look up what it is now and take the difference
(returns previous-current).
TODO - if we start using a compensation table for the linear axes, we may
need to apply the compensation table to the offset as well, but hopefully
a compensation table would make a negligible enough difference that we could
avoid it.
"""
meta = state.getStageMeta(Stages.PROBE_SPINDLE_POS)
overlayData = ini.read_ini_string(meta['calibration']['overlay'])
x_home_offset = float(ini.get_parameter(overlayData, "JOINT_0", "HOME_OFFSET")["values"]["value"])
current_x_home_offset = interpreter.params["_hal[ini.0.home_offset]"]
dx = -(current_x_home_offset-x_home_offset)*25.4 # Machine units is in inches, so convert to mm
return dx
def getHomeOffsetDy(state, interpreter):
"""
Our current Y home offset may have changed since the PROBE_FIXTURE_BALL_POS stage
so look up what it is now, look up what it was and return the difference (returns current-previous).
Note that this value is inverted when compared with getHomeOffsetDx because the table moves down
when going in a positive direction, whereas the spindle moves in the same direction as the X axis.
Assumes that the machine units are in inches. Returns mm.
TODO - if we start using a compensation table for the linear axes, we may
need to apply the compensation table to the offset as well, but hopefully
a compensation table would make a negligible enough difference that we could
avoid it.
"""
meta = state.getStageMeta(Stages.PROBE_FIXTURE_BALL_POS)
overlayData = ini.read_ini_string(meta['calibration']['overlay'])
y_home_offset = float(ini.get_parameter(overlayData, "JOINT_1", "HOME_OFFSET")["values"]["value"])
current_y_home_offset = interpreter.params["_hal[ini.1.home_offset]"]
dy = (current_y_home_offset-y_home_offset)*25.4 # Machine units is in inches, so convert to mm
return dy
def getFixtureBallPos(state):
return state.getStage(Stages.PROBE_FIXTURE_BALL_POS)["fixture_ball_pos"]
def getZeroSpindlePos(state):
return state.getStage(Stages.PROBE_SPINDLE_POS)["zero_spindle_pos"]
def getOriginSpindlePos(state):
return state.getStage(Stages.CHARACTERIZE_Z)["zero"]
def getToolProbePos(state):
return state.getStage(Stages.TOOL_PROBE_OFFSET)["tool_probe_pos"]
def getPlaneA90(state):
return state.getStage(Stages.TOOL_PROBE_OFFSET)["plane_a90"]
def getFeaturesX(state):
return state.getStage(Stages.CHARACTERIZE_X)['features']
def getFeaturesXFixtureBall(state):
return state.getStage(Stages.CHARACTERIZE_X_FIXTURE_BALL)['features']
def getFeaturesY(state):
return state.getStage(Stages.CHARACTERIZE_Y)['features']
def getFeaturesZ(state):
return state.getStage(Stages.CHARACTERIZE_Z)['features']
def getFeaturesA(state):
return state.getStage(Stages.CHARACTERIZE_A)['features']
def getFeaturesB(state):
return state.getStage(Stages.CHARACTERIZE_B)['features']
def getFeaturesHomingA(state):
return state.getStage(Stages.HOMING_A)['features']
def getFeaturesHomingB(state):
return state.getStage(Stages.HOMING_B)['features']
def getXDirection(state):
feats = getFeaturesX(state)
# Points are collected from positive to negative, so reverse the direction
# so we are returning the positive X direction
spheres = v2calculations.calc_sphere_centers(reversed(feats))
(pt,dir) = spheres.line()
return dir
def getXDirectionOOS(state):
x_stage = state.getStage(Stages.CHARACTERIZE_X)
x_stage_fixture = state.getStage(Stages.CHARACTERIZE_X_FIXTURE_BALL)
spheres = v2calculations.calc_sphere_centers(reversed(x_stage["features"][0:-1]))
spheres_fixture = v2calculations.calc_sphere_centers(reversed(x_stage_fixture["features"][0:-1]))
spindle_ball_points = spheres.points()
fixture_ball_points = spheres_fixture.points()
fixture_ref_pt = x_stage_fixture["features"][-1].sphere()[1]
line_feature = Feature()
for (spindle_ball_pt,fixture_ball_pt) in zip(spindle_ball_points, fixture_ball_points):
pt = spindle_ball_pt + (fixture_ref_pt - fixture_ball_pt)
line_feature.addPoint(*pt)
(pt,dir) = line_feature.line()
return dir
def getYDirectionOOS(state):
y_stage = state.getStage(Stages.CHARACTERIZE_Y)
y_stage_fixture = state.getStage(Stages.CHARACTERIZE_Y_SPINDLE_BALL)
spheres = v2calculations.calc_sphere_centers(y_stage["features"][0:-1])
spheres_spindle = v2calculations.calc_sphere_centers(y_stage_fixture["features"][0:-1])
fixture_ball_points = spheres.points()
spindle_ball_points = spheres_spindle.points()
spindle_ref_pt = y_stage_fixture["features"][-1].sphere()[1]
line_feature = Feature()
for (fixture_ball_pt,spindle_ball_pt) in zip(fixture_ball_points, spindle_ball_points):
pt = fixture_ball_pt + (spindle_ref_pt - spindle_ball_pt)
line_feature.addPoint(*pt)
(pt,dir) = line_feature.line()
return dir
def getYDirection(state):
feats = getFeaturesY(state)
spheres = v2calculations.calc_sphere_centers(feats)
(pt,dir) = spheres.line()
logger.debug('y spheres %s', spheres.points())
logger.debug('y dir %s', dir)
if dir[1] < 0:
dir = -1*dir
return dir
def getZDirection(state):
feats = getFeaturesZ(state)
# Points are collected from positive to negative, so reverse the direction
# so we are returning the positive Z direction
spheres = v2calculations.calc_sphere_centers(reversed(feats))
(pt,dir) = spheres.line()
return dir
def getAxisDirections(state):
x = getXDirection(state)
y = getYDirection(state)
z = getZDirection(state)
return (x,y,z)
def getAZeroPt(state):
feat = state.getStage(Stages.CHARACTERIZE_A)['zero']
(rad, pt) = feat.sphere()
return pt
def getBZeroPt(state):
feat = state.getStage(Stages.CHARACTERIZE_B)['zero']
(rad, pt) = feat.sphere()
return pt
def getAPositionsLine(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
characterize_stage = state.getStage(Stages.CHARACTERIZE_A_LINE)
zero_feat = characterize_stage['zero']
zero_pos = v2calculations.calc_pos_a(zero_feat, x_dir, y_dir, z_dir, APPROX_COR)
nom_zero_pos = characterize_stage['zero_a_pos']
positions = []
for (feat, nom_pos) in zip(characterize_stage["features"],characterize_stage["positions"]):
zeroed_nom_pos = nom_pos['a'] - nom_zero_pos
a_pos = v2calculations.calc_pos_a(feat, x_dir, y_dir, z_dir, APPROX_COR)
positions.append((zeroed_nom_pos, a_pos))
return positions
def getBPositionsLine(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
characterize_stage = state.getStage(Stages.CHARACTERIZE_B_LINE)
zero_feat = characterize_stage['zero']
zero_pos = v2calculations.calc_pos_b(zero_feat, x_dir, y_dir, z_dir, APPROX_COR)
nom_zero_pos = characterize_stage['zero_b_pos']
positions = []
for (feat, nom_pos) in zip(characterize_stage["features"],characterize_stage["positions"]):
zeroed_nom_pos = nom_pos['b'] - nom_zero_pos
b_pos = v2calculations.calc_pos_b(feat, x_dir, y_dir, z_dir, APPROX_COR)
if b_pos-zeroed_nom_pos > 180:
b_pos -= 360
logger.debug("nom_pos %s", nom_pos['b'])
logger.debug("zeroed_nom_pos %s", zeroed_nom_pos)
logger.debug("b_pos %s", b_pos)
positions.append((zeroed_nom_pos, b_pos))
return positions
def getBErrorLineAndSphere(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
b_stage = state.getStage(Stages.CHARACTERIZE_B_SPHERE)
features = b_stage["features"]
positions = b_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
cor_pt = circle[1]
zero_pt = b_stage["zero"].sphere()[1]
axis_of_rotation = circle[2]
if np.dot(y_dir,axis_of_rotation) < 0:
axis_of_rotation *= -1
# Each point needs to be projected to the plane perpendicular
# to the axis of rotation.
plane = (cor_pt,axis_of_rotation)
b_line_stage = state.getStage(Stages.CHARACTERIZE_B_LINE)
b_line_features = b_line_stage["features"]
b_line_positions = b_line_stage["positions"]
zero_b_pos = b_line_stage["zero_b_pos"]
zero_line = b_line_stage["zero"].projectToPlane(plane)
unit_zero_vec = zero_line.line()[1]
# We need to convert all out points into a 2D space for calculating a ccw rotation,
# so we're going to define an x_vec and y_vec that define that 2D space.
x_vec = -unit_zero_vec
y_vec = np.cross(axis_of_rotation, x_vec)
b_err = []
for (feature,pos) in zip(b_line_features,b_line_positions):
line = feature.projectToPlane(plane).line()
dir = -1*projectDirectionToPlane(line[1], plane)
vec = dir/np.linalg.norm(dir)
x = np.dot(x_vec, vec)
y = np.dot(y_vec, vec)
angle = angle_between_ccw_2d([1,0], [x,y])
if angle < 0:
angle += 360
commanded_angle = pos["b"]-zero_b_pos
error = commanded_angle-angle
if error > 300:
error -= 360
elif error < -300:
error += 360
b_err.append((commanded_angle, error))
return b_err
def getAErrorSphere(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
a_stage = state.getStage(Stages.CHARACTERIZE_A_SPHERE)
zero_a_pos = a_stage["zero_a_pos"]
features = a_stage["features"]
positions = a_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
cor_pt = circle[1]
zero_pt = a_stage["zero"].sphere()[1]
axis_of_rotation = circle[2]
if np.dot(x_dir,axis_of_rotation) < 0:
axis_of_rotation *= -1
# We're defining the zero point that we measured to be A0.
# Each point needs to be projected to the plane perpendicular
# to the axis of rotation.
plane = (cor_pt,axis_of_rotation)
zero_pt_proj = projectPointToPlane(zero_pt, plane)
zero_vec = zero_pt_proj-cor_pt
unit_zero_vec = zero_vec/np.linalg.norm(zero_vec)
# We need to convert all out points into a 2D space for calculating a ccw rotation,
# so we're going to define an x_vec and y_vec that define that 2D space.
y_vec = np.cross(axis_of_rotation,unit_zero_vec)
x_vec = unit_zero_vec
a_err = []
for (feature,pos) in zip(features,positions):
pt = feature.sphere()[1]
pt_proj = projectPointToPlane(pt, plane)
vec = pt_proj-cor_pt
r = np.linalg.norm(vec)
x = np.dot(x_vec, vec)
y = np.dot(y_vec, vec)
angle = angle_between_ccw_2d([1,0], [x,y])
commanded_angle = pos["a"]-zero_a_pos
print(f"{commanded_angle}\t{r-circle[0]}")
error = commanded_angle-angle
a_err.append((commanded_angle, error))
return a_err
def getBErrorSphere(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
b_stage = state.getStage(Stages.CHARACTERIZE_B_SPHERE)
zero_b_pos = b_stage["zero_b_pos"]
features = b_stage["features"]
positions = b_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
cor_pt = circle[1]
zero_pt = b_stage["zero"].sphere()[1]
axis_of_rotation = circle[2]
if np.dot(y_dir,axis_of_rotation) < 0:
axis_of_rotation *= -1
# We're defining the zero point that we measured to be B0.
# Each point needs to be projected to the plane perpendicular
# to the axis of rotation.
plane = (cor_pt,axis_of_rotation)
zero_pt_proj = projectPointToPlane(zero_pt, plane)
zero_vec = zero_pt_proj-cor_pt
unit_zero_vec = zero_vec/np.linalg.norm(zero_vec)
# We need to convert all out points into a 2D space for calculating a ccw rotation,
# so we're going to define an x_vec and y_vec that define that 2D space.
y_vec = np.cross(axis_of_rotation,unit_zero_vec)
x_vec = unit_zero_vec
b_err = []
for (feature,pos) in zip(features,positions):
pt = feature.sphere()[1]
pt_proj = projectPointToPlane(pt, plane)
vec = pt_proj-cor_pt
x = np.dot(x_vec, vec)
y = np.dot(y_vec, vec)
angle = angle_between_ccw_2d([1,0], [x,y])
if angle < 0:
angle += 360
commanded_angle = pos["b"]-zero_b_pos
error = commanded_angle-angle
if error > 300:
error -= 360
elif error < -300:
error += 360
r = np.linalg.norm(vec)
print(f"{commanded_angle}\t{r-circle[0]}")
b_err.append((commanded_angle, error))
print("Circle", circle)
return b_err
def getAPositionsSphere(state):
stage = state.getStage(Stages.CHARACTERIZE_A_SPHERE)
zero_pt = getAZeroPt(state)
feats = getFeaturesA(stage)
pts = v2calculations.calc_sphere_centers(feats)
(rad, cor, norm) = Feature(pts).circle()
zero_line = zero_pt - cor
positions = []
for pt in pts:
a_line = pt - cor
a_pos = angle_between_ccw_2d(zero_line, a_line)
positions.append(a_pos)
def getAErrorLineAndSphere(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
a_stage = state.getStage(Stages.CHARACTERIZE_A_SPHERE)
features = a_stage["features"]
positions = a_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
cor_pt = circle[1]
zero_pt = a_stage["zero"].sphere()[1]
axis_of_rotation = circle[2]
if np.dot(x_dir,axis_of_rotation) < 0:
axis_of_rotation *= -1
# We're defining the zero point that we measured to be A0.
# Each point needs to be projected to the plane perpendicular
# to the axis of rotation.
plane = (cor_pt,axis_of_rotation)
a_line_stage = state.getStage(Stages.CHARACTERIZE_A_LINE)
a_line_features = a_line_stage["features"]
a_line_positions = a_line_stage["positions"]
zero_a_pos = a_line_stage["zero_a_pos"]
zero_line = a_line_stage["zero"].projectToPlane(plane)
unit_zero_vec = zero_line.line()[1]
# We need to convert all out points into a 2D space for calculating a ccw rotation,
# so we're going to define an x_vec and y_vec that define that 2D space.
y_vec = -unit_zero_vec
x_vec = np.cross(axis_of_rotation, y_vec)
a_err = []
for (feature,pos) in zip(a_line_features,a_line_positions):
line = feature.projectToPlane(plane).line()
dir = -1*projectDirectionToPlane(line[1], plane)
vec = dir/np.linalg.norm(dir)
x = np.dot(x_vec, vec)
y = np.dot(y_vec, vec)
angle = -angle_between_ccw_2d([0,1], [x,y])
commanded_angle = pos["a"]-zero_a_pos
error = commanded_angle-angle
a_err.append((commanded_angle, error))
return a_err
def getBErrorLineAndSphere(state):
(x_dir,y_dir,z_dir) = getAxisDirections(state)
b_stage = state.getStage(Stages.CHARACTERIZE_B_SPHERE)
features = b_stage["features"]
positions = b_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
cor_pt = circle[1]
zero_pt = b_stage["zero"].sphere()[1]
axis_of_rotation = circle[2]
if np.dot(y_dir,axis_of_rotation) < 0:
axis_of_rotation *= -1
# Each point needs to be projected to the plane perpendicular
# to the axis of rotation.
plane = (cor_pt,axis_of_rotation)
b_line_stage = state.getStage(Stages.CHARACTERIZE_B_LINE)
b_line_features = b_line_stage["features"]
b_line_positions = b_line_stage["positions"]
zero_b_pos = b_line_stage["zero_b_pos"]
zero_line = b_line_stage["zero"].projectToPlane(plane)
unit_zero_vec = zero_line.line()[1]
# We need to convert all out points into a 2D space for calculating a ccw rotation,
# so we're going to define an x_vec and y_vec that define that 2D space.
x_vec = -unit_zero_vec
y_vec = np.cross(axis_of_rotation, x_vec)
b_err = []
for (feature,pos) in zip(b_line_features,b_line_positions):
line = feature.projectToPlane(plane).line()
dir = -1*projectDirectionToPlane(line[1], plane)
vec = dir/np.linalg.norm(dir)
x = np.dot(x_vec, vec)
y = np.dot(y_vec, vec)
angle = angle_between_ccw_2d([1,0], [x,y])
if angle < 0:
angle += 360
commanded_angle = pos["b"]-zero_b_pos
error = commanded_angle-angle
if error > 300:
error -= 360
elif error < -300:
error += 360
b_err.append((commanded_angle, error))
return b_err
def getYHomeOffsetFromASpheres(state):
a_stage = state.getStage(Stages.CHARACTERIZE_A_SPHERE)
features = a_stage["features"]
positions = a_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
z_stage = state.getStage(Stages.HOMING_Z)
z_features = z_stage["features"]
z_centers = v2calculations.calc_sphere_centers(z_features)
z0 = z_centers.average()
z_char_stage = state.getStage(Stages.CHARACTERIZE_Z)
z_3 = z_char_stage["features"][-1].sphere()[1]
y_stage = state.getStage(Stages.HOMING_Y)
y_features = y_stage["features"]
y_centers = v2calculations.calc_sphere_centers(y_features)
y0 = y_centers.average()
vec = circle[1]-z0
y_dir = getYDirectionOOS(state)
return np.dot(y_dir, vec)
def getXHomeOffsetFromBSpheres(state):
b_stage = state.getStage(Stages.CHARACTERIZE_B_SPHERE)
features = b_stage["features"]
positions = b_stage["positions"]
centers = v2calculations.calc_sphere_centers(features)
circle = centers.circle()
x_stage = state.getStage(Stages.HOMING_X)
x_features = x_stage["features"]
x_centers = v2calculations.calc_sphere_centers(x_features)
x0 = x_centers.average()
vec = circle[1]-x0
x_dir = getXDirectionOOS(state)
return np.dot(x_dir, vec)