4
from cnc.gmachine import *
5
from cnc.coordinates import *
6
from cnc.heater import *
8
from cnc.config import *
11
class TestGMachine(unittest.TestCase):
14
Heater.LOOP_INTERVAL_S = 0.001
22
m.do_command(GCode.parse_line("G20"))
23
m.do_command(GCode.parse_line("G91"))
24
m.do_command(GCode.parse_line("X1Y1Z1"))
26
m.do_command(GCode.parse_line("X3Y4Z5E6"))
27
self.assertEqual(m.position(), Coordinates(3, 4, 5, 6))
29
def test_safe_zero(self):
31
m.do_command(GCode.parse_line("X1Y2Z3E4"))
33
self.assertEqual(m.position(), Coordinates(0, 0, 0, 4))
40
self.assertEqual(m.position(), Coordinates(0, 0, 0, 0))
42
def test_unknown(self):
45
self.assertRaises(GMachineException,
46
m.do_command, GCode.parse_line("G99699X1Y2Z3"))
47
self.assertRaises(GMachineException,
48
m.do_command, GCode.parse_line("M99699"))
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"))
68
def test_feed_rate(self):
69
PulseGenerator.AUTO_VELOCITY_ADJUSTMENT = False
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,
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
100
def test_g2_g3(self):
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"
116
self.assertRaises(GMachineException,
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))
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"))
161
def test_g17_g18_g19(self):
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)
170
def test_g20_g21(self):
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))
179
def test_g90_g91(self):
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))
192
def test_g53_g92(self):
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))
215
def test_g53_g91_g92(self):
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))
224
def test_m3_m5(self):
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"))
233
def test_m104_m109(self):
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)
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"))
254
def test_m106_m107(self):
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())
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
274
def test_m140_m190(self):
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)
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"))
295
if __name__ == '__main__':