def connect_stm(): for port in list_ports.comports(): if (port.serial_number == SNR) and (port.pid == PID) and (port.vid == VID): port = '/dev/' + port.name else: print 'No STM32 found. Aborting' computerPort = serialWrapper.SerialWrapper(port) commands = packetBuilder.CommandsList() return computerPort, commands
import serialWrapper import packetBuilder import sys from serial.tools import list_ports commands_to_stm = packetBuilder.CommandsList() current_module = sys.modules[__name__] com_port = None def create_connection_to_stm(): port_number = get_com_port_number() return serialWrapper.SerialWrapper(port_number) def get_com_port_number(): vid = 1155 pid = 22336 snr = '365032713431' # stm Serial number return '/dev/tty.usbmodem1411' for port in list_ports.comports(): if port.serial_number == snr: return port.device def process_request(command, parameters): if parameters is '': reply = getattr(current_module, command)() else: reply = getattr(current_module, command)(parameters) return reply
def __init__(self, initialCoordinates): portNumber = self.GetPortNumber() #self.computerPort = serialWrapper.SerialWrapper("/dev/ttyACM2") self.computerPort = serialWrapper.SerialWrapper(portNumber) self.commands = packetBuilder.CommandsList() self.InitializeRobot(initialCoordinates)
recievedPacket = computerPort.sendRequest(packet.bytearray) #if recievedPacket.reply != 'Ok': # raise Exception('setCoordinates failed') port = port_number() if port: print 'STM32 found on port %s' %port else: print 'No STM32 found. Aborting' sys.exit() # COM port initialization computerPort = serialWrapper.SerialWrapper(port) # we will choose commands which we want to send from this list commands = packetBuilder.CommandsList() # Initialize PID, Trajectory and Kinematics initPTC() while True: print '\nList of available commands: \n1 Movement\n2 Get Coordinates',\ '\n3 Set Coordinates' command = raw_input('Command number: ') if command == '1': movement() elif command == '2': pass elif command == '3': setCoord()
class ParsePacket(object): synchronization = 'FA' adress = 'FA' typeConvertor = typeConvertor.TypeConvertor() crcCalculator = crc.CrcCalculator() listOfComands = packetBuilder.CommandsList() def __init__(self, byteString): self.string = byteString self.byteArray = self.GetByteArrayFromRecievedString() self.recievedSynchronizationByte = self.byteArray[0] self.recievedAdress = self.byteArray[1] self.packetLenght = self.byteArray[2] self.command = self.byteArray[3] self.reply = self.GetReply() self.recievedcrc = self.typeConvertor.IntToHex( self.byteArray[len(self.byteArray) - 2]) + self.typeConvertor.IntToHex( self.byteArray[len(self.byteArray) - 1]) self.CheckCrc() def GetReply(self): if self.listOfComands.getCurentCoordinates == self.command or self.listOfComands.getCurrentSpeed == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) coordinatesList = [] coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[16:24])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[8:16])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[0:8])) return coordinatesList if self.listOfComands.getDataIKSensors == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) coordinatesList = [] coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[24:32])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[16:24])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[8:16])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[0:8])) return coordinatesList if self.listOfComands.getDataUSSensors == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) coordinatesList = [] coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[32:40])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[24:32])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[16:24])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[8:16])) coordinatesList.append( self.typeConvertor.HexToFloat(invertedParametersString[0:8])) return coordinatesList if self.listOfComands.closeCubeCollector == self.command or self.listOfComands.getADCPinState == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) # check int 16 or int 8 return int(invertedParametersString, 16) if self.listOfComands.isPointWasReached == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) return int(invertedParametersString, 16) if self.listOfComands.getManipulatorAngle == self.command: recievedPametersString = binascii.hexlify( self.byteArray[4:len(self.byteArray) - 2]) invertedParametersString = self.typeConvertor.InvertStringArray( recievedPametersString) return self.typeConvertor.HexToFloat(invertedParametersString) return self.byteArray[4:len(self.byteArray) - 2][:-1] def CheckCrc(self): calculatedCrc = self.crcCalculator.GetTwoBytesFromCrc32( self.byteArray[0:len(self.byteArray) - 2]) if calculatedCrc != self.recievedcrc: raise Exception('Expected crc: ' + self.recievedcrc + ', but calculated: ' + calculatedCrc) def GetByteArrayFromRecievedString(self): byteString = "" for index in range(0, len(self.string)): hexSymbol = self.typeConvertor.IntToHex( ord(str(self.string[index]))) #print str(index) + ': ' + hexSymbol byteString = byteString + hexSymbol byteString = bytearray.fromhex(byteString) #print 'end' return byteString