general refactoring

This commit is contained in:
Simon Pirkelmann 2020-10-24 21:16:17 +02:00
parent 8728db82e8
commit 119d857531
2 changed files with 14 additions and 9 deletions

View File

@ -10,4 +10,4 @@ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(f"{robot_id}\n".encode(), (HOST, PORT)) sock.sendto(f"{robot_id}\n".encode(), (HOST, PORT))
while True: while True:
received = str(sock.recv(1024), "utf-8") received = str(sock.recv(1024), "utf-8")
print("Received: {}".format(received)) print("Received: {}".format(received))

View File

@ -4,6 +4,7 @@ import time
from aruco_estimator import ArucoEstimator from aruco_estimator import ArucoEstimator
class MeasurementHandler(socketserver.BaseRequestHandler): class MeasurementHandler(socketserver.BaseRequestHandler):
def handle(self) -> None: def handle(self) -> None:
data = self.request[0] data = self.request[0]
@ -11,11 +12,12 @@ class MeasurementHandler(socketserver.BaseRequestHandler):
cur_thread = threading.current_thread() cur_thread = threading.current_thread()
print(f"current thread {cur_thread}") print(f"current thread {cur_thread}")
try: try:
id = int(data) marker_id = int(data)
if id in self.server.estimator.robot_marker_estimates: if marker_id in self.server.estimator.robot_marker_estimates:
while True: while True:
socket.sendto(f"{self.server.estimator.robot_marker_estimates[id]}\n".encode(), self.client_address) socket.sendto(f"{self.server.estimator.robot_marker_estimates[marker_id]}\n".encode(),
time.sleep(1.0/self.server.max_measurements_per_second) self.client_address)
time.sleep(1.0 / self.server.max_measurements_per_second)
else: else:
socket.sendto("error: unknown robot marker id\n".encode(), socket.sendto("error: unknown robot marker id\n".encode(),
self.client_address) self.client_address)
@ -24,18 +26,21 @@ class MeasurementHandler(socketserver.BaseRequestHandler):
return return
class MeasurementServer(socketserver.ThreadingMixIn, socketserver.UDPServer): class MeasurementServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
def __init__(self, server_address, RequestHandlerClass, estimator, max_measurements_per_second=30): def __init__(self, server_address, RequestHandlerClass, estimator, max_measurements_per_second=30):
super().__init__(server_address, RequestHandlerClass) super().__init__(server_address, RequestHandlerClass)
self.estimator = estimator self.estimator = estimator
self.max_measurements_per_second = max_measurements_per_second self.max_measurements_per_second = max_measurements_per_second
if __name__ == "__main__": if __name__ == "__main__":
estimator = ArucoEstimator(use_realsense=False, robot_marker_ids=[15]) aruco_estimator = ArucoEstimator(use_realsense=False, robot_marker_ids=[15])
estimator_thread = threading.Thread(target=estimator.run_tracking) estimator_thread = threading.Thread(target=aruco_estimator.run_tracking)
estimator_thread.start() estimator_thread.start()
with MeasurementServer(('127.0.0.1', 42424), MeasurementHandler, estimator, max_measurements_per_second=30) as measurement_server: with MeasurementServer(('127.0.0.1', 42424), MeasurementHandler, aruco_estimator,
max_measurements_per_second=30) as measurement_server:
measurement_server.serve_forever() measurement_server.serve_forever()
# receive with: nc 127.0.0.1 42424 -u -> 15 + Enter # receive with: nc 127.0.0.1 42424 -u -> 15 + Enter