-
Notifications
You must be signed in to change notification settings - Fork 3
/
instrument_scan.py
153 lines (119 loc) · 4.02 KB
/
instrument_scan.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
#!/usr/bin/python
# Goal: Use LinuxCNC to move a probe in a rectilinear grid pattern and take measurements at each point
# Precondition: Before running this program, configure LinuxCNC, home axes, touch off probe, configure
# desired units, etc. Failure to do this could lead to damage.
# Written October 2, 2016 by Kent A. Vander Velden <[email protected]>
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
#
#
# If you use this software, please consider contacting me. I'd like to hear
# about your work.
# Documentation for external libraries
# http://linuxcnc.org/docs/2.6/html/common/python-interface.html
# http://linuxcnc.org/docs/2.7/html/config/python-interface.html
# https://github.com/python-ivi/python-vxi11
import sys
import time
import datetime
import linuxcnc
import vxi11
if len(sys.argv) != 1 + 3*3:
print 'usage: {0:s} <xs> <xe> <xd> <ys> <ye> <yd> <zs> <ze> <zd>'.format(sys.argv[0])
sys.exit(1)
args = map(float, sys.argv[1:])
cnc_s = linuxcnc.stat()
cnc_c = linuxcnc.command()
# to calculate homed, we iterate over the axes, finding those that are present on this machine,
# and logically combining their homed state (for LinuxCNC 2.7)
def ok_for_mdi27():
cnc_s.poll()
homed = True
for axis in cnc_s.axis:
homed = homed and ((not axis['enabled']) or (axis['homed'] != 0))
return not cnc_s.estop and cnc_s.enabled and homed and (cnc_s.interp_state == linuxcnc.INTERP_IDLE)
def verify_ok_for_mdi():
if not ok_for_mdi27():
print 'Not ready for MDI commands'
sys.exit(1)
verify_ok_for_mdi()
cnc_c.mode(linuxcnc.MODE_MDI)
cnc_c.wait_complete()
def move_to(x, y, z):
cmd = 'G1 G54 X{0:f} Y{1:f} Z{2:f} f5'.format(x, y, z)
print 'Command,' + cmd
verify_ok_for_mdi()
cnc_c.mdi(cmd)
rv = cnc_c.wait_complete(60)
if rv != 1:
print 'MDI command timed out'
sys.exit(1)
instr = vxi11.Instrument('192.168.0.38')
idn = instr.ask('*IDN?')
if not idn.startswith('Agilent Technologies,34461A'):
print 'Unknown instrument:', idn
sys.exit(1)
instr.write('*RST')
instr.write('CONF:VOLT:DC AUTO,DEF')
# read three values with a small delay between each reading
def sample():
rv = []
for i in range(3):
rv += [instr.ask('READ?')]
time.sleep(.05)
rv = map(float, rv)
return rv
# generate grid points in a zig-zag rectalinear pattern, helper function
# s: vector of start positions for each axis
# d: vector of step size for each axis
# n: vector of number of steps for each axis
# o: vector of 1,-1 controlling direction axis is traversed
# l: recursion depth
# p: vector containing current position
# rv: vector containing accumulated positions
def gen_grid_(s, d, n, o, l, p, rv):
if l == len(p):
pp = [x for x in p]
for i in range(len(pp)):
pp[i] = s[i] + d[i] * pp[i]
rv += [pp]
return
for i in range(n[l]):
gen_grid_(s, d, n, o, l+1, p, rv)
p[l] += o[l]
if o[l] == 1:
o[l] = -1
p[l] = n[l] - 1
elif o[l] == -1:
o[l] = 1
p[l] = 0
# generate grid points in a zig-zag rectalinear pattern
# s: vector of start positions for each axis
# e: vector of end positions for each axis
# d: vector of step size for each axis
# returns: vector containing ordered grid positions
def gen_grid(s, e, d):
n = [1] * len(s)
for i in range(len(n)):
n[i] = int((e[i] - s[i]) / d[i] + 1.5)
grid_pts = []
gen_grid_(s[::-1], d[::-1], n[::-1], [1]*len(s), 0, [0]*len(s), grid_pts)
return grid_pts
s = args[0::3]
e = args[1::3]
d = args[2::3]
grid_pts = gen_grid(s, e, d)
for pp in grid_pts:
z, y, x = pp
move_to(x, y, z)
time.sleep(.1) # settling time
rv = sample()
dt = str(datetime.datetime.now())
print 'Result,' + ','.join(map(str, [dt, x, y, z] + rv))
sys.stdout.flush()