class sensorUnit(): def __init__(self): self.__bus=SPI(3,1) self.__bus.msh=20000 def getLeftDistance(self): self.__bus.writebytes([0x0C]) return self.__bus.readbytes(1)[0] def getRightDistance(self): self.__bus.writebytes([0x0B]) return self.__bus.readbytes(1)[0] def getLineSensor(self,temp): if temp<0 or temp>10: raise SyntaxError("Linesensor value out of bounds") self.__bus.writebytes([temp]) return self.__bus.readbytes(1)[0] def getLeftMiddleSensor(self): self.__bus.writebytes([14]) return self.__bus.readbytes(1)[0] def getRightMiddleSensor(self): self.__bus.writebytes([15]) return self.__bus.readbytes(1)[0]
class driveUnit(): def __init__(self): self.__bus=SPI(3,0) self.__bus.msh=20000 #def testReceive(self): # self.__bus.writebytes([0x50]) # print self.__bus.readbytes(1) def setMotorLeft(self,speed): speed = int(speed) if speed >= 0: direction = 1 else: direction = 0 self.send_startbit() #längd self.__bus.writebytes([0x03]) #kommandot self.__bus.writebytes([0x11]) self.__bus.writebytes([direction]) self.__bus.writebytes([abs(speed)]) def setMotorRight(self,speed): speed = int(speed) if speed >= 0: direction = 1 else: direction = 0 self.send_startbit() #length self.__bus.writebytes([0x03]) #kommandot self.__bus.writebytes([0x10]) self.__bus.writebytes([direction]) self.__bus.writebytes([abs(speed)]) def setArmAxis(self, id, value): self.send_startbit() #length self.__bus.writebytes([0x03]) #commando self.__bus.writebytes([16+(id+1)]) self.__bus.writebytes([value >> 8, 0x00FF & value]) def sendAxis(self,id): self.send_startbit() self.__bus.writebytes([0x01]) self.__bus.writebytes([48+(id+1)]) def sendAllAxis(self): self.send_startbit() self.__bus.writebytes([0x01]) self.__bus.writebytes([0x3E]) def sendAllMotor(self): self.send_startbit() #längd self.__bus.writebytes([0x01]) #action self.__bus.writebytes([0x3D]) def sendMotorRight(self): self.send_startbit() self.__bus.writebytes([0x01]) self.__bus.writebytes([0x30]) def sendMotorLeft(self): self.send_startbit() self.__bus.writebytes([0x01]) self.__bus.writebytes([0x31]) def send_startbit(self): self.__bus.writebytes([0xFF]) self.__bus.writebytes([0xFF])
#!/usr/bin/env python from spi import SPI spi2_1 = SPI(2, 1) spi2_1.bpw = 8 spi2_1.mode = 0 spi2_1.writebytes([0x30])
#!/usr/bin/env python from spi import SPI # Set up the SPI dac = SPI(2, 0) dac.mode = 1 # SPI mode 1 dac.bpw = 8 # 8 bits pr word dac.lsbfirst = False # MSB transfers # Calculate the value for the DAC iChop = 2.0 # Current chopping limit (This is the value you can change) vRef = 3.3 # Voltage reference on the DAC rSense = 0.1 # Resistance for the vOut = iChop*5.0*rSense # Calculated voltage out from the DAC (See page 9 in the datasheet for the DAC) dacval = int((vOut*256.0)/vRef) # Update all channels with the value for addr in range(8): byte1 = ((dacval & 0xF0)>>4) + (addr<<4) byte2 = (dacval & 0x0F)<<4 dac.writebytes([byte1, byte2]) # Update all channels dac.writebytes([0xA0, 0xFF]) print "All channels now have vOut = "+str(vOut)+", iChop = "+str(iChop)