diff --git a/api/api_recv.py b/api/api_recv.py index 9f466874..00101a9d 100644 --- a/api/api_recv.py +++ b/api/api_recv.py @@ -16,7 +16,7 @@ def __init__(self, match, address, port): self.buffer_size = BUFFER_SIZE self.decod_data = None - self.kill_recieved = False + self.kill_received = False def connect_info(self,info_api): self.info_api = info_api @@ -27,7 +27,7 @@ def run(self): self.obj_socket.bind((self.address, self.port)) print("Starting api_recv...") - while not self.kill_recieved: + while not self.kill_received: data, origem = self.obj_socket.recvfrom(self.buffer_size) decoded_data = json.loads(data.decode()) # Feedback commands from socket (e.g. an interface) diff --git a/api/new_api.py b/api/new_api.py index 08a71cdb..ec052bcd 100644 --- a/api/new_api.py +++ b/api/new_api.py @@ -29,9 +29,10 @@ class Api(metaclass=SingletonMeta): #sender_thread: threading.Thread def __init__(self, address, port): + self.obj_socket: socket | None = None self.address = address self.port = port - self.kill_recieved = False + self.kill_received = False # Initiate socket connection def start(self): @@ -39,7 +40,7 @@ def start(self): # Sends dict game data to socket listener def send_data(self, info_api): - while not self.kill_recieved: + while not self.kill_received: data_dict = info_api.organize_send() msg = json.dumps(data_dict) self.obj_socket.sendto(msg.encode(), (self.address, self.port)) diff --git a/controller/Controller.py b/controller/Controller.py new file mode 100644 index 00000000..899e0ee8 --- /dev/null +++ b/controller/Controller.py @@ -0,0 +1,16 @@ + +class Controller(object): + v = 0.0; w = 0.0 + + def __init__(self, robot): + self.robot = robot + self.environment = robot.game.environment + self.match = robot.game.match + self.game = robot.game + + def set_desired(self, desired): + self.v = desired[0] + self.w = desired[1] + + def update(self): + return self.v, self.w diff --git a/controller/PID.py b/controller/PID.py index 019d7ba5..5daede98 100644 --- a/controller/PID.py +++ b/controller/PID.py @@ -2,6 +2,9 @@ import math import numpy as np +from controller.Controller import Controller + + class PID(object): def __init__(self, kp, kd ,ki, _ilimit=1000): self.desired_PID = 0.0 @@ -35,10 +38,9 @@ def update_PID(self, now, fps): return output -class Robot_PID(object): +class Robot_PID(Controller): def __init__(self, robot, send_data=False): - self.robot = robot - self.game = self.robot.game + super().__init__(robot) self.desired = np.array([0, 0]) self.linear_pid = PID(2, 1.2, 0) @@ -75,6 +77,7 @@ def update(self): def set_desired(self, vector): self.desired = vector + linear_desired = 0.0; angular_desired = 0 def update_Speed(self, linear_desired, angular_desired, now_linear, now_angular): self.linear_desired = linear_desired diff --git a/controller/PID_control.py b/controller/PID_control.py index 8f1cadaf..bee2005d 100644 --- a/controller/PID_control.py +++ b/controller/PID_control.py @@ -1,6 +1,7 @@ import math import numpy as np from commons.math import speed_to_power, angle_between +from controller.Controller import Controller def angle_adjustment(angle): @@ -12,7 +13,7 @@ def angle_adjustment(angle): return phi -class PID_control(object): +class PID_control(Controller): """ An implementation of the PID controller on linear and angular speed @@ -72,12 +73,10 @@ class PID_control(object): } def __init__(self, robot, default_fps=60, **kwargs): + super().__init__(robot) self.vision = robot.game.vision self.field_w, self.field_h = robot.game.field.get_dimensions() - self.robot = robot - self.match = robot.game.match self.desired = [0, 0] - self.environment = robot.game.environment self.l = self.robot.dimensions.get('L')/2 # half_distance_between_robot_wheels self.R = self.robot.dimensions.get('R') # radius of the wheel diff --git a/controller/noController.py b/controller/noController.py index 4e6d92c1..bf6c4c48 100644 --- a/controller/noController.py +++ b/controller/noController.py @@ -1,12 +1,13 @@ -class NoController(object): +from controller.Controller import Controller + + +class NoController(Controller): def __init__(self, robot): - self.robot = robot - self.environment = robot.game.environment - self.match = robot.game.match + super().__init__(robot) def set_desired(self, desired): self.v = desired[0] self.w = desired[1] def update(self): - return self.v, self.w \ No newline at end of file + return self.v, self.w diff --git a/controller/simple_LQR.py b/controller/simple_LQR.py index 2843467f..1d169fba 100644 --- a/controller/simple_LQR.py +++ b/controller/simple_LQR.py @@ -2,7 +2,9 @@ import numpy as np import numpy.linalg as la from commons.math import speed_to_power - +from controller.Controller import Controller + + def py_ang(v1, v2): """ Returns the angle in radians between vectors 'v1' and 'v2' """ cosang = np.dot(v1, v2) @@ -15,11 +17,10 @@ def py_ang(v1, v2): """ EXPERIMENTAL_SPEED_CONSTANT = 7000 -class SimpleLQR(object): +class SimpleLQR(Controller): def __init__(self, robot, l=0.185): + super().__init__(robot) self.desired = np.array([0, 0]) - self.robot = robot - self.environment = robot.game.environment self.l = l self.L = self.robot.dimensions.get('L') diff --git a/controller/uni_controller.py b/controller/uni_controller.py index 9a0a6295..f1aeccc9 100644 --- a/controller/uni_controller.py +++ b/controller/uni_controller.py @@ -1,6 +1,7 @@ import math import numpy as np from commons.math import speed_to_power +from controller.Controller import Controller """ Angle based controller @@ -8,7 +9,7 @@ """ -class UniController(object): +class UniController(Controller): """ An implementation of the Uni controller specified on the soccer robotics article @@ -32,7 +33,7 @@ class UniController(object): the target position used for calculating the linear speed P """ - CONSTANTS = { + CONSTANTS = { # TODO huh dá uma olhada nisso 'simulation': { 'V_M': 100, 'R_M': 3 * 100, # 3 * V_M @@ -48,9 +49,7 @@ class UniController(object): } def __init__(self, robot, control_speed=False): - self.robot = robot - self.environment = robot.game.environment - self.match = robot.game.match + super().__init__(robot) self.L = self.robot.dimensions.get("L") # m self.R = self.robot.dimensions.get("R") # m diff --git a/entities/coach/coach.py b/entities/coach/coach.py index c05e363c..c11d1e12 100644 --- a/entities/coach/coach.py +++ b/entities/coach/coach.py @@ -1,14 +1,16 @@ from abc import ABC, abstractmethod from os import replace + class BaseCoach(ABC): NAME = "BASE_COACH" + def __init__(self, match): self.match = match @abstractmethod - def decide (self): + def decide(self): raise NotImplementedError("Coach needs decide implementation!") def get_positions(self, foul, team_color, foul_color, quadrant): - return None \ No newline at end of file + pass diff --git a/fields/field.py b/entities/fields/field.py similarity index 56% rename from fields/field.py rename to entities/fields/field.py index dbedd334..79b75a7a 100644 --- a/fields/field.py +++ b/entities/fields/field.py @@ -1,34 +1,36 @@ import json +# TODO possivelmente mover o field para o pacote entities (só tá ele aqui) + class Field(): def __init__(self, category="3v3"): self.game_mode = category self.source = json.loads(open('fields.json', 'r').read()) self.field = self.source.get(self.game_mode) - #the dimensios are defined in fields.json and all of them are in meters + # the dimensions are defined in fields.json and all of them are in meters def get_dimensions(self): - #get field dimensions and return width and height + # get field dimensions and return width and height field_size = self.field.get('field_size', 0) return field_size def get_small_area(self, team): - #this function return the coordinates (x1, y1, width, height) of the small area based on arg team, - #that recieves team color + # this function return the coordinates (x1, y1, width, height) of the small area based on arg team, + # that receives team color areas = self.field.get('small_area', 0) small_area = areas.get(team) return small_area def get_quadrant_position(self, quad): - #quadrants are definied as the quadrants from trigonometry, therefore, q1 is in upper right corner - #and they are in counterclockwise. - #return quadrant positions (x, y) based on arg quad, that recieves an integer number. + # quadrants are defined as the quadrants from trigonometry, therefore, q1 is in upper right corner + # and they are in counterclockwise. + # return quadrant positions (x, y) based on arg quad, that receives an integer number. quadrants = self.field.get('quadrant_positions') quad_dimen = quadrants.get(f'q{quad}') return quad_dimen def get_free_kick_position(self, side): - #return free kick position (x, y) based on arg side, that recieves the field side + # return free kick position (x, y) based on arg side, that receives the field side free_kicks = self.field.get("free_kick") fk_pos = free_kicks.get(side) return fk_pos diff --git a/fields/__init__.py b/fields/__init__.py deleted file mode 100644 index e1a655e3..00000000 --- a/fields/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from fields.field import Field diff --git a/main.py b/main.py index f320ebb4..ad6ebca7 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import vision import match import argparse -import fields as pitch +from entities import fields as pitch from pyVSSSReferee.RefereeComm import RefereeComm from commons.utils import get_config import time diff --git a/main_real_life.py b/main_real_life.py index e85fa398..e9ef0d9f 100644 --- a/main_real_life.py +++ b/main_real_life.py @@ -2,7 +2,7 @@ import comm import match import argparse -import fields as pitch +from entities import fields as pitch from commons.utils import get_config from pyVSSSReferee.RefereeComm import RefereeComm from pySSLVision.VisionComm import SSLVision, assign_empty_values @@ -97,7 +97,7 @@ def update(self): def stop(self): for t in threading.enumerate(): - t.kill_recieved = True + t.kill_received = True diff --git a/match/match.py b/match/match.py index 0b1a628c..a4ef91b9 100644 --- a/match/match.py +++ b/match/match.py @@ -76,7 +76,7 @@ def update(self, frame): entity.update(frame) - def update_information(self, **kwargs): #Function to update values recieved in api + def update_information(self, **kwargs): #Function to update values received in api for key, value in kwargs.items(): if hasattr(self, key): setattr(self, key, value) diff --git a/match/match_real_life.py b/match/match_real_life.py index a0f66440..51d267c2 100644 --- a/match/match_real_life.py +++ b/match/match_real_life.py @@ -9,6 +9,7 @@ } class MatchRealLife(object): + # TODO possivelmente remover o Match e simplesmente usar o RealLife como Match? def __init__(self, game, team_side, team_color, coach_name=None, category="3v3", robot_ids=[0,1,2], opposite_ids=[0,1,2]): super().__init__() self.game = game @@ -94,7 +95,7 @@ def check_foul(self, ref): self.match_event['mine'] = ref.get_color() == self.team_color.upper() - def update_information(self, info): #Function to update values recieved in api + def update_information(self, info): #Function to update values received in api for key, value in info.items(): setattr(self, key.lower(), value) diff --git a/strategy/tests/newAttacker.py b/strategy/tests/newAttacker.py index 97497777..79cb718b 100644 --- a/strategy/tests/newAttacker.py +++ b/strategy/tests/newAttacker.py @@ -2,12 +2,8 @@ #from random import betavariate #from scipy.spatial.qhull import Voronoi import algorithms -import numpy as np -import controller -from fields.field import Field as fd from strategy.BaseStrategy import Strategy -from commons.math import unit_vector, distance -from strategy import DebugTools +from commons.math import distance from algorithms.astar import AStar, Node from algorithms.astar.fieldGraph import FieldGraph from scipy.spatial import Voronoi