def __init__(self, component): """ :param component: Name of component, i.e 'left_arm'. """ self.component = str(component).lower() self.servos = [] self.motors = [] tmp_servo_list = Component.constants['components'][ self.component]['servo_list'] tmp_motor_list = Component.constants['components'][ self.component]['motor_list'] if tmp_servo_list: self.servos = [ ServoMotor(value['dependencies'], value['pin'], value['max_pulse'], value['min_pulse'], value['abs_max_pulse'], value['abs_min_pulse'], value['pulse_width'], value['init_pulse']) for (key, value) in tmp_servo_list.items() ] if tmp_motor_list: self.motors = [ GearedMotor(value['pin'], value['init_percentage'], value['init_direction'], value['pulse_width']) for (key, value) in tmp_motor_list.items() ] Controller.add_servos(self.servos)
def execute_stack(): """ :return: True for successful execution. Parallel execution of pending commands on the SSC32U board. """ try: Controller.execute_stack() return True except: print("Executing stack failed!") raise
def validate_results(feedback_list, method): """ :param feedback_list: List of booleans corresponding to the return value of a method. :param method: Method name, i.e initialize. :return: True if all booleans in feedback_list are True. Validates results of executed method and executes stack. """ if False not in feedback_list: Controller.execute_stack() print('JohnnyV: ' + method + ' was successful.') return True else: print('JohnnyV: ' + method + ' failed. Commands could not be written to SSC board.') return False
def range(self): """ :return: Call to Controller.sensor_range(). Method to return raw range of SRF08 sensor. """ return Controller.sensor_range(self)
def light_level(self): """ :return: Call to Controller.light_level(). Method for reading current light level of SRF08 sensor. """ return Controller.light_level(self)
def measure_range(self, unit=None): """ :param unit: Unit of measurement, i.e 'inches'. Standard is on 'centimeter'. :return: Call to Controller.measure_range(). Method for returning aggregated range. """ if unit is None: return Controller.measure_range(self, self.unit) else: if isinstance(unit, str): if unit in self.unit_set: return Controller.measure_range(self, unit) else: print("Camera: Measure Range must be in " + self.unit_set) return False else: print("Camera: Measure Range must be a string).") return False
def write(self, unit=None): """ :param unit: Unit of measurement, i.e 'inches'. Standard is on 'centimeter'. :return: Call to Controller.write_to_sensor(). Method to write commands to sensor. """ if unit is None: return Controller.write_to_sensor(self, self.unit) else: if isinstance(unit, str): if unit in self.unit_set: return Controller.write_to_sensor(self, unit) else: print("Camera: Measure Range must be in " + self.unit_set) return False else: print("Camera: Measure Range must be a string).") return False
def __init__(self, sensor_id): """ :param sensor_id: ID/Name of sensor. """ self.sensor_id = sensor_id self.sensor_specs = SRF08.constants['sensors'][sensor_id] if self.sensor_specs: self.sensor_addr = Controller.get_sensor_addr() self.unit = self.sensor_specs["unit"] self.unit_set = self.sensor_specs["unit_set"] else: print(self.sensor_id+": No specifications found for given Sensor-ID.")
def __init__(self): self.components = { key: Component(key) for (key, value) in self.constants['components'].items() } self.peripherals = { key: Camera(key) for (key, value) in self.constants['cameras'].items() } self.peripherals.update({ key: SRF08(key) for (key, value) in self.constants['sensors'].items() }) self.controller = Controller()
def capture(self, file_name): """ :param file_name: Name of the picture that will be taken without extension. :return: Call to Controller.capture(). Creates a picture with given filename. """ if isinstance(file_name, str): pattern = re.compile("^([a-zA-Z0-9]+)$") if pattern.match(file_name): return Controller.capture(self, file_name) else: print( "Camera: File_name does not fit pattern: ^([a-zA-Z0-9]+)$") return False else: print("Camera: File_name must be a string.") return False
def record(self, file_name, duration): """ :param file_name: Name of the video that will be taken without extension. :param duration: Recording time. :return: Call to Controller.record(). Creates a video with given filename. """ if isinstance(file_name, str): pattern = re.compile("^([a-zA-Z0-9]+)$") if pattern.match(file_name): if isinstance(duration, int) and duration > 0: return Controller.record(self, file_name, duration) else: print("Camera: Duration must be a integer greater 0.") return False else: print( "Camera: File_name does not fit pattern: ^([a-zA-Z0-9]+)$") return False else: print("Camera: File_name must be a string.") return False
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
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
def move_servo(self, command_list): """ :param command_list: List of commands to be executed. :return: True for successful execution. Move servos according to the command_list. """ if self.servos: if isinstance(command_list, list): if all(isinstance(command, tuple) for command in command_list): if all(len(command) == 3 for command in command_list): if all( isinstance(number, numbers.Number) for command in command_list for number in command[:1]): if all( isinstance(command[2], bool) for command in command_list): for command in command_list: pin = command[0] degree = command[1] servo = next(servo for servo in self.servos if servo.pin == pin) if servo: if servo.min_pulse <= degree <= servo.max_pulse: if command[2]: if Controller.execute_servo( servo, degree): self.error( 'Execution of single command was successfull' ) else: self.error( 'Execution of single command failed' ) return False else: if Controller.add_servo_to_stack( servo, degree): self.error( 'Command was added to Execution-list.' ) else: self.error( 'Command could not be added to Execution-list.' ) return False else: self.error( str(degree) + ' is not allowed.') return False else: self.error('Desired servo 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 two items must be numbers' ) 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 servos.') return False