PyCNC

Форк
0
/
test_heater.py 
82 строки · 2.4 Кб
1
import unittest
2

3
from cnc.heater import *
4
from cnc.pid import *
5
from cnc.config import *
6

7

8
class TestHeater(unittest.TestCase):
9
    def setUp(self):
10
        self._target_temp = 100
11
        Pid.FIX_TIME_S = 0
12
        Heater.LOOP_INTERVAL_S = 0.001
13
        self._control_counter = 0
14

15
    def tearDown(self):
16
        pass
17

18
    def __get_temperature(self):
19
        return self._target_temp
20

21
    def __get_bad_temperature(self):
22
        return self._target_temp / 2
23

24
    # noinspection PyUnusedLocal
25
    def __control(self, percent):
26
        self._control_counter += 1
27

28
    def test_start_stop(self):
29
        # check if thread stops correctly
30
        he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
31
                    self.__control)
32
        self.assertEqual(self._target_temp, he.target_temperature())
33
        he.stop()
34
        self._control_counter = 0
35
        he.join(5)
36
        self.assertEqual(self._control_counter, 0)
37
        self.assertFalse(he.is_alive())
38

39
    def test_async(self):
40
        # check asynchronous heating
41
        self._control_counter = 0
42
        he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
43
                    self.__control)
44
        j = 0
45
        while self._control_counter < 3:
46
            time.sleep(0.01)
47
            j += 1
48
            if j > 500:
49
                he.stop()
50
                raise Exception("Heater timeout")
51
        he.stop()
52
        self.assertTrue(he.is_fixed())
53

54
    def test_sync(self):
55
        # test wait() method
56
        self._control_counter = 0
57
        he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
58
                    self.__control)
59
        he.wait()
60
        he.stop()
61
        self.assertGreater(self._control_counter, 1)  # one call for stop()
62
        self.assertTrue(he.is_fixed())
63

64
    def test_fail(self):
65
        # check if heater will not fix with incorrect temperature
66
        self._control_counter = 0
67
        he = Heater(self._target_temp, EXTRUDER_PID,
68
                    self.__get_bad_temperature, self.__control)
69
        j = 0
70
        while self._control_counter < 10:
71
            time.sleep(0.01)
72
            j += 1
73
            if j > 500:
74
                he.stop()
75
                raise Exception("Heater timeout")
76
        he.stop()
77
        self.assertGreater(self._control_counter, 10)  # one call for stop()
78
        self.assertFalse(he.is_fixed())
79

80

81
if __name__ == '__main__':
82
    unittest.main()
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.