hyst.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import random
  2. from enum import Enum
  3. from math import fabs
  4. import matplotlib.pyplot as plt
  5. class Hyst(Enum):
  6. HYST_IDLE = 0
  7. HYST_UP = 1
  8. HYST_DOWN = 2
  9. hyst_high_state = Hyst.HYST_IDLE.name
  10. hyst_low_state = Hyst.HYST_IDLE.name
  11. def grapth():
  12. hyst_high_limit = 6.0
  13. hyst_low_limit = 1.0
  14. x = []
  15. in_data = []
  16. hyst_high = []
  17. hyst_low = []
  18. hyst_value_low = []
  19. hyst_value_high = []
  20. for i in range(200):
  21. x.append(i)
  22. in_data.append(random.uniform(0.0, 7.0))
  23. hyst_high.append(hyst_high_limit)
  24. hyst_low.append(hyst_low_limit)
  25. hyst_value_low.append(trig_low_value(in_data[i], hyst_low_limit, 1.5))
  26. hyst_value_high.append(trig_high_value(in_data[i], hyst_high_limit, 1.5))
  27. plt.plot(x, in_data, x, hyst_high, x, hyst_low, x, hyst_value_low, x, hyst_value_high)
  28. plt.title('Тестовые данные для гистерезиса')
  29. plt.xlabel('X')
  30. plt.ylabel('Y')
  31. plt.show()
  32. def trig_low_value(value: float, lowlevel: float, hyst: float):
  33. global hyst_low_state
  34. if hyst_low_state == Hyst.HYST_IDLE.name:
  35. if value <= lowlevel:
  36. hyst_low_state = Hyst.HYST_DOWN.name
  37. return True
  38. else:
  39. return False
  40. elif hyst_low_state == Hyst.HYST_DOWN.name:
  41. if value > (lowlevel + fabs(hyst)):
  42. hyst_low_state = Hyst.HYST_IDLE.name
  43. return False
  44. else:
  45. return True
  46. return False
  47. def trig_high_value(value: float, highlevel: float, hyst: float):
  48. global hyst_high_state
  49. if hyst_high_state == Hyst.HYST_IDLE.name:
  50. if value >= highlevel:
  51. hyst_high_state = Hyst.HYST_UP.name
  52. return True
  53. else:
  54. return False
  55. elif hyst_high_state == Hyst.HYST_UP.name:
  56. if value < (highlevel - fabs(hyst)):
  57. hyst_high_state = Hyst.HYST_IDLE.name
  58. return False
  59. else:
  60. return True
  61. return False
  62. def main():
  63. grapth()
  64. if __name__ == '__main__':
  65. main()