4
from cnc.config import *
7
class TestPid(unittest.TestCase):
9
self._environment_temp = 25
15
self._extruder_c = 0.0108
16
self._extruder_h = 3.4070
21
def __simulate(self, target_temp, pid_c, environment_temp, cool, heat):
27
pid = Pid(target_temp, pid_c, 0)
28
temperature = environment_temp
35
for k in range(1, 20 * 60 * iter_pes_s + 1):
36
j = k / float(iter_pes_s)
38
temperature -= ((temperature - environment_temp) * cool
41
temperature += heat * heater_power / float(iter_pes_s)
42
heater_power = pid.update(temperature, j)
47
self.assertLess(abs(temperature - target_temp),
48
pid.FIX_ACCURACY * target_temp * 5.0,
49
msg="PID failed to control temperature "
50
"{}/{} {}".format(temperature, target_temp, j))
51
if heater_power == 0.0:
54
self.assertLess(abs(temperature - target_temp),
55
pid.FIX_ACCURACY * target_temp,
56
msg="PID failed to control temperature "
57
"{}/{} {}".format(temperature, target_temp, j))
58
self.assertLess(zeros_counter, total_counter * 0.05,
59
msg="PID turns on/off, instead of fine control")
60
self.assertLess(fixed_at, 900,
61
msg="failed to heat in 15 minutes, final temperature "
62
"{}/{}".format(temperature, target_temp))
64
def test_simple(self):
65
pid = Pid(50, EXTRUDER_PID, 0)
66
self.assertEqual(0, pid.update(100, 1))
67
self.assertEqual(1, pid.update(0, 2))
68
pid = Pid(50, BED_PID, 0)
69
self.assertEqual(0, pid.update(100, 1))
70
self.assertEqual(1, pid.update(0, 2))
72
def test_extruder(self):
74
for target in range(150, 251, 10):
75
self.__simulate(target, EXTRUDER_PID, self._environment_temp,
76
self._extruder_c, self._extruder_h)
80
for target in range(50, 101, 10):
81
self.__simulate(target, BED_PID, self._environment_temp,
82
self._bed_c, self._bed_h)
85
if __name__ == '__main__':