4305 |
kaklik |
1 |
#!/usr/bin/python |
|
|
2 |
# ------------------------------------------- |
|
|
3 |
# HBSTEP01A Stepper Motor control Example |
|
|
4 |
# ------------------------------------------- |
|
|
5 |
|
|
|
6 |
#uncomment for debbug purposes |
|
|
7 |
#import logging |
|
|
8 |
#logging.basicConfig(level=logging.DEBUG) |
|
|
9 |
|
|
|
10 |
import sys |
|
|
11 |
import time |
|
|
12 |
from pymlab import config |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class axis: |
|
|
16 |
def __init__(self, SPI_CS, Direction, StepsPerUnit): |
|
|
17 |
' One axis of robot ' |
|
|
18 |
self.CS = SPI_CS |
|
|
19 |
self.Dir = Direction |
|
|
20 |
self.SPU = StepsPerUnit |
|
|
21 |
self.Reset() |
|
|
22 |
|
|
|
23 |
def Reset(self): |
|
|
24 |
' Reset Axis and set default parameters for H-bridge ' |
|
|
25 |
spi.SPI_write_byte(self.CS, 0xC0) # reset |
|
|
26 |
# spi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setup |
|
|
27 |
# spi.SPI_write_byte(self.CS, 0xFF) |
|
|
28 |
# spi.SPI_write_byte(self.CS, 0x13) # Over Current Treshold setup |
|
|
29 |
# spi.SPI_write_byte(self.CS, 0xFF) |
|
|
30 |
#spi.SPI_write_byte(self.CS, 0x15) # Full Step speed |
|
|
31 |
#spi.SPI_write_byte(self.CS, 0x00) |
|
|
32 |
#spi.SPI_write_byte(self.CS, 0x30) |
|
|
33 |
#spi.SPI_write_byte(self.CS, 0x0A) # KVAL_RUN |
|
|
34 |
#spi.SPI_write_byte(self.CS, 0x50) |
|
|
35 |
spi.SPI_write_byte(self.CS, 0x18) # KVAL_RUN |
|
|
36 |
spi.SPI_write_byte(self.CS, 0b11000000) |
|
|
37 |
spi.SPI_write_byte(self.CS, 0x88) |
|
|
38 |
|
|
|
39 |
def MaxSpeed(self, speed): |
|
|
40 |
' Setup of maximum speed ' |
|
|
41 |
spi.SPI_write_byte(self.CS, 0x07) # Max Speed setup |
|
|
42 |
spi.SPI_write_byte(self.CS, 0x00) |
|
|
43 |
spi.SPI_write_byte(self.CS, speed) |
|
|
44 |
|
|
|
45 |
def ReleaseSW(self): |
|
|
46 |
' Go away from Limit Switch ' |
|
|
47 |
while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? |
|
|
48 |
spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SW |
|
|
49 |
while self.IsBusy(): |
|
|
50 |
pass |
|
|
51 |
self.MoveWait(10) # move 10 units away |
|
|
52 |
|
|
|
53 |
def GoZero(self, speed): |
|
|
54 |
' Go to Zero position ' |
|
|
55 |
self.ReleaseSW() |
|
|
56 |
|
|
|
57 |
spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zero |
|
|
58 |
spi.SPI_write_byte(self.CS, 0x00) |
|
|
59 |
spi.SPI_write_byte(self.CS, speed) |
|
|
60 |
while self.IsBusy(): |
|
|
61 |
pass |
|
|
62 |
time.sleep(0.3) |
|
|
63 |
self.ReleaseSW() |
|
|
64 |
|
|
|
65 |
def Move(self, units): |
|
|
66 |
' Move some distance units from current position ' |
|
|
67 |
steps = units * self.SPU # translate units to steps |
|
|
68 |
if steps > 0: # look for direction |
|
|
69 |
spi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1)) |
|
|
70 |
else: |
|
|
71 |
spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1)) |
|
|
72 |
steps = int(abs(steps)) |
|
|
73 |
spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF) |
|
|
74 |
spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) |
|
|
75 |
spi.SPI_write_byte(self.CS, steps & 0xFF) |
|
|
76 |
|
|
|
77 |
def MoveWait(self, units): |
|
|
78 |
' Move some distance units from current position and wait for execution ' |
|
|
79 |
self.Move(units) |
|
|
80 |
while self.IsBusy(): |
|
|
81 |
pass |
|
|
82 |
|
|
|
83 |
def Float(self): |
|
|
84 |
' switch H-bridge to High impedance state ' |
|
|
85 |
spi.SPI_write_byte(self.CS, 0xA0) |
|
|
86 |
|
|
|
87 |
def ReadStatusBit(self, bit): |
|
|
88 |
' Report given status bit ' |
|
|
89 |
spi.SPI_write_byte(self.CS, 0x39) # Read from address 0x19 (STATUS) |
|
|
90 |
spi.SPI_write_byte(self.CS, 0x00) |
|
|
91 |
data0 = spi.SPI_read_byte() # 1st byte |
|
|
92 |
spi.SPI_write_byte(self.CS, 0x00) |
|
|
93 |
data1 = spi.SPI_read_byte() # 2nd byte |
|
|
94 |
#print hex(data0), hex(data1) |
|
|
95 |
if bit > 7: # extract requested bit |
|
|
96 |
OutputBit = (data0 >> (bit - 8)) & 1 |
|
|
97 |
else: |
|
|
98 |
OutputBit = (data1 >> bit) & 1 |
|
|
99 |
return OutputBit |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
def IsBusy(self): |
|
|
103 |
""" Return True if tehre are motion """ |
|
|
104 |
if self.ReadStatusBit(1) == 1: |
|
|
105 |
return False |
|
|
106 |
else: |
|
|
107 |
return True |
|
|
108 |
|
|
|
109 |
# End Class axis -------------------------------------------------- |
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
cfg = config.Config( |
|
|
114 |
i2c = { |
|
|
115 |
"port": 1, |
|
|
116 |
}, |
|
|
117 |
|
|
|
118 |
bus = [ |
|
|
119 |
{ |
|
|
120 |
"type": "i2chub", |
|
|
121 |
"address": 0x70, |
|
|
122 |
"children": [ |
|
|
123 |
{ "name":"spi", "type":"i2cspi", "channel": 1, }, |
|
|
124 |
], |
|
|
125 |
}, |
|
|
126 |
], |
|
|
127 |
) |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
cfg.initialize() |
|
|
131 |
|
|
|
132 |
print "Stepper motor control example. \r\n" |
|
|
133 |
|
|
|
134 |
spi = cfg.get_device("spi") |
|
|
135 |
|
|
|
136 |
spi.route() |
|
|
137 |
|
|
|
138 |
try: |
|
|
139 |
print "SPI configuration.." |
|
|
140 |
spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) |
|
|
141 |
time.sleep(1) |
|
|
142 |
|
|
|
143 |
print "Axis inicialization" |
|
|
144 |
X = axis(spi.I2CSPI_SS0, 0, 641) # set Number of Steps per axis Unit and set Direction of Rotation |
|
|
145 |
X.MaxSpeed(2) # set maximal motor speed |
|
|
146 |
|
|
|
147 |
print "Axis is running" |
|
|
148 |
|
|
|
149 |
for i in range(5): |
|
|
150 |
X.MoveWait(50) # move 50 unit forward and wait for motor stop |
|
|
151 |
time.sleep(0.5) |
|
|
152 |
X.MoveWait(-50) # move 50 unit backward and wait for motor stop |
|
|
153 |
time.sleep(0.5) |
|
|
154 |
|
|
|
155 |
X.Float() # release power |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
finally: |
|
|
159 |
print "stop" |