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