2020-10-18 13:16:28 +00:00
|
|
|
import socket
|
2020-11-11 20:33:48 +00:00
|
|
|
import threading
|
|
|
|
import json
|
2021-08-25 20:53:09 +00:00
|
|
|
import numpy as np
|
|
|
|
import time
|
|
|
|
import math
|
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
|
2020-11-14 15:06:57 +00:00
|
|
|
self.port = 1234
|
2020-10-18 13:16:28 +00:00
|
|
|
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-11-17 20:23:22 +00:00
|
|
|
# mark thread as daemon -> it terminates automatically when program shuts down
|
|
|
|
self.measurement_thread.daemon = True
|
2020-11-14 15:06:57 +00:00
|
|
|
self.receiving = False
|
2020-11-11 20:33:48 +00:00
|
|
|
|
2020-10-18 13:16:28 +00:00
|
|
|
def connect(self):
|
|
|
|
# connect to robot
|
|
|
|
try:
|
2020-11-14 15:06:57 +00:00
|
|
|
print(f"connecting to robot {self.ip} at {self.ip}:{self.port} ...")
|
|
|
|
self.socket.connect((self.ip, self.port)) # connect to robot
|
2020-10-18 13:16:28 +00:00
|
|
|
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-14 15:06:57 +00:00
|
|
|
print(f"error: could not connect to robot {self.id} at {self.ip}:{self.port}")
|
2020-11-11 20:33:48 +00:00
|
|
|
|
|
|
|
# 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)
|
2020-11-17 20:23:22 +00:00
|
|
|
if 'error' not in str(response):
|
2020-11-14 15:06:57 +00:00
|
|
|
print("... connected! -> start listening for measurements")
|
2020-11-11 20:33:48 +00:00
|
|
|
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):
|
2020-11-14 15:06:57 +00:00
|
|
|
if self.socket and self.connected:
|
2020-10-18 13:16:28 +00:00
|
|
|
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-14 15:06:57 +00:00
|
|
|
else:
|
|
|
|
print(f"error: robot {self.id} is not connected to {self.ip}")
|
2020-11-11 20:33:48 +00:00
|
|
|
|
|
|
|
def receive_measurements(self):
|
2020-11-14 15:06:57 +00:00
|
|
|
self.receiving = True
|
|
|
|
while self.receiving:
|
2020-11-11 20:33:48 +00:00
|
|
|
received = str(self.measurement_socket.recv(1024), "utf-8")
|
|
|
|
if len(received) > 0:
|
2020-11-14 15:06:57 +00:00
|
|
|
last_received = received.split('\n')[-2]
|
|
|
|
measurement = json.loads(last_received)
|
2020-11-11 20:33:48 +00:00
|
|
|
self.t_last_measurement = measurement['t']
|
|
|
|
self.x = measurement['x']
|
|
|
|
self.y = measurement['y']
|
|
|
|
self.angle = measurement['angle']
|
|
|
|
else:
|
2020-11-14 15:06:57 +00:00
|
|
|
self.receiving = False
|
2020-11-11 20:33:48 +00:00
|
|
|
print(f"measurement server stopped sending data for robot {self.id}")
|
|
|
|
|
|
|
|
def get_measurement(self):
|
2020-11-17 20:23:22 +00:00
|
|
|
return self.t_last_measurement, self.x, self.y, self.angle
|
2020-11-14 15:06:57 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-08-30 22:05:16 +00:00
|
|
|
connection_state = '' if self.connected else ' not'
|
2020-11-14 15:06:57 +00:00
|
|
|
state = self.get_measurement()
|
2020-11-17 20:23:22 +00:00
|
|
|
last_measurement = f'last measurement = {state}' if None not in state else 'no measurements available'
|
2021-08-30 22:05:16 +00:00
|
|
|
return f"Robot {self.id}: ip = {self.ip}:{self.port} ({connection_state} connected ) " + last_measurement
|
2020-11-14 15:06:57 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
|
|
|
class ControlledRobot(Robot):
|
|
|
|
def __init__(self, marker_id, ip):
|
|
|
|
super().__init__(marker_id, ip)
|
|
|
|
self.controller = None
|
|
|
|
|
|
|
|
def start_control(self):
|
|
|
|
if self.controller is not None:
|
2021-09-09 19:36:38 +00:00
|
|
|
print("starting control")
|
2020-11-14 15:06:57 +00:00
|
|
|
self.controller.start()
|
|
|
|
else:
|
|
|
|
raise Exception("Error: Cannot start control: there is not controller attached to the robot!")
|
|
|
|
|
|
|
|
def stop_control(self):
|
|
|
|
if self.controller is not None:
|
2021-09-09 19:36:38 +00:00
|
|
|
print("stopping control")
|
2020-11-14 15:06:57 +00:00
|
|
|
self.controller.stop()
|
|
|
|
else:
|
|
|
|
raise Exception("Error: Cannot stop control: there is not controller attached to the robot!")
|
|
|
|
|
|
|
|
def attach_controller(self, controller):
|
|
|
|
self.controller = controller
|
|
|
|
self.controller.attach_robot(self)
|
|
|
|
|
2021-08-25 20:53:09 +00:00
|
|
|
def move_to_pos(self, target_pos, blocking=False):
|
2020-11-14 15:06:57 +00:00
|
|
|
if self.controller is not None:
|
2021-08-25 20:53:09 +00:00
|
|
|
if not blocking:
|
|
|
|
self.controller.set_target_position(target_pos)
|
|
|
|
else: # only return after the robot has reached the target
|
|
|
|
self.stop_control()
|
|
|
|
self.controller.set_target_position(target_pos)
|
|
|
|
self.start_control()
|
|
|
|
|
|
|
|
close_to_target = False
|
|
|
|
while not close_to_target:
|
|
|
|
current_pos = np.array([self.x, self.y, self.angle])
|
|
|
|
v = target_pos - current_pos
|
|
|
|
angles_unwrapped = np.unwrap([current_pos[2], target_pos[2]]) # unwrap angle to avoid jump in data
|
|
|
|
e_angle = angles_unwrapped[0] - angles_unwrapped[1] # angle difference
|
|
|
|
e_pos = np.linalg.norm(v[0:2])
|
2021-09-09 19:36:38 +00:00
|
|
|
#print(f"target_pos = {target_pos}, current_pos = {current_pos}, e_pos = {e_pos}, e_ang = {e_angle}")
|
2021-08-25 20:53:09 +00:00
|
|
|
print(f"e_pos = {e_pos}, e_ang = {e_angle}")
|
2021-09-09 23:23:14 +00:00
|
|
|
close_to_target = e_pos < 0.075 and abs(e_angle) < 0.1
|
2021-08-25 20:53:09 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
print("target reached!")
|
|
|
|
self.stop_control()
|
2020-11-14 15:06:57 +00:00
|
|
|
else:
|
2021-08-25 20:53:09 +00:00
|
|
|
raise Exception("Error: Cannot move to position: there is not controller attached to the robot!")
|