PyCNC

Форк
0
/
test_gmachine.py 
296 строк · 13.4 Кб
1
import unittest
2

3
from cnc.gcode import *
4
from cnc.gmachine import *
5
from cnc.coordinates import *
6
from cnc.heater import *
7
from cnc.pid import *
8
from cnc.config import *
9

10

11
class TestGMachine(unittest.TestCase):
12
    def setUp(self):
13
        Pid.FIX_TIME_S = 0.01
14
        Heater.LOOP_INTERVAL_S = 0.001
15

16
    def tearDown(self):
17
        pass
18

19
    def test_reset(self):
20
        # reset() resets all configurable from gcode things.
21
        m = GMachine()
22
        m.do_command(GCode.parse_line("G20"))
23
        m.do_command(GCode.parse_line("G91"))
24
        m.do_command(GCode.parse_line("X1Y1Z1"))
25
        m.reset()
26
        m.do_command(GCode.parse_line("X3Y4Z5E6"))
27
        self.assertEqual(m.position(), Coordinates(3, 4, 5, 6))
28

29
    def test_safe_zero(self):
30
        m = GMachine()
31
        m.do_command(GCode.parse_line("X1Y2Z3E4"))
32
        m.safe_zero()
33
        self.assertEqual(m.position(), Coordinates(0, 0, 0, 4))
34

35
    def test_none(self):
36
        # GMachine must ignore None commands, since GCode.parse_line()
37
        # returns None if no gcode found in line.
38
        m = GMachine()
39
        m.do_command(None)
40
        self.assertEqual(m.position(), Coordinates(0, 0, 0, 0))
41

42
    def test_unknown(self):
43
        # Test commands which doesn't exists
44
        m = GMachine()
45
        self.assertRaises(GMachineException,
46
                          m.do_command, GCode.parse_line("G99699X1Y2Z3"))
47
        self.assertRaises(GMachineException,
48
                          m.do_command, GCode.parse_line("M99699"))
49

50
    # Test gcode commands.
51
    def test_g0_g1(self):
52
        m = GMachine()
53
        m.do_command(GCode.parse_line("G0X10Y10Z11"))
54
        self.assertEqual(m.position(), Coordinates(10, 10, 11, 0))
55
        m.do_command(GCode.parse_line("G0X3Y2Z1E-2"))
56
        self.assertEqual(m.position(), Coordinates(3, 2, 1, -2))
57
        m.do_command(GCode.parse_line("G1X1Y2Z3E4"))
58
        self.assertEqual(m.position(), Coordinates(1, 2, 3, 4))
59
        self.assertRaises(GMachineException,
60
                          m.do_command, GCode.parse_line("G1F-1"))
61
        self.assertRaises(GMachineException,
62
                          m.do_command, GCode.parse_line("G1X-1Y0Z0"))
63
        self.assertRaises(GMachineException,
64
                          m.do_command, GCode.parse_line("G1X0Y-1Z0"))
65
        self.assertRaises(GMachineException,
66
                          m.do_command, GCode.parse_line("G1X0Y0Z-1"))
67

68
    def test_feed_rate(self):
69
        PulseGenerator.AUTO_VELOCITY_ADJUSTMENT = False
70
        m = GMachine()
71
        self.assertRaises(GMachineException,
72
                          m.do_command, GCode.parse_line("G1X1F-1"))
73
        cl = "G1X1F" + str(MIN_VELOCITY_MM_PER_MIN - 0.0000001)
74
        self.assertRaises(GMachineException, m.do_command,
75
                          GCode.parse_line(cl))
76
        m.do_command(GCode.parse_line("G1X100F"
77
                                      + str(MAX_VELOCITY_MM_PER_MIN_X)))
78
        m.do_command(GCode.parse_line("G1Y100F"
79
                                      + str(MAX_VELOCITY_MM_PER_MIN_Y)))
80
        m.do_command(GCode.parse_line("G1Z100F"
81
                                      + str(MAX_VELOCITY_MM_PER_MIN_Z)))
82
        m.do_command(GCode.parse_line("G1E100F"
83
                                      + str(MAX_VELOCITY_MM_PER_MIN_E)))
84
        self.assertRaises(GMachineException,
85
                          m.do_command, GCode.parse_line("G1X0F999999"))
86
        s = "G1X0F" + str(MAX_VELOCITY_MM_PER_MIN_X + 1)
