Rev 4573 Rev 4574
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # ------------------------------------------- 2 # -------------------------------------------
3 # HBSTEP01B Stepper Motor control test code 3 # HBSTEP01B Stepper Motor control test code
4 # ------------------------------------------- 4 # -------------------------------------------
5 # 5 #
6 # Program uses MLAB Python modules library from https://github.com/MLAB-project/pymlab 6 # Program uses MLAB Python modules library from https://github.com/MLAB-project/pymlab
7   7  
8   8  
9 #uncomment for debbug purposes 9 #uncomment for debbug purposes
10 import logging 10 import logging
11 logging.basicConfig(level=logging.DEBUG) 11 logging.basicConfig(level=logging.DEBUG)
12   12  
13 import sys 13 import sys
14 import time 14 import time
15 import spidev 15 import spidev
16   16  
17 #### Script Arguments ############################################### 17 #### Script Arguments ###############################################
18   18  
19 if len(sys.argv) < 2: 19 if len(sys.argv) < 2:
20 sys.stderr.write("Invalid number of arguments.\n") 20 sys.stderr.write("Invalid number of arguments.\n")
21 sys.stderr.write("Usage: %s PORT ADDRESS SPEED MOVE_DISTANCE\n" % (sys.argv[0], )) 21 sys.stderr.write("Usage: %s PORT ADDRESS SPEED MOVE_DISTANCE\n" % (sys.argv[0], ))
22 sys.exit(1) 22 sys.exit(1)
23   23  
24 elif len(sys.argv) == 2: 24 elif len(sys.argv) == 2:
25 PORT = eval(sys.argv[1]) 25 PORT = eval(sys.argv[1])
26 SPEED = 5 26 SPEED = 5
27 DISTANCE = 50 27 DISTANCE = 50
28   28  
29 elif len(sys.argv) == 3: 29 elif len(sys.argv) == 3:
30 SPEED = eval(sys.argv[2]) 30 SPEED = eval(sys.argv[2])
31 DISTANCE = 100 31 DISTANCE = 100
32   32  
33 elif len(sys.argv) == 4: 33 elif len(sys.argv) == 4:
34 SPEED = eval(sys.argv[2]) 34 SPEED = eval(sys.argv[2])
35 DISTANCE = eval(sys.argv[3]) 35 DISTANCE = eval(sys.argv[3])
36   36  
37 else: 37 else:
38 PORT = 0 38 PORT = 0
39 SPEED = 10 39 SPEED = 10
40 DISTANCE = 50 40 DISTANCE = 50
41   41  
42 class axis: 42 class axis:
43 def __init__(self, SPI_handler, Direction, StepsPerUnit, MaxSpeed): 43 def __init__(self, SPI_handler, Direction, StepsPerUnit, MaxSpeed):
44 ' One axis of robot ' 44 ' One axis of robot '
45 self.spi = SPI_handler 45 self.spi = SPI_handler
46 self.Dir = Direction 46 self.Dir = Direction
47 self.SPU = StepsPerUnit 47 self.SPU = StepsPerUnit
48 self.maxseed = MaxSpeed 48 self.maxseed = MaxSpeed
49 self.Reset() 49 self.Reset()
50 self.Initialize() 50 self.Initialize()
51   51  
52 def Reset(self): 52 def Reset(self):
53 'Reset the Axis' 53 'Reset the Axis'
54 self.spi.xfer([0xC0]) # reset 54 self.spi.xfer([0xC0]) # reset
55   55  
56 def Initialize(self): 56 def Initialize(self):
57 'set default parameters for H-bridge ' 57 'set default parameters for H-bridge '
58 # self.spi.xfer( 0x14) # Stall Treshold setup 58 # self.spi.xfer( 0x14) # Stall Treshold setup
59 # self.spi.xfer( 0xFF) 59 # self.spi.xfer( 0xFF)
60 # self.spi.xfer( 0x13) # Over Current Treshold setup 60 # self.spi.xfer( 0x13) # Over Current Treshold setup
61 # self.spi.xfer( 0xFF) 61 # self.spi.xfer( 0xFF)
62 self.spi.xfer([0x15]) # Full Step speed 62 self.spi.xfer([0x15]) # Full Step speed
63 self.spi.xfer([0xFF]) 63 self.spi.xfer([0xFF])
64 self.spi.xfer([0xFF]) 64 self.spi.xfer([0xFF])
65 self.spi.xfer([0x05]) # ACC 65 self.spi.xfer([0x05]) # ACC
66 self.spi.xfer([0x00]) 66 self.spi.xfer([0x00])
67 self.spi.xfer([0x10]) 67 self.spi.xfer([0x10])
68 self.spi.xfer([0x06]) # DEC 68 self.spi.xfer([0x06]) # DEC
69 self.spi.xfer([0x00]) 69 self.spi.xfer([0x00])
70 self.spi.xfer([0x10]) 70 self.spi.xfer([0x10])
71 self.spi.xfer([0x0A]) # KVAL_RUN 71 self.spi.xfer([0x0A]) # KVAL_RUN
72 self.spi.xfer([0x50]) 72 self.spi.xfer([0x50])
73 self.spi.xfer([0x0B]) # KVAL_ACC 73 self.spi.xfer([0x0B]) # KVAL_ACC
74 self.spi.xfer([0x50]) 74 self.spi.xfer([0x50])
75 self.spi.xfer([0x0C]) # KVAL_DEC 75 self.spi.xfer([0x0C]) # KVAL_DEC
76 self.spi.xfer([0x05]) 76 self.spi.xfer([0x50])
77 # self.spi.xfer([0x18]) # CONFIG 77 # self.spi.xfer([0x18]) # CONFIG
78 # self.spi.xfer([0b00111000]) 78 # self.spi.xfer([0b00111000])
79 # self.spi.xfer([0b00000000]) 79 # self.spi.xfer([0b00000000])
80 80
81 def MaxSpeed(self, speed): 81 def MaxSpeed(self, speed):
82 'Setup of maximum speed in steps/s' 82 'Setup of maximum speed in steps/s'
83 speed_value = int(speed / 15.25) 83 speed_value = int(speed / 15.25)
84 if (speed_value == 0): 84 if (speed_value == 0):
85 speed_value = 1 85 speed_value = 1
86 print hex(speed_value) 86 print hex(speed_value)
87   87  
88 data = [(speed_value >> i & 0xff) for i in (16,8,0)] 88 data = [(speed_value >> i & 0xff) for i in (16,8,0)]
89 self.spi.xfer([data[0]]) # Max Speed setup 89 self.spi.xfer([data[0]]) # Max Speed setup
90 self.spi.xfer([data[1]]) 90 self.spi.xfer([data[1]])
91 self.spi.xfer([data[2]]) 91 self.spi.xfer([data[2]])
92 return (speed_value * 15.25) 92 return (speed_value * 15.25)
93   93  
94 def ReleaseSW(self): 94 def ReleaseSW(self):
95 ' Go away from Limit Switch ' 95 ' Go away from Limit Switch '
96 while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? 96 while self.ReadStatusBit(2) == 1: # is Limit Switch ON ?
97 self.spi.xfer([0x92 | (~self.Dir & 1)]) # release SW 97 self.spi.xfer([0x92 | (~self.Dir & 1)]) # release SW
98 while self.GetStatus()['BUSY']: 98 while self.GetStatus()['BUSY']:
99 pass 99 pass
100 self.MoveWait(10) # move 10 units away 100 self.MoveWait(10) # move 10 units away
101 101
102 def GoZero(self, speed): 102 def GoZero(self, speed):
103 ' Go to Zero position ' 103 ' Go to Zero position '
104 self.ReleaseSW() 104 self.ReleaseSW()
105 self.spi.xfer([0x82 | (self.Dir & 1)]) # Go to Zero 105 self.spi.xfer([0x82 | (self.Dir & 1)]) # Go to Zero
106 self.spi.xfer([0x00]) 106 self.spi.xfer([0x00])
107 self.spi.xfer([speed]) 107 self.spi.xfer([speed])
108 while self.GetStatus()['BUSY']: 108 while self.GetStatus()['BUSY']:
109 pass 109 pass
110 time.sleep(0.3) 110 time.sleep(0.3)
111 self.ReleaseSW() 111 self.ReleaseSW()
112   112  
113 def GetStatus(self): 113 def GetStatus(self):
114 #self.spi.xfer([0b11010000]) # Get status command from datasheet - does not work for uknown rasons 114 #self.spi.xfer([0b11010000]) # Get status command from datasheet - does not work for uknown rasons
115 self.spi.xfer([0x39]) # Gotparam command on status register 115 self.spi.xfer([0x39]) # Gotparam command on status register
116 data = self.spi.readbytes(1) 116 data = self.spi.readbytes(1)
117 data = data + self.spi.readbytes(1) 117 data = data + self.spi.readbytes(1)
118   118  
119 status = dict([('SCK_MOD',data[0] & 0x80 == 0x80), #The SCK_MOD bit is an active high flag indicating that the device is working in Step-clock mode. In this case the step-clock signal should be provided through the STCK input pin. The DIR bit indicates the current motor direction 119 status = dict([('SCK_MOD',data[0] & 0x80 == 0x80), #The SCK_MOD bit is an active high flag indicating that the device is working in Step-clock mode. In this case the step-clock signal should be provided through the STCK input pin. The DIR bit indicates the current motor direction
120 ('STEP_LOSS_B',data[0] & 0x40 == 0x40), 120 ('STEP_LOSS_B',data[0] & 0x40 == 0x40),
121 ('STEP_LOSS_A',data[0] & 0x20 == 0x20), 121 ('STEP_LOSS_A',data[0] & 0x20 == 0x20),
122 ('OCD',data[0] & 0x10 == 0x10), 122 ('OCD',data[0] & 0x10 == 0x10),
123 ('TH_SD',data[0] & 0x08 == 0x08), 123 ('TH_SD',data[0] & 0x08 == 0x08),
124 ('TH_WRN',data[0] & 0x04 == 0x04), 124 ('TH_WRN',data[0] & 0x04 == 0x04),
125 ('UVLO',data[0] & 0x02 == 0x02), 125 ('UVLO',data[0] & 0x02 == 0x02),
126 ('WRONG_CMD',data[0] & 0x01 == 0x01), #The NOTPERF_CMD and WRONG_CMD flags are active high and indicate, respectively, that the command received by SPI cannot be performed or does not exist at all. 126 ('WRONG_CMD',data[0] & 0x01 == 0x01), #The NOTPERF_CMD and WRONG_CMD flags are active high and indicate, respectively, that the command received by SPI cannot be performed or does not exist at all.
127 ('NOTPERF_CMD',data[1] & 0x80 == 0x80), 127 ('NOTPERF_CMD',data[1] & 0x80 == 0x80),
128 ('MOT_STATUS',data[1] & 0x60), 128 ('MOT_STATUS',data[1] & 0x60),
129 ('DIR',data[1] & 0x10 == 0x10), 129 ('DIR',data[1] & 0x10 == 0x10),
130 ('SW_EVN',data[1] & 0x08 == 0x08), 130 ('SW_EVN',data[1] & 0x08 == 0x08),
131 ('SW_F',data[1] & 0x04 == 0x04), #The SW_F flag reports the SW input status (low for open and high for closed). 131 ('SW_F',data[1] & 0x04 == 0x04), #The SW_F flag reports the SW input status (low for open and high for closed).
132 ('BUSY',data[1] & 0x02 != 0x02), 132 ('BUSY',data[1] & 0x02 != 0x02),
133 ('HIZ',data[1] & 0x01 == 0x01)]) 133 ('HIZ',data[1] & 0x01 == 0x01)])
134 return status 134 return status
135   135  
136 def Move(self, units): 136 def Move(self, units):
137 ' Move some distance units from current position ' 137 ' Move some distance units from current position '
138 steps = units * self.SPU # translate units to steps 138 steps = units * self.SPU # translate units to steps
139 if steps > 0: # look for direction 139 if steps > 0: # look for direction
140 self.spi.xfer([0x40 | (~self.Dir & 1)]) 140 self.spi.xfer([0x40 | (~self.Dir & 1)])
141 else: 141 else:
142 self.spi.xfer([0x40 | (self.Dir & 1)]) 142 self.spi.xfer([0x40 | (self.Dir & 1)])
143 steps = int(abs(steps)) 143 steps = int(abs(steps))
144 self.spi.xfer([(steps >> 16) & 0xFF]) 144 self.spi.xfer([(steps >> 16) & 0xFF])
145 self.spi.xfer([(steps >> 8) & 0xFF]) 145 self.spi.xfer([(steps >> 8) & 0xFF])
146 self.spi.xfer([steps & 0xFF]) 146 self.spi.xfer([steps & 0xFF])
147   147  
148 def Run(self, direction, speed): 148 def Run(self, direction, speed):
149 speed_value = int(speed / 0.015) 149 speed_value = int(speed / 0.015)
150 print hex(speed_value) 150 print hex(speed_value)
151   151  
152 data = [0b01010000 + direction] 152 data = [0b01010000 + direction]
153 data = data +[(speed_value >> i & 0xff) for i in (16,8,0)] 153 data = data +[(speed_value >> i & 0xff) for i in (16,8,0)]
154 self.spi.xfer([data[0]]) # Max Speed setup 154 self.spi.xfer([data[0]]) # Max Speed setup
155 self.spi.xfer([data[1]]) 155 self.spi.xfer([data[1]])
156 self.spi.xfer([data[2]]) 156 self.spi.xfer([data[2]])
157 self.spi.xfer([data[3]]) 157 self.spi.xfer([data[3]])
158 return (speed_value * 0.015) 158 return (speed_value * 0.015)
159   159  
160 def MoveWait(self, units): 160 def MoveWait(self, units):
161 ' Move some distance units from current position and wait for execution ' 161 ' Move some distance units from current position and wait for execution '
162 self.Move(units) 162 self.Move(units)
163 while self.GetStatus()['BUSY']: 163 while self.GetStatus()['BUSY']:
164 pass 164 pass
165 time.sleep(0.8) 165 time.sleep(0.8)
166   166  
167 def Float(self, hard = False): 167 def Float(self, hard = False):
168 ' switch H-bridge to High impedance state ' 168 ' switch H-bridge to High impedance state '
169 if (hard == False): 169 if (hard == False):
170 self.spi.xfer([0xA0]) 170 self.spi.xfer([0xA0])
171 else: 171 else:
172 self.spi.xfer([0xA8]) 172 self.spi.xfer([0xA8])
173   173  
174   174  
175 # End Class axis -------------------------------------------------- 175 # End Class axis --------------------------------------------------
176   176  
177 print "Stepper motor control test started. \r\n" 177 print "Stepper motor control test started. \r\n"
178 print "Max motor speed: %f " % SPEED 178 print "Max motor speed: %f " % SPEED
179 print "Distance to run: %f " % DISTANCE 179 print "Distance to run: %f " % DISTANCE
180   180  
181 try: 181 try:
182 print "SPI configuration.." 182 print "SPI configuration.."
183 spi = spidev.SpiDev() # create a spi object 183 spi = spidev.SpiDev() # create a spi object
184 spi.open(0, 0) # open spi port 0, device (CS) 0 184 spi.open(0, 0) # open spi port 0, device (CS) 0
185 spi.mode = 0b01 185 spi.mode = 0b01
186 spi.lsbfirst = False 186 spi.lsbfirst = False
187 spi.bits_per_word = 8 187 spi.bits_per_word = 8
188 spi.cshigh = False 188 spi.cshigh = False
189 spi.max_speed_hz = 100000 189 spi.max_speed_hz = 100000
190 #spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) 190 #spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz)
191 time.sleep(1) 191 time.sleep(1)
192   192  
193 print "Axis inicialization" 193 print "Axis inicialization"
194 X = axis(spi, 0, 641, MaxSpeed = SPEED) # set Number of Steps per axis Unit and set Direction of Rotation 194 X = axis(spi, 0, 641, MaxSpeed = SPEED) # set Number of Steps per axis Unit and set Direction of Rotation
195   195  
196 print X.MaxSpeed(SPEED) # set maximal motor speed 196 print X.MaxSpeed(SPEED) # set maximal motor speed
197   197  
198 print X.Run(1, 200.456431) 198 #print X.Run(1, 200.456431)
199 time.sleep(10) 199 #time.sleep(10)
200 ''' 200
201 print "Axis is running" 201 print "Axis is running"
202 for i in range(5): 202 for i in range(5):
203 print i 203 print i
204 X.MoveWait(DISTANCE) # move forward and wait for motor stop 204 X.MoveWait(DISTANCE) # move forward and wait for motor stop
205 print "Changing direction of rotation.." 205 print "Changing direction of rotation.."
206 time.sleep(1.1) 206 time.sleep(1.1)
207 X.MoveWait(-DISTANCE) # move backward and wait for motor stop 207 X.MoveWait(-DISTANCE) # move backward and wait for motor stop
208 print "Changing direction of rotation.." 208 print "Changing direction of rotation.."
209 time.sleep(1.1) 209 time.sleep(1.1)
210 ''' 210
211 X.Float(hard=False) # release power 211 X.Float(hard=False) # release power
212   212  
213 finally: 213 finally:
214 print "stop" 214 print "stop"