Rev 4567 Rev 4570
Line 5... Line 5...
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  
Line 114... Line 114...
114 def MoveWait(self, units): 114 def MoveWait(self, units):
115 ' Move some distance units from current position and wait for execution ' 115 ' Move some distance units from current position and wait for execution '
116 self.Move(units) 116 self.Move(units)
117 while self.IsBusy(): 117 while self.IsBusy():
118 pass 118 pass
-   119 time.sleep(0.8)
119   120  
120 def Float(self): 121 def Float(self):
121 ' switch H-bridge to High impedance state ' 122 ' switch H-bridge to High impedance state '
122 self.spi.xfer([0xA0]) 123 self.spi.xfer([0xA0])
123   124  
124 def ReadStatusBit(self, bit): 125 def ReadStatusBit(self, bit):
125 ' Report given status bit ' 126 ' Report given status bit '
126 self.spi.xfer([0x39]) # Read from address 0x19 (STATUS) 127 self.spi.xfer([0x39]) # Get status command
127 self.spi.xfer([0x00]) -  
128 data = self.spi.readbytes(2) # 1st byte 128 data = self.spi.readbytes(1) # 1st byte
129 #self.spi.xfer([0x00]) -  
130 #data1 = self.spi.readbytes(1) # 2nd byte 129 data = data + (self.spi.readbytes(1)) # 1st byte
131 #print hex(data0), hex(data1) 130 print data
132 if bit > 7: # extract requested bit 131 if bit > 7: # extract requested bit
133 OutputBit = (data[0] >> (bit - 8)) & 1 132 OutputBit = (data[0] >> (bit - 8)) & 1
134 else: 133 else:
135 OutputBit = (data[1] >> bit) & 1 134 OutputBit = (data[1] >> bit) & 1
136 return OutputBit 135 return OutputBit
137   136  
-   137  
138 138
139 def IsBusy(self): 139 def IsBusy(self):
140 """ Return True if tehre are motion """ 140 """ Return True if tehre are motion """
141 if self.ReadStatusBit(1) == 1: 141 if self.ReadStatusBit(1) == 1:
142 return False 142 return False
Line 151... Line 151...
151   151  
152 try: 152 try:
153 print "SPI configuration.." 153 print "SPI configuration.."
154 spi = spidev.SpiDev() # create a spi object 154 spi = spidev.SpiDev() # create a spi object
155 spi.open(0, 0) # open spi port 0, device (CS) 0 155 spi.open(0, 0) # open spi port 0, device (CS) 0
156 spi.mode = 1 156 spi.mode = 0b01
157 spi.lsbfirst = False 157 spi.lsbfirst = False
158 spi.bits_per_word = 8 158 spi.bits_per_word = 8
159 spi.cshigh = False 159 spi.cshigh = False
160 #spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) 160 #spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz)
161 time.sleep(1) 161 time.sleep(1)
Line 168... Line 168...
168   168  
169 for i in range(5): 169 for i in range(5):
170 print i 170 print i
171 X.MoveWait(DISTANCE) # move forward and wait for motor stop 171 X.MoveWait(DISTANCE) # move forward and wait for motor stop
172 print "Changing direction of rotation.." 172 print "Changing direction of rotation.."
-   173 time.sleep(1.1)
173 X.MoveWait(-DISTANCE) # move backward and wait for motor stop 174 X.MoveWait(-DISTANCE) # move backward and wait for motor stop
174 print "Changing direction of rotation.." 175 print "Changing direction of rotation.."
175   -  
-   176 time.sleep(1.1)
176 X.Float() # release power 177 X.Float() # release power
177   178  
178   179  
179 finally: 180 finally:
180 print "stop" 181 print "stop"