Skip to content

Commit

Permalink
Bugfix/autofocus (#21)
Browse files Browse the repository at this point in the history
* autofocus fixes

* refactor set_z_position

* update autofocus docstring

* formatting
  • Loading branch information
ieivanov authored Apr 18, 2023
1 parent a81e0be commit d5619a4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# isort
65de377b45fea923ddce70677b2364edbce16d2a
# linting
dd016a50659c285c27d458dd341275bb2147d240
dd016a50659c285c27d458dd341275bb2147d240
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ make format # run black and isort formatting
make lint # run flark8 linting
make pre-commit # run pre-commit hooks on all files
make test # run pytest
```
```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Data are acquired using the `run-mantis-acquisition` command. A list of the comm

```
run-mantis-acquisition --help
```
```

The mantis acquisition is configures using a YAML settings file. An example of a settings file can be found [here](mantis/acquisition/settings/example_acquisition_settings.yaml).

Expand All @@ -50,4 +50,4 @@ run-mantis-acquisition \\

## Contributing

If you would like to contribute to this package, please read the [contributing guide](CONTRIBUTING.md)
If you would like to contribute to this package, please read the [contributing guide](CONTRIBUTING.md)
20 changes: 14 additions & 6 deletions mantis/acquisition/acq_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,20 @@ def go_to_position(self, position_index: int):
microscope_operations.wait_for_device(
self.lf_acq.mmc, self.lf_acq.mmc.get_xy_stage_device()
)
# Note: moving z stage disengages autofocus
# microscope_operations.set_z_position(
# self.lf_acq.mmc,
# self.lf_acq.microscope_settings.autofocus_stage,
# self.position_settings.xyz_positions[position_index][2]
# )

# Note: only set the z position if not using autofocus. Calling
# set_z_position will disengage continuous autofocus. The autofocus
# algorithm sets the z position independently
if not self.lf_acq.microscope_settings.use_autofocus:
microscope_operations.set_z_position(
self.lf_acq.mmc,
self.lf_acq.microscope_settings.autofocus_stage,
self.position_settings.xyz_positions[position_index][2],
)
microscope_operations.wait_for_device(
self.lf_acq.mmc,
self.lf_acq.microscope_settings.autofocus_stage,
)

def setup(self):
"""
Expand Down
48 changes: 28 additions & 20 deletions mantis/acquisition/microscope_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,44 @@ def get_total_num_daq_counter_samples(CtrTask: nidaqmx.Task or list):


def autofocus(mmc, mmStudio, z_stage_name: str, z_position):
"""_summary_
0000000000000000 - Off and out of range?
0000000100000000 - Off
0000001100001001 - In Range
0000001100001010 - In range and engaged?
0000001000011001 - Out of Range
Args:
mmc (_type_): _description_
mmStudio (_type_): _description_
z_stage_name (str): _description_
z_position (_type_): _description_
Returns:
_type_: _description_
"""
Attempt to engage Nikon PFS continuous autofocus. This function will log a
message and continue if continuous autofocus is already engaged. Otherwise,
it will attempt to engage autofocus, moving the z stage by amounts given in
`z_offsets`, if necessary.
Nikon PFS status codes:
0000000000000000 - Off and out of range?
0000000100000000 - Off
0000001100001001 - In Range
0000001100001010 - In range and engaged?
0000001000011001 - Out of Range
0010001000001001 - ?
Returns
-------
bool
True if continuous autofocus successfully engaged, False otherwise.
"""
logger.debug('Engaging autofocus')
autofocus_success = False
error_occurred = False

af_method = mmStudio.get_autofocus_manager().get_autofocus_method()
z_offsets = [0, -10, 10, -20, 20, -30, 30]
z_offsets = [0, -10, 10, -20, 20, -30, 30] # in um

# turn on autofocus if it has been turned off
if af_method.get_property_value('PFS Status') in ['0000000000000000', '0000000100000000']:
# Turn on autofocus if it has been turned off. This call has no effect is
# continuous autofocus is already engaged
try:
af_method.full_focus()
except Exception:
logger.debug('Call to full_focus() method failed')
else:
logger.debug('Call to full_focus() method succeeded')

if af_method.is_continuous_focus_locked(): # True if autofocus is engaged
autofocus_success = True
logger.debug('Autofocus is already engaged')
logger.debug('Continuous autofocus is already engaged')
else:
for z_offset in z_offsets:
mmc.set_position(z_stage_name, z_position + z_offset)
Expand All @@ -204,7 +212,7 @@ def autofocus(mmc, mmStudio, z_stage_name: str, z_position):
logger.debug(f'Autofocus call failed with z offset of {z_offset} um')

if error_occurred and autofocus_success:
logger.debug(f'Autofocus call succeeded with z offset of {z_offset} um')
logger.debug(f'Continuous autofocus call succeeded with z offset of {z_offset} um')

if not autofocus_success:
logger.error(f'Autofocus call failed after {len(z_offsets)} tries')
Expand Down

0 comments on commit d5619a4

Please sign in to comment.