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])
26 SPEED = 5
27 DISTANCE = 50
28  
29 elif len(sys.argv) == 3:
30 SPEED = eval(sys.argv[2])
31 DISTANCE = 100
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
39 SPEED = 10
40 DISTANCE = 50
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
4573 kaklik 48 self.maxseed = MaxSpeed
4557 kaklik 49 self.Reset()
4573 kaklik 50 self.Initialize()
4557 kaklik 51  
52 def Reset(self):
4573 kaklik 53 'Reset the Axis'
4565 kaklik 54 self.spi.xfer([0xC0]) # reset
4573 kaklik 55  
56 def Initialize(self):
57 'set default parameters for H-bridge '
4557 kaklik 58 # self.spi.xfer( 0x14) # Stall Treshold setup
59 # self.spi.xfer( 0xFF)
60 # self.spi.xfer( 0x13) # Over Current Treshold setup
61 # self.spi.xfer( 0xFF)
4565 kaklik 62 self.spi.xfer([0x15]) # Full Step speed
63 self.spi.xfer([0xFF])
64 self.spi.xfer([0xFF])
65 self.spi.xfer([0x05]) # ACC
66 self.spi.xfer([0x00])
67 self.spi.xfer([0x10])
68 self.spi.xfer([0x06]) # DEC
69 self.spi.xfer([0x00])
70 self.spi.xfer([0x10])
71 self.spi.xfer([0x0A]) # KVAL_RUN
4573 kaklik 72 self.spi.xfer([0x50])
4565 kaklik 73 self.spi.xfer([0x0B]) # KVAL_ACC
4573 kaklik 74 self.spi.xfer([0x50])
4565 kaklik 75 self.spi.xfer([0x0C]) # KVAL_DEC
4574 kaklik 76 self.spi.xfer([0x50])
4573 kaklik 77 # self.spi.xfer([0x18]) # CONFIG
78 # self.spi.xfer([0b00111000])
79 # self.spi.xfer([0b00000000])
4557 kaklik 80  
81 def MaxSpeed(self, speed):
4573 kaklik 82 'Setup of maximum speed in steps/s'
83 speed_value = int(speed / 15.25)
84 if (speed_value == 0):
85 speed_value = 1
86 print hex(speed_value)
4557 kaklik 87  
4573 kaklik 88 data = [(speed_value >> i & 0xff) for i in (16,8,0)]
89 self.spi.xfer([data[0]]) # Max Speed setup
90 self.spi.xfer([data[1]])
91 self.spi.xfer([data[2]])
92 return (speed_value * 15.25)
93  
4557 kaklik 94 def ReleaseSW(self):
95 ' Go away from Limit Switch '
96 while self.ReadStatusBit(2) == 1: # is Limit Switch ON ?
4565 kaklik 97 self.spi.xfer([0x92 | (~self.Dir & 1)]) # release SW
4573 kaklik 98 while self.GetStatus()['BUSY']:
4557 kaklik 99 pass
100 self.MoveWait(10) # move 10 units away
101  
102 def GoZero(self, speed):
103 ' Go to Zero position '
104 self.ReleaseSW()
4565 kaklik 105 self.spi.xfer([0x82 | (self.Dir & 1)]) # Go to Zero
106 self.spi.xfer([0x00])
107 self.spi.xfer([speed])
4573 kaklik 108 while self.GetStatus()['BUSY']:
4557 kaklik 109 pass
110 time.sleep(0.3)
111 self.ReleaseSW()
112  
4573 kaklik 113 def GetStatus(self):
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
116 data = self.spi.readbytes(1)
117 data = data + self.spi.readbytes(1)
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
120 ('STEP_LOSS_B',data[0] & 0x40 == 0x40),
121 ('STEP_LOSS_A',data[0] & 0x20 == 0x20),
122 ('OCD',data[0] & 0x10 == 0x10),
123 ('TH_SD',data[0] & 0x08 == 0x08),
124 ('TH_WRN',data[0] & 0x04 == 0x04),
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.
127 ('NOTPERF_CMD',data[1] & 0x80 == 0x80),
128 ('MOT_STATUS',data[1] & 0x60),
129 ('DIR',data[1] & 0x10 == 0x10),
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).
132 ('BUSY',data[1] & 0x02 != 0x02),
133 ('HIZ',data[1] & 0x01 == 0x01)])
134 return status
135  
4557 kaklik 136 def Move(self, units):
137 ' Move some distance units from current position '
138 steps = units * self.SPU # translate units to steps
139 if steps > 0: # look for direction
4565 kaklik 140 self.spi.xfer([0x40 | (~self.Dir & 1)])
4557 kaklik 141 else:
4565 kaklik 142 self.spi.xfer([0x40 | (self.Dir & 1)])
4557 kaklik 143 steps = int(abs(steps))
4565 kaklik 144 self.spi.xfer([(steps >> 16) & 0xFF])
145 self.spi.xfer([(steps >> 8) & 0xFF])
146 self.spi.xfer([steps & 0xFF])
4557 kaklik 147  
4573 kaklik 148 def Run(self, direction, speed):
149 speed_value = int(speed / 0.015)
150 print hex(speed_value)
151  
152 data = [0b01010000 + direction]
153 data = data +[(speed_value >> i & 0xff) for i in (16,8,0)]
154 self.spi.xfer([data[0]]) # Max Speed setup
155 self.spi.xfer([data[1]])
156 self.spi.xfer([data[2]])
157 self.spi.xfer([data[3]])
158 return (speed_value * 0.015)
159  
4557 kaklik 160 def MoveWait(self, units):
161 ' Move some distance units from current position and wait for execution '
162 self.Move(units)
4573 kaklik 163 while self.GetStatus()['BUSY']:
4557 kaklik 164 pass
4570 kaklik 165 time.sleep(0.8)
4557 kaklik 166  
4573 kaklik 167 def Float(self, hard = False):
4557 kaklik 168 ' switch H-bridge to High impedance state '
4573 kaklik 169 if (hard == False):
170 self.spi.xfer([0xA0])
4557 kaklik 171 else:
4573 kaklik 172 self.spi.xfer([0xA8])
4557 kaklik 173  
4570 kaklik 174  
4557 kaklik 175 # End Class axis --------------------------------------------------
176  
177 print "Stepper motor control test started. \r\n"
4573 kaklik 178 print "Max motor speed: %f " % SPEED
179 print "Distance to run: %f " % DISTANCE
4557 kaklik 180  
181 try:
182 print "SPI configuration.."
183 spi = spidev.SpiDev() # create a spi object
4561 kaklik 184 spi.open(0, 0) # open spi port 0, device (CS) 0
4570 kaklik 185 spi.mode = 0b01
4563 kaklik 186 spi.lsbfirst = False
187 spi.bits_per_word = 8
188 spi.cshigh = False
4573 kaklik 189 spi.max_speed_hz = 100000
4563 kaklik 190 #spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz)
4557 kaklik 191 time.sleep(1)
192  
193 print "Axis inicialization"
4573 kaklik 194 X = axis(spi, 0, 641, MaxSpeed = SPEED) # set Number of Steps per axis Unit and set Direction of Rotation
4557 kaklik 195  
4573 kaklik 196 print X.MaxSpeed(SPEED) # set maximal motor speed
197  
4574 kaklik 198 #print X.Run(1, 200.456431)
199 #time.sleep(10)
200  
4557 kaklik 201 print "Axis is running"
202 for i in range(5):
203 print i
204 X.MoveWait(DISTANCE) # move forward and wait for motor stop
205 print "Changing direction of rotation.."
4570 kaklik 206 time.sleep(1.1)
4557 kaklik 207 X.MoveWait(-DISTANCE) # move backward and wait for motor stop
208 print "Changing direction of rotation.."
4570 kaklik 209 time.sleep(1.1)
4574 kaklik 210  
4573 kaklik 211 X.Float(hard=False) # release power
4557 kaklik 212  
213 finally:
214 print "stop"