PyCNC

Форк
0
/
test_pid.py 
86 строк · 3.6 Кб
1
import unittest
2

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

6

7
class TestPid(unittest.TestCase):
8
    def setUp(self):
9
        self._environment_temp = 25
10
        # Coefficients below were chosen by an experimental way with a real
11
        # hardware: reprap heating bed and extruder.
12
        # See ../utils/heater_model_finder.py to find out this coefficients.
13
        self._bed_c = 0.0027  # bed cooling coefficient
14
        self._bed_h = 0.2522  # bed heating coefficient
15
        self._extruder_c = 0.0108  # extruder cooling coefficient
16
        self._extruder_h = 3.4070  # extruder heating coefficient
17

18
    def tearDown(self):
19
        pass
20

21
    def __simulate(self, target_temp, pid_c, environment_temp, cool, heat):
22
        # Simulate heating some hypothetical thing with heater(h is a heat
23
        # transfer coefficient, which becomes just a delta temperature each
24
        # second) from environment temperature to target_temp. Consider that
25
        # there is natural air cooling process with some heat transfer
26
        # coefficient c. Heating power is controlled by PID.
27
        pid = Pid(target_temp, pid_c, 0)
28
        temperature = environment_temp
29
        heater_power = 0
30
        fixed_at = None
31
        zeros_counter = 0
32
        total_counter = 0
33
        iter_pes_s = 2  # step is 0.5s
34
        j = 1
35
        for k in range(1, 20 * 60 * iter_pes_s + 1):  # simulate for 20 minutes
36
            j = k / float(iter_pes_s)
37
            # natural cooling
38
            temperature -= ((temperature - environment_temp) * cool
39
                            / float(iter_pes_s))
40
            # heating
41
            temperature += heat * heater_power / float(iter_pes_s)
42
            heater_power = pid.update(temperature, j)
43
            if fixed_at is None:
44
                if pid.is_fixed():
45
                    fixed_at = j
46
            else:
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:
52
                zeros_counter += 1
53
            total_counter += 1
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))
63

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))
71

72
    def test_extruder(self):
73
        # check if extruder typical temperatures can be reached in simulation
74
        for target in range(150, 251, 10):
75
            self.__simulate(target, EXTRUDER_PID, self._environment_temp,
76
                            self._extruder_c, self._extruder_h)
77

78
    def test_bed(self):
79
        # check if bed typical temperatures can be reached in simulation
80
        for target in range(50, 101, 10):
81
            self.__simulate(target, BED_PID, self._environment_temp,
82
                            self._bed_c, self._bed_h)
83

84

85
if __name__ == '__main__':
86
    unittest.main()
87

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

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

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

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