|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +## Copyright (C) 2019 David Miguel Susano Pinto <david.pinto@bioch.ox.ac.uk> |
| 5 | +## Copyright (C) 2019 久保俊貴 <kubo@ap.eng.osaka-u.ac.jp> |
| 6 | +## |
| 7 | +## Microscope is free software: you can redistribute it and/or modify |
| 8 | +## it under the terms of the GNU General Public License as published by |
| 9 | +## the Free Software Foundation, either version 3 of the License, or |
| 10 | +## (at your option) any later version. |
| 11 | +## |
| 12 | +## Microscope is distributed in the hope that it will be useful, |
| 13 | +## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +## GNU General Public License for more details. |
| 16 | +## |
| 17 | +## You should have received a copy of the GNU General Public License |
| 18 | +## along with Microscope. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +"""Imagine Optic Mirao 52-e deformable mirror. |
| 21 | +
|
| 22 | +The Mirao 52-e deformable mirror is not capable of receiving hardware |
| 23 | +triggers. It is only capable of sending hardware triggers. That |
| 24 | +sending of hardware triggers is not implemented on this module because |
| 25 | +it's pointless. |
| 26 | +
|
| 27 | +The Mirao 52-e deformable mirror has a limitation on valid patterns. |
| 28 | +From the vendor documentation (the command is the pattern to be |
| 29 | +applied): |
| 30 | +
|
| 31 | + [...] the sum of the absolute values defining the command must be |
| 32 | + lower than or equal to 24 and each value must be comprised between |
| 33 | + -1.0 and 1.0. |
| 34 | +
|
| 35 | +In microscope, a pattern must be specified in the [0 1] range. |
| 36 | +However, the limit of 24, after rescaling to [-1 1] range, still |
| 37 | +applies. |
| 38 | +
|
| 39 | +""" |
| 40 | + |
| 41 | +import ctypes |
| 42 | + |
| 43 | +import microscope.devices |
| 44 | +import microscope._wrappers.mirao52e as mro |
| 45 | + |
| 46 | + |
| 47 | +class Mirao52e(microscope.devices.DeformableMirror): |
| 48 | + def __init__(self, **kwargs): |
| 49 | + super().__init__(**kwargs) |
| 50 | + ## Status is not the return code of the function calls. |
| 51 | + ## Status is where we can find the error code in case a |
| 52 | + ## function call returns false. This _status variable will be |
| 53 | + ## an argument in all function calls. |
| 54 | + self._status = ctypes.pointer(ctypes.c_int(mro.OK)) |
| 55 | + if not mro.open(self._status): |
| 56 | + self._raise_status(mro.open) |
| 57 | + |
| 58 | + ## super class needs this, but maybe it should be calling the |
| 59 | + ## property directly? |
| 60 | + self._n_actuators = mro.NB_COMMAND_VALUES |
| 61 | + |
| 62 | + @property |
| 63 | + def n_actuators(self): |
| 64 | + return mro.NB_COMMAND_VALUES |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def _normalize_patterns(patterns): |
| 68 | + """ |
| 69 | + mirao52e SDK expects values in the [-1 1] range, so we normalize |
| 70 | + them from the [0 1] range we expect in our interface. |
| 71 | + """ |
| 72 | + patterns = (patterns * 2) -1 |
| 73 | + return patterns |
| 74 | + |
| 75 | + def apply_pattern(self, pattern): |
| 76 | + self._validate_patterns(pattern) |
| 77 | + pattern = self._normalize_patterns(pattern) |
| 78 | + command = pattern.ctypes.data_as(mro.Command) |
| 79 | + if not mro.applyCommand(command, mro.FALSE, self._status): |
| 80 | + self._raise_status(mro.applyCommand) |
| 81 | + |
| 82 | + def _raise_status(self, func): |
| 83 | + error_code = self._status.contents.value |
| 84 | + raise RuntimeError('mro_%s() failed (error code %d)' |
| 85 | + % (func.__name__, error_code)) |
| 86 | + |
| 87 | + def __del__(self): |
| 88 | + if not mro.close(self._status): |
| 89 | + self._raise_status(mro.close) |
| 90 | + super().__del__() |
0 commit comments