87
        self.assertRaises(GMachineException, m.do_command, GCode.parse_line(s))
88
        s = "G1Y0F" + str(MAX_VELOCITY_MM_PER_MIN_Y + 1)
89
        self.assertRaises(GMachineException, m.do_command, GCode.parse_line(s))
90
        s = "G1Z0F" + str(MAX_VELOCITY_MM_PER_MIN_Z + 1)
91
        self.assertRaises(GMachineException, m.do_command, GCode.parse_line(s))
92
        s = "G1E0F" + str(MAX_VELOCITY_MM_PER_MIN_E + 1)
93
        self.assertRaises(GMachineException, m.do_command, GCode.parse_line(s))
94
        PulseGenerator.AUTO_VELOCITY_ADJUSTMENT = True
95
        m.do_command(GCode.parse_line("G1X10Y10Z10F9999999999999999999"))
96
        m.do_command(GCode.parse_line("G2I0.1F9999999999999999999"))
97
        m.do_command(GCode.parse_line("G2I10F9999999999999999999"))
98
        PulseGenerator.AUTO_VELOCITY_ADJUSTMENT = AUTO_VELOCITY_ADJUSTMENT
99

100
    def test_g2_g3(self):
101
        m = GMachine()
102
        self.assertRaises(GMachineException,
103
                          m.do_command, GCode.parse_line("G3I1J1F-1"))
104
        m.do_command(GCode.parse_line("G19"))
105
        self.assertRaises(GMachineException,
106
                          m.do_command, GCode.parse_line("G3I1J0K0"))
107
        m.do_command(GCode.parse_line("G18"))
108
        self.assertRaises(GMachineException,
109
                          m.do_command, GCode.parse_line("G3I0J1K0"))
110
        m.do_command(GCode.parse_line("G17"))
111
        self.assertRaises(GMachineException,
112
                          m.do_command, GCode.parse_line("G3I0J0K1"))
113
        self.assertRaises(GMachineException,
114
                          m.do_command, GCode.parse_line("G2X99999999Y99999999"
115
                                                         "I1J1"))
116
        self.assertRaises(GMachineException,
117
                          m.do_command,
118
                          GCode.parse_line("G2X2Y2Z99999999I1J1"))
119
        self.assertEqual(m.position(), Coordinates(0, 0, 0, 0))
120
        self.assertRaises(GMachineException,
121
                          m.do_command, GCode.parse_line("G2X4Y4I2J2"))
122
        self.assertRaises(GMachineException,
123
                          m.do_command, GCode.parse_line("G3X4Y4I2J2"))
124
        m.do_command(GCode.parse_line("G17"))
125
        m.do_command(GCode.parse_line("G1X1"))
126
        m.do_command(GCode.parse_line("G2J1"))
127
        m.do_command(GCode.parse_line("G3J1"))
128
        self.assertEqual(m.position(), Coordinates(1, 0, 0, 0))
129
        m.do_command(GCode.parse_line("G1X10Y10"))
130
        m.do_command(GCode.parse_line("G2X9I1"))
131
        self.assertEqual(m.position(), Coordinates(9, 10, 0, 0))
132
        m.do_command(GCode.parse_line("G19"))
133
        m.do_command(GCode.parse_line("G1X10Y10Z10"))
134
        m.do_command(GCode.parse_line("G3Y8K1"))
135
        self.assertEqual(m.position(), Coordinates(10, 8, 10, 0))
136
        m.do_command(GCode.parse_line("G17"))
137
        m.do_command(GCode.parse_line("G1X5Y5Z0"))
138
        m.do_command(GCode.parse_line("G2X0Y0Z5I-2J-2"))
139
        self.assertEqual(m.position(), Coordinates(0, 0, 5, 0))
140
        m.do_command(GCode.parse_line("G17"))
141
        m.do_command(GCode.parse_line("G1X90Y90"))
142
        m.do_command(GCode.parse_line("G2X90Y70I-5J-5"))
143
        self.assertEqual(m.position(), Coordinates(90, 70, 5, 0))
144
        m.do_command(GCode.parse_line("G18"))
145
        m.do_command(GCode.parse_line("G1X90Y90Z20E0"))
146
        m.do_command(GCode.parse_line("G2Z20X70I-5K-5E22"))
147
        self.assertEqual(m.position(), Coordinates(70, 90, 20, 22))
148
        m.do_command(GCode.parse_line("G19"))
