web analytics

Python Class

This class is used to control the master tables and thus affect each of the slaves.


import serial

# Master Control
#
# Use this API to control each of the slaves
# This supports all 6 servos and the three analog inputs

class wixelMasterController:
    def __init__(self, usbPort):
        #usbPort = '/dev/ttyACM0'
        self.sc = serial.Serial(usbPort, timeout=1)
        self.initializeTables()

    def initializeTables(self):
        self.masterTxTable = []			# create the transmit and
        self.masterRxTable = []			# receive tables
        for i in range(0,64):			# as lists
            self.masterTxTable.append([])
            for j in range(0,6):
                self.masterTxTable[i].append(0)
            
    def closeMaster(self):
        self.sc.close()				# close serial port

    def setNumSlaves(self, slaves):
        datapacket = []                         # init list
        datapacket.append(chr(0x84))            # command byte, write tx table
        datapacket.append(chr(slaves))          # slave to control goes here
        for d in datapacket:                    # send command to the serial port
            self.sc.write(d)

    def setServoPosition(self, slave, servo, data):
        self.masterTxTable[slave][servo] = data
        self.sendSlaveData(slave)

    def getSlaveData(self, slave):
        datapacket = []                         # init list
        datapacket.append(chr(0x81))            # command byte, write tx table
        datapacket.append(chr(slave))           # slave to control goes here
        for d in datapacket:                    # send command to the serial port
            self.sc.write(d)

        returndata = []                         # now read data returned by master
        for d in range(0,16):                   # for this slave
            returndata.append(self.sc.read(1))
        return returndata

    def sendSlaveData(self, slave):
        datapacket = []                         # init list
        datapacket.append(chr(0x82))            # command byte, write tx table
        datapacket.append(chr(slave))           # slave to control goes here
        
        for i in range(0,6):                    # queue up the master data for tx
            db = self.masterTxTable[slave][i]
            d0 = db & 0x7f                      # data must be adj for 7 bit chars
            d1 = (db & 0x7f80) >> 7             # or reception gets confused
            datapacket.append(chr(d0))
            datapacket.append(chr(d1))

        datapacket.append(chr(0))    		# only have six servos, 12 bytes plus the command and slave
        datapacket.append(chr(0))    		#  so add last two for 16 total packet size

        for d in datapacket:   		        # send them to the serial port
            self.sc.write(d)    		# note that it HAS to be exactly 16 bytes total