142 lines
4.3 KiB
Python
142 lines
4.3 KiB
Python
|
# coding=gb2312
|
|||
|
import json
|
|||
|
import os.path
|
|||
|
import random
|
|||
|
|
|||
|
from UWB.uwb_driver import *
|
|||
|
|
|||
|
this_dir = os.path.dirname(os.path.realpath(__file__))
|
|||
|
UWB_ASSIGNMENT_FILE_PATH = os.path.join(this_dir, "uwb_id_assignment.json")
|
|||
|
|
|||
|
|
|||
|
class MultiUWB:
|
|||
|
|
|||
|
def __init__(self) -> None:
|
|||
|
super().__init__()
|
|||
|
# <20><><EFBFBD><EFBFBD>uwb<77>豸
|
|||
|
self.uwb_driver_table = {}
|
|||
|
self.uwb_gather_thread = {}
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>վ<EFBFBD><D5BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if os.path.exists(UWB_ASSIGNMENT_FILE_PATH):
|
|||
|
with open(UWB_ASSIGNMENT_FILE_PATH, "r") as file:
|
|||
|
uwb_assignment = json.load(file)
|
|||
|
else:
|
|||
|
uwb_assignment = [i for i in range(8)]
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڻ<EFBFBD>վ
|
|||
|
for i in range(8):
|
|||
|
uwb = UwbController()
|
|||
|
if uwb.is_initialed():
|
|||
|
anchor_id = uwb.anchor_id
|
|||
|
self.uwb_driver_table[anchor_id] = uwb
|
|||
|
if uwb_assignment:
|
|||
|
seq = uwb_assignment.pop(0)
|
|||
|
else:
|
|||
|
seq = random.randint(0, 255)
|
|||
|
uwb.set_anchor_config(anchor_seq=seq, anchor_no=seq)
|
|||
|
# else:
|
|||
|
# break
|
|||
|
# uwb<77><62>¼
|
|||
|
self.record_log = []
|
|||
|
# <20>ź<EFBFBD>
|
|||
|
self.__running = threading.Event()
|
|||
|
self.__running.set()
|
|||
|
self.__get_data = threading.Event()
|
|||
|
self.__get_data.clear()
|
|||
|
|
|||
|
"""************************************<2A>߳̿<DFB3><CCBF><EFBFBD>ģ<EFBFBD><C4A3>************************************"""
|
|||
|
|
|||
|
def start(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.start()
|
|||
|
|
|||
|
def pause(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.pause()
|
|||
|
|
|||
|
def resume(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.resume()
|
|||
|
|
|||
|
def stop(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.stop()
|
|||
|
|
|||
|
# <20><><EFBFBD>ݼ<EFBFBD>¼<EFBFBD>߳<EFBFBD>
|
|||
|
def _record_threading(self, uwb: UwbController):
|
|||
|
while self.__running.is_set():
|
|||
|
# time.sleep(10000)
|
|||
|
data = uwb.get_data()
|
|||
|
if data:
|
|||
|
# print(data)
|
|||
|
self.record_log.append(data)
|
|||
|
self.__get_data.set()
|
|||
|
|
|||
|
# <20><>ʼ<EFBFBD><CABC>¼
|
|||
|
def start_all_record(self):
|
|||
|
self.__running.set()
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.clear_all()
|
|||
|
for _ in range(5):
|
|||
|
if unity.set_anchor_status(MODE_POSITION):
|
|||
|
time.sleep(0.3)
|
|||
|
anchor_status = unity.get_anchor_status()
|
|||
|
if anchor_status == MODE_POSITION:
|
|||
|
break
|
|||
|
threading.Thread(target=self._record_threading, args=(unity, )).start()
|
|||
|
return True
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>¼
|
|||
|
def clear_all_record(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
unity.clear_all()
|
|||
|
|
|||
|
# ֹͣ<CDA3><D6B9>¼
|
|||
|
def stop_all_record(self):
|
|||
|
for anchor, unity in self.uwb_driver_table.items():
|
|||
|
self.__running.clear()
|
|||
|
for _ in range(10):
|
|||
|
if unity.set_anchor_status(MODE_FREE):
|
|||
|
break
|
|||
|
time.sleep(0.1)
|
|||
|
if unity.get_anchor_status() != MODE_FREE:
|
|||
|
return False
|
|||
|
unity.clear_all()
|
|||
|
return True
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
def get_data(self, timeout=0.1):
|
|||
|
self.__get_data.wait(timeout=timeout)
|
|||
|
if self.record_log:
|
|||
|
data = self.record_log.pop(0)
|
|||
|
return data
|
|||
|
else:
|
|||
|
self.__get_data.clear()
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
def get_all_data(self):
|
|||
|
data = self.record_log.copy()
|
|||
|
self.record_log.clear()
|
|||
|
return data
|
|||
|
|
|||
|
# <20>ر<EFBFBD><D8B1>ֻ<EFBFBD>
|
|||
|
def close_tag(self, tag_id):
|
|||
|
for uwb in self.uwb_driver_table.values():
|
|||
|
uwb.close_tag(tag_id)
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
def set_tag(self, tag_id, freq=None, alarm_status=None, detection_config=None):
|
|||
|
for uwb in self.uwb_driver_table.values():
|
|||
|
uwb.set_tag(tag_id, freq=freq, alarm_status=alarm_status, detection_config=detection_config)
|
|||
|
|
|||
|
# <20>ػ<EFBFBD>ģʽ<C4A3><CABD><EFBFBD>ر<EFBFBD><D8B1>ܱ<EFBFBD><DCB1><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ֻ<EFBFBD>
|
|||
|
def close_all_tag(self, ):
|
|||
|
for uwb in self.uwb_driver_table.values():
|
|||
|
uwb.close_all_tag()
|
|||
|
|
|||
|
|
|||
|
# <20>رչػ<D5B9>ģʽ<C4A3><CABD>ֹͣ<CDA3>ر<EFBFBD><D8B1>ܱ<EFBFBD><DCB1><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ֻ<EFBFBD>
|
|||
|
def stop_close_all_tag(self):
|
|||
|
for uwb in self.uwb_driver_table.values():
|
|||
|
uwb.stop_close_all_tag()
|
|||
|
|