2020-10-18 13:16:28 +00:00
|
|
|
import socket
|
2020-11-11 20:33:48 +00:00
|
|
|
import threading
|
|
|
|
import json
|
|
|
|
|
2020-10-18 13:16:28 +00:00
|
|
|
|
|
|
|
class Robot:
|
2020-11-11 20:33:48 +00:00
|
|
|
def __init__(self, marker_id, ip, measurement_server=('127.0.0.1', 42424)):
|
2020-10-18 13:16:28 +00:00
|
|
|
self.id = marker_id
|
2020-11-11 20:33:48 +00:00
|
|
|
|
|
|
|
self.t_last_measurement = None
|
|
|
|
self.x = None
|
|
|
|
self.y = None
|
|
|
|
self.angle = None
|
2020-10-18 13:16:28 +00:00
|
|
|
|
|
|
|
self.ip = ip
|
|
|
|
self.socket = socket.socket()
|
|
|
|
|
|
|
|
# currently active control
|
|
|
|
self.u1 = 0.0
|
|
|
|
self.u2 = 0.0
|
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
self.connected = False
|
|
|
|
|
2020-11-11 20:33:48 +00:00
|
|
|
self.measurement_server = measurement_server
|
|
|
|
self.measurement_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP socket
|
|
|
|
self.measurement_thread = threading.Thread(target=self.receive_measurements)
|
|
|
|
|
|
|
|
|
2020-10-18 13:16:28 +00:00
|
|
|
def connect(self):
|
|
|
|
# connect to robot
|
|
|
|
try:
|
|
|
|
print("connecting to robot {} with ip {} ...".format(self.id, self.ip))
|
|
|
|
self.socket.connect((self.ip, 1234)) # connect to robot
|
|
|
|
print("connected!")
|
2020-10-18 16:03:33 +00:00
|
|
|
self.connected = True
|
2020-10-18 13:16:28 +00:00
|
|
|
except socket.error:
|
2020-11-11 20:33:48 +00:00
|
|
|
print("error: could not connect to robot {} with ip {}".format(self.id, self.ip))
|
|
|
|
|
|
|
|
# connect to measurement server
|
|
|
|
print(f"connecting to measurement server on {self.measurement_server} ...")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.measurement_socket.connect(self.measurement_server)
|
|
|
|
self.measurement_socket.sendall(f"{self.id}\n".encode())
|
|
|
|
|
|
|
|
self.measurement_socket.settimeout(0.1)
|
|
|
|
# check if we receive data from the measurement server
|
|
|
|
response = self.measurement_socket.recv(1024)
|
|
|
|
if not 'error' in str(response):
|
|
|
|
print("... connected! -> start listening for events")
|
|
|
|
self.measurement_socket.settimeout(None)
|
|
|
|
# if so we start the measurement thread
|
|
|
|
self.measurement_thread.start()
|
|
|
|
else:
|
|
|
|
print(f"error: cannot communicate with the measurement server.\n The response was: {response}")
|
|
|
|
except socket.timeout:
|
|
|
|
print(f"error: the measurement server did not respond with data.")
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
print(f"error: could not connect to measurement server at {self.measurement_server}.")
|
|
|
|
|
2020-10-18 13:16:28 +00:00
|
|
|
|
|
|
|
def send_cmd(self, u1=0.0, u2=0.0):
|
|
|
|
if self.socket:
|
|
|
|
try:
|
|
|
|
self.socket.send(f'({u1},{u2})\n'.encode())
|
|
|
|
except BrokenPipeError:
|
2020-10-18 16:03:33 +00:00
|
|
|
print(f"error: connection to robot {self.id} with ip {self.ip} lost")
|
2020-10-18 13:16:28 +00:00
|
|
|
pass
|
2020-10-24 18:01:16 +00:00
|
|
|
except ConnectionResetError:
|
|
|
|
print(f"error: connection to robot {self.id} with ip {self.ip} lost")
|
|
|
|
pass
|
2020-11-11 20:33:48 +00:00
|
|
|
|
|
|
|
def receive_measurements(self):
|
|
|
|
receiving = True
|
|
|
|
while receiving:
|
|
|
|
received = str(self.measurement_socket.recv(1024), "utf-8")
|
|
|
|
if len(received) > 0:
|
|
|
|
measurement = json.loads(received)
|
|
|
|
self.t_last_measurement = measurement['t']
|
|
|
|
self.x = measurement['x']
|
|
|
|
self.y = measurement['y']
|
|
|
|
self.angle = measurement['angle']
|
|
|
|
else:
|
|
|
|
receiving = False
|
|
|
|
print(f"measurement server stopped sending data for robot {self.id}")
|
|
|
|
|
|
|
|
def get_measurement(self):
|
|
|
|
return (self.t_last_measurement, self.x, self.y, self.angle)
|