class Controller(object): """description of class""" def CheckStatus(self, status): if status == PozyxConstants.STATUS_FAILURE: raise ConnectionError('Pozyx function failed') if status == PozyxConstants.STATUS_TIMEOUT: raise ConnectionError('Pozyx function timed out') def __init__(self, anchors): self.anchors = [DeviceCoordinates()] self.anchors = anchors self.port = get_first_pozyx_serial_port() if self.port is None: self.error = "No Pozyx connected" return self.pozyx = PozyxSerial(self.port) networkId = NetworkID() status = self.pozyx.getNetworkId(networkId) self.id = networkId.id self.CheckStatus(status) self.ConfigureAnchor(self.id) def ConfigureAnchor(self, anchorId): for devCoords in self.anchors: if devCoords.network_id == anchorId: position = devCoords.pos break if anchorId == self.id: anchorId = None status = self.pozyx.setCoordinates(position, anchorId) self.CheckStatus(status) def ConfigureTag(self, tagId): for anchor in self.anchors: status = self.pozyx.addDevice(anchor, tagId) self.CheckStatus(status) status = self.pozyx.configureAnchors(self.anchors, PozyxConstants.ANCHOR_SELECT_AUTO, tagId) self.CheckStatus(status) mode = SingleRegister(PozyxConstants.GPIO_DIGITAL_INPUT) pullup = SingleRegister(PozyxConstants.GPIO_PULL_UP) status = self.pozyx.setConfigGPIO(1, mode, pullup, tagId) self.CheckStatus(status) status = self.pozyx.setConfigGPIO(2, mode, pullup, tagId) self.CheckStatus(status) status = self.pozyx.setConfigGPIO(3, mode, pullup, tagId) self.CheckStatus(status) status = self.pozyx.setConfigGPIO(4, mode, pullup, tagId) self.CheckStatus(status) def ConfigureUWB(self, id, txPower=33.0, channel=1, bitrate=0, gain=67, prf=2, preamble=40): if txPower < 0.0 or txPower > 33.0: raise ValueError("Invalid txPower") if id == self.id: id = None settings = UWBSettings() settings.bitrate = bitrate settings.channel = channel settings.gain_db = gain settings.plen = preamble settings.prf = prf status = self.pozyx.setUWBSettings(settings, id) self.CheckStatus(status) status = self.pozyx.setTxPower(txPower, id) self.CheckStatus(status) def GetError(self): code = SingleRegister() status = self.pozyx.getErrorCode(code) self.CheckStatus(status) return self.pozyx.getErrorMessage(code) def GetCalibrationStatus(self, tagId): result = SingleRegister() status = self.pozyx.getCalibrationStatus(result, tagId) self.CheckStatus(status) value = dict() value['magnetic'] = result.value & 3 value['accelerometer'] = (result.value & 12) >> 2 value['gyroscope'] = (result.value & 48) >> 4 value['system'] = (result.value & 192) >> 6 return value def GetGPIO(self, tagId, pinId): value = SingleRegister() status = self.pozyx.getGPIO(pinId, value, tagId) self.CheckStatus(status) return value.value == 1 def GetPosition(self, tagId): target = Coordinates() status = self.pozyx.doPositioning( target, PozyxConstants.DIMENSION_3D, algorithm=PozyxConstants.POSITIONING_ALGORITHM_UWB_ONLY, remote_id=tagId) self.CheckStatus(status) result = dict() result['x'] = target.x result['y'] = target.y result['z'] = target.z return target def GetEulerAngles(self, tagId): result = EulerAngles() status = self.pozyx.getEulerAngles_deg(result, tagId) self.CheckStatus(status) res = dict() res['heading'] = result.heading res['roll'] = result.roll res['pitch'] = result.pitch return res def GetPressure(self, tagId): data = Pressure() status = self.pozyx.getPressure_Pa(data, tagId) self.CheckStatus(status) return {'pressure': data.value} def GetTemperature(self, tagId): data = Temperature() status = self.pozyx.getTemperature_c(data, tagId) self.CheckStatus(status) return {'temperature': data.value} def DiscoverTags(self): status = self.pozyx.doDiscovery(PozyxConstants.DISCOVERY_TAGS_ONLY) self.CheckStatus(status) count = SingleRegister() status = self.pozyx.getDeviceListSize(count) self.CheckStatus(status) allIds = DeviceList(list_size=count.value) status = self.pozyx.getDeviceIds(allIds) self.CheckStatus(status) tagIds = list() for id in allIds: any = False for tagId in self.anchors: if tagId.network_id == id: any = True if not any: tagIds.append(id) return tagIds
class Tag: def __init__(self, anchors): self.serial = PozyxSerial(self.getSerialport) self.anchors = anchors # position calculation algorithm and tracking dimension self.algorithm = PozyxConstants.POSITIONING_ALGORITHM_UWB_ONLY self.dimension = PozyxConstants.DIMENSION_3D def setup(self): # sets up the Pozyx for positioning by calibrating its anchor list print("") print("POZYX POSITIONING Version {}".format(version)) print("-------------------------------------------------------") print("") print("- System will manually configure tag") print("") print("- System will auto start positioning") print("") print("-------------------------------------------------------") print("") self.setAnchors() self.printConfig() print("") print("-------------------------------------------------------") print("") def setAnchors(self): # adds the manually measured anchors to the Pozyx's device list one for one status = self.serial.clearDevices(remote_id=None) for anchor in self.anchors: status &= self.serial.addDevice(anchor, remote_id=None) if len(self.anchors) > 4: status &= self.serial.setSelectionOfAnchors(PozyxConstants.ANCHOR_SELECT_AUTO, len(self.anchors), remote_id=None) @property def getSerialport(self): # serialport connection test serial_port = get_first_pozyx_serial_port() if serial_port is None: print("No Pozyx connected. Check your USB cable or your driver!") return None else: return serial_port def getPosition(self): # performs positioning and exports the results position = Coordinates() try: status = self.serial.doPositioning(position, self.dimension, self.algorithm, remote_id=None) if status == POZYX_SUCCESS: # print("POZYX data:", position) return position else: self.printError("positioning") except: self.printError("positioning") return None def getOrientation(self): # reads euler angles (yaw, roll, pitch) and exports the results orientation = EulerAngles() status = self.serial.getEulerAngles_deg(orientation) if status == POZYX_SUCCESS: # print("POZYX data:", orientation) return orientation else: print("Sensor data not found") return None @classmethod def mockedPosition(cls): # return Coordinates(random.randint(0, 1000), random.randint(0, 1000), random.randint(0, 1000)) return Coordinates(random.randint(0, 2000), random.randint(0, 2000), random.randint(0, 2000)) @classmethod def mockedOrientation(cls): return EulerAngles(random.randint(0, 30), random.randint(0, 30), random.randint(0, 30)) def printConfig(self): # prints the anchor configuration result list_size = SingleRegister() # prints the anchors list size self.serial.getDeviceListSize(list_size, None) if list_size[0] != len(self.anchors): self.printError("configuration") return # prints the anchors list device_list = DeviceList(list_size=list_size[0]) self.serial.getDeviceIds(device_list, None) print("Calibration result:") print("Anchors found: {0}".format(list_size[0])) print("Anchor IDs: ", device_list) for i in range(list_size[0]): anchor_coordinates = Coordinates() self.serial.getDeviceCoordinates(device_list[i], anchor_coordinates, None) print("ANCHOR: 0x%0.4x, %s" % (device_list[i], str(anchor_coordinates))) sleep(0.025) def printError(self, operation): # Prints Pozyx's errors error_code = SingleRegister() if None is None: self.serial.getErrorCode(error_code) print("LOCAL ERROR %s, %s" % (operation, self.serial.getErrorMessage(error_code))) return status = self.serial.getErrorCode(error_code, None) if status == POZYX_SUCCESS: print("ERROR %s on ID %s, %s" % (operation, "0x%0.4x" % None, self.serial.getErrorMessage(error_code))) else: self.serial.getErrorCode(error_code) print("ERROR %s, couldn't retrieve remote error code, LOCAL ERROR %s" % (operation, self.serial.getErrorMessage(error_code)))