-
Notifications
You must be signed in to change notification settings - Fork 5
/
probe_surface.py
138 lines (119 loc) · 4.55 KB
/
probe_surface.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
#!/usr/bin/env python
#probing uneven surfaces with robots
#by Mike Erickstad using libraries developed by A J Mendez PhD
from numpy import arange
from lib import argv
from lib.grbl_status import GRBL_status
from lib.communicate import Communicate
from clint.textui import puts, colored
import time, readline
import numpy as np
args = argv.arg(description='Simple python grbl surface probe pattern')
DEBUG_VERBOSE = False
X_MAX = 2.5
Y_MAX = 2.0
X_STEP = 0.5
Y_STEP = 0.5
HIGH_Z = 0.020
LOW_Z = -0.030
PROBE_FEED_RATE = 0.2
DESCENT_SPEED = 2.0/60.0
DESCENT_TIME = HIGH_Z/DESCENT_SPEED
Surface_Data = np.empty([len(arange(0, X_MAX+X_STEP/2.0, X_STEP)),len(arange(0, Y_MAX+Y_STEP/2.0, Y_STEP))])
Z=HIGH_Z
converged = False
# get a serial device and wake up the grbl, by sending it some enters
with Communicate(args.device, args.speed, timeout=args.timeout, debug=args.debug, quiet=args.quiet) as serial:
time.sleep(10)
command = "G90"
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
command = "G20"
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
num_x = -1
for X in arange(0, X_MAX+X_STEP/2.0, X_STEP):
num_x=num_x+1
num_y=-1
for Y in arange(0, Y_MAX+Y_STEP/2.0, Y_STEP):
num_y=num_y+1
puts(colored.yellow("going to x:{:.4f} and y:{:.4f}".format(X,Y)))
command = "G0 X{:.4f} Y{:.4f} Z{:.4f}".format(X,Y,HIGH_Z)
if DEBUG_VERBOSE:
print command
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
command = "G38.2 Z{:.4f} F{:.4f}".format(LOW_Z,PROBE_FEED_RATE)
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
converged = False
while not converged:
time.sleep(2)
status_report_string = serial.run('?',singleLine=True)
current_status = GRBL_status().parse_grbl_status(status_report_string)
print ''
puts(colored.yellow(''.join('Z=' + '{:.4f}'.format((float(current_status.get_z()))))))
#print 'z position :'
#print float(current_status.get_z())
if current_status.is_idle():
converged = True
Z=current_status.get_z()
Surface_Data[num_x,num_y] = Z
if current_status.is_alarmed():
print 'PyGRBL: did not detect surface in specified z range, alarm tripped'
serial.run('$X')
serial.run("G0 X{:.4f} Y{:.4f} Z{:.4f}".format(X,Y,HIGH_Z))
break
command = "G0 X{:.4f} Y{:.4f} Z{:.4f}".format(X,Y,HIGH_Z)
if DEBUG_VERBOSE:
print command
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
command = "G0 X{:.4f} Y{:.4f} Z{:.4f}".format(0.0,0.0,HIGH_Z)
if DEBUG_VERBOSE:
print command
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
command = "G0 X{:.4f} Y{:.4f} Z{:.4f}".format(0.0,0.0,0.0)
if DEBUG_VERBOSE:
print command
try:
serial.run(command)
except KeyboardInterrupt:
puts(colored.red('Emergency Feed Hold. Enter "~" to continue'))
serial.run('!\n?')
print Surface_Data
np.savetxt('probe_test.out', Surface_Data, delimiter=',',header='X_STEP:,{:.4f}, Y_STEP:,{:.4f},'.format(X_STEP,Y_STEP))
puts(
colored.green('''
gCode finished streaming!''' +
colored.red('''
!!! WARNING: Please make sure that the buffer clears before finishing...''') )
try:
raw_input('<Press any key to finish>')
raw_input(' Are you sure? Any key to REALLY exit.')
except KeyboardInterrupt as e:
serial.run('!\n?')
puts(colored.red('Emergency Stop! Enter "~" to continue. You can enter gCode to run here as well.'))
while True:
x = raw_input('GRBL> ').strip()
serial.run(x)
if '~' in x: break