forked from Alasterer/kkengraver_enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEST1.py
322 lines (301 loc) · 16.3 KB
/
TEST1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python3
########################################################################
# Copyright 2019 Bernd Breitenbach
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
#
########################################################################
# from engraver_B_b1 import main as laser
import sys
#import os
import subprocess
import time
global COM_port
global laser_matrix
laser_matrix = {}
def showComPortsAvailable():
import serial.tools.list_ports
for i in serial.tools.list_ports.comports():
print(i)
def laser(command):
global COM_port
#os.system('"python engraver_B_b2.py -d' + COM_port + ' -v ' + command +'"')
subprocess.call('python engraver_B_b2.py -d' + COM_port + ' ' + command, shell=True)
# def laser_separate_window(command):
# global COM_port
# p = subprocess.Popen('t3.bat', creationflags=subprocess.CREATE_NEW_CONSOLE)
def show_laser_area(x, y):
# laser('-H') # HOME = python engraver_v4.py -d COM9 -v -H
laser('-m ' + str(x) + 'mm:0mm')
laser('-m 0:' + str(y) + 'mm')
laser('-m " -' + str(x) + 'mm:0mm"')
laser('-m " 0mm:-' + str(y) + 'mm"')
def calcDiagonalMatrix(tiles_per_dimension, width_of_tile, power_depth_delta):
global laser_matrix
#laser_matrix[0] = {'x': 0, 'y': 0, 'power': 10, 'depth': 10, 'path': '-m " ' + str(0) + 'mm:' + str(width_of_tile) + 'mm"'}
current_element_nr = 0
previous_element_nr = 0
path = ''
for y in range(tiles_per_dimension):
for x in range(tiles_per_dimension):
# print('current: x / y: {} / {}'.format(x, y))
if x == (tiles_per_dimension-1):
if y == (tiles_per_dimension-1):
path = '-m " -' + str((tiles_per_dimension-1) * width_of_tile) + 'mm:-' + str((tiles_per_dimension-1) * width_of_tile) + 'mm"'
else:
path = '-m " -' + str(((tiles_per_dimension-2)-y) * width_of_tile) + 'mm:' + str(((tiles_per_dimension-1)-y) * width_of_tile) + 'mm"'
elif y == 0:
path = '-m " -' + str(x * width_of_tile) + 'mm:' + str((x+1) * width_of_tile) + 'mm"'
else:
path = '-m " ' + str(width_of_tile) + 'mm:-' + str(width_of_tile) + 'mm"'
if x+y <= (tiles_per_dimension-1): # top diagonal of matrix
if x == 0:
# print('A')
current_element_nr = int(((y+1)/2)*y)
# print('x / y: {} / {}'.format(x, y))
# print('current_element_nr: ', current_element_nr)
# print('previous_element_nr: ', previous_element_nr)
else:
# print('B')
current_element_nr = previous_element_nr + x + y + 1
# print('x / y: {} / {}'.format(x, y))
# print('current_element_nr: ', current_element_nr)
# print('previous_element_nr: ', previous_element_nr)
laser_matrix[current_element_nr] = {'x': x, 'y': y, 'power': (x+1)*power_depth_delta, 'depth': (y+1)*power_depth_delta, 'path': path}
else: # bottom diagonal of matrix
# print('c')
current_element_nr = previous_element_nr - x - y + (2 * tiles_per_dimension)
# print('x / y: {} / {}'.format(x, y))
# print('current_element_nr: ', current_element_nr)
# print('previous_element_nr: ', previous_element_nr)
laser_matrix[current_element_nr] = {'x': x, 'y': y, 'power': (x+1)*power_depth_delta, 'depth': (y+1)*power_depth_delta, 'path': path}
previous_element_nr = current_element_nr
return laser_matrix
try:
print('-----------------------------------------------------------------------------------------------')
print('| KKMoon 3000mW laser test pattern program |')
print('-----------------------------------------------------------------------------------------------\n')
ls_choice = input('Do you want to engrave with active laser [Y/n]?... ')
if ls_choice == 'y' or ls_choice == 'Y' or ls_choice == '':
laser_active_mode = True # for testing: laser_active_mode = False - for active engraving: laser_active_mode = True
print('--> Laser will be used')
else:
laser_active_mode = False # for testing: laser_active_mode = False - for active engraving: laser_active_mode = True
print('--> Laser will be deactivated and only dummy movements and drawing frames will be made')
# laser_active_mode = True # for testing: laser_active_mode = False - for active engraving: laser_active_mode = True
global COM_port
print('COM ports available')
print('-------------------')
showComPortsAvailable()
COM_port_number = input('Please select COM port number [1 to 256] or tty device (complete path) : ')
try:
COM_port_number = int(COM_port_number)
except:
None
if isinstance(COM_port_number, int):
if COM_port_number >= 1 and COM_port_number <= 256:
COM_port = 'COM' + str(COM_port_number)
else:
print('COM port number not correct. Testing if COM9 is working')
COM_port = 'COM9'
else:
print('COM port number not in Windows format. Using what was entered')
COM_port = COM_port_number
print('Port ' + COM_port + ' selected')
# laser('--fan') # Fan ON
laser('--no-fan') # Fan OFF
# TEST1 = Power and Depth test (from 10% to 100% in 10% steps)
# x = power axis
# y = depth axis
print('-----------------------------------------------------------------------------------------------')
print('| Menu for testing laser engraver |')
print('-----------------------------------------------------------------------------------------------')
print('Please select menu option:')
print('')
print('Engraving test pattern:')
print(' A. Burn test pattern Power vs. Depth, 80x80mm, 10x10 tiles, 8x8mm per tile, in steps of 10%')
print(' B. Burn test pattern Power vs. Depth, 40x40mm, 10x10 tiles, 4x4mm per tile, in steps of 10%')
print(' C. Burn test pattern Power vs. Depth, 30x30mm, 10x10 tiles, 3x3mm per tile, in steps of 10%')
print(' D. Burn test pattern Power vs. Depth, 40x40mm, 5x5 tiles, 8x8mm per tile, in steps of 20%')
print(' E. Burn test pattern Power vs. Depth, 20x20mm, 5x5 tiles, 4x4mm per tile, in steps of 20%')
print('')
print('Cutting test pattern:')
print(' 1. Cut test pattern of 40x40mm size, 5x5 tiles, 8x8mm per tile, one more pass per tile, 1px cut line')
print(' 2. Cut test pattern of 40x40mm size, 5x5 tiles, 8x8mm per tile, one more pass per tile, 2px cut line')
print(' 3. Cut test pattern of 40x40mm size, 5x5 tiles, 8x8mm per tile, one more pass per tile, 3px cut line')
print(' 4. Cut test pattern of 20x20mm size, 5x5 tiles, 4x4mm per tile, one more pass per tile, 1px cut line')
print(' 5. Cut test pattern of 20x20mm size, 5x5 tiles, 4x4mm per tile, one more pass per tile, 2px cut line')
print(' 6. Cut test pattern of 20x20mm size, 5x5 tiles, 4x4mm per tile, one more pass per tile, 3px cut line')
print('')
print(' Q. Quit')
print('')
menu_selection = input('> ')
mode = 'none'
if menu_selection == 'A' or menu_selection == 'a':
mode = 'engrave'
# depth = 10
# power = 10
delta = 10 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 8 # in mm
x_y_amount_tiles = 10
laser('-H') # Home machine
laser('-F TEST1_background_80x80mm.png')
engrave_image = 'TEST1_8x8mm.png'
elif menu_selection == 'B' or menu_selection == 'b':
mode = 'engrave'
# depth = 20
# power = 20
delta = 10 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 4 # in mm
x_y_amount_tiles = 10
laser('-H') # Home machine
laser('-F TEST1_background_40x40mm.png')
engrave_image = 'TEST1_4x4mm.png'
elif menu_selection == 'C' or menu_selection == 'c':
mode = 'engrave'
# depth = 20
# power = 20
delta = 10 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 3 # in mm
x_y_amount_tiles = 10
laser('-H') # Home machine
laser('-F TEST1_background_30x30mm.png')
engrave_image = 'TEST1_3x3mm.png'
elif menu_selection == 'D' or menu_selection == 'd':
mode = 'engrave'
# depth = 20
# power = 20
delta = 20 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 8 # in mm
x_y_amount_tiles = 5
laser('-H') # Home machine
laser('-F TEST1_background_40x40mm.png')
engrave_image = 'TEST1_8x8mm.png'
elif menu_selection == 'E' or menu_selection == 'e':
mode = 'engrave'
# depth = 20
# power = 20
delta = 20 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 4 # in mm
x_y_amount_tiles = 5
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
engrave_image = 'TEST1_4x4mm.png'
elif menu_selection == 'Z' or menu_selection == 'z':
mode = 'engrave'
# depth = 20
# power = 20
delta = 33 # delta for power and delta for depth (step size from tile to tile)
x_y_delta = 10 # in mm
x_y_amount_tiles = 3
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
engrave_image = 'TEST1_4x4mm.png'
elif menu_selection == '1':
mode = 'cut'
cutting_image = 'TEST2_cutting_8x8mm_1px.png'
x_y_delta = 8 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_40x40mm.png')
elif menu_selection == '2':
mode = 'cut'
cutting_image = 'TEST2_cutting_8x8mm_2px.png'
x_y_delta = 8 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_40x40mm.png')
elif menu_selection == '3':
mode = 'cut'
cutting_image = 'TEST2_cutting_8x8mm_3px.png'
x_y_delta = 8 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_40x40mm.png')
elif menu_selection == '4':
mode = 'cut'
cutting_image = 'TEST2_cutting_4x4mm_1px.png'
x_y_delta = 4 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
elif menu_selection == '5':
mode = 'cut'
cutting_image = 'TEST2_cutting_4x4mm_2px.png'
x_y_delta = 4 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
elif menu_selection == '6':
mode = 'cut'
cutting_image = 'TEST2_cutting_4x4mm_3px.png'
x_y_delta = 4 # in mm
x_y_amount_tiles = 5
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
elif menu_selection == '3':
mode = 'cut'
cutting_image = 'TEST2_cutting_4x4mm.png'
x_y_delta = 10 # in mm
x_y_amount_tiles = 3
delta = 20 # only needed for calcDiagonalMatrix() function # delta for power and delta for depth (step size from tile to tile)
laser('-H') # Home machine
laser('-F TEST1_background_20x20mm.png')
else:
sys.exit("Error: No valid value has been entered! Please try again.\nExiting script.")
resume_choice = input("Do you want to use the previously selected area [y/N]?... ")
if resume_choice == 'y' or resume_choice == 'Y':
calcDiagonalMatrix(x_y_amount_tiles, x_y_delta, delta)
if mode == 'engrave':
laser('--fan') # Fan ON
# laser('-H') # Home machine
for i in range(len(laser_matrix)):
print('engraving: x={}, y={}, power={}, depth={}, tile_number={}, path_to_next_tile={}'.format(laser_matrix[i]['x'], laser_matrix[i]['y'], laser_matrix[i]['power'], laser_matrix[i]['depth'], i+1, laser_matrix[i]['path']))
if laser_active_mode:
laser('-D ' + str(laser_matrix[i]['depth']) + ' -P ' + str(laser_matrix[i]['power']) + ' -i ' + engrave_image) # burn image TEST1.png with depth and power set to the current loop values
else:
time.sleep(0.5)
laser(laser_matrix[i]['path']) # get move command to move to next diagonal tile and perform the operation
elif mode == 'cut':
selected_power = input("Enter power output for cutting [0% to 100%, as integer]?... ")
selected_depth = input("Enter cutting depth for cutting [0% to 100%, as integer]?... ")
if int(selected_power) >= 0 and int(selected_power) <= 100 and int(selected_depth) >= 0 and int(selected_depth) <= 100:
laser('--fan') # Fan ON
# laser('-H') # Home machine
passes=1 # initial amount of cutting passes
for y in range(x_y_amount_tiles):
for x in range(x_y_amount_tiles):
if x > 0:
laser('-m ' + str(x_y_delta) + 'mm:0mm') # move laser in x direction only
for p in range(passes):
print('cutting: x={}, y={}, power={}, depth={}, passes={}'.format(x, y, selected_power, selected_depth, passes))
if laser_active_mode:
laser('-D ' + str(selected_depth) + ' -P ' + str(selected_power) + ' -i ' + cutting_image) # cut line
else:
time.sleep(0.5)
passes = passes + 1
if x == x_y_amount_tiles-1 and y == x_y_amount_tiles-1:
laser('-m " -' + str((x_y_amount_tiles-1)*x_y_delta) + 'mm:-' + str((x_y_amount_tiles-1)*x_y_delta) + 'mm"') # move laser to home without using home command
else:
laser('-m " -' + str((x_y_amount_tiles-1)*x_y_delta) + 'mm:' + str(x_y_delta) + 'mm') # move laser into new line of matrix
else:
print('Value for selected_power=' + str(selected_power) + 'or selected_depth=' + str(selected_depth) + ' is not correct. Please try again with values ranging from 0% to 100% as integer.')
laser('-H') # Home machine
except Exception as e:
print('Exception in TEST')
print(e)