-
Notifications
You must be signed in to change notification settings - Fork 0
/
machining.py
307 lines (268 loc) · 7.8 KB
/
machining.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
import json
import numpy as np
import serial
import time
import pdb
"""
=============================
==== PRIVATE FUNCTIONS ====
=============================
"""
def _confirmingReturnMessage(tg):
isConfirmed = False
message = tg.readlines()
for line in message:
line_split = line.split('"')
if (len(line_split)>1 and line_split[1]=='r' ):
isConfirmed = True
return isConfirmed
def _setValue(tg, config, value):
print('Sets: "'+str(config)+'" to '+str(value))
tg.write('{"'+str(config)+'":'+str(value)+'}\n')
time.sleep(0.1)
return tg.readlines()
def _setGcodeValue(tg, config):
print('Sets: "'+str(config)+'"')
tg.write('{"gc":"'+str(config)+'"}\n')
time.sleep(0.1)
return tg.readlines()
def _readValue(tg, config):
tg.write('{'+str(config)+':null}\n')
#time.sleep(0.1)
replies = tg.readlines()
for reply in replies:
dict_reply = json.loads(reply.rstrip())
if 'r' in dict_reply:
return dict_reply['r'][config]
"""
dict_reply = json.loads(reply)
print('dict_reply: ', dict_reply)
value = dict_reply['r'][config]
print('Reads value: "'+str(config)+'" as '+ str(value))
return value
"""
def _move(tg, x, y, z, f):
tg.write('{"gc":"g1 x+'+str(x)+' y'+str(y)+' z'+str(z)+' f'+str(f)+'"}\n')
_printStatusReport(tg)
time.sleep(0.1)
_setGcodeValue(tg,'M2')
time.sleep(0.1)
return tg.readline()
def _move_x(tg, x, f):
tg.write('{"gc":"g1 x+'+str(x)+' f'+str(f)+'"}\n')
_printStatusReport(tg)
time.sleep(0.05)
return tg.readline()
def _move_y(tg, y, f):
tg.write('{"gc":"g1 y+'+str(y)+' f'+str(f)+'"}\n')
_printStatusReport(tg)
time.sleep(0.05)
return tg.readline()
def _move_xy(tg, x, y, f):
tg.write('{"gc":"g1 x+'+str(x)+' y'+str(y)+' f'+str(f)+'"}\n')
_printStatusReport(tg)
time.sleep(0.05)
return tg.readline()
def _move_z(tg, z, f):
tg.write('{"gc":"g1 z+'+str(z)+' f'+str(f)+'"}\n')
_printStatusReport(tg)
time.sleep(0.05)
return tg.readline()
def _disableAmaxLimitSwitch(tg):
_setValue(tg, 'asx', 0)
value = _readValue(tg, 'asx')
if value == 0:
print('Success!')
if value != 0:
print('Panic wrong value set!')
def _enableAmaxLimitSwitch(tg):
_setValue(tg, 'asx', 2)
value = _readValue(tg, 'asx')
if value == 2:
print('Success!')
if value != 2:
print('Panic wrong value set!')
def _setGcodeValues(tg):
_setGcodeValue(tg, "G40")
_setGcodeValue(tg, "G49")
_setGcodeValue(tg, "G80")
_setGcodeValue(tg, "G54")
_setGcodeValue(tg, "G90")
_setGcodeValue(tg, "G21")
_setGcodeValue(tg, "M3 S1000")
def _setGcodeMovesFromFile(tg, fname):
tg.flush()
with open(fname) as f:
lines = f.readlines()
for i in range(4):
tg.write(lines[i])
print(lines[i].rstrip())
index = 4
len_lines = len(lines)
while True:
report = tg.readline()
print(report.rstrip())
if report.split('"')[1] == 'r':
tg.flush()
print(lines[index].rstrip())
print('Adding line: ' + str(index) + ' of ' + str(len_lines) + ' ' + str(100*index/len_lines) + '%')
tg.write(lines[index])
index = index + 1
if index == len(lines) - 1:
break
def _connect():
print('connecting...')
tg = serial.Serial('/dev/ttyUSB0', baudrate=115200, timeout=2, rtscts=True, xonxoff=False)
time.sleep(1)
tg.write('$ej=1\n')
time.sleep(0.2)
lines = tg.readlines()
for line in lines:
print(line.rstrip())
if len(lines)>0:
print('...we\'re in\n')
return tg
def _disconnect(tg):
print('disconnecting...\n')
tg.close()
def _printStatusReport(tg):
tg.write('{"sr":"n"}\n')
message = tg.readline()
while message != '':
print(message.rstrip())
message = tg.readline()
time.sleep(0.05)
print
def _millControl(command):
tg = _connect()
if (command.split()[0]=='G1' or command.split()[0]=='g1'):
val = _setGcodeValue(tg, command)
else:
config = command.split()[0]
if len(command.split())>1:
command = command.split()[1]
else:
command = 'n'
val = _setValue(tg, config, command)
_disconnect(tg)
return val
def _getCurrentPosition():
tg = _connect()
x = _readValue(tg, 'posx')
y = _readValue(tg, 'posy')
z = _readValue(tg, 'posz')
return [x, y, z]
def _hasDoneHoming():
tg = _connect()
val = _readValue(tg, 'home')
_disconnect(tg)
if val==1:
return True
return False
def _getSoftLimits():
tg = _connect()
com = ['xtn', 'xtm', 'ytn', 'ytm', 'ztn', 'ztm']
r_com = [_readValue(tg,c) for c in com]
_disconnect(tg)
return r_com
def _isAtHomePosition():
# getting the home position
with open('settings.json') as f:
home_pos = np.array(json.load(f)['HomingOffset'])
# getting the current position
current_pos = np.array(_getCurrentPosition())
# testing
if np.sum(current_pos - home_pos) == 0:
return True
return False
def _isAtStartPosition():
# getting the home position
with open('settings.json') as f:
start_pos = np.array(json.load(f)['HomingOffset'])
start_pos[1] = 1200
# getting the current position
current_pos = np.array(_getCurrentPosition())
# testing
if np.sum(current_pos - start_pos) == 0:
return True
return False
def _moveToStartPosition():
# reads the homing offset
with open('settings.json') as f:
data = json.load(f)
dx = data['HomingOffset'][0]
dz = data['HomingOffset'][2]
frt = data['FeedrateTransport']
starty = data['StartPositionY']
# move to start position
tg = _connect()
_disableAmaxLimitSwitch(tg)
_move_z(tg, dz, frt)
_move_xy(tg, 0, starty, frt)
_enableAmaxLimitSwitch(tg)
_disconnect(tg)
def _millSide(side):
# reads the homing offset
with open('settings.json') as f:
data = json.load(f)
dx = data['HomingOffset'][0]
dy = data['HomingOffset'][1]
dz = data['HomingOffset'][2]
frt = data['FeedrateTransport']
starty = data['StartPositionY']
tg = _connect()
# sets correct control flow
_setValue(tg, 'ex', 2)
# sets microsteps to None
_setValue(tg, '1mi', 1)
_setValue(tg, '2mi', 1)
_setValue(tg, '3mi', 1)
_setValue(tg, '4mi', 1)
# move to start position
_disableAmaxLimitSwitch(tg)
_move_z(tg, dz, frt)
_move_xy(tg, 0, starty, frt)
_enableAmaxLimitSwitch(tg)
# milling the side
_setGcodeValues(tg)
_setGcodeMovesFromFile(tg, 'cam/'+side+'.gc')
# move to home position (not homing)
_setGcodeValue(tg,'M2')
_move_z(tg, dz, frt)
_disableAmaxLimitSwitch(tg)
_move_xy(tg, dx, dy, frt)
_disconnect(tg)
"""
=============================
==== PUBLIC FUNCTIONS ====
=============================
"""
# --- moves --- #
def homing():
# moving home
tg = _connect()
_disableAmaxLimitSwitch(tg)
tg.write('{"gc":"g28.2 x0 y0 z0"}')
_printStatusReport(tg)
# reads the setting.json file
with open('settings.json') as f:
data = json.load(f)
# sets the homing offset
homingOffset = data['HomingOffset']
dx = homingOffset[0]
dy = homingOffset[1]
dz = homingOffset[2]
print('Homing Offset = ', dx, dy, dz)
tg.write('{"gc":"g28.3 x'+str(dx)+' y'+str(dy)+' z'+str(dz)+'"}')
_printStatusReport(tg)
_disconnect(tg)
def millDeck():
if _hasDoneHoming():
_millSide('deck')
return 'Milling done.'
return 'Homing not done.'
def millBottom():
if _hasDoneHoming():
_millSide('bottom')
return 'Milling done.'
return 'Homing not done.'