diff --git a/lib/APPReinforcementLearning/src/App.h b/lib/APPReinforcementLearning/src/App.h index 40b56323..8615bfa0 100644 --- a/lib/APPReinforcementLearning/src/App.h +++ b/lib/APPReinforcementLearning/src/App.h @@ -138,7 +138,7 @@ class App /** SerialMuxProt Server Instance. */ SMPServer m_smpServer; - /* Ensue that the mode is only sent once*/ + /** Ensue that the mode is only sent once */ bool m_modeSelectionSent; /** diff --git a/lib/APPReinforcementLearning/src/SerialMuxchannels.h b/lib/APPReinforcementLearning/src/SerialMuxchannels.h index a376d66d..75daf582 100644 --- a/lib/APPReinforcementLearning/src/SerialMuxchannels.h +++ b/lib/APPReinforcementLearning/src/SerialMuxchannels.h @@ -102,12 +102,13 @@ namespace SMPChannelPayload } Status; /**< Status flag */ + /** Mode flags */ typedef enum : uint8_t { TRAINING_MODE = 0, /**< Driving Mode Selected. */ DRIVING_MODE /**< Training Mode Selected. */ - } Mode; /**< Status flag */ + } Mode; /**< Mode flag */ } // namespace SMPChannelPayload diff --git a/webots/controllers/RL_Supervisor/Serial_webots.py b/webots/controllers/RL_Supervisor/Serial_webots.py index a0e27d9c..7da0566a 100644 --- a/webots/controllers/RL_Supervisor/Serial_webots.py +++ b/webots/controllers/RL_Supervisor/Serial_webots.py @@ -1,13 +1,41 @@ +""" Implementation of a Serial Webots for Serial Communication """ + +# MIT License +# +# Copyright (c) 2023 - 2024 Gabryel Reyes +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# + + ################################################################################ # Imports ################################################################################ -from controller import device + +from controller import device # pylint: disable=import-error from SerialMuxProt import Stream ################################################################################ # Classes ################################################################################ + class SerialWebots(Stream): """ Serial Webots Communication Class @@ -44,40 +72,50 @@ def write(self, payload: bytearray) -> int: """ self.__emitter.send(bytes(payload)) bytes_sent = len(payload) + return bytes_sent def available(self) -> int: - """ - Check if there is anything available for reading. + """Check if there is anything available for reading. Returns ---------- - int - Number of bytes that are available for reading. + Number of bytes that are available for reading. """ + if len(self.__buffer) > 0: return len(self.__buffer) - elif self.__receiver.getQueueLength() > 0: + + if self.__receiver.getQueueLength() > 0: return self.__receiver.getDataSize() + return 0 def read_bytes(self, length: int) -> tuple[int, bytearray]: - """ - Read a given number of Bytes from Serial. + """Read a given number of Bytes from Serial. + + Parameters + ---------- + lenght : int + Number of bytes to read. Returns ---------- - tuple[int, bytearray] - - int: Number of bytes received. - - bytearray: Received data. + Tuple: + - int: Number of bytes received. + - bytearray: Received data. """ + read = 0 data = bytearray() + # Check if there is data in the buffer. Read the specified Bytes. if len(self.__buffer) > 0: read = min(len(self.__buffer), length) data = self.__buffer[:read] self.__buffer = self.__buffer[read:] + + # Check if there is data in the receiver queue and process it elif self.__receiver.getQueueLength() > 0: received_data = self.__receiver.getBytes() received_data_size = self.__receiver.getDataSize() @@ -93,6 +131,7 @@ def read_bytes(self, length: int) -> tuple[int, bytearray]: return read, data + ################################################################################ # Functions ################################################################################