Rev 4735 Rev 4736
Line 23... Line 23...
23 else: 23 else:
24 sys.stderr.write("Invalid number of arguments.\n") 24 sys.stderr.write("Invalid number of arguments.\n")
25 sys.stderr.write("Usage: %s BASE_SPEED (in steps/s)\n" % (sys.argv[0], )) 25 sys.stderr.write("Usage: %s BASE_SPEED (in steps/s)\n" % (sys.argv[0], ))
26 sys.exit(1) 26 sys.exit(1)
27   27  
28   -  
29   -  
30 # Begin of Class Axis -------------------------------------------------- 28 # Begin of Class Axis --------------------------------------------------
31   29  
32 class axis: 30 class axis:
33 def __init__(self, SPI_CS, Direction, StepsPerUnit, MaxSpeed): 31 def __init__(self, SPI_CS, Direction, StepsPerUnit, MaxSpeed):
34 ' One axis of robot ' 32 ' One axis of robot '
Line 91... Line 89...
91 spi.SPI_write_byte(self.CS, self.L6470_KVAL_ACC) # KVAL_ACC 89 spi.SPI_write_byte(self.CS, self.L6470_KVAL_ACC) # KVAL_ACC
92 spi.SPI_write_byte(self.CS, 0x58) 90 spi.SPI_write_byte(self.CS, 0x58)
93 spi.SPI_write_byte(self.CS, self.L6470_KVAL_DEC) # KVAL_DEC 91 spi.SPI_write_byte(self.CS, self.L6470_KVAL_DEC) # KVAL_DEC
94 spi.SPI_write_byte(self.CS, 0x58) 92 spi.SPI_write_byte(self.CS, 0x58)
95 spi.SPI_write_byte(self.CS, 0x18) # CONFIG 93 spi.SPI_write_byte(self.CS, 0x18) # CONFIG
96 spi.SPI_write_byte(self.CS, 0b00101110) 94 spi.SPI_write_byte(self.CS, 0b00101110) # spolecny byte pro obe konfigurace
-   95 spi.SPI_write_byte(self.CS, 0b10000000) # konfigurace pro interni oscilator
97 spi.SPI_write_byte(self.CS, 0b10000110) 96 # spi.SPI_write_byte(self.CS, 0b10000110) # konfigurace s externim oscilatorem
98 self.MaxSpeed(self.maxspeed) 97 self.MaxSpeed(self.maxspeed)
99   98  
100 def setKVAL(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): 99 def setKVAL(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5):
101 """ The available range is from 0 to 0.996 x VS with a resolution of 0.004 x VS """ 100 """ The available range is from 0 to 0.996 x VS with a resolution of 0.004 x VS """
102   101  
103 def setOverCurrentTH(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): 102 def setOverCurrentTH(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5):
104 """ The available range is from 375 mA to 6 A, in steps of 375 mA """ 103 """ The available range is from 375 mA to 6 A, in steps of 375 mA """
105   104  
106   -  
107 -  
108 def MaxSpeed(self, speed): 105 def MaxSpeed(self, speed):
109 '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.' 106 '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.'
110 speed_value = int(speed / 15.25) 107 speed_value = int(speed / 15.25)
111 if (speed_value <= 0): 108 if (speed_value <= 0):
112 speed_value = 1 109 speed_value = 1
113 elif (speed_value >= 1023): 110 elif (speed_value >= 1023):
114 speed_value = 1023 111 speed_value = 1023
115   112  
116 data = [(speed_value >> i & 0xff) for i in (8,0)] 113 data = [(speed_value >> i & 0xff) for i in (8,0)]
117 print data -  
118 spi.SPI_write_byte(self.CS, self.L6470_MAX_SPEED) # Max Speed setup 114 spi.SPI_write_byte(self.CS, self.L6470_MAX_SPEED) # Max Speed setup
119 spi.SPI_write_byte(self.CS, data[0]) 115 spi.SPI_write_byte(self.CS, data[0])
120 spi.SPI_write_byte(self.CS, data[1]) 116 spi.SPI_write_byte(self.CS, data[1])
121 return (speed_value * 15.25) 117 return (speed_value * 15.25)
122   118  
Line 187... Line 183...
187 spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) 183 spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF)
188 spi.SPI_write_byte(self.CS, steps & 0xFF) 184 spi.SPI_write_byte(self.CS, steps & 0xFF)
189   185  
190 def Run(self, direction, speed): 186 def Run(self, direction, speed):
191 speed_value = int(speed / 0.015) 187 speed_value = int(speed / 0.015)
192 print hex(speed_value) -  
193   188  
194 command = 0b01010000 + int(direction) 189 command = 0b01010000 + int(direction)
195 data = [(speed_value >> i & 0xff) for i in (16,8,0)] 190 data = [(speed_value >> i & 0xff) for i in (16,8,0)]
196 spi.SPI_write_byte(self.CS, command) # Max Speed setup 191 spi.SPI_write_byte(self.CS, command) # Max Speed setup
197 spi.SPI_write_byte(self.CS, data[0]) 192 spi.SPI_write_byte(self.CS, data[0])
Line 249... Line 244...
249 print "Configuring SPI.." 244 print "Configuring SPI.."
250 spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) 245 spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz)
251 time.sleep(0.1) 246 time.sleep(0.1)
252   247  
253 print "Configuring stepper motor.." 248 print "Configuring stepper motor.."
254 X = axis(spi.I2CSPI_SS0, 0, 1, MaxSpeed = SPEED) # set Number of Steps per axis Unit and set Direction of Rotation 249 X = axis(spi.I2CSPI_SS0, 0, 1, MaxSpeed = 2*SPEED) # set Number of Steps per axis Unit and set Direction of Rotation
255 # maximum_speed = X.MaxSpeed(100.0) -  
256 #X.GetStatus() -  
257   250  
-   251  
258 # print "Motor speed limit is: %f steps/s" % maximum_speed 252 print "Motor speed limit is: %f steps/s" % maximum_speed
259 running = False 253 running = False
260   254  
261 print "Waiting for IR command.." 255 print "Waiting for IR command.."
262 while True: # set maximal motor speed 256 while True: # set maximal motor speed
263 key = pylirc.nextcode() ## preccessing the IR remote control commands. 257 key = pylirc.nextcode() ## preccessing the IR remote control commands.
Line 285... Line 279...
285 if running == True: 279 if running == True:
286 real_speed = X.Run(direction, requested_speed) 280 real_speed = X.Run(direction, requested_speed)
287 print "Motor running at: %f steps/s" % real_speed 281 print "Motor running at: %f steps/s" % real_speed
288 else: 282 else:
289 X.Reset() 283 X.Reset()
290 #X.Initialize() 284 X.Initialize()
291 X.Float(hard=False) # release power 285 X.Float(hard=False) # release power
292 print "Stopping the motor." 286 print "Stopping the motor."
293   287  
294 except KeyboardInterrupt: 288 except KeyboardInterrupt:
295 print "stop" 289 print "stop"
296 X.Float(hard=False) # release power 290 X.Float(hard=False) # release power
297 sys.exit(0) 291 sys.exit(0)
298   292  
299 except Exception, e: 293 except Exception, e:
300 # X.Float(hard=False) # release power 294 X.Float(hard=False) # release power
301 print >> sys.stderr, "Exception: %s" % str(e) 295 print >> sys.stderr, "Exception: %s" % str(e)
302 sys.exit(1) 296 sys.exit(1)