3
from cnc.heater import *
5
from cnc.config import *
8
class TestHeater(unittest.TestCase):
10
self._target_temp = 100
12
Heater.LOOP_INTERVAL_S = 0.001
13
self._control_counter = 0
18
def __get_temperature(self):
19
return self._target_temp
21
def __get_bad_temperature(self):
22
return self._target_temp / 2
24
# noinspection PyUnusedLocal
25
def __control(self, percent):
26
self._control_counter += 1
28
def test_start_stop(self):
29
# check if thread stops correctly
30
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
32
self.assertEqual(self._target_temp, he.target_temperature())
34
self._control_counter = 0
36
self.assertEqual(self._control_counter, 0)
37
self.assertFalse(he.is_alive())
40
# check asynchronous heating
41
self._control_counter = 0
42
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
45
while self._control_counter < 3:
50
raise Exception("Heater timeout")
52
self.assertTrue(he.is_fixed())
56
self._control_counter = 0
57
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
61
self.assertGreater(self._control_counter, 1) # one call for stop()
62
self.assertTrue(he.is_fixed())
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)
70
while self._control_counter < 10:
75
raise Exception("Heater timeout")
77
self.assertGreater(self._control_counter, 10) # one call for stop()
78
self.assertFalse(he.is_fixed())
81
if __name__ == '__main__':