149
        m.do_command(GCode.parse_line("G1X90Y90Z20"))
150
        m.do_command(GCode.parse_line("G2Y90Z0J-5K-5E27"))
151
        self.assertEqual(m.position(), Coordinates(90, 90, 0, 27))
152

153
    def test_g4(self):
154
        m = GMachine()
155
        st = time.time()
156
        m.do_command(GCode.parse_line("G4P0.5"))
157
        self.assertLess(0.5, time.time() - st)
158
        self.assertRaises(GMachineException,
159
                          m.do_command, GCode.parse_line("G4P-0.5"))
160

161
    def test_g17_g18_g19(self):
162
        m = GMachine()
163
        m.do_command(GCode.parse_line("G19"))
164
        self.assertEqual(m.plane(), PLANE_YZ)
165
        m.do_command(GCode.parse_line("G18"))
166
        self.assertEqual(m.plane(), PLANE_ZX)
167
        m.do_command(GCode.parse_line("G17"))
168
        self.assertEqual(m.plane(), PLANE_XY)
169

170
    def test_g20_g21(self):
171
        m = GMachine()
172
        m.do_command(GCode.parse_line("G20"))
173
        m.do_command(GCode.parse_line("X3Y2Z1E0.5"))
174
        self.assertEqual(m.position(), Coordinates(76.2, 50.8, 25.4, 12.7))
175
        m.do_command(GCode.parse_line("G21"))
176
        m.do_command(GCode.parse_line("X3Y2Z1E0.5"))
177
        self.assertEqual(m.position(), Coordinates(3, 2, 1, 0.5))
178

179
    def test_g90_g91(self):
180
        m = GMachine()
181
        m.do_command(GCode.parse_line("G91"))
182
        m.do_command(GCode.parse_line("X1Y1Z1E1"))
183
        m.do_command(GCode.parse_line("X1Y1Z1"))
184
        m.do_command(GCode.parse_line("X1Y1"))
185
        m.do_command(GCode.parse_line("X1"))
186
        self.assertEqual(m.position(), Coordinates(4, 3, 2, 1))
187
        m.do_command(GCode.parse_line("X-1Y-1Z-1E-1"))
188
        m.do_command(GCode.parse_line("G90"))
189
        m.do_command(GCode.parse_line("X1Y1Z1E1"))
190
        self.assertEqual(m.position(), Coordinates(1, 1, 1, 1))
191

192
    def test_g53_g92(self):
193
        m = GMachine()
194
        m.do_command(GCode.parse_line("G92X100Y100Z100E100"))
195
        m.do_command(GCode.parse_line("X101Y102Z103E104"))
196
        self.assertEqual(m.position(), Coordinates(1, 2, 3, 4))
197
        m.do_command(GCode.parse_line("G92X-1Y-1Z-1E-1"))
198
        m.do_command(GCode.parse_line("X1Y1Z1E1"))
199
        self.assertEqual(m.position(), Coordinates(3, 4, 5, 6))
200
        m.do_command(GCode.parse_line("G92X3Y4Z5E6"))
201
        m.do_command(GCode.parse_line("X0Y0Z0E0"))
202
        self.assertEqual(m.position(), Coordinates(0, 0, 0, 0))
203
        m.do_command(GCode.parse_line("X1Y2Z3E4"))
204
        self.assertEqual(m.position(), Coordinates(1, 2, 3, 4))
205
        m.do_command(GCode.parse_line("G53"))
206
        m.do_command(GCode.parse_line("X6Y7Z8E9"))
207
        self.assertEqual(m.position(), Coordinates(6, 7, 8, 9))
208
        m.do_command(GCode.parse_line("G92E0"))
209
        m.do_command(GCode.parse_line("X6Y7Z8E1"))
210
        self.assertEqual(m.position(), Coordinates(6, 7, 8, 10))
211
        m.do_command(GCode.parse_line("G92"))
212
        m.do_command(GCode.parse_line("X1Y1Z1E1"))
213
        self.assertEqual(m.position(), Coordinates(7, 8, 9, 11))
214

215
    def test_g53_g91_g92(self):
216
        m = GMachine()
217
        m.do_command(GCode.parse_line("G92X-50Y-60Z-70E-80"))
218
        m.do_command(GCode.parse_line("X-45Y-55Z-65E-75"))
219
        self.assertEqual(m.position(), Coordinates(5, 5, 5, 5))
220
        m.do_command(GCode.parse_line("G91"))
221
        m.do_command(GCode.parse_line("X-1Y-2Z-3E-4"))
222
        self.assertEqual(m.position(), Coordinates(4, 3, 2, 1))
223

224
    def test_m3_m5(self):
225
        m = GMachine()
226
        m.do_command(GCode.parse_line("M3S" + str(SPINDLE_MAX_RPM)))
227
        self.assertRaises(GMachineException,
228
                          m.do_command, GCode.parse_line("M3S-10"))
229
        self.assertRaises(GMachineException,
230
                          m.do_command, GCode.parse_line("M3S999999999"))
231
        m.do_command(GCode.parse_line("M5"))
232

233
    def test_m104_m109(self):
234
        m = GMachine()
235
        m.do_command(GCode.parse_line("M104S"+str(MIN_TEMPERATURE)))
236
        self.assertEqual(m.extruder_target_temperature(), MIN_TEMPERATURE)
237
        m.do_command(GCode.parse_line("M104S0"))
238
        self.assertEqual(m.extruder_target_temperature(), 0)
239
        # blocking heating should be called with max temperature since virtual
240
        # hal always return this temperature.
241
        m.do_command(GCode.parse_line("M109S" + str(EXTRUDER_MAX_TEMPERATURE)))
242
        self.assertEqual(m.extruder_target_temperature(),
243
                         EXTRUDER_MAX_TEMPERATURE)
244
        m.do_command(GCode.parse_line("M104S0"))
245
        self.assertEqual(m.extruder_target_temperature(), 0)
246
        self.assertRaises(GMachineException, m.do_command,
247
                          GCode.parse_line("M104S"+str(MIN_TEMPERATURE - 1)))
248
        et = EXTRUDER_MAX_TEMPERATURE + 1
249
        self.assertRaises(GMachineException, m.do_command,
250
                          GCode.parse_line("M109S" + str(et)))
251
        self.assertRaises(GMachineException, m.do_command,
252
                          GCode.parse_line("M109"))
253

254
    def test_m106_m107(self):
255
        m = GMachine()
256
        m.do_command(GCode.parse_line("M106"))
257
        self.assertTrue(m.fan_state())
258
        m.do_command(GCode.parse_line("M106S0"))
259
        self.assertFalse(m.fan_state())
260
        m.do_command(GCode.parse_line("M106S123"))
261
        self.assertTrue(m.fan_state())
262
        m.do_command(GCode.parse_line("M107"))
263
        self.assertFalse(m.fan_state())
264
        # check auto fan feature
265
        m.AUTO_FAN_ON = True
266
        m.do_command(GCode.parse_line("M104S" + str(MIN_TEMPERATURE)))
267
        self.assertTrue(m.fan_state())
268
        m.do_command(GCode.parse_line("M104S0"))
269
        self.assertTrue(m.fan_state())
270
        m.do_command(GCode.parse_line("M107"))
271
        self.assertFalse(m.fan_state())
272
        m.AUTO_FAN_ON = False
273

274
    def test_m140_m190(self):
275
        m = GMachine()
276
        m.do_command(GCode.parse_line("M140S"+str(MIN_TEMPERATURE)))
277
        self.assertEqual(m.bed_target_temperature(), MIN_TEMPERATURE)
278
        m.do_command(GCode.parse_line("M140S0"))
279
        self.assertEqual(m.bed_target_temperature(), 0)
280
        # blocking heating should be called with max temperature since virtual
281
        # hal always return this temperature.
282
        m.do_command(GCode.parse_line("M190S" + str(BED_MAX_TEMPERATURE)))
283
        self.assertEqual(m.bed_target_temperature(), BED_MAX_TEMPERATURE)
284
        m.do_command(GCode.parse_line("M190S0"))
285
        self.assertEqual(m.bed_target_temperature(), 0)
286
        self.assertRaises(GMachineException, m.do_command,
287
                          GCode.parse_line("M140S"+str(MIN_TEMPERATURE - 1)))
288
        self.assertRaises(GMachineException, m.do_command,
289
                          GCode.parse_line("M190S"
290
                                           + str(BED_MAX_TEMPERATURE + 1)))
291
        self.assertRaises(GMachineException, m.do_command,
292
                          GCode.parse_line("M190"))
293

294

295
if __name__ == '__main__':
296
    unittest.main()
297

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

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

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

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