Code formatting
This commit is contained in:
parent
9ff24e8e37
commit
1a6088a816
1 changed files with 31 additions and 22 deletions
|
|
@ -14,19 +14,22 @@ from camera_stage_tracker import Tracker, move_until_motion_detected
|
||||||
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
def backlash_corrected_move(get_position, move, backlash_amount, pos):
|
def backlash_corrected_move(get_position, move, backlash_amount, pos):
|
||||||
"""Make two moves, arriving at `pos` from a consistent direction"""
|
"""Make two moves, arriving at `pos` from a consistent direction"""
|
||||||
displacement = pos - get_position()
|
displacement = pos - get_position()
|
||||||
backlash_vector = (displacement < 0).astype(np.int)*backlash_amount
|
backlash_vector = (displacement < 0).astype(np.int) * backlash_amount
|
||||||
if np.any(backlash_vector > 0):
|
if np.any(backlash_vector > 0):
|
||||||
move(pos - backlash_vector)
|
move(pos - backlash_vector)
|
||||||
move(pos)
|
move(pos)
|
||||||
|
|
||||||
|
|
||||||
def bake_backlash_corrected_move(get_position, move, backlash_amount):
|
def bake_backlash_corrected_move(get_position, move, backlash_amount):
|
||||||
"""Return a function that performs backlash-corrected moves"""
|
"""Return a function that performs backlash-corrected moves"""
|
||||||
return partial(backlash_corrected_move, get_position, move, backlash_amount)
|
return partial(backlash_corrected_move, get_position, move, backlash_amount)
|
||||||
|
|
||||||
def calibrate_xy_grid(tracker, move, step = 100, n_steps=4, backlash_compensation=0):
|
|
||||||
|
def calibrate_xy_grid(tracker, move, step=100, n_steps=4, backlash_compensation=0):
|
||||||
"""Make a series of moves in X and Y to determine the XY components of the pixel-to-sample matrix.
|
"""Make a series of moves in X and Y to determine the XY components of the pixel-to-sample matrix.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
@ -38,44 +41,50 @@ def calibrate_xy_grid(tracker, move, step = 100, n_steps=4, backlash_compensatio
|
||||||
step : float, optional (default 100)
|
step : float, optional (default 100)
|
||||||
The amount to move the stage by. This should move the sample by approximately 1/10th of the field of view.
|
The amount to move the stage by. This should move the sample by approximately 1/10th of the field of view.
|
||||||
"""
|
"""
|
||||||
try: # Ensure that the tracker has a template set
|
try: # Ensure that the tracker has a template set
|
||||||
_ = tracker.template
|
_ = tracker.template
|
||||||
except:
|
except:
|
||||||
tracker.acquire_template()
|
tracker.acquire_template()
|
||||||
tracker.reset_history() # make sure we get rid of the initial (0,0) point
|
tracker.reset_history() # make sure we get rid of the initial (0,0) point
|
||||||
starting_position = tracker.get_position()
|
starting_position = tracker.get_position()
|
||||||
# Move the stage in a square, recording the displacement from both the stage and the camera
|
# Move the stage in a square, recording the displacement from both the stage and the camera
|
||||||
try:
|
try:
|
||||||
for x in (np.arange(n_steps) - n_steps/2.0)*step:
|
for x in (np.arange(n_steps) - n_steps / 2.0) * step:
|
||||||
for y in (np.arange(n_steps) - n_steps/2.0)*step:
|
for y in (np.arange(n_steps) - n_steps / 2.0) * step:
|
||||||
move(starting_position + np.array([x, y, 0]))
|
move(starting_position + np.array([x, y, 0]))
|
||||||
tracker.append_point()
|
tracker.append_point()
|
||||||
finally:
|
finally:
|
||||||
move(starting_position)
|
move(starting_position)
|
||||||
# We then use least-squares to fit the XY part of the matrix relating
|
# We then use least-squares to fit the XY part of the matrix relating
|
||||||
# pixels to distance
|
# pixels to distance
|
||||||
# stage_positions should be the stage positions, with a zero mean.
|
# stage_positions should be the stage positions, with a zero mean.
|
||||||
# image_positions should be the same, but calculated from the images
|
# image_positions should be the same, but calculated from the images
|
||||||
stage_positions, image_positions = tracker.history
|
stage_positions, image_positions = tracker.history
|
||||||
stage_positions = stage_positions.astype(np.float)
|
stage_positions = stage_positions.astype(np.float)
|
||||||
stage_positions -= np.mean(stage_positions, axis=0)
|
stage_positions -= np.mean(stage_positions, axis=0)
|
||||||
stage_positions = stage_positions[:,:2] # ensure it's 2d
|
stage_positions = stage_positions[:, :2] # ensure it's 2d
|
||||||
image_positions -= np.mean(image_positions, axis=0)
|
image_positions -= np.mean(image_positions, axis=0)
|
||||||
#image_positions *= -1 # To get the matrix right, we want the position of each
|
# image_positions *= -1 # To get the matrix right, we want the position of each
|
||||||
# image relative to the template, rather than the other way around
|
# image relative to the template, rather than the other way around
|
||||||
A, res, rank, s = np.linalg.lstsq(image_positions, stage_positions) # we solve pixel_shifts*A = location_shifts
|
A, res, rank, s = np.linalg.lstsq(
|
||||||
|
image_positions, stage_positions
|
||||||
|
) # we solve pixel_shifts*A = location_shifts
|
||||||
|
|
||||||
transformed_image_positions = np.dot(image_positions, A)
|
transformed_image_positions = np.dot(image_positions, A)
|
||||||
residuals = transformed_image_positions - stage_positions
|
residuals = transformed_image_positions - stage_positions
|
||||||
fractional_error = norm(residuals) / stage_positions.shape[0] step
|
fractional_error = norm(residuals) / stage_positions.shape[0]
|
||||||
print(f"Ratio of residuals to displacement is {fractional_error})")
|
print(f"Ratio of residuals to displacement is {fractional_error})")
|
||||||
if fractional_error > 0.05: # Check it was a reasonably good fit
|
if fractional_error > 0.05: # Check it was a reasonably good fit
|
||||||
print("Warning: the error fitting measured displacements was %.1f%%" % (fractional_error*100))
|
print(
|
||||||
print(f"Calibrated the pixel-location matrix.\nResiduals were {fractional_error*100:.1f}% of the shift.")
|
"Warning: the error fitting measured displacements was %.1f%%"
|
||||||
|
% (fractional_error * 100)
|
||||||
return {
|
)
|
||||||
"image_to_stage_displacement": A,
|
print(
|
||||||
"moves": (stage_positions, image_positions),
|
f"Calibrated the pixel-location matrix.\nResiduals were {fractional_error*100:.1f}% of the shift."
|
||||||
"fractional_error": fractional_error
|
)
|
||||||
}
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
"image_to_stage_displacement": A,
|
||||||
|
"moves": (stage_positions, image_positions),
|
||||||
|
"fractional_error": fractional_error,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue