#!/usr/bin/python#uncomment for debbug purposes#import logging#logging.basicConfig(level=logging.DEBUG)import sysimport timefrom pymlab import configclass axis:def __init__(self, SPI_CS, Direction, StepsPerUnit):' One axis of robot 'self.CS = SPI_CSself.Dir = Directionself.SPU = StepsPerUnitself.Reset()def Reset(self):' Reset Axis and set default parameters for H-bridge 'spi.SPI_write_byte(self.CS, 0xC0) # resetspi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setupspi.SPI_write_byte(self.CS, 0x7F)spi.SPI_write_byte(self.CS, 0x14) # Over Current Treshold setupspi.SPI_write_byte(self.CS, 0x0F)#spi.SPI_write_byte(self.CS, 0x15) # Full Step speed#spi.SPI_write_byte(self.CS, 0x00)#spi.SPI_write_byte(self.CS, 0x30)#spi.SPI_write_byte(self.CS, 0x0A) # KVAL_RUN#spi.SPI_write_byte(self.CS, 0x50)def MaxSpeed(self, speed):' Setup of maximum speed 'spi.SPI_write_byte(self.CS, 0x07) # Max Speed setupspi.SPI_write_byte(self.CS, 0x00)spi.SPI_write_byte(self.CS, speed)def ReleaseSW(self):' Go away from Limit Switch 'while self.ReadStatusBit(2) == 1: # is Limit Switch ON ?spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SWwhile self.IsBusy():passself.MoveWait(10) # move 10 units awaydef GoZero(self, speed):' Go to Zero position 'self.ReleaseSW()spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zerospi.SPI_write_byte(self.CS, 0x00)spi.SPI_write_byte(self.CS, speed)while self.IsBusy():passtime.sleep(0.3)self.ReleaseSW()def Move(self, units):' Move some distance units from current position 'steps = units * self.SPU # translate units to stepsif steps > 0: # look for directionspi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1))else:spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1))steps = int(abs(steps))spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF)spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF)spi.SPI_write_byte(self.CS, steps & 0xFF)def MoveWait(self, units):' Move some distance units from current position and wait for execution 'self.Move(units)while self.IsBusy():passdef Float(self):' switch H-bridge to High impedance state 'spi.SPI_write_byte(self.CS, 0xA0)def ReadStatusBit(self, bit):' Report given status bit 'spi.SPI_write_byte(self.CS, 0x39) # Read from address 0x19 (STATUS)spi.SPI_write_byte(self.CS, 0x00)data0 = spi.SPI_read_byte() # 1st bytespi.SPI_write_byte(self.CS, 0x00)data1 = spi.SPI_read_byte() # 2nd byte#print hex(data0), hex(data1)if bit > 7: # extract requested bitOutputBit = (data0 >> (bit - 8)) & 1else:OutputBit = (data1 >> bit) & 1return OutputBitdef IsBusy(self):""" Return True if tehre are motion """if self.ReadStatusBit(1) == 1:return Falseelse:return True# End Class axis --------------------------------------------------cfg = config.Config(i2c = {"port": 1,},bus =[{"type": "i2chub","address": 0x70,"children":[{ "name":"spi", "type":"i2cspi", "channel":7}],},],)cfg.initialize()print "Irradiation unit. \r\n"spi = cfg.get_device("spi")spi.route()try:while True:print "SPI configuration.."spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz)print "Robot inicialization"X = axis(spi.I2CSPI_SS1, 0, 641)Y = axis(spi.I2CSPI_SS0, 1, 642)Z = axis(spi.I2CSPI_SS2, 1, 32256)X.MaxSpeed(35) # max. 50Y.MaxSpeed(35) # max. 50Z.MaxSpeed(20) # max. 30Z.GoZero(100)Y.GoZero(20)X.GoZero(20)time.sleep(1)X.Move(16)Y.Move(150)Z.MoveWait(39)time.sleep(1)Z.MoveWait(-5)print "Robot is running"xcorner = 72xsteps = 9ysteps = 6 # *2 + 1 lineyy = 0space = 4grid = 8delay = 50X.MoveWait(xcorner)for y in range(ysteps):for x in range(xsteps):print x+1, yy+1Z.MoveWait(space)time.sleep(delay)Z.MoveWait(-space)if x < (xsteps - 1):X.MoveWait(grid)Y.MoveWait(-grid)yy += 1for x in range(xsteps):print x+1, yy+1Z.MoveWait(space)time.sleep(delay)Z.MoveWait(-space)if x < (xsteps - 1):X.MoveWait(-grid)Y.MoveWait(-grid)yy += 1for x in range(xsteps):print x+1, yy+1Z.MoveWait(space)time.sleep(delay)Z.MoveWait(-space)if x < (xsteps - 1):X.MoveWait(grid)Y.MoveWait(-20)X.MoveWait(-(xcorner+(xsteps-1)*grid))#Z.MoveWait(-10)Y.MoveWait(ysteps*grid*2+20)X.Float()Y.Float()Z.Float()# while True:# print X.ReadStatusBit(2), "end"# time.sleep(1)finally:print "stop"