Exemple #1
0
    def init_command(self, init_list):
        """
        :param init_list: The list of pins to be initialized.
        :return: True for successful execution.
        Creates the initialization command for later execution.
        """
        if isinstance(init_list, list):
            if all(isinstance(command, tuple) for command in init_list):
                if all(len(command) == 2 for command in init_list):
                    if all(
                            isinstance(command[0], numbers.Number)
                            for command in init_list):
                        if all(
                                isinstance(command[1], bool)
                                for command in init_list):
                            if self.servos:
                                for command in init_list:
                                    pin = command[0]
                                    servo = next(servo for servo in self.servos
                                                 if servo.pin == pin)

                                    if servo:
                                        init_list = [
                                            command for command in init_list
                                            if command[0] != pin
                                        ]

                                        if command[1]:
                                            if Controller.execute_servo(
                                                    servo, servo.init_pulse):
                                                self.error(
                                                    'Execution of single command was successful'
                                                )
                                            else:
                                                self.error(
                                                    'Execution of single command failed'
                                                )
                                                return False
                                        else:
                                            if Controller.add_servo_to_stack(
                                                    servo, servo.init_pulse):
                                                self.error(
                                                    'Command was added to Execution-list.'
                                                )
                                            else:
                                                self.error(
                                                    'Command could not be added to Execution-list.'
                                                )
                                                return False
                                    else:
                                        self.error('Desired servo with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                            if self.motors:
                                for command in init_list:
                                    pin = command[0]
                                    motor = next(motor for motor in self.motors
                                                 if motor.pin == pin)

                                    if motor:
                                        if command[1]:
                                            if Controller.execute_motor(
                                                    motor,
                                                    motor.init_direction,
                                                    motor.init_percentage):
                                                self.error(
                                                    'Execution of single command was successful'
                                                )
                                            else:
                                                self.error(
                                                    'Execution of single command failed'
                                                )
                                                return False
                                        else:
                                            if Controller.add_motor_to_stack(
                                                    motor,
                                                    motor.init_direction,
                                                    motor.init_percentage):
                                                self.error(
                                                    'Command was added to Execution-list.'
                                                )
                                            else:
                                                self.error(
                                                    'Command could not be added to Execution-list.'
                                                )
                                                return False
                                    else:
                                        self.error('Desired motor with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                            if self.servos or self.motors:
                                return True
                            else:
                                self.error('No servo or motor list available.')
                                return False
                        else:
                            self.error(
                                'Unexpected tuple element. Second element must be a boolean.'
                            )
                            return False
                    else:
                        self.error(
                            'Unexpected tuple element. First element must be a number'
                        )
                        return False
                else:
                    self.error('Unexpected tuple size. Expected tuple of two')
                    return False
            else:
                self.error('Unexpected list element. Expected list of tuples')
                return False
        else:
            self.error('Unexpected input: ' + type(init_list).__name__ +
                       '. Expected: list')
            return False
Exemple #2
0
    def move_motor(self, command_list):
        """
        :param command_list: List of commands to be executed.
        :return: True for successful execution.
        Moves motor according to command_list.
        """
        if self.motors:
            if isinstance(command_list, list):
                if all(isinstance(command, tuple) for command in command_list):
                    if all(len(command) == 4 for command in command_list):
                        if all(
                                isinstance(number, numbers.Number)
                                for command in command_list
                                for number in command[:2]):
                            if all(
                                    isinstance(command[3], bool)
                                    for command in command_list):
                                for command in command_list:
                                    pin = command[0]
                                    direction = command[1]
                                    percent = command[2]
                                    motor = next(motor for motor in self.motors
                                                 if motor.pin == pin)

                                    if motor:
                                        if direction in [-1, 0, 1]:
                                            if 0 <= percent <= 100:
                                                if command[2]:
                                                    if Controller.execute_motor(
                                                            motor, direction,
                                                            percent):
                                                        self.error(
                                                            'Execution of single command was successful'
                                                        )
                                                    else:
                                                        self.error(
                                                            'Execution of single command failed'
                                                        )
                                                        return False
                                                else:
                                                    if Controller.add_motor_to_stack(
                                                            motor, direction,
                                                            percent):
                                                        self.error(
                                                            'Command was added to Execution-list.'
                                                        )
                                                    else:
                                                        self.error(
                                                            'Command could not be added to Execution-list.'
                                                        )
                                                        return False
                                            else:
                                                self.error(
                                                    'Invalid percentage. Percentage must be beween 0 and 100.'
                                                )
                                                return False
                                        else:
                                            self.error(
                                                'Invalid direction. Direction must be 1 or -1.'
                                            )
                                            return False
                                    else:
                                        self.error('Desired motor with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                                return True

                            else:
                                self.error(
                                    'Unexpected tuple element. Last Element must be a boolean.'
                                )
                                return False
                        else:
                            self.error(
                                'Unexpected tuple element. First three items must be of type Number.'
                            )
                            return False
                    else:
                        self.error(
                            'Unexpected tuple size. Expected tuple of three')
                        return False
                else:
                    self.error(
                        'Unexpected list element. Expected list of tuples')
                    return False
            else:
                self.error('Unexpected input: ' + type(command_list).__name__ +
                           '. Expected: list')
                return False
        else:
            self.error('Component does not contain any motors.')
            return False