1 |
#!/usr/bin/python |
1 |
#!/usr/bin/python |
2 |
|
2 |
|
3 |
#uncomment for debbug purposes |
3 |
#uncomment for debbug purposes |
4 |
#import logging |
4 |
#import logging |
5 |
#logging.basicConfig(level=logging.DEBUG) |
5 |
#logging.basicConfig(level=logging.DEBUG) |
6 |
|
6 |
|
7 |
import sys |
7 |
import sys |
8 |
import time |
8 |
import time |
9 |
from pymlab import config |
9 |
from pymlab import config |
10 |
|
10 |
|
11 |
class axis: |
11 |
class axis: |
12 |
def __init__(self, SPI_CS, Direction, StepsPerUnit): |
12 |
def __init__(self, SPI_CS, Direction, StepsPerUnit): |
13 |
' One axis of robot ' |
13 |
' One axis of robot ' |
14 |
self.CS = SPI_CS |
14 |
self.CS = SPI_CS |
15 |
self.Dir = Direction |
15 |
self.Dir = Direction |
16 |
self.SPU = StepsPerUnit |
16 |
self.SPU = StepsPerUnit |
17 |
self.Reset() |
17 |
self.Reset() |
18 |
|
18 |
|
19 |
def Reset(self): |
19 |
def Reset(self): |
20 |
' Reset Axis and set default parameters for H-bridge ' |
20 |
' Reset Axis and set default parameters for H-bridge ' |
21 |
spi.SPI_write_byte(self.CS, 0xC0) # reset |
21 |
spi.SPI_write_byte(self.CS, 0xC0) # reset |
22 |
spi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setup |
22 |
spi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setup |
23 |
spi.SPI_write_byte(self.CS, 0x7F) |
23 |
spi.SPI_write_byte(self.CS, 0x7F) |
24 |
spi.SPI_write_byte(self.CS, 0x14) # Over Current Treshold setup |
24 |
spi.SPI_write_byte(self.CS, 0x14) # Over Current Treshold setup |
25 |
spi.SPI_write_byte(self.CS, 0x0F) |
25 |
spi.SPI_write_byte(self.CS, 0x0F) |
26 |
#spi.SPI_write_byte(self.CS, 0x15) # Full Step speed |
26 |
#spi.SPI_write_byte(self.CS, 0x15) # Full Step speed |
27 |
#spi.SPI_write_byte(self.CS, 0x00) |
27 |
#spi.SPI_write_byte(self.CS, 0x00) |
28 |
#spi.SPI_write_byte(self.CS, 0x30) |
28 |
#spi.SPI_write_byte(self.CS, 0x30) |
29 |
#spi.SPI_write_byte(self.CS, 0x0A) # KVAL_RUN |
29 |
#spi.SPI_write_byte(self.CS, 0x0A) # KVAL_RUN |
30 |
#spi.SPI_write_byte(self.CS, 0x50) |
30 |
#spi.SPI_write_byte(self.CS, 0x50) |
31 |
|
31 |
|
32 |
def MaxSpeed(self, speed): |
32 |
def MaxSpeed(self, speed): |
33 |
' Setup of maximum speed ' |
33 |
' Setup of maximum speed ' |
34 |
spi.SPI_write_byte(self.CS, 0x07) # Max Speed setup |
34 |
spi.SPI_write_byte(self.CS, 0x07) # Max Speed setup |
35 |
spi.SPI_write_byte(self.CS, 0x00) |
35 |
spi.SPI_write_byte(self.CS, 0x00) |
36 |
spi.SPI_write_byte(self.CS, speed) |
36 |
spi.SPI_write_byte(self.CS, speed) |
37 |
|
37 |
|
38 |
def ReleaseSW(self): |
38 |
def ReleaseSW(self): |
39 |
' Go away from Limit Switch ' |
39 |
' Go away from Limit Switch ' |
40 |
while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? |
40 |
while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? |
41 |
spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SW |
41 |
spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SW |
42 |
while self.IsBusy(): |
42 |
while self.IsBusy(): |
43 |
pass |
43 |
pass |
44 |
self.MoveWait(10) # move 10 units away |
44 |
self.MoveWait(10) # move 10 units away |
45 |
|
45 |
|
46 |
def GoZero(self, speed): |
46 |
def GoZero(self, speed): |
47 |
' Go to Zero position ' |
47 |
' Go to Zero position ' |
48 |
self.ReleaseSW() |
48 |
self.ReleaseSW() |
49 |
|
49 |
|
50 |
spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zero |
50 |
spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zero |
51 |
spi.SPI_write_byte(self.CS, 0x00) |
51 |
spi.SPI_write_byte(self.CS, 0x00) |
52 |
spi.SPI_write_byte(self.CS, speed) |
52 |
spi.SPI_write_byte(self.CS, speed) |
53 |
while self.IsBusy(): |
53 |
while self.IsBusy(): |
54 |
pass |
54 |
pass |
55 |
time.sleep(0.3) |
55 |
time.sleep(0.3) |
56 |
self.ReleaseSW() |
56 |
self.ReleaseSW() |
57 |
|
57 |
|
58 |
def Move(self, units): |
58 |
def Move(self, units): |
59 |
' Move some distance units from current position ' |
59 |
' Move some distance units from current position ' |
60 |
steps = units * self.SPU # translate units to steps |
60 |
steps = units * self.SPU # translate units to steps |
61 |
if steps > 0: # look for direction |
61 |
if steps > 0: # look for direction |
62 |
spi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1)) |
62 |
spi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1)) |
63 |
else: |
63 |
else: |
64 |
spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1)) |
64 |
spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1)) |
65 |
steps = int(abs(steps)) |
65 |
steps = int(abs(steps)) |
66 |
spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF) |
66 |
spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF) |
67 |
spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) |
67 |
spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) |
68 |
spi.SPI_write_byte(self.CS, steps & 0xFF) |
68 |
spi.SPI_write_byte(self.CS, steps & 0xFF) |
69 |
|
69 |
|
70 |
def MoveWait(self, units): |
70 |
def MoveWait(self, units): |
71 |
' Move some distance units from current position and wait for execution ' |
71 |
' Move some distance units from current position and wait for execution ' |
72 |
self.Move(units) |
72 |
self.Move(units) |
73 |
while self.IsBusy(): |
73 |
while self.IsBusy(): |
74 |
pass |
74 |
pass |
75 |
|
75 |
|
76 |
def Float(self): |
76 |
def Float(self): |
77 |
' switch H-bridge to High impedance state ' |
77 |
' switch H-bridge to High impedance state ' |
78 |
spi.SPI_write_byte(self.CS, 0xA0) |
78 |
spi.SPI_write_byte(self.CS, 0xA0) |
79 |
|
79 |
|
80 |
def ReadStatusBit(self, bit): |
80 |
def ReadStatusBit(self, bit): |
81 |
' Report given status bit ' |
81 |
' Report given status bit ' |
82 |
spi.SPI_write_byte(self.CS, 0x39) # Read from address 0x19 (STATUS) |
82 |
spi.SPI_write_byte(self.CS, 0x39) # Read from address 0x19 (STATUS) |
83 |
spi.SPI_write_byte(self.CS, 0x00) |
83 |
spi.SPI_write_byte(self.CS, 0x00) |
84 |
data0 = spi.SPI_read_byte() # 1st byte |
84 |
data0 = spi.SPI_read_byte() # 1st byte |
85 |
spi.SPI_write_byte(self.CS, 0x00) |
85 |
spi.SPI_write_byte(self.CS, 0x00) |
86 |
data1 = spi.SPI_read_byte() # 2nd byte |
86 |
data1 = spi.SPI_read_byte() # 2nd byte |
87 |
#print hex(data0), hex(data1) |
87 |
#print hex(data0), hex(data1) |
88 |
if bit > 7: # extract requested bit |
88 |
if bit > 7: # extract requested bit |
89 |
OutputBit = (data0 >> (bit - 8)) & 1 |
89 |
OutputBit = (data0 >> (bit - 8)) & 1 |
90 |
else: |
90 |
else: |
91 |
OutputBit = (data1 >> bit) & 1 |
91 |
OutputBit = (data1 >> bit) & 1 |
92 |
return OutputBit |
92 |
return OutputBit |
93 |
|
93 |
|
94 |
|
94 |
|
95 |
def IsBusy(self): |
95 |
def IsBusy(self): |
96 |
""" Return True if tehre are motion """ |
96 |
""" Return True if tehre are motion """ |
97 |
if self.ReadStatusBit(1) == 1: |
97 |
if self.ReadStatusBit(1) == 1: |
98 |
return False |
98 |
return False |
99 |
else: |
99 |
else: |
100 |
return True |
100 |
return True |
101 |
|
101 |
|
102 |
# End Class axis -------------------------------------------------- |
102 |
# End Class axis -------------------------------------------------- |
103 |
|
103 |
|
104 |
|
104 |
|
105 |
|
105 |
|
106 |
|
106 |
|
107 |
|
107 |
|
108 |
cfg = config.Config( |
108 |
cfg = config.Config( |
109 |
i2c = { |
109 |
i2c = { |
110 |
"port": 1, |
110 |
"port": 1, |
111 |
}, |
111 |
}, |
112 |
|
112 |
|
113 |
bus = |
113 |
bus = |
114 |
[ |
114 |
[ |
115 |
{ |
115 |
{ |
116 |
"type": "i2chub", |
116 |
"type": "i2chub", |
117 |
"address": 0x70, |
117 |
"address": 0x70, |
118 |
"children": |
118 |
"children": |
119 |
[ |
119 |
[ |
120 |
{ "name":"spi", "type":"i2cspi", "channel":7} |
120 |
{ "name":"spi", "type":"i2cspi", "channel":7} |
121 |
], |
121 |
], |
122 |
}, |
122 |
}, |
123 |
], |
123 |
], |
124 |
) |
124 |
) |
125 |
|
125 |
|
126 |
|
126 |
|
127 |
cfg.initialize() |
127 |
cfg.initialize() |
128 |
|
128 |
|
129 |
print "Irradiation unit. \r\n" |
129 |
print "Irradiation unit. \r\n" |
130 |
|
130 |
|
131 |
spi = cfg.get_device("spi") |
131 |
spi = cfg.get_device("spi") |
132 |
|
132 |
|
133 |
spi.route() |
133 |
spi.route() |
134 |
|
134 |
|
135 |
|
135 |
|
136 |
try: |
136 |
try: |
137 |
print "SPI configuration.." |
137 |
print "SPI configuration.." |
138 |
spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) |
138 |
spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) |
139 |
|
139 |
|
140 |
print "Robot inicialization" |
140 |
print "Robot inicialization" |
141 |
X = axis(spi.I2CSPI_SS1, 0, 641) |
141 |
X = axis(spi.I2CSPI_SS1, 0, 641) |
142 |
Y = axis(spi.I2CSPI_SS0, 1, 642) |
142 |
Y = axis(spi.I2CSPI_SS0, 1, 642) |
143 |
Z = axis(spi.I2CSPI_SS2, 1, 32256) |
143 |
Z = axis(spi.I2CSPI_SS2, 1, 32256) |
144 |
|
144 |
|
145 |
X.MaxSpeed(35) # max. 50 |
145 |
X.MaxSpeed(35) # max. 50 |
146 |
Y.MaxSpeed(35) # max. 50 |
146 |
Y.MaxSpeed(35) # max. 50 |
147 |
Z.MaxSpeed(20) # max. 30 |
147 |
Z.MaxSpeed(20) # max. 30 |
148 |
|
148 |
|
149 |
Z.GoZero(100) |
149 |
Z.GoZero(100) |
150 |
Y.GoZero(20) |
150 |
Y.GoZero(20) |
151 |
X.GoZero(20) |
151 |
X.GoZero(20) |
152 |
|
152 |
|
153 |
time.sleep(1) |
153 |
time.sleep(1) |
154 |
|
154 |
|
155 |
X.Move(16) |
155 |
X.Move(16) |
156 |
Y.Move(150) |
156 |
Y.Move(150) |
157 |
Z.MoveWait(39) |
157 |
Z.MoveWait(39) |
158 |
time.sleep(1) |
158 |
time.sleep(1) |
159 |
Z.MoveWait(-5) |
159 |
Z.MoveWait(-5) |
160 |
|
160 |
|
161 |
|
161 |
|
162 |
print "Robot is running" |
162 |
print "Robot is running" |
163 |
|
163 |
|
164 |
xcorner = 72 |
164 |
xcorner = 72 |
165 |
xsteps = 9 |
165 |
xsteps = 9 |
166 |
ysteps = 6 # *2 + 1 line |
166 |
ysteps = 6 # *2 + 1 line |
167 |
yy = 0 |
167 |
yy = 0 |
168 |
space = 4 |
168 |
space = 4 |
169 |
grid = 8 |
169 |
grid = 8 |
170 |
delay = 1 #50 |
170 |
delay = 50 |
171 |
|
171 |
|
172 |
X.MoveWait(xcorner) |
172 |
X.MoveWait(xcorner) |
173 |
|
173 |
|
174 |
for y in range(ysteps): |
174 |
for y in range(ysteps): |
175 |
for x in range(xsteps): |
175 |
for x in range(xsteps): |
176 |
print x+1, yy+1 |
176 |
print x+1, yy+1 |
177 |
Z.MoveWait(space) |
177 |
Z.MoveWait(space) |
178 |
time.sleep(delay) |
178 |
time.sleep(delay) |
179 |
Z.MoveWait(-space) |
179 |
Z.MoveWait(-space) |
180 |
if x < (xsteps - 1): |
180 |
if x < (xsteps - 1): |
181 |
X.MoveWait(grid) |
181 |
X.MoveWait(grid) |
182 |
Y.MoveWait(-grid) |
182 |
Y.MoveWait(-grid) |
183 |
yy += 1 |
183 |
yy += 1 |
184 |
for x in range(xsteps): |
184 |
for x in range(xsteps): |
185 |
print x+1, yy+1 |
185 |
print x+1, yy+1 |
186 |
Z.MoveWait(space) |
186 |
Z.MoveWait(space) |
187 |
time.sleep(delay) |
187 |
time.sleep(delay) |
188 |
Z.MoveWait(-space) |
188 |
Z.MoveWait(-space) |
189 |
if x < (xsteps - 1): |
189 |
if x < (xsteps - 1): |
190 |
X.MoveWait(-grid) |
190 |
X.MoveWait(-grid) |
191 |
Y.MoveWait(-grid) |
191 |
Y.MoveWait(-grid) |
192 |
yy += 1 |
192 |
yy += 1 |
193 |
|
193 |
|
194 |
for x in range(xsteps): |
194 |
for x in range(xsteps): |
195 |
print x+1, yy+1 |
195 |
print x+1, yy+1 |
196 |
Z.MoveWait(space) |
196 |
Z.MoveWait(space) |
197 |
time.sleep(delay) |
197 |
time.sleep(delay) |
198 |
Z.MoveWait(-space) |
198 |
Z.MoveWait(-space) |
199 |
if x < (xsteps - 1): |
199 |
if x < (xsteps - 1): |
200 |
X.MoveWait(grid) |
200 |
X.MoveWait(grid) |
201 |
Y.MoveWait(-20) |
201 |
Y.MoveWait(-20) |
202 |
|
202 |
|
203 |
X.MoveWait(-(xcorner+(xsteps-1)*grid)) |
203 |
X.MoveWait(-(xcorner+(xsteps-1)*grid)) |
204 |
#Z.MoveWait(-10) |
204 |
#Z.MoveWait(-10) |
205 |
Y.MoveWait(ysteps*grid*2+20) |
205 |
Y.MoveWait(ysteps*grid*2+20) |
206 |
X.Float() |
206 |
X.Float() |
207 |
Y.Float() |
207 |
Y.Float() |
208 |
Z.Float() |
208 |
Z.Float() |
209 |
|
209 |
|
210 |
|
210 |
|
211 |
# while True: |
211 |
# while True: |
212 |
# print X.ReadStatusBit(2), "end" |
212 |
# print X.ReadStatusBit(2), "end" |
213 |
# time.sleep(1) |
213 |
# time.sleep(1) |
214 |
# sys.exit("STOP") |
214 |
# sys.exit("STOP") |
215 |
|
215 |
|
216 |
finally: |
216 |
finally: |
217 |
print "stop" |
217 |
print "stop" |