Exemplo n.º 1
0
    def update_data(self):

        # Only handle valid keys
        try:

            # Fetch the value from the data manager and update the text
            self.setText("{}: {}".format(self._key, dm.get_data(self._key)[self._key]))

        except KeyError:
            pass
Exemplo n.º 2
0
    def driving_mode(self, value):
        """
        Setter for the driving mode. Throws :class:`AttributeError` if the value passed is not a :class:`DrivingMode`.

        :param value: Driving mode to be set
        """

        # Assign the value if it is a driving mode
        if isinstance(value, self.DrivingMode):

            # Set the default values (to turn the ROV motion off)
            self._set_default_values()

            # Set the driving mode
            self._driving_mode = value

            # If the automatic mode is being turned on
            if value == self.DrivingMode.AUTONOMOUS:

                # Fetch the yaw data and set the default yaw, pitch and roll
                self._default_yaw = float(
                    dm.get_data("Sen_IMU_X")["Sen_IMU_X"])
                self._default_pitch = DEFAULT_PITCH
                self._default_roll = DEFAULT_ROLL

            # If the balancing mode is being turned on
            elif value == self.DrivingMode.BALANCING:

                # Fetch the sensor values
                sensors = dm.get_data("Sen_IMU_X", "Sen_IMU_Y", "Sen_IMU_Z")

                # Set the default yaw, pitch and roll
                self._default_yaw = float(sensors["Sen_IMU_X"])
                self._default_pitch = float(sensors["Sen_IMU_Y"])
                self._default_roll = float(sensors["Sen_IMU_Z"])

        # Otherwise raise an error
        else:
            raise AttributeError
Exemplo n.º 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)
Exemplo n.º 4
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
Exemplo n.º 5
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))
Exemplo n.º 6
0
def blocking_test_text_debug():
    while True:
        print(dm.get_data(dm.SURFACE), dm.get_data(dm.ARDUINO_A),
              dm.get_data(dm.ARDUINO_I), dm.get_data(dm.ARDUINO_M))
        sleep(0.5)
Exemplo n.º 7
0
    def _auto_balance(self):
        """
        Function used to balance the ROV automatically. Roll, pitch and yaw are affected.
        """
        print("Auto balance ON")

        # Fetch the sensor values
        sensors = dm.get_data("Sen_IMU_X", "Sen_IMU_Y", "Sen_IMU_Z")

        # Extract the roll, yaw and pitch values
        yaw = float(sensors["Sen_IMU_X"])
        pitch = float(sensors["Sen_IMU_Y"])
        roll = float(sensors["Sen_IMU_Z"])

        # Check if the ROV is rolled too much
        if abs(self._default_roll - roll) > BALANCE_TOLERANCE:

            # If the ROV is rolled starboard
            if roll < self._default_roll:

                # Adjust the roll sensitivity
                self._roll_sensitivity = normalise(
                    abs(roll - self._default_roll), 0,
                    abs(-90 - self._default_roll), 0, self._button_sensitivity)

                # Roll pot
                self.button_X = True
                self.button_B = False

            # If the ROV is rolled pot
            else:

                # Adjust the roll sensitivity
                self._roll_sensitivity = normalise(
                    abs(self._default_roll - roll), 0,
                    abs(self._default_roll - 90), 0, self._button_sensitivity)

                # Roll starboard
                self.button_B = True
                self.button_X = False

        # Check if the ROV is pitched too much
        if abs(self._default_pitch - pitch) > BALANCE_TOLERANCE:

            # Adjust the pitch
            self.right_axis_y = normalise(pitch, self._default_pitch - 90,
                                          self._default_pitch + 90,
                                          self._AXIS_MIN, self._AXIS_MAX)

        # Calculate the yaw difference
        yaw_difference = self._default_yaw - yaw

        # Check if the ROV is yawed too much
        if abs(yaw_difference) > BALANCE_TOLERANCE:

            # If it's easier to yaw the ROV starboard
            if 0 >= yaw_difference <= 180:

                # Yaw right
                self.left_trigger = normalise(yaw_difference, 0, 180,
                                              self._TRIGGER_MIN,
                                              self._TRIGGER_MAX)
                self.right_trigger = self._TRIGGER_MIN

            # If it's easier to yaw the ROV pot
            else:

                # Yaw left
                self.right_trigger = normalise(abs(yaw_difference), 0, 180,
                                               self._TRIGGER_MIN,
                                               self._TRIGGER_MAX)
                self.left_trigger = self._TRIGGER_MIN