Advertisement
DeaD_EyE

Pumpe

Mar 2nd, 2024
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. import time
  2. from enum import Enum
  3. from dataclasses import dataclass, field
  4.  
  5. from rich import get_console
  6.  
  7. from wled import set_level
  8.  
  9.  
  10. console = get_console()
  11.  
  12.  
  13. class Direction(str, Enum):
  14.     a_to_b = "A to B"
  15.     b_to_a = "B to A"
  16.  
  17.  
  18. @dataclass
  19. class Height:
  20.     min : int | float
  21.     max : int | float
  22.     step : int | float = field(default=1)
  23.  
  24.     def __post_init__(self):
  25.         self._value = 50.0
  26.         console.print("Height measurement initialized")
  27.         console.print(f"Min-Height: {self.min} mm")
  28.         console.print(f"Max-Height: {self.max} mm")
  29.         console.print(f"Step: {self.step} mm")
  30.         console.print(f"Value: {self.value:.2f} mm")
  31.  
  32.     @property
  33.     def value(self) -> int | float:
  34.         return self._value
  35.  
  36.     @value.setter
  37.     def value(self, value):
  38.         console.print("Setting new height to:", value)
  39.         self._value = value
  40.  
  41.     @property
  42.     def ok(self) -> bool:
  43.         return self.min <= self.value <= self.max
  44.  
  45.     @property
  46.     def min_ok(self) -> bool:
  47.         return self.min < self.value
  48.  
  49.     @property
  50.     def max_ok(self) -> bool:
  51.         return self.max > self.value
  52.  
  53.     def update(self, direction: Direction | None) -> int | float:
  54.         if direction is Direction.a_to_b:
  55.             self._value -= self.step
  56.         elif direction is Direction.b_to_a:
  57.             self._value += self.step
  58.  
  59.  
  60. @dataclass
  61. class Pumpe:
  62.     name : str
  63.  
  64.     def __post_init__(self):
  65.          self._state = None
  66.          console.print(f"Created {self.name}.")
  67.  
  68.     @property
  69.     def state(self) -> Direction:
  70.         return self._state
  71.  
  72.     def a_to_b(self):
  73.         self._state = Direction.a_to_b
  74.         console.print(f"{self.name}: {self._state.value}")
  75.  
  76.     def b_to_a(self):
  77.         self._state = Direction.b_to_a
  78.         console.print(f"{self.name}: {self._state.value}")
  79.  
  80.     def stop(self):
  81.         self._state = None
  82.         console.print(f"{self.name}: Stop")
  83.  
  84.  
  85.  
  86. def main():
  87.     delay = 0.01
  88.     step_size = (140 / 14) * delay
  89.     console.print(f"Simulation with step_size of {step_size:.2f} and a delay of {delay:.3f}s")
  90.  
  91.     height = Height(min=15.0, max=100.0, step=step_size)
  92.     height.value = 100.0
  93.  
  94.     pumpe = Pumpe("Pupe 1")
  95.  
  96.     console.print("Starting main loop")
  97.     step = 0
  98.     timer = 1.0
  99.  
  100.     while True:
  101.         time.sleep(delay)
  102.  
  103.         height.update(pumpe.state)
  104.         console.print(f"Height: {height.value:.2f}")
  105.         console.print("\nStep:", step)  
  106.         set_level(height.value, min_value=0.0, max_value=140.0)
  107.  
  108.         #  0 [      start  ] -> Pumpe stoppen | ->  1
  109.  
  110.         #              Verzweigung
  111.         #  1 [      max_ok ] -> Von b nach a  | -> 10
  112.         #  1 [ not max_ok  ] -> Von a nach b  | -> 20
  113.  
  114.         #              von b nach a
  115.         # 10 [ not max_ok  ] -> Pumpe stoppen | -> 11
  116.         # 11 [ time 4 s    ] ->               | -> 20
  117.  
  118.         #              von a nach b
  119.         # 20 [             ] -> Von a nach b  | -> 21
  120.         # 21 [  not min_ok ] -> Pumpe stoppen | -> 30
  121.  
  122.         # 30 [             ] ->               | ->  0
  123.  
  124.         match step:
  125.             case 0:
  126.                 step = 1
  127.                 pumpe.stop()
  128.                 input("Enter to start: ")
  129.  
  130.             case 1 if height.max_ok:
  131.                 step = 10
  132.                 pumpe.b_to_a()
  133.  
  134.             case 1 if not height.max_ok:
  135.                 step = 20
  136.                 pumpe.a_to_b()
  137.  
  138.             case 10 if not height.max_ok:
  139.                 step = 11
  140.                 pumpe.stop()
  141.  
  142.             case 11:
  143.                 step = 20
  144.                 console.print(f"Delay of {timer}s")
  145.                 time.sleep(timer)
  146.  
  147.             case 20:
  148.                 step = 21
  149.                 pumpe.a_to_b()
  150.  
  151.             case 21 if not height.min_ok:
  152.                 step = 30
  153.                 pumpe.stop()
  154.  
  155.             case 30:
  156.                 step = 0
  157.  
  158.  
  159. if __name__ == "__main__":
  160.     console.print("Starting simulation")
  161.     try:
  162.         main()
  163.     except KeyboardInterrupt:
  164.         print()
  165.  
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement