2020-10-24 17:56:18 +00:00
|
|
|
import socketserver
|
|
|
|
import threading
|
|
|
|
import time
|
2020-11-11 20:33:48 +00:00
|
|
|
import json
|
2021-09-07 20:31:28 +00:00
|
|
|
from queue import Queue
|
2020-10-24 17:56:18 +00:00
|
|
|
|
|
|
|
from aruco_estimator import ArucoEstimator
|
|
|
|
|
2020-10-24 19:16:17 +00:00
|
|
|
|
2020-10-24 17:56:18 +00:00
|
|
|
class MeasurementHandler(socketserver.BaseRequestHandler):
|
|
|
|
def handle(self) -> None:
|
2020-11-17 20:23:22 +00:00
|
|
|
data = self.request.recv(1024).strip()
|
2020-11-11 20:33:48 +00:00
|
|
|
|
2020-10-24 17:56:18 +00:00
|
|
|
cur_thread = threading.current_thread()
|
|
|
|
print(f"current thread {cur_thread}")
|
|
|
|
|
2020-11-17 20:23:22 +00:00
|
|
|
if 'events' in data.decode():
|
2020-11-11 20:33:48 +00:00
|
|
|
self.request.sendall('subscribed to events\n'.encode())
|
2021-09-07 20:31:28 +00:00
|
|
|
# send any events in the event queue to the subscriber
|
2020-11-11 20:33:48 +00:00
|
|
|
while True:
|
|
|
|
while not self.server.estimator.event_queue.empty():
|
|
|
|
event = self.server.estimator.event_queue.get()
|
2021-09-07 20:31:28 +00:00
|
|
|
# we distinguish two kinds of events:
|
|
|
|
if event[0] == 'response_event':
|
|
|
|
# 1. for 'response_event' events we expect the event subscriber to give a reply which will then
|
|
|
|
# by passed on to the response queue for transmitting to the original correspondent
|
|
|
|
message = event[1]['event']
|
|
|
|
#print(f"passing command {message} on to subscriber")
|
|
|
|
self.request.sendall((json.dumps(message) + '\n').encode())
|
|
|
|
reply = self.request.recv(1024)
|
|
|
|
#print(f"putting reply {reply} in response queue")
|
|
|
|
self.server.response_queue.put(reply)
|
|
|
|
else:
|
|
|
|
# 2. for other types of events we don't expect a reply and just pass them on to the subscriber
|
|
|
|
self.request.sendall((json.dumps(event) + '\n').encode())
|
2020-11-14 15:40:33 +00:00
|
|
|
self.server.estimator.last_event = None
|
|
|
|
time.sleep(1.0 / self.server.max_measurements_per_second)
|
2021-08-25 19:59:24 +00:00
|
|
|
elif 'corners' in data.decode():
|
|
|
|
# send positions of the board markers
|
|
|
|
corner_estimates = self.server.estimator.corner_estimates
|
|
|
|
response = {}
|
|
|
|
for corner, data in corner_estimates.items():
|
|
|
|
response[corner] = {'x': data['x'], 'y': data['y']}
|
|
|
|
self.request.sendall((json.dumps(response) + '\n').encode())
|
2021-09-07 20:31:28 +00:00
|
|
|
elif 'move_grid_blocking' in data.decode():
|
|
|
|
# if we receive a move_grid event
|
|
|
|
# ( e.g. move_grid;{"x":1,"y":1,"dimx":10,"dimy":10,"orientation":"^","require_response":True} )
|
|
|
|
# we compute the corresponding real-world position the robot should drive to
|
|
|
|
# and then create a new move event which is put in the event queue and will be propagated to the ControlCommander
|
2021-09-06 22:23:36 +00:00
|
|
|
data_decode = data.decode()
|
2021-09-07 20:31:28 +00:00
|
|
|
# print("data: ", data_decode)
|
2021-09-06 22:23:36 +00:00
|
|
|
payload = data.decode().split(';')[1]
|
|
|
|
grid_pos = json.loads(payload)
|
2021-09-07 20:31:28 +00:00
|
|
|
# print("grid_pos = ", grid_pos)
|
|
|
|
pos = self.server.estimator.get_pos_from_grid_point(grid_pos['x'], grid_pos['y'], grid_pos['dimx'],
|
|
|
|
grid_pos['dimy'], grid_pos['orientation'])
|
|
|
|
# print("pos = ", pos)
|
|
|
|
# print("event requiring response")
|
|
|
|
# put blocking move command in event queue
|
|
|
|
self.server.estimator.event_queue.put(('response_event',
|
|
|
|
{'event': ('move_blocking', {'x': pos[0], 'y': pos[1], 'angle': pos[2]})}))
|
|
|
|
|
|
|
|
# wait for response of the move command
|
|
|
|
# TODO this assumes that we wait only for at most one response at a time
|
|
|
|
# we could add some kind of reference here to handle multiple responses (e.g. id of the response to wait for)
|
|
|
|
while self.server.response_queue.empty():
|
|
|
|
pass
|
|
|
|
reply = self.server.response_queue.get()
|
|
|
|
|
|
|
|
# send back response to the original source
|
|
|
|
#print(f"sending reply {reply} back to correspondent {self.request}")
|
|
|
|
self.request.sendall(reply)
|
2020-11-11 20:33:48 +00:00
|
|
|
else:
|
|
|
|
# send robot position
|
|
|
|
try:
|
2020-11-17 20:23:22 +00:00
|
|
|
marker_id = int(data)
|
2020-11-11 20:33:48 +00:00
|
|
|
except ValueError:
|
|
|
|
marker_id = None
|
|
|
|
|
|
|
|
if marker_id is not None:
|
|
|
|
if marker_id in self.server.estimator.robot_marker_estimates:
|
|
|
|
while True:
|
2020-11-17 20:23:22 +00:00
|
|
|
self.request.sendall((json.dumps(self.server.estimator.robot_marker_estimates[marker_id])
|
|
|
|
+ '\n').encode())
|
2020-11-11 20:33:48 +00:00
|
|
|
time.sleep(1.0 / self.server.max_measurements_per_second)
|
|
|
|
else:
|
|
|
|
self.request.sendall("error: unknown robot marker id\n".encode())
|
|
|
|
else:
|
2020-11-17 20:23:22 +00:00
|
|
|
self.request.sendall("error: data not understood. "
|
|
|
|
"expected <robot marker id (int)> or 'events'\n".encode())
|
2020-10-24 17:56:18 +00:00
|
|
|
return
|
|
|
|
|
2020-10-24 19:16:17 +00:00
|
|
|
|
2020-11-11 20:33:48 +00:00
|
|
|
class MeasurementServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
|
|
|
allow_reuse_address = True
|
2020-11-17 20:23:22 +00:00
|
|
|
|
2020-10-24 17:56:18 +00:00
|
|
|
def __init__(self, server_address, RequestHandlerClass, estimator, max_measurements_per_second=30):
|
|
|
|
super().__init__(server_address, RequestHandlerClass)
|
|
|
|
self.estimator = estimator
|
|
|
|
self.max_measurements_per_second = max_measurements_per_second
|
2021-09-07 20:31:28 +00:00
|
|
|
self.response_queue = Queue()
|
2020-10-24 17:56:18 +00:00
|
|
|
|
2020-11-11 20:33:48 +00:00
|
|
|
def handle_error(self, request, client_address):
|
|
|
|
print("an error occurred -> terminating connection")
|
2020-10-24 19:16:17 +00:00
|
|
|
|
2020-11-17 20:23:22 +00:00
|
|
|
|
2020-10-24 17:56:18 +00:00
|
|
|
if __name__ == "__main__":
|
2021-09-07 20:31:28 +00:00
|
|
|
aruco_estimator = ArucoEstimator(use_realsense=False, robot_marker_ids=[12, 13])
|
2021-08-25 19:59:24 +00:00
|
|
|
|
|
|
|
# first we start thread for the measurement server
|
2021-09-07 20:31:28 +00:00
|
|
|
measurement_server = MeasurementServer(('0.0.0.0', 42424), MeasurementHandler, aruco_estimator,
|
|
|
|
max_measurements_per_second=30)
|
2021-08-25 19:59:24 +00:00
|
|
|
server_thread = threading.Thread(target=measurement_server.serve_forever)
|
|
|
|
server_thread.start()
|
|
|
|
|
|
|
|
# now we start the Aruco estimator GUI
|
|
|
|
aruco_estimator.process_frame()
|
|
|
|
import sys
|
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
|
|
|
|
|
|
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
|
|
|
QtGui.QApplication.instance().exec_()
|
2020-10-24 17:56:18 +00:00
|
|
|
|
2021-08-25 19:59:24 +00:00
|
|
|
#with MeasurementServer(('0.0.0.0', 42424), MeasurementHandler, aruco_estimator,
|
|
|
|
# max_measurements_per_second=30) as measurement_server:
|
|
|
|
# measurement_server.serve_forever()
|
2020-10-24 17:56:18 +00:00
|
|
|
|
2020-11-11 20:33:48 +00:00
|
|
|
# receive with: nc 127.0.0.1 42424 -> 12 + Enter
|