From: W. Trevor King Date: Fri, 19 Oct 2012 13:53:22 +0000 (-0400) Subject: calibration: support to-physical-only softcal boards. X-Git-Tag: 0.6~7 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ac38a7987800c3982ac363af5b7780885c88d3a1;p=pycomedi.git calibration: support to-physical-only softcal boards. This is a hack to fix Éric Piel's issue: On Thu, Oct 18, 2012 at 12:10:49PM +0200, Éric Piel wrote: > Unfortunately, with my device there seems to still be a problem with > comedi_get_softcal_converter(), which returns -1 with direction > from_physical. I get the same problem when using the SWIG wrapper, so > it's probably not an error in pycomedi. So when I run cmd.py I get this > error, but it works anyway as the _to_phys converter works: > ./cmd.py -f /dev/comedi0 -s 0 -c 0 -a diff -N 1 -p > Exception pycomedi.PyComediError: > PyComediError('comedi_get_softcal_converter: Resource temporarily > unavailable (-1)',) in > 'pycomedi.channel.AnalogChannel.get_softcal_converter' ignored > 0.220157 I feel like we should make an effort to invert the from_physical polynomial, but as a stop-gap measure, we can just raise the original exception and let the user decide what to do about it. --- diff --git a/pycomedi/calibration.pxd b/pycomedi/calibration.pxd index a0a687a..12a4e3f 100644 --- a/pycomedi/calibration.pxd +++ b/pycomedi/calibration.pxd @@ -21,6 +21,7 @@ cimport _comedilib_h cdef class CalibratedConverter (object): cdef _comedilib_h.comedi_polynomial_t _to_physical, _from_physical + cdef object _from_physical_error cdef _str_poly(self, _comedilib_h.comedi_polynomial_t polynomial) cpdef to_physical(self, data) diff --git a/pycomedi/calibration.pyx b/pycomedi/calibration.pyx index 70388de..a3183e5 100644 --- a/pycomedi/calibration.pyx +++ b/pycomedi/calibration.pyx @@ -161,11 +161,32 @@ cdef class CalibratedConverter (object): 1.0 >>> c.get_to_physical_coefficients() array([ 1., 2., 3.]) + + For some soft-calibrated boards, there is no from_physical + conversion polynomial. + + >>> c = CalibratedConverter( + ... from_physical_error=Exception('no conversion polynomial')) + >>> c.from_physical(1.0) + Traceback (most recent call last): + ... + Exception: no conversion polynomial + + However, even with the error, you can extract dummy coefficients. + + >>> c.get_from_physical_expansion_origin() + 0.0 + >>> c.get_from_physical_coefficients() + array([ 0.]) """ + def __cinit__(self): + self._from_physical_error = None + def __init__(self, to_physical_coefficients=None, to_physical_expansion_origin=0, from_physical_coefficients=None, - from_physical_expansion_origin=0): + from_physical_expansion_origin=0, + from_physical_error=None): if to_physical_coefficients: _setup_comedi_polynomial_t( &self._to_physical, to_physical_coefficients, @@ -174,6 +195,7 @@ cdef class CalibratedConverter (object): _setup_comedi_polynomial_t( &self._from_physical, from_physical_coefficients, from_physical_expansion_origin) + self._from_physical_error = from_physical_error cdef _str_poly(self, _comedilib_h.comedi_polynomial_t polynomial): return '{coefficients:%s origin:%s}' % ( @@ -194,6 +216,8 @@ cdef class CalibratedConverter (object): _constant.CONVERSION_DIRECTION.to_physical) cpdef from_physical(self, data): + if self._from_physical_error is not None: + raise self._from_physical_error return _convert(&self._from_physical, data, _constant.CONVERSION_DIRECTION.from_physical) diff --git a/pycomedi/channel.pyx b/pycomedi/channel.pyx index 81f6df0..41cdbd3 100644 --- a/pycomedi/channel.pyx +++ b/pycomedi/channel.pyx @@ -420,23 +420,28 @@ cdef class AnalogChannel (Channel): cdef _comedilib_h.comedi_polynomial_t to_physical, from_physical cdef _CalibratedConverter ret flags = self.subdevice.get_flags() + from_physical_error = None if flags.soft_calibrated: if calibration is None: calibration = self.subdevice.device.parse_calibration() to_physical = self.get_softcal_converter( _constant.CONVERSION_DIRECTION.to_physical, calibration) - from_physical = self.get_softcal_converter( - _constant.CONVERSION_DIRECTION.from_physical, - calibration) + try: + from_physical = self.get_softcal_converter( + _constant.CONVERSION_DIRECTION.from_physical, + calibration) + except _PyComediError as e: + from_physical_error = e else: to_physical = self.get_hardcal_converter( _constant.CONVERSION_DIRECTION.to_physical) from_physical = self.get_hardcal_converter( _constant.CONVERSION_DIRECTION.from_physical) - ret = _CalibratedConverter() + ret = _CalibratedConverter(from_physical_error=from_physical_error) ret._to_physical = to_physical - ret._from_physical = from_physical + if from_physical_error is not None: + ret._from_physical = from_physical return ret def get_converter(self, calibration=None):