From 7f99f03feda544c24c928fa1bea0b527e4b856d2 Mon Sep 17 00:00:00 2001 From: joaquinchinobelmar Date: Sat, 27 Jul 2024 02:18:38 -0400 Subject: [PATCH] =?UTF-8?q?Implementaci=C3=B3n=20de=20archivo=20config.ini?= =?UTF-8?q?=20con=20su=20respectiva=20implementaci=C3=B3n=20para=20reempla?= =?UTF-8?q?zar=20(y=20eliminar)=20a=20constants.py,=20con=20todas=20sus=20?= =?UTF-8?q?variables.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python-engine/src/communications/radio.py | 13 ++++++++++-- .../src/communications/robot_comunication.py | 13 ++++++++++-- python-engine/src/communications/wrapper.py | 19 +++++++++++++---- python-engine/src/config.ini | 21 +++++++++++++++++++ python-engine/src/constants.py | 10 --------- 5 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 python-engine/src/config.ini delete mode 100644 python-engine/src/constants.py diff --git a/python-engine/src/communications/radio.py b/python-engine/src/communications/radio.py index adc1be0..8fac4d3 100644 --- a/python-engine/src/communications/radio.py +++ b/python-engine/src/communications/radio.py @@ -1,7 +1,8 @@ import threading import time import serial -import constants +import configparser +from pathlib import Path import numpy as np class Radio: @@ -28,7 +29,15 @@ def __init__(self): "r": rotational speed in rad/s} ''' # se enlaza al puerto serial de la base station - self.serial_port = serial.Serial(port= constants.SERIAL_PORT, baudrate=constants.BAUDRATE, timeout=constants.TIMEOUT) + config = configparser.ConfigParser() + config_path = Path(__file__).parent.parent / 'config.ini' + config.read(config_path) + + Serial_Port = config.get('Network', 'Serial_Port') + BaudRate = config.getint('Network', 'BaudRate') + Timeout = config.getint('Network', 'Timeout') + + self.serial_port = serial.Serial(port= Serial_Port, baudrate=BaudRate, timeout=Timeout) if not self.serial_port.is_open: raise ValueError("No se pudo abrir el puerto serial") diff --git a/python-engine/src/communications/robot_comunication.py b/python-engine/src/communications/robot_comunication.py index 96b1407..159b98d 100644 --- a/python-engine/src/communications/robot_comunication.py +++ b/python-engine/src/communications/robot_comunication.py @@ -1,7 +1,8 @@ import pickle import numpy as np -import constants +import configparser +from pathlib import Path import serial import serial.tools.list_ports @@ -13,7 +14,15 @@ def list_serial_ports(): class USBSerial: def __init__(self): - self.serial_port = serial.Serial(port= constants.SERIAL_PORT, baudrate=constants.BAUDRATE, timeout=constants.TIMEOUT) + config = configparser.ConfigParser() + config_path = Path(__file__).parent.parent / 'config.ini' + config.read(config_path) + + Serial_Port = config.get('Network', 'Serial_Port') + BaudRate = config.getint('Network', 'BaudRate') + Timeout = config.getint('Network', 'Timeout') + + self.serial_port = serial.Serial(port= Serial_Port, baudrate=BaudRate, timeout=Timeout) if not self.serial_port.is_open: raise ValueError("No se pudo abrir el puerto serial") else: diff --git a/python-engine/src/communications/wrapper.py b/python-engine/src/communications/wrapper.py index 38012bc..9f4021b 100644 --- a/python-engine/src/communications/wrapper.py +++ b/python-engine/src/communications/wrapper.py @@ -1,21 +1,32 @@ # Interfaz de la radio para comunicarse con sim y cancha . #Esto es para evitar la divergencia entre codigo simulador y cancha from communications.grsim import Grsim -from constants import * +import configparser +from pathlib import Path import math class CommandSender: def __init__(self) -> None: - if COMMUNICATION_MODE == 1: + config = configparser.ConfigParser() + config_path = Path(__file__).parent.parent / 'config.ini' + config.read(config_path) + + Communication_Mode = config.getint('Network', 'Communication_Mode') + if Communication_Mode == 1: self.grsim = Grsim() def send_robot_data(self, id = 0, is_blue = 0, velangular = 0, kickspeedx = 0, kickspeedz = 0, veltangent = 0, velnormal = 0, spinner = 0, wheelsspeed = False) -> None: - if COMMUNICATION_MODE == 1: + config = configparser.ConfigParser() + config_path = Path(__file__).parent.parent / 'config.ini' + config.read(config_path) + + Communication_Mode = config.getint('Network', 'Communication_Mode') + if Communication_Mode == 1: isteamyellow = 1 - is_blue self.grsim.communicate_grsim(id, isteamyellow, velangular, kickspeedx, kickspeedz, veltangent, velnormal, spinner, wheelsspeed) - + \ No newline at end of file diff --git a/python-engine/src/config.ini b/python-engine/src/config.ini new file mode 100644 index 0000000..122c3f8 --- /dev/null +++ b/python-engine/src/config.ini @@ -0,0 +1,21 @@ +[Geometry] +field_width = 9 +field_height = 6 + +[Network] + +Communication_Mode = 1 +# 0: Cancha, 1: Grsim + +Serial_Port = /dev/ttyUSB0 +BaudRate = 115200 +Timeout = 1 + +# Grsim +GrSim_Vision_Port = 10020 +GrSim_Command_Listen_Port = 20011 +GrSim_Multicast_Address = 224.5.23.2 + +[Physics] + +Update_Frequency = 1 \ No newline at end of file diff --git a/python-engine/src/constants.py b/python-engine/src/constants.py deleted file mode 100644 index 1154158..0000000 --- a/python-engine/src/constants.py +++ /dev/null @@ -1,10 +0,0 @@ -SERIAL_PORT = '/dev/ttyUSB0' -BAUDRATE = 115200 -TIMEOUT = 1 - -COMMUNICATION_MODE = 1 # 0: Cancha, 1: Grsim - -# Grsim -GRSIM_VISION_PORT = 10020 -GRSIM_COMMAND_LISTEN_PORT = 20011 -GRSIM_MULTICAST_ADDRESS = "224.5.23.2"