Example #1
0
    def _tick_update_data(self):
        """
        Function used to update the data manager with the current controller values depending on the driving mode.
        """

        # Check if the non-manual mode is one
        if self.driving_mode != self.DrivingMode.MANUAL:

            # If autonomous mode is on
            if self.driving_mode == self.DrivingMode.AUTONOMOUS:

                # Auto-drive the ROV
                self._auto_drive()

            # If self-balancing mode is on
            else:

                # Auto-balance the ROV
                self._auto_balance()

        # Iterate over all keys that should be updated (use copy of the set to avoid runtime concurrency errors)
        for key in self._data_manager_keys:

            # Fetch the current attribute's value
            value = self.__getattribute__(self._data_manager_map[key])

            # Check if the last saved value and the current reading mismatch
            if key not in self._data_manager_last_saved or self._data_manager_last_saved[
                    key] != value:

                # Update the corresponding values
                dm.set_data(**{key: value})
                self._data_manager_last_saved[key] = value
Example #2
0
    def _on_surface_disconnected(self):
        """

        Function used to clean up any resources or set appropriate flags when a connection to surface is closed.

        """

        # Close the socket
        self._client_socket.close()

        # Inform that the connection has been closed
        print("Connection from {} address closed successfully".format(self._client_address))

        # Set the keys to their default values, BEWARE: might add keys that haven't yet been received from surface
        dm.set_data(dm.SURFACE, **dm.DEFAULT)
Example #3
0
    def _handle_data(self):
        """

        Function used to exchange and process the data.

        ** Modifications **

            1. Modify any try, except blocks to change the error-handling (keep in mind to use the DataError exception).

        """

        # Send current state of the data
        self._serial.write(bytes(dumps(dm.get_data(self._id, transmit=True)) + "\n", encoding='utf-8'))

        # Read until the specified character is found ("\n" by default)
        data = self._serial.read_until()

        # Convert bytes to string, remove white spaces, ignore invalid data
        try:
            data = data.decode("utf-8").strip()
        except UnicodeDecodeError:
            data = None

        # Handle valid data
        if data:

            try:
                # Attempt to decode the JSON data
                data = loads(data)

                # Override the ID
                self._id = data["deviceID"]

                # Update the Arduino data (and the surface data)
                dm.set_data(self._id, **data)

            except JSONDecodeError:
                print("Received invalid data: {}".format(data))
                raise self.DataError
            except KeyError:
                print("Received valid data with invalid ID: {}".format(data))
                raise self.DataError

        # Delay the communication to allow the Arduino to catch up
        sleep(self._COMMUNICATION_DELAY)
Example #4
0
    def _tick_update_data(self):
        """
        Function used to update the data manager with the current controller values.
        """

        # Iterate over all keys that should be updated (use copy of the set to avoid runtime concurrency errors)
        for key in self._data_manager_keys:

            # Fetch the current attribute's value
            value = self.__getattribute__(self._data_manager_map[key])

            # Check if the last saved value and the current reading mismatch
            if key not in self._data_manager_last_saved or self._data_manager_last_saved[
                    key] != value:

                # Update the corresponding values
                dm.set_data(**{key: value})
                self._data_manager_last_saved[key] = value
Example #5
0
    def _handle_data(self):
        """

        Function used to exchange and process the data.

        ** Modifications **

            1. Modify any try, except blocks to change the error-handling (keep in mind to use the DataError exception).

        """

        # Once connected, keep receiving and sending the data, raise exception in case of errors
        try:
            data = self._client_socket.recv(4096)

            # If 0-byte was received, close the connection
            if not data:
                raise self.DataError

        except (ConnectionResetError, ConnectionAbortedError, socket.timeout):
            raise self.DataError

        # Convert bytes to string, remove the white spaces, ignore any invalid data
        try:
            data = data.decode("utf-8").strip()
        except UnicodeDecodeError:
            data = None

        # Handle valid data
        if data:

            # Attempt to decode from JSON, inform about invalid data received
            try:
                dm.set_data(dm.SURFACE, **loads(data))
            except JSONDecodeError:
                print("Received invalid data: {}".format(data))

        # Send the current state of the data manager, break in case of errors
        try:
            self._client_socket.sendall(bytes(dumps(
                dm.get_data(dm.SURFACE, transmit=True)), encoding="utf-8"))

        except (ConnectionResetError, ConnectionAbortedError, socket.timeout):
            raise self.DataError
Example #6
0
    def _handle_data(self):
        """
        Function used to receive and send the processed data.

        Any data-related modifications should be introduced here, preferably encapsulated in another function.
        """

        # Once connected, keep receiving and sending the data, raise exception in case of errors
        try:
            # Send current state of the data manager
            self._socket.sendall(
                bytes(dumps(dm.get_data(transmit=True)), encoding="utf-8"))

            # Receive the data
            data = self._socket.recv(4096)

            # If 0-byte was received, raise exception
            if not data:
                sleep(self._RECONNECT_DELAY)
                raise self.DataError

        except (ConnectionResetError, ConnectionAbortedError, socket.error):
            sleep(self._RECONNECT_DELAY)
            raise self.DataError

        # Convert bytes to string, remove white spaces, ignore invalid data
        try:
            data = data.decode("utf-8").strip()
        except UnicodeDecodeError:
            data = None

        # Handle valid data
        if data:

            # Attempt to decode from JSON, inform about invalid data received
            try:
                dm.set_data(**loads(data))
            except JSONDecodeError:
                print("Received invalid data: {}".format(data))
Example #7
0
    print("Initialising...")

    # Create a new QT application instance
    app = QApplication()

    # Create a new Manager instance
    manager = Manager()

    # Get the loading screen
    loading = manager.get_current_screen()

    # Clear the cache on start
    dm.clear()

    # Set some default values for the sonar
    dm.set_data(Sen_Sonar_Start=5, Sen_Sonar_Len=30)

    # Update the progress
    loading.progress = 10

    # Initialise and start the server connection
    connection = Connection(ip=ip, port=50000)

    # Initialise the port iterator
    port = 50010

    # Initialise the video streams
    streams = [
        VideoStream(ip=ip, port=p) for p in range(port, port + CAMERAS_COUNT)
    ]