Skip to content

Commit

Permalink
Add comments and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Akram authored and Akram committed Jul 30, 2024
1 parent 607656e commit 4b99c27
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/APPReinforcementLearning/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/APPReinforcementLearning/src/SerialMuxchannels.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
61 changes: 50 additions & 11 deletions webots/controllers/RL_Supervisor/Serial_webots.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
""" Implementation of a Serial Webots for Serial Communication """

# MIT License
#
# Copyright (c) 2023 - 2024 Gabryel Reyes <[email protected]>
#
# 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
Expand Down Expand Up @@ -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()
Expand All @@ -93,6 +131,7 @@ def read_bytes(self, length: int) -> tuple[int, bytearray]:

return read, data


################################################################################
# Functions
################################################################################
Expand Down

0 comments on commit 4b99c27

Please sign in to comment.