DeaD_EyE

norm input, scale output advanced

Oct 3rd, 2020
39
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from functools import partial
  2.  
  3. # pip3 install pint
  4. # https://pint.readthedocs.io/en/stable/
  5. from pint import UnitRegistry
  6.  
  7.  
  8. reg = UnitRegistry()
  9.  
  10.  
  11. def scale(value, /, *, min_value, max_value, min_unit, max_unit):
  12.     """
  13.    value := positional only argument for input
  14.    
  15.    keyword only
  16.    min_value := minimum input of value
  17.    max_value := maximum input of value
  18.    min_unit := minimum value of scaled unit
  19.    max_unit := maximum value of scaled unit
  20.  
  21.    :return: scaled unit
  22.    """
  23.     v_range = max_value - min_value
  24.     u_range = max_unit - min_unit
  25.     norm = (value - min_value) / v_range
  26.     return norm * u_range + min_unit
  27.  
  28.  
  29. normal_10V = {
  30.     "min_value": 0 * reg.V,
  31.     "max_value": 10 * reg.V,
  32. }
  33.  
  34. current_4_20 = {
  35.     "min_value": 4 * reg.mA,
  36.     "max_value": 20 * reg.mA,
  37. }
  38.  
  39. sensor_1_data = {
  40.     **normal_10V,
  41.     "min_unit": 10 * reg.cm,
  42.     "max_unit": 100 * reg.cm,
  43. }
  44.  
  45. sensor_2_data = {
  46.     **normal_10V,
  47.     "min_unit": 10 * reg.mm,
  48.     "max_unit": 100 * reg.mm,
  49. }
  50.  
  51.  
  52. sensor_3_data = {
  53.     **current_4_20,
  54.     "min_unit": 0 * reg.bar,
  55.     "max_unit": 100 * reg.bar,
  56. }
  57.  
  58.  
  59. input_voltage = 5 * reg.V
  60. input_current = 14 * reg.mA
  61. input_current_max = current_4_20["max_value"]
  62.  
  63. sensor_1 = partial(scale, **sensor_1_data)
  64. sensor_2 = partial(scale, **sensor_2_data)
  65. pressure_sensor = partial(scale, **sensor_3_data)
  66.  
  67. distance_1 = sensor_1(input_voltage)
  68. distance_2 = sensor_2(input_voltage)
  69. pressure = pressure_sensor(input_current)
  70.  
  71. print("Input voltage:", input_voltage)
  72. print("sensor 1:", distance_1)
  73. print("sensor 2:", distance_2)
  74. print()
  75. print("Input current:", input_current)
  76. print("pressure sensor:", pressure)
  77. print()
  78. print("Input current:", input_current_max)
  79. print("pressure sensor:", pressure_sensor(input_current_max))
  80.  
RAW Paste Data