def loadAllHandlers(self): """ Load all handlers in the handler folder """ # get a list of folders in lib/handlers/ directory handler_folders = self._getSubdirectories(self.handler_path) try: handler_folders.remove(os.path.join(self.handler_path, 'share')) except ValueError: logging.warning('No shared handler directory found in {!r}'.format( self.handler_path)) else: handler_folders.extend( self._getSubdirectories( os.path.join(self.handler_path, 'share'))) for folder in handler_folders: for handler_file in os.listdir( os.path.join(globalConfig.get_ltlmop_root(), folder)): abs_path = os.path.join(globalConfig.get_ltlmop_root(), folder, handler_file) # find all handler files and ignore internal files if not (os.path.isfile(abs_path) and handler_file.endswith('.py') and not handler_file.startswith('_')): continue module_info = re.split(r"[\\/]", folder) robot_type = module_info[2] # handler type if len(module_info) == 4: # this is a shared handler h_type = module_info[3] else: # this a robot handler h_type = None # handler name h_name = os.path.splitext(handler_file)[0] handler_config = self.loadHandler(robot_type, h_type, h_name) if handler_config is None: # the handler cannot be loaded continue # save it into the dictionary if robot_type not in self.handler_configs.keys(): self.handler_configs[robot_type] = {} if handler_config.h_type not in self.handler_configs[ robot_type].keys(): self.handler_configs[robot_type][ handler_config.h_type] = [] self.handler_configs[robot_type][handler_config.h_type].append( handler_config)
def __init__(self): self.project_basename = None self.project_root = None self.spec_data = None self.silent = False self.regionMapping = None self.rfi = None self.specText = "" self.all_sensors = [] self.enabled_sensors = [] self.all_actuators = [] self.enabled_actuators = [] self.all_customs = [] self.internal_props = [] self.current_config = "" self.shared_data = {} # This is for storing things like server connection objects, etc. self.h_instance = {'init':{},'pose':None,'locomotionCommand':None,'motionControl':None,'drive':None,'sensor':{},'actuator':{}} # Compilation options (with defaults) self.compile_options = {"convexify": True, # Decompose workspace into convex regions "fastslow": False, # Enable "fast-slow" synthesis algorithm "symbolic": False, # Use BDDs instead of explicit-state strategies "decompose": True, # Create regions for free space and region overlaps (required for Locative Preposition support) "use_region_bit_encoding": True, # Use a vector of "bitX" propositions to represent regions, for efficiency "synthesizer": "jtlv", # Name of synthesizer to use ("jtlv" or "slugs") "parser": "structured"} # Spec parser: SLURP ("slurp"), structured English ("structured"), or LTL ("ltl") self.ltlmop_root = globalConfig.get_ltlmop_root()
def _getSubdirectories(self, path): """ Return subdirectories in `path` relative to ltlmop root. Only goes one level. """ abs_path = os.path.join(globalConfig.get_ltlmop_root(), path) return [os.path.join(path, item) for item in os.listdir(abs_path) \ if os.path.isdir(os.path.join(abs_path, item))]
def loadAllHandlers(self): """ Load all handlers in the handler folder """ # get a list of folders in lib/handlers/ directory handler_folders = self._getSubdirectories(self.handler_path) try: handler_folders.remove(os.path.join(self.handler_path, 'share')) except ValueError: logging.warning('No shared handler directory found in {!r}'.format(self.handler_path)) else: handler_folders.extend(self._getSubdirectories(os.path.join(self.handler_path, 'share'))) for folder in handler_folders: for handler_file in os.listdir(os.path.join(globalConfig.get_ltlmop_root(), folder)): abs_path = os.path.join(globalConfig.get_ltlmop_root(), folder, handler_file) # find all handler files and ignore internal files if not (os.path.isfile(abs_path) and handler_file.endswith('.py') and not handler_file.startswith('_')): continue module_info = re.split(r"[\\/]", folder) robot_type = module_info[2] # handler type if len(module_info) == 4: # this is a shared handler h_type = module_info[3] else: # this a robot handler h_type = None # handler name h_name = os.path.splitext(handler_file)[0] handler_config = self.loadHandler(robot_type, h_type, h_name) if handler_config is None: # the handler cannot be loaded continue # save it into the dictionary if robot_type not in self.handler_configs.keys(): self.handler_configs[robot_type] = {} if handler_config.h_type not in self.handler_configs[robot_type].keys(): self.handler_configs[robot_type][handler_config.h_type] = [] self.handler_configs[robot_type][handler_config.h_type].append(handler_config)
def __init__(self): self.project_basename = None self.project_root = None self.spec_data = None self.silent = False self.regionMapping = None self.rfi = None self.specText = "" self.all_sensors = [] self.enabled_sensors = [] self.all_actuators = [] self.enabled_actuators = [] self.all_customs = [] self.internal_props = [] self.current_config = "" self.shared_data = { } # This is for storing things like server connection objects, etc. self.h_instance = { 'init': {}, 'pose': None, 'locomotionCommand': None, 'motionControl': None, 'drive': None, 'sensor': {}, 'actuator': {} } # Compilation options (with defaults) self.compile_options = { "convexify": True, # Decompose workspace into convex regions "fastslow": False, # Enable "fast-slow" synthesis algorithm "symbolic": False, # Use BDDs instead of explicit-state strategies "decompose": True, # Create regions for free space and region overlaps (required for Locative Preposition support) "use_region_bit_encoding": True, # Use a vector of "bitX" propositions to represent regions, for efficiency "synthesizer": "jtlv", # Name of synthesizer to use ("jtlv" or "slugs") "parser": "structured" } # Spec parser: SLURP ("slurp"), structured English ("structured"), or LTL ("ltl") self.ltlmop_root = globalConfig.get_ltlmop_root()
else: logging.error("Could not save config file {0}".format( experiment_config.file_name)) # remove deleted files # do not delete unsuccessfully loaded configs for config_file in os.listdir(self.config_path): if (os.path.join(self.config_path, config_file) not in saved_file_name) \ and (os.path.join(self.config_path, config_file) not in self.configs_incomplete): os.remove(os.path.join(self.config_path, config_file)) if __name__ == '__main__': import execute e = execute.LTLMoPExecutor() filename = os.path.join(globalConfig.get_ltlmop_root(), "examples", "firefighting", "firefighting.spec") e.proj.loadProject(filename) e.hsub = HandlerSubsystem(e, e.proj.project_root) logging.info("Setting current executing config...") config, success = e.hsub.loadConfigFile(e.proj.current_config) if success: e.hsub.configs.append(config) e.hsub.setExecutingConfig(e.proj.current_config) logging.info("Importing handler functions...") e.hsub.prepareMapping() logging.info("Initializing all functions...") e.hsub.initializeAllMethods() while True: sensor_state = e.hsub.getSensorValue(e.proj.enabled_sensors)
saved_file_name.append(experiment_config.file_name) else: logging.error("Could not save config file {0}".format(experiment_config.file_name)) # remove deleted files # do not delete unsuccessfully loaded configs for config_file in os.listdir(self.config_path): if (os.path.join(self.config_path, config_file) not in saved_file_name) \ and (os.path.join(self.config_path, config_file) not in self.configs_incomplete): os.remove(os.path.join(self.config_path, config_file)) if __name__ == '__main__': import execute e = execute.LTLMoPExecutor() filename = os.path.join(globalConfig.get_ltlmop_root(), "examples", "firefighting", "firefighting.spec") e.proj.loadProject(filename) e.hsub = HandlerSubsystem(e, e.proj.project_root) logging.info("Setting current executing config...") config, success = e.hsub.loadConfigFile(e.proj.current_config) if success: e.hsub.configs.append(config) e.hsub.setExecutingConfig(e.proj.current_config) logging.info("Importing handler functions...") e.hsub.prepareMapping() logging.info("Initializing all functions...") e.hsub.initializeAllMethods() while True: sensor_state = e.hsub.getSensorValue(e.proj.enabled_sensors) e.hsub.setActuatorValue({'pick_up': not sensor_state['person']})