|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import List, Optional, cast |
| 4 | +from xml.etree import ElementTree |
| 5 | + |
| 6 | +from ..exceptions import DecodeError, EncodeError, odxassert, odxraise |
| 7 | +from ..odxlink import OdxDocFragment |
| 8 | +from ..odxtypes import AtomicOdxType, DataType |
| 9 | +from ..utils import dataclass_fields_asdict |
| 10 | +from .compumethod import CompuCategory, CompuMethod |
| 11 | +from .ratfuncsegment import RatFuncSegment |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class RatFuncCompuMethod(CompuMethod): |
| 16 | + """A compu method using a rational function |
| 17 | +
|
| 18 | + i.e. internal values are converted to physical ones using the |
| 19 | + function `f(x) = (a0 + a1*x + a2*x^2 ...)/(b0 + b0*x^2 ...)` where `f(x)` |
| 20 | + is the physical value and `x` is the internal value. In contrast |
| 21 | + to `ScaleRatFuncCompuMethod`, this compu method only exhibits a |
| 22 | + single segment) |
| 23 | +
|
| 24 | + For details, refer to ASAM specification MCD-2 D (ODX), section 7.3.6.6.5. |
| 25 | + """ |
| 26 | + |
| 27 | + @property |
| 28 | + def int_to_phys_segment(self) -> RatFuncSegment: |
| 29 | + return self._int_to_phys_segment |
| 30 | + |
| 31 | + @property |
| 32 | + def phys_to_int_segment(self) -> Optional[RatFuncSegment]: |
| 33 | + return self._phys_to_int_segment |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def compu_method_from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment], *, |
| 37 | + internal_type: DataType, |
| 38 | + physical_type: DataType) -> "RatFuncCompuMethod": |
| 39 | + cm = CompuMethod.compu_method_from_et( |
| 40 | + et_element, doc_frags, internal_type=internal_type, physical_type=physical_type) |
| 41 | + kwargs = dataclass_fields_asdict(cm) |
| 42 | + |
| 43 | + return RatFuncCompuMethod(**kwargs) |
| 44 | + |
| 45 | + def __post_init__(self) -> None: |
| 46 | + odxassert(self.category == CompuCategory.RAT_FUNC, |
| 47 | + "RatFuncCompuMethod must exhibit RAT-FUNC category") |
| 48 | + |
| 49 | + odxassert(self.physical_type in [ |
| 50 | + DataType.A_FLOAT32, |
| 51 | + DataType.A_FLOAT64, |
| 52 | + DataType.A_INT32, |
| 53 | + DataType.A_UINT32, |
| 54 | + ]) |
| 55 | + odxassert(self.internal_type in [ |
| 56 | + DataType.A_FLOAT32, |
| 57 | + DataType.A_FLOAT64, |
| 58 | + DataType.A_INT32, |
| 59 | + DataType.A_UINT32, |
| 60 | + ]) |
| 61 | + |
| 62 | + if self.compu_internal_to_phys is None: |
| 63 | + odxraise("RAT-FUNC compu methods require COMPU-INTERNAL-TO-PHYS") |
| 64 | + return |
| 65 | + |
| 66 | + int_to_phys_scales = self.compu_internal_to_phys.compu_scales |
| 67 | + if len(int_to_phys_scales) != 1: |
| 68 | + odxraise("RAT-FUNC compu methods expect exactly one compu scale within " |
| 69 | + "COMPU-INTERNAL-TO-PHYS") |
| 70 | + return cast(None, RatFuncCompuMethod) |
| 71 | + |
| 72 | + self._int_to_phys_segment = RatFuncSegment.from_compu_scale( |
| 73 | + int_to_phys_scales[0], value_type=self.physical_type) |
| 74 | + |
| 75 | + self._phys_to_int_segment = None |
| 76 | + if self.compu_phys_to_internal is not None: |
| 77 | + phys_to_int_scales = self.compu_phys_to_internal.compu_scales |
| 78 | + if len(phys_to_int_scales) != 1: |
| 79 | + odxraise("RAT-FUNC compu methods expect exactly one compu scale within " |
| 80 | + "COMPU-PHYS-TO-INTERNAL") |
| 81 | + return cast(None, RatFuncCompuMethod) |
| 82 | + |
| 83 | + self._phys_to_int_segment = RatFuncSegment.from_compu_scale( |
| 84 | + phys_to_int_scales[0], value_type=self.internal_type) |
| 85 | + |
| 86 | + def convert_internal_to_physical(self, internal_value: AtomicOdxType) -> AtomicOdxType: |
| 87 | + if not self._int_to_phys_segment.applies(internal_value): |
| 88 | + odxraise(f"Cannot decode internal value {internal_value!r}", DecodeError) |
| 89 | + return cast(AtomicOdxType, None) |
| 90 | + |
| 91 | + return self._int_to_phys_segment.convert(internal_value) |
| 92 | + |
| 93 | + def convert_physical_to_internal(self, physical_value: AtomicOdxType) -> AtomicOdxType: |
| 94 | + if self._phys_to_int_segment is None or not self._phys_to_int_segment.applies( |
| 95 | + physical_value): |
| 96 | + odxraise(f"Cannot encode physical value {physical_value!r}", EncodeError) |
| 97 | + return cast(AtomicOdxType, None) |
| 98 | + |
| 99 | + return self._phys_to_int_segment.convert(physical_value) |
| 100 | + |
| 101 | + def is_valid_physical_value(self, physical_value: AtomicOdxType) -> bool: |
| 102 | + return self._phys_to_int_segment is not None and self._phys_to_int_segment.applies( |
| 103 | + physical_value) |
| 104 | + |
| 105 | + def is_valid_internal_value(self, internal_value: AtomicOdxType) -> bool: |
| 106 | + return self._int_to_phys_segment.applies(internal_value) |
0 commit comments