Debian Codegraph @

pymeasure/instruments/heidenhain/nd287.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import logging
26 from pyvisa.errors import VisaIOError
27 from pymeasure.instruments import Instrument
28
29
30 log = logging.getLogger(__name__)
31 log.addHandler(logging.NullHandler())
32
33
34 class ND287(Instrument):
35 """ Represents the Heidenhain ND287 position display unit used to readout and display
36 absolute position measured by Heidenhain encoders.
37 """
38
39 status = Instrument.measurement(
40 "\x1BA0800", "Get the encoder's status bar"
41 )
42
43 # get_process lambda functions used in the position property
44 position_get_process_map = {
45 "mm": lambda p: float(p.split("\x02")[-1]) * 1e-4,
46 "inch": lambda p: float(p.split("\x02")[-1]) * 1e-5
47 }
48
49 position = Instrument.measurement(
50 "\x1BA0200", """Measure the encoder's current position (float).
51 Note that the get_process performs a mapping from the returned
52 value to a float measured in the units specified by :attr:`.ND287.units`.
53 The get_process is modified dynamically as this mapping changes slightly
54 between different units.""",
55 get_process=position_get_process_map["mm"],
56 dynamic=True
57 )
58
59 def __init__(self, adapter, name="Heidenhain ND287", units="mm", **kwargs):
60 """ Initialize the nd287 with a carriage return write termination.
61
62 :param: units: Specify the units that the gauge is working in.
63 Valid values are "inch" and "mm" with "mm" being the default.
64 """
65 self._units = units
66
67 super().__init__(
68 adapter,
69 name,
70 includeSCPI=False,
71 write_termination="\r",
72 **kwargs
73 )
74
75 @property
76 def id(self):
77 """ Get the string identification property for the device.
78 """
79 self.write("\x1BA0000")
80 id_str = self.read_bytes(37).decode("utf-8")
81 return id_str
82
83 @property
84 def units(self):
85 """ Control the unit of measure set on the device.
86 Valid values are 'mm' and 'inch' Note that this parameter can only be set
87 manually on the device. So this argument only ensures that the instance units
88 and physical device settings match. I.e., this property does not change any
89 physical device setting.
90 """
91 return self._units
92
93 @units.setter
94 def units(self, unit):
95 if unit in self.position_get_process_map.keys():
96 self._units = unit
97 self.position_get_process = self.position_get_process_map[unit]
98
99 def check_errors(self):
100 """ Method to read an error status message and log when an error is detected.
101
102 :return: String with the error message as its contents.
103 """
104 self.write("\x1BA0301")
105 try:
106 err_str = self.read_bytes(36).decode("utf-8")
107 except VisaIOError:
108 err_str = None
109
110 if err_str is not None:
111 log.error("Heidenhain ND287 error message received: %s" % err_str)
112
113 return err_str