def getMessageFromJSON(aadl_namespace, aadl_type, json_source, associated_class):
    file_path = os.path.join(json_default_path, json_source)

    try:
        parsed = json.load(open(file_path))
    except json.JSONDecodeError:
        log.error("Unable to load JSON file {}".format(file_path))
        return None

    # new_msg_header = list(parsed.keys())[0]

    message = Message(aadl_namespace, aadl_type)

    # parameters_asn = parsed[new_msg_header]['values']

    for key, value in parsed["properties"].items():
        var = Variable()
        var.setName(key)
        # TODO string format + nested messages
        var.setType(getROSMsgtypeFromJSON(value["type"], associated_class))
        var.setIsParameter()

        message.addParameter(var)

    # Aggiungo il messaggio al suo relativo system
    message.system.addMessage(message)

    return message
def getServiceFromJSON(aadl_namespace, aadl_type, json_source,
                       associated_class):
    file_path = os.path.join(asn_default_path, json_source)

    try:
        parsed = json.load(open(file_path))
    except json.JSONDecodeError:
        log.error("Unable to load JSON file {}".format(file_path))
        return None

    service = Service(aadl_namespace, aadl_type)

    requests_json = parsed['Request']['properties']
    responses_json = parsed['Response']['properties']

    for key, value in requests_json.items():
        var = Variable()
        var.setName(key)
        var.setType(getROSMsgtypeFromJSON(value['type'], associated_class))
        var.setIsParameter()
        service.addRequest(var)

    for key, value in responses_json.items():
        var = Variable()
        var.setName(key)
        # TODO string format + nested messages
        var.setType(getROSMsgtypeFromJSON(value['type'], associated_class))
        var.setIsParameter()
        service.addResponse(var)

    # Aggiungo il messaggio al suo relativo system
    service.system.addService(service)

    return service
Ejemplo n.º 3
0
    def getVariableFromJSON(self, pv_name, pv_schema, pv_value):
        if pv_schema.get("type", "empty") != "object":
            # p is the name of the variable, list_of_params[p]["type"] is the type
            tmp_param = Variable(self)
            tmp_param.setName(pv_name)
            if pv_schema.get("$ref", "empty") == "#/complex":
                tmp_param.setType(manageComplexType(pv_value["type"], pv_value["include"], self.associated_class))
            else:
                tmp_param.setType(getROSDatatypeFromJSON(pv_schema["type"], self.associated_class))
                if isinstance(tmp_param.type, String) and pv_value:
                    tmp_param.setDefaultValue("\"{}\"".format(pv_value))
                elif isinstance(tmp_param.type, Bool) and pv_value:
                    tmp_param.setDefaultValue(pv_value.lower())
                elif pv_value:
                    tmp_param.setDefaultValue(pv_value)

            return tmp_param
        else:
            tmp_struct = Struct(self.associated_class, "{}_t".format(pv_name))
            tmp_struct.createInstanceWithName(pv_name)

            for key, value in pv_schema["properties"].items():
                tmp_struct.addVariable(self.getVariableFromJSON(key, value, pv_value[key]))
            return tmp_struct
Ejemplo n.º 4
0
    def populateData(self):
        main_thread = self.associated_class.getMainThread()

        if main_thread == None:
            return False, "Unable to get the Main Thread"

        #########################
        # TRANSFORMATION FRAME #
        ########################

        # Controllo l'uso del Transformation Frame
        self.thread_uses_tf = self.setUsesTransformationFrame()

        ###############
        # Output Port #
        ###############

        # Essendo birezeizonale posso trovare la connessione sia come source che come dest
        conn_by_source = True
        process_input_port = tfs.getConnectionPortInfoBySource(
            self.process, self.type, self.input_port_name)

        if process_input_port is None:
            conn_by_source = False
            process_output_port = tfs.getConnectionPortInfoByDest(
                self.process, self.type, self.input_port_name)

        if process_input_port is None:
            return (
                False,
                "Unable to find the right binding between process requires subprogram access port and "
                "thread input port")

        if conn_by_source:
            (source_parent_name,
             source_name) = tfs.getDestFromPortInfo(process_input_port)
        else:
            (source_parent_name,
             source_name) = tfs.getSourceFromPortInfo(process_output_port)

        if source_parent_name is None or source_name is None:
            return False, "Unable to find the process provides subprogram access port name"

        self.process_port = tfs.getFeatureByName(self.process,
                                                 name=source_name)

        if self.process_port is None:
            return False, "Unable to find the process provides subprogram access port feature"

        # Dopo aver trovato la porta del process, controllo il nome di default del
        # services associato
        (topic_namespace, self.default_service_name) = tfs.getDefaultTopicName(
            self.process_port)

        if self.default_service_name == None:
            self.default_service_name = "service_name_default"
            log.warning(
                "Default Service Name not found, set as {} as default.".format(
                    self.default_service_name))

        ##################################
        ### ASN.1 Request and Response ###
        ##################################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.process_port)
        if aadl_namespace == None or aadl_type == None:
            return (False, "Unable to identify process port type")

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di servizio
        # è custom e lo dovrò generare, mentre se non c'è allora è un servizio standard ROS
        port_data_info = tfs.getPortDataInfo(self.process_port)
        if port_data_info == None:
            return (False, "Unable to get the port data info for process port")

        self.asn_description = tfs.getSourceText(port_data_info)

        if self.asn_description == None:
            # @TODO: Standard Service
            log.warning("STANDARD SERVICE")
            # return (False, "Unable to find property Source_Text for the services caller with ASN.1 description")
        else:
            # Creo il servizio custom e lo associo al nodo che lo ha generato
            self.service = sfs.getServiceFromJSON(aadl_namespace, aadl_type,
                                                  self.asn_description,
                                                  self.associated_class)
            if self.service == None:
                return (False, "Error in ASN.1 parsing")

        # Genero ed aggiungo la libreria del services al nodo
        service_library = Library(self.associated_class)
        service_library.setPath("{}/{}.h".format(self.service.namespace,
                                                 self.service.name))
        self.associated_class.addLibrary(service_library)

        ##########################
        ### SERVICE SERVER VAR ###
        ##########################

        var_service_server = Variable(self.associated_class)
        var_service_server.setName("service_server_{}".format(self.name))
        var_service_server.setType(ROS_ServiceServer(self.associated_class))
        self.associated_class.addInternalVariable(var_service_server)

        ###############################
        ### SERVICE SERVER CALLBACK ###
        ###############################

        self.server_callback = Method(self.associated_class)
        self.server_callback.method_name = "{}_service_callback".format(
            self.name)
        self.server_callback.return_type = Bool(self.associated_class)
        self.server_callback.namespace = self.associated_class.class_name

        # REQUEST Parameter
        input_param_req = Variable(self.associated_class)
        input_param_req.setName("&req")
        input_param_req.setType(
            ROS_ServiceServer_Request(
                self.associated_class, "{}::{}".format(self.service.namespace,
                                                       self.service.name)))
        input_param_req.setIsParameter()

        # RESPONSE Parameter
        input_param_res = Variable(self.associated_class)
        input_param_res.setName("&res")
        input_param_res.setType(
            ROS_ServiceServer_Response(
                self.associated_class, "{}::{}".format(self.service.namespace,
                                                       self.service.name)))
        input_param_res.setIsParameter()

        self.server_callback.addInputParameter(input_param_req)
        self.server_callback.addInputParameter(input_param_res)

        ###############
        # SOURCE TEXT #
        ###############
        function = tfs.getSubcomponentByInfo(self.thread,
                                             name=self.function_name,
                                             namespace="ros",
                                             category="subprogram")
        if function is None:
            return False, "Unable to find the function subprogram"

        self.source_text_function = self.createSourceTextFileFromSourceText(
            tfs.getSourceText(function), tfs.getSourceName(function))

        if self.source_text_function is None:
            return False, "Unable to find property Source_Text or Source_Name"

        self.source_text_function.setTF(self.thread_uses_tf)

        # Aggiungo la chiamata alla funzione custom
        if self.source_text_function != None:
            self.source_text_function.addServiceReqAndRes(
                input_param_req, input_param_res)
            self.source_text_function.addLibrary(service_library)

            self.source_text_function.setFunctionType(
                Bool(self.associated_class))
            code = "return {};".format(
                self.source_text_function.generateInlineCode())
            self.server_callback.addMiddleCode(code)

        self.associated_class.addPrivateMethod(self.server_callback)

        main_thread.prepare.addMiddleCode(
            "{} = handle.advertiseService(\"{}\", {}, this);".format(
                var_service_server.name, self.default_service_name,
                self.server_callback.getThreadPointer()))

        return (True, "")
Ejemplo n.º 5
0
    def populateData(self):
        # Ottengo tutti i dati relativi al main thread
        (prepare, tearDown, errorHandler) = tfs.getMainThreadFunctions(self.thread)

        self.prepare = Prepare(self.associated_class)
        self.prepare.source_text_function = self.createSourceTextFileFromSourceText(tfs.getSourceText(prepare),
                                                                                    "custom_prepare")

        self.tearDown = TearDown(self.associated_class)
        self.tearDown.source_text_function = self.createSourceTextFileFromSourceText(tfs.getSourceText(tearDown),
                                                                                     "custom_teardown")

        # Aggiungo la chiamata alla funzione custome
        if self.tearDown.source_text_function:
            code = "{};".format(self.tearDown.source_text_function.generateInlineCode())
            self.tearDown.addMiddleCode(code)

        self.errorHandling = ErrorHandling(self.associated_class)
        self.errorHandling.source_text_function = self.createSourceTextFileFromSourceText(
            tfs.getSourceText(errorHandler),
            "custom_errorHandling")

        # Aggiungo la chiamata alla funzione custome
        if self.errorHandling.source_text_function:
            code = "{};".format(self.errorHandling.source_text_function.generateInlineCode())
            self.errorHandling.addBottomCode(code)

        self.associated_class.addPrivateMethod(self.prepare)
        self.associated_class.addPrivateMethod(self.tearDown)
        self.associated_class.addPrivateMethod(self.errorHandling)

        # Ottengo lo stato (parameters, variables) in ASN.1 del nodo
        # (parameters, variables) = self.getStateASN()
        (parameters, variables) = self.getStateJSON()

        # Se ho almeno parametri o variabili, allora genero
        # anche la node configuration, altrimenti no
        if len(parameters) > 0 or len(variables) > 0:
            type_internal_state = Type()
            type_internal_state.setTypeName("InternalState")

            internal_state_is = Variable(self.associated_class)
            internal_state_is.setName("is")
            internal_state_is.setType(type_internal_state)
            self.associated_class.addInternalVariable(internal_state_is)

            node_conf = NodeConfiguration(self.associated_class)
            self.associated_class.setNodeConfiguration(node_conf)

            if len(variables) > 0:
                vars_struct = VariablesStruct(self.associated_class)

                for v in variables:
                    vars_struct.addVariable(v)

                self.associated_class.node_configuration.addStruct(vars_struct)
                self.associated_class.node_configuration.has_variables = True

            # Se ho dei parametri, allora genero la struct
            if len(parameters) > 0:
                params_struct = ParametersStruct(self.associated_class)
                self.prepare.addTopCode("Parameters p;")

                for p in parameters:
                    params_struct.addVariable(p)
                    self.createHandlerForParameter(p)

                self.prepare.addMiddleCode("is.initialize(&p);")
                self.associated_class.node_configuration.addStruct(params_struct)
                self.associated_class.node_configuration.has_parameters = True
            else:
                self.prepare.addMiddleCode("is.initialize();")

        # Aggiungo la chiamata alla funzione custome
        if self.prepare.source_text_function:
            self.prepare.source_text_function.setFunctionType(Bool(self.associated_class))
            code = "return {};".format(self.prepare.source_text_function.generateInlineCode())
            self.prepare.addBottomCode(code)
        else:
            # Return alla fine del metodo main
            self.prepare.addBottomCode("return true;")

        return True, ""
Ejemplo n.º 6
0
    def __init__(self, _associated_class):
        super().__init__(_associated_class)
        self.namespace = None
        self.method_name = "main"
        self.return_type = Int(self.associated_class)

        # int argc
        input_argc = Variable(self.associated_class)
        input_argc.setIsParameter()
        input_argc.setName("argc")
        input_argc.setType(Int(self.associated_class))

        # char **argv
        input_argv = Variable(self.associated_class)
        input_argv.setIsParameter()
        input_argv.setName("argv")
        input_argv.setType(PointerToCharPointer(self.associated_class))

        self.addInputParameter(input_argc)
        self.addInputParameter(input_argv)

        self.addTopCode(
            "ros::init(argc, argv, \"{}\", ros::init_options::NoSigintHandler);"
            .format(self.associated_class.node_name))
        self.addTopCode("signal(SIGINT, nodeSigintHandler);")

        self.addBottomCode("return 0;")
Ejemplo n.º 7
0
    def populateData(self):
        main_thread = self.associated_class.getMainThread()

        if main_thread is None:
            return False, "Unable to get the Main Thread"

        # Ottengo le informazioni necessarie per i thread di tipo Timer:
        # - Source Text
        # - Period

        thread_function = tfs.getSubprogram(self.thread)
        if thread_function is None:
            return False, "Unable to find the right Subprogram"

        # TRANSFORMATION FRAME

        # Controllo l'uso del Transformation Frame
        self.thread_uses_tf = self.setUsesTransformationFrame()

        # Source Text

        self.source_text_function = self.createSourceTextFileFromSourceText(
            tfs.getSourceText(thread_function),
            tfs.getSourceName(thread_function))

        if self.source_text_function is None:
            return False, "Unable to find property Source_Text or Source_Name"

        self.source_text_function.setTF(self.thread_uses_tf)

        # FREQUENCY

        (period, period_unit) = tfs.getPeriod(self.thread)

        if period is None or period_unit is None:
            return False, "Unable to find property Period with relative value and unit"

        # Conversione in secondi della frequenza a partire da qualunque unità di misura
        try:
            period_quantity = ureg("{} {}".format(period, period_unit))
            period_quantity.ito(ureg.second)
            self.frequency_in_hz = 1.0 / period_quantity.magnitude
            self.period_in_seconds = period_quantity.magnitude
        except ValueError:
            return False, "Unable to convert Period in seconds"

        # TIMER
        var_timer = Variable(self.associated_class)
        var_timer.setName("timer_{}".format(self.name))
        var_timer.setType(ROS_Timer(self.associated_class))
        self.associated_class.addInternalVariable(var_timer)

        ######################
        ### TIMER CALLBACK ###
        ######################

        self.timerCallback = Method(self.associated_class)
        self.timerCallback.method_name = "{}_callback".format(self.name)
        self.timerCallback.return_type = Void(self.associated_class)
        self.timerCallback.namespace = self.associated_class.class_name

        input_par = Variable(self.associated_class)
        input_par.setIsParameter()
        input_par.setType(ROS_TimerEvent(self.associated_class))
        input_par.setName("")
        self.timerCallback.addInputParameter(input_par)

        # Aggiungo la chiamata alla funzione custom
        if self.source_text_function:
            code = "{};".format(self.source_text_function.generateInlineCode())
            self.timerCallback.addMiddleCode(code)

        self.associated_class.addPrivateMethod(self.timerCallback)

        main_thread.prepare.addMiddleCode(
            "{} = handle.createTimer(ros::Duration({}), {}, this);".format(
                var_timer.name, self.period_in_seconds,
                self.timerCallback.getThreadPointer()))

        return True, ""
Ejemplo n.º 8
0
    def populateSubscriberData(self):
        ##################
        ### Input Port ###
        ##################

        # Ottengo la connesione che mappa la porta di input del thread subscriber
        # con quella che entra nel process
        process_input_port = tfs.getConnectionPortInfoByDest(
            self.process, self.type, self.input_port_name)
        if process_input_port == None:
            return (
                False,
                "Unable to find the right binding between process input port and thread input port"
            )

        (source_parent_name,
         source_name) = tfs.getSourceFromPortInfo(process_input_port)

        if source_parent_name == None or source_name == None:
            return (False, "Unable to find the process input port name")

        self.sub_process_port = tfs.getFeatureByName(self.process,
                                                     name=source_name)

        if self.sub_process_port == None:
            return (False,
                    "Unable to find the process input port name feature")

        ##################
        ### INPUT TYPE ###
        ##################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.sub_process_port)
        if aadl_namespace == None or aadl_type == None:
            return (False, "Unable to identify process port type")

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di messaggio
        # è custom e lo dovrò generare, mentre se non c'è allora è un messaggio standard ROS
        port_data_info = tfs.getPortDataInfo(self.sub_process_port)
        if port_data_info == None:
            return (False, "Unable to get the port data info for process port")

        port_data_source_asn = tfs.getSourceText(port_data_info)
        if port_data_source_asn == None:
            # Se è None allora non c'è alcun file ASN.1 associato e quindi è un messaggio standard ROS
            raw_output_type = dt.getROSDatatypeFromAADL(
                aadl_namespace, aadl_type, self.associated_class)
            if raw_output_type == None:
                return (False,
                        "Datatype {} NOT supported".format(raw_output_type))
            else:
                self.input_type = raw_output_type
        else:
            self.custom_message = mfs.getMessageFromJSON(
                aadl_namespace, aadl_type, port_data_source_asn,
                self.associated_class)

            self.input_type = Type(self.associated_class)
            self.input_type.setTypeName(self.custom_message.name)
            self.input_type.setNamespace(self.custom_message.namespace)

        self.input_type.setConst(_const=True)
        self.input_type.setAfterTypeName("::ConstPtr&")

        # Associo la librerie del messaggio al tipo di output, sia custom che standard
        input_type_library = Library()
        input_type_library.setPath("{}/{}.h".format(self.input_type.namespace,
                                                    self.input_type.type_name))

        self.input_type.setLibrary(input_type_library)

        ########################
        ### SUBSCRIBER TOPIC ###
        ########################

        (status, desc) = self.getDefaultTopicName(self.input_port_name,
                                                  input=True)
        if status == False:
            return (status, desc)

        ##################
        ### QUEUE SIZE ###
        ##################

        queue_size_default_value = 1
        self.queue_size = tfs.getSubscriberQueueSize(
            self.thread, port_name=self.input_port_name)

        if self.queue_size == None:
            self.queue_size = queue_size_default_value
            log.info("Queue size set to default value: {}".format(
                self.queue_size))

        ######################
        ### SUBSCRIBER VAR ###
        ######################

        var_subscriber_pub = Variable(self.associated_class)
        var_subscriber_pub.setName("sub_{}".format(self.name))
        var_subscriber_pub.setType(ROS_Subscriber(self.associated_class))

        self.associated_class.addInternalVariable(var_subscriber_pub)

        ###########################
        ### SUBSCRIBER CALLBACK ###
        ###########################

        self.sub_callback = Method(self.associated_class)
        self.sub_callback.method_name = "{}_callback".format(self.name)
        self.sub_callback.return_type = Void(self.associated_class)
        self.sub_callback.namespace = self.associated_class.class_name

        self.sub_input_var = Variable(self.associated_class)
        self.sub_input_var.setType(self.input_type)
        self.sub_input_var.setName("msg")
        self.sub_input_var.setIsParameter()

        self.sub_callback.addInputParameter(self.sub_input_var)

        self.associated_class.addPrivateMethod(self.sub_callback)

        self.main_thread.prepare.addMiddleCode(
            "{} = handle.subscribe(\"{}\", {}, {}, this);".format(
                var_subscriber_pub.name, self.topic, self.queue_size,
                self.sub_callback.getThreadPointer()))

        return (True, "")
Ejemplo n.º 9
0
class SubscriberPublisher(AADLThread):
    def __init__(self, _system_root, _process, _thread, _associated_class):
        super().__init__(_system_root, _process, _thread, _associated_class)
        log.info("SubscriberPublisher thread {}".format(self.name))

        # Parametri del SubscriberPubslisher
        self.main_thread = None
        self.input_port_name = "msg_in"
        self.output_port_name = "msg_out"

        # Parametri comuni
        self.source_text_function = None

        # Parametri della parte Subcriber
        self.sub_process_port = None
        self.sub_asn1_source_file = None
        self.sub_topic = None
        self.sub_queue_size = None
        self.sub_callback = None
        self.input_type = None
        self.sub_custom_msg = None
        self.sub_input_var = None

        # Parametri della parte Publisher
        self.pub_process_port = None
        self.pub_topic = None
        self.pub_asn1_source_file = None
        self.output_type = None
        self.pub_custom_msg = None
        self.var_publisher_pub = None

    def populateSubscriberData(self):
        ##################
        ### Input Port ###
        ##################

        # Ottengo la connesione che mappa la porta di input del thread subscriber
        # con quella che entra nel process
        process_input_port = tfs.getConnectionPortInfoByDest(
            self.process, self.type, self.input_port_name)
        if process_input_port == None:
            return (
                False,
                "Unable to find the right binding between process input port and thread input port"
            )

        (source_parent_name,
         source_name) = tfs.getSourceFromPortInfo(process_input_port)

        if source_parent_name == None or source_name == None:
            return (False, "Unable to find the process input port name")

        self.sub_process_port = tfs.getFeatureByName(self.process,
                                                     name=source_name)

        if self.sub_process_port == None:
            return (False,
                    "Unable to find the process input port name feature")

        ##################
        ### INPUT TYPE ###
        ##################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.sub_process_port)
        if aadl_namespace == None or aadl_type == None:
            return (False, "Unable to identify process port type")

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di messaggio
        # è custom e lo dovrò generare, mentre se non c'è allora è un messaggio standard ROS
        port_data_info = tfs.getPortDataInfo(self.sub_process_port)
        if port_data_info == None:
            return (False, "Unable to get the port data info for process port")

        port_data_source_asn = tfs.getSourceText(port_data_info)
        if port_data_source_asn == None:
            # Se è None allora non c'è alcun file ASN.1 associato e quindi è un messaggio standard ROS
            raw_output_type = dt.getROSDatatypeFromAADL(
                aadl_namespace, aadl_type, self.associated_class)
            if raw_output_type == None:
                return (False,
                        "Datatype {} NOT supported".format(raw_output_type))
            else:
                self.input_type = raw_output_type
        else:
            self.custom_message = mfs.getMessageFromJSON(
                aadl_namespace, aadl_type, port_data_source_asn,
                self.associated_class)

            self.input_type = Type(self.associated_class)
            self.input_type.setTypeName(self.custom_message.name)
            self.input_type.setNamespace(self.custom_message.namespace)

        self.input_type.setConst(_const=True)
        self.input_type.setAfterTypeName("::ConstPtr&")

        # Associo la librerie del messaggio al tipo di output, sia custom che standard
        input_type_library = Library()
        input_type_library.setPath("{}/{}.h".format(self.input_type.namespace,
                                                    self.input_type.type_name))

        self.input_type.setLibrary(input_type_library)

        ########################
        ### SUBSCRIBER TOPIC ###
        ########################

        (status, desc) = self.getDefaultTopicName(self.input_port_name,
                                                  input=True)
        if status == False:
            return (status, desc)

        ##################
        ### QUEUE SIZE ###
        ##################

        queue_size_default_value = 1
        self.queue_size = tfs.getSubscriberQueueSize(
            self.thread, port_name=self.input_port_name)

        if self.queue_size == None:
            self.queue_size = queue_size_default_value
            log.info("Queue size set to default value: {}".format(
                self.queue_size))

        ######################
        ### SUBSCRIBER VAR ###
        ######################

        var_subscriber_pub = Variable(self.associated_class)
        var_subscriber_pub.setName("sub_{}".format(self.name))
        var_subscriber_pub.setType(ROS_Subscriber(self.associated_class))

        self.associated_class.addInternalVariable(var_subscriber_pub)

        ###########################
        ### SUBSCRIBER CALLBACK ###
        ###########################

        self.sub_callback = Method(self.associated_class)
        self.sub_callback.method_name = "{}_callback".format(self.name)
        self.sub_callback.return_type = Void(self.associated_class)
        self.sub_callback.namespace = self.associated_class.class_name

        self.sub_input_var = Variable(self.associated_class)
        self.sub_input_var.setType(self.input_type)
        self.sub_input_var.setName("msg")
        self.sub_input_var.setIsParameter()

        self.sub_callback.addInputParameter(self.sub_input_var)

        self.associated_class.addPrivateMethod(self.sub_callback)

        self.main_thread.prepare.addMiddleCode(
            "{} = handle.subscribe(\"{}\", {}, {}, this);".format(
                var_subscriber_pub.name, self.topic, self.queue_size,
                self.sub_callback.getThreadPointer()))

        return (True, "")

    def populatePublisherData(self):
        ###################
        ### Output Port ###
        ###################

        # Ottengo la connesione che mappa la porta di input del thread subscriber
        # con quella che entra nel process
        process_output_port = tfs.getConnectionPortInfoBySource(
            self.process, self.type, self.output_port_name)
        if process_output_port == None:
            return (
                False,
                "Unable to find the right binding between process input port and thread input port"
            )

        (dest_parent_name,
         dest_name) = tfs.getDestFromPortInfo(process_output_port)

        if dest_parent_name == None or dest_name == None:
            return (False, "Unable to find the process input port name")

        self.pub_process_port = tfs.getFeatureByName(self.process,
                                                     name=dest_name)
        if self.pub_process_port == None:
            return (False,
                    "Unable to find the process input port name feature")

        ###################
        ### OUTPUT TYPE ###
        ###################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.pub_process_port)
        if aadl_namespace == None or aadl_type == None:
            return (False, "Unable to identify process port type")

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di messaggio
        # è custom e lo dovrò generare, mentre se non c'è allora è un messaggio standard ROS
        port_data_info = tfs.getPortDataInfo(self.pub_process_port)
        if port_data_info == None:
            return (False, "Unable to get the port data info for process port")

        port_data_source_asn = tfs.getSourceText(port_data_info)
        if port_data_source_asn == None:
            # Se è None allora non c'è alcun file ASN.1 associato e quindi è un messaggio standard ROS
            raw_output_type = dt.getROSDatatypeFromAADL(
                aadl_namespace, aadl_type, self.associated_class)
            if raw_output_type == None:
                return (False,
                        "Datatype {} NOT supported".format(raw_output_type))
            else:
                self.output_type = raw_output_type
        else:
            self.custom_message = mfs.getMessageFromJSON(
                aadl_namespace, aadl_type, port_data_source_asn,
                self.associated_class)

            self.output_type = Type(self.associated_class)
            self.output_type.setTypeName(self.custom_message.name)
            self.output_type.setNamespace(self.custom_message.namespace)

        # Associo la librerie del messaggio al tipo di output, sia custom che standard
        output_type_library = Library()
        output_type_library.setPath("{}/{}.h".format(
            self.output_type.namespace, self.output_type.type_name))

        self.output_type.setLibrary(output_type_library)

        #############
        ### TOPIC ###
        #############

        (status, desc) = self.getDefaultTopicName(self.output_port_name,
                                                  output=True)
        if not status:
            return status, desc

        #####################
        ### PUBLISHER VAR ###
        #####################

        self.var_publisher_pub = Variable(self.associated_class)
        self.var_publisher_pub.setName("pub_{}".format(self.name))
        self.var_publisher_pub.setType(ROS_Publisher(self.associated_class))
        self.associated_class.addInternalVariable(self.var_publisher_pub)

        self.main_thread.prepare.addMiddleCode(
            "{} = handle.advertise<{}>(\"{}\", 10);".format(
                self.var_publisher_pub.name, self.output_type.generateCode(),
                self.topic))

        return True, ""

    def populateData(self):
        self.main_thread = self.associated_class.getMainThread()

        if self.main_thread is None:
            return False, "Unable to get the Main Thread"

        thread_function = tfs.getSubprogram(self.thread)
        if thread_function is None:
            return False, "Unable to find the right Subprogram"

        ############################
        ### TRANSFORMATION FRAME ###
        ############################

        # Controllo l'uso del Transformation Frame
        self.thread_uses_tf = self.setUsesTransformationFrame()

        ##################
        ### SUBSCRIBER ###
        ##################
        (status, desc) = self.populateSubscriberData()
        if not status:
            return status, desc

        #################
        ### PUBLISHER ###
        #################
        (status, desc) = self.populatePublisherData()
        if not status:
            return status, desc

        ###################
        ### SOURCE TEXT ###
        ###################

        self.source_text_function = self.createSourceTextFileFromSourceText(
            tfs.getSourceText(thread_function),
            tfs.getSourceName(thread_function))
        # Aggiungo la chiamata alla funzione custome
        if self.source_text_function:
            self.source_text_function.setFunctionType(self.output_type)
            self.source_text_function.addFunctionParameter(self.sub_input_var)
            self.source_text_function.setTF(self.thread_uses_tf)

            code = "{}.publish({});".format(
                self.var_publisher_pub.name,
                self.source_text_function.generateInlineCode())
            self.sub_callback.addMiddleCode(code)
        else:
            return False, "Unable to find Source_Text or Source_Name"

        return True, ""
Ejemplo n.º 10
0
    def populatePublisherData(self):
        ###################
        ### Output Port ###
        ###################

        # Ottengo la connesione che mappa la porta di input del thread subscriber
        # con quella che entra nel process
        process_output_port = tfs.getConnectionPortInfoBySource(
            self.process, self.type, self.output_port_name)
        if process_output_port == None:
            return (
                False,
                "Unable to find the right binding between process input port and thread input port"
            )

        (dest_parent_name,
         dest_name) = tfs.getDestFromPortInfo(process_output_port)

        if dest_parent_name == None or dest_name == None:
            return (False, "Unable to find the process input port name")

        self.pub_process_port = tfs.getFeatureByName(self.process,
                                                     name=dest_name)
        if self.pub_process_port == None:
            return (False,
                    "Unable to find the process input port name feature")

        ###################
        ### OUTPUT TYPE ###
        ###################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.pub_process_port)
        if aadl_namespace == None or aadl_type == None:
            return (False, "Unable to identify process port type")

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di messaggio
        # è custom e lo dovrò generare, mentre se non c'è allora è un messaggio standard ROS
        port_data_info = tfs.getPortDataInfo(self.pub_process_port)
        if port_data_info == None:
            return (False, "Unable to get the port data info for process port")

        port_data_source_asn = tfs.getSourceText(port_data_info)
        if port_data_source_asn == None:
            # Se è None allora non c'è alcun file ASN.1 associato e quindi è un messaggio standard ROS
            raw_output_type = dt.getROSDatatypeFromAADL(
                aadl_namespace, aadl_type, self.associated_class)
            if raw_output_type == None:
                return (False,
                        "Datatype {} NOT supported".format(raw_output_type))
            else:
                self.output_type = raw_output_type
        else:
            self.custom_message = mfs.getMessageFromJSON(
                aadl_namespace, aadl_type, port_data_source_asn,
                self.associated_class)

            self.output_type = Type(self.associated_class)
            self.output_type.setTypeName(self.custom_message.name)
            self.output_type.setNamespace(self.custom_message.namespace)

        # Associo la librerie del messaggio al tipo di output, sia custom che standard
        output_type_library = Library()
        output_type_library.setPath("{}/{}.h".format(
            self.output_type.namespace, self.output_type.type_name))

        self.output_type.setLibrary(output_type_library)

        #############
        ### TOPIC ###
        #############

        (status, desc) = self.getDefaultTopicName(self.output_port_name,
                                                  output=True)
        if not status:
            return status, desc

        #####################
        ### PUBLISHER VAR ###
        #####################

        self.var_publisher_pub = Variable(self.associated_class)
        self.var_publisher_pub.setName("pub_{}".format(self.name))
        self.var_publisher_pub.setType(ROS_Publisher(self.associated_class))
        self.associated_class.addInternalVariable(self.var_publisher_pub)

        self.main_thread.prepare.addMiddleCode(
            "{} = handle.advertise<{}>(\"{}\", 10);".format(
                self.var_publisher_pub.name, self.output_type.generateCode(),
                self.topic))

        return True, ""
Ejemplo n.º 11
0
    def populateData(self):
        main_thread = self.associated_class.getMainThread()

        if main_thread == None:
            return (False, "Unable to get the Main Thread")

        ###################
        ### Output Port ###
        ###################

        # Essendo bidirezionale posso trovare la connessione sia come source che come dest
        conn_by_source = True
        process_output_port = tfs.getConnectionPortInfoBySource(
            self.process, self.type, self.output_port_name)
        if process_output_port == None:
            conn_by_source = False
            process_output_port = tfs.getConnectionPortInfoByDest(
                self.process, self.type, self.output_port_name)

        if process_output_port == None:
            return (
                False,
                "Unable to find the right binding between process requires subprogram access port and "
                "thread input port")

        if conn_by_source:
            (dest_parent_name,
             dest_name) = tfs.getDestFromPortInfo(process_output_port)
        else:
            (dest_parent_name,
             dest_name) = tfs.getSourceFromPortInfo(process_output_port)

        if dest_parent_name == None or dest_name == None:
            return (
                False,
                "Unable to find the process requires subprogram access port name"
            )

        self.process_port = tfs.getFeatureByName(self.process, name=dest_name)

        if self.process_port == None:
            return (
                False,
                "Unable to find the process requires subprogram access port feature"
            )

        # Dopo aver trovato la porta del process, controllo il nome di default del
        # services associato
        (topic_namespace, self.default_service_name) = tfs.getDefaultTopicName(
            self.process_port)

        if self.default_service_name == None:
            self.default_service_name = "service_name_default"
            log.warning(
                "Default Service Name not found, set as {} as default.".format(
                    self.default_service_name))

        ##################################
        ### ASN.1 Request and Response ###
        ##################################

        (aadl_namespace,
         aadl_type) = tfs.getPortDatatypeByPort(self.process_port)
        if aadl_namespace is None or aadl_type is None:
            return False, "Unable to identify process port type"

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di servizio
        # è custom e lo dovrò generare, mentre se non c'è allora è un servizio standard ROS
        port_data_info = tfs.getPortDataInfo(self.process_port)
        if port_data_info is None:
            return False, "Unable to get the port data info for process port"

        self.asn_description = tfs.getSourceText(port_data_info)

        if self.asn_description is None:
            self.service = Service(aadl_namespace, aadl_type)
        else:
            # Creo il servizio custom e lo associo al nodo che lo ha generato
            self.service = sfs.getServiceFromJSON(self.asn_description,
                                                  self.associated_class)
        # if self.service == None:
        #     return (False, "Error in ASN.1 parsing")

        # self.associated_class.addService( self.service )

        # Genero ed aggiungo la libreria del services al nodo
        service_library = Library(self.service.namespace)
        service_library.setPath("{}/{}.h".format(self.service.namespace,
                                                 self.service.name))
        self.associated_class.addLibrary(service_library)

        ##########################
        ### SERVICE CLIENT VAR ###
        ##########################

        var_serviceclient = Variable(self.associated_class)
        var_serviceclient.setName("service_client_{}".format(self.name))
        var_serviceclient.setType(ROS_ServiceClient(self.associated_class))
        self.associated_class.addInternalVariable(var_serviceclient)

        main_thread.prepare.addMiddleCode(
            "{} = handle.serviceClient<{}::{}>(\"{}\");".format(
                var_serviceclient.name, self.service.namespace,
                self.service.name, self.default_service_name))

        return True, ""
Ejemplo n.º 12
0
    def populateData(self):
        main_thread = self.associated_class.getMainThread()

        if main_thread == None:
            return (False, "Unable to get the Main Thread")

        # Ottengo le informazioni necessarie per i thread di tipo Publisher:
        # - Source Text
        # - Period

        thread_function = tfs.getSubprogram(self.thread)
        if thread_function == None:
            return (False, "Unable to find the right Subprogram")

        ############################
        ### TRANSFORMATION FRAME ###
        ############################

        # Controllo l'uso del Transformation Frame
        self.thread_uses_tf = self.setUsesTransformationFrame()

        ###################
        ### Output Port ###
        ###################

        # Ottengo la connesione che mappa la porta di input del thread subscriber
        # con quella che entra nel process
        process_output_port = tfs.getConnectionPortInfoBySource(self.process, self.type, self.output_port_name)
        if process_output_port is None:
            return False, "Unable to find the right binding between process input port and thread input port"

        (dest_parent_name, dest_name) = tfs.getDestFromPortInfo(process_output_port)

        if dest_parent_name is None or dest_name is None:
            return False, "Unable to find the process input port name"

        self.process_port = tfs.getFeatureByName(self.process, name=dest_name)
        if self.process_port is None:
            return False, "Unable to find the process input port name feature"

        (aadl_namespace, aadl_type) = tfs.getPortDatatypeByPort(self.process_port)
        if aadl_namespace is None or aadl_type is None:
            return False, "Unable to identify process port type"

        # Controllo se c'è un file ASN.1 associato alla porta. Se c'è allora il tipo di messaggio
        # è custom e lo dovrò generare, mentre se non c'è allora è un messaggio standard ROS
        port_data_info = tfs.getPortDataInfo(self.process_port)
        if port_data_info is None:
            return False, "Unable to get the port data info for process port"

        port_data_source_asn = tfs.getSourceText(port_data_info)
        if port_data_source_asn is None:
            # Se è None allora non c'è alcun file ASN.1 associato e quindi è un messaggio standard ROS
            raw_output_type = dt.getROSDatatypeFromAADL(aadl_namespace, aadl_type, self.associated_class)
            if raw_output_type is None:
                return False, "Datatype {} NOT supported".format(raw_output_type)
            else:
                self.output_type = raw_output_type
        else:
            self.custom_message = mfs.getMessageFromJSON(aadl_namespace,
                                                         aadl_type,
                                                         port_data_source_asn,
                                                         self.associated_class)

            self.output_type = Type(self.associated_class)
            self.output_type.setTypeName(self.custom_message.name)
            self.output_type.setNamespace(self.custom_message.namespace)

        # Associo la librerie del messaggio al tipo di output, sia custom che standard
        output_type_library = Library()
        output_type_library.setPath("{}/{}.h".format(self.output_type.namespace, self.output_type.type_name))

        self.output_type.setLibrary(output_type_library)

        ###################
        ### Source Text ###
        ###################

        self.source_text_function = self.createSourceTextFileFromSourceText(tfs.getSourceText(thread_function),
                                                                            tfs.getSourceName(thread_function))

        if self.source_text_function is None:
            return False, "Unable to find property Source_Text or Source_Name"

        self.source_text_function.setTF(self.thread_uses_tf)
        self.source_text_function.setFunctionType(self.output_type)

        #################
        ### FREQUENCY ###
        #################

        (period, period_unit) = tfs.getPeriod(self.thread)

        if period is None or period_unit is None:
            return False, "Unable to find property Period with relative value and unit"

        # Conversione in secondi della frequenza a partire da qualunque unità di misura
        try:
            period_quantity = ureg("{} {}".format(period, period_unit))
            period_quantity.ito(ureg.second)
            self.frequency_in_hz = 1.0 / period_quantity.magnitude
            self.period_in_seconds = period_quantity.magnitude
        except ValueError:
            return False, "Unable to convert Period in seconds"

        # param_freq = Variable( self.associated_class )
        # param_freq.setName( "frequency_{}".format(self.name) )
        # param_freq.setType( Int( self.associated_class ))
        # self.associated_class.addParameter( param_freq )

        #############
        ### TOPIC ###
        #############

        (status, desc) = self.getDefaultTopicName(self.output_port_name, output=True)
        if status is False:
            return status, desc

        #####################
        ### PUBLISHER VAR ###
        #####################

        var_publisher_pub = Variable(self.associated_class)
        var_publisher_pub.setName("pub_{}".format(self.name))
        var_publisher_pub.setType(ROS_Publisher(self.associated_class))
        self.associated_class.addInternalVariable(var_publisher_pub)

        #######################
        ### PUBLISHER TIMER ###
        #######################

        var_timer_pub = Variable(self.associated_class)
        var_timer_pub.setName("timer_{}".format(self.name))
        var_timer_pub.setType(ROS_Timer(self.associated_class))
        self.associated_class.addInternalVariable(var_timer_pub)

        ##########################
        ### PUBLISHER CALLBACK ###
        ##########################

        self.publisherCallback = Method(self.associated_class)
        self.publisherCallback.method_name = "{}_callback".format(self.name)
        self.publisherCallback.return_type = Void(self.associated_class)
        self.publisherCallback.namespace = self.associated_class.class_name

        input_par = Variable(self.associated_class)
        input_par.setIsParameter()
        input_par.setType(ROS_TimerEvent(self.associated_class))
        input_par.setName("")
        self.publisherCallback.addInputParameter(input_par)

        # Aggiungo la chiamata alla funzione custome
        if self.source_text_function:
            code = "{}.publish({});".format(var_publisher_pub.name,
                                            self.source_text_function.generateInlineCode())
            self.publisherCallback.addMiddleCode(code)

        self.associated_class.addPrivateMethod(self.publisherCallback)

        main_thread.prepare.addMiddleCode("{} = handle.advertise < {} > (\"{}\", 10);"
                                          .format(var_publisher_pub.name, self.output_type.generateCode(), self.topic))

        main_thread.prepare.addMiddleCode("{} = handle.createTimer(ros::Duration({}), {}, this);"
                                          .format(var_timer_pub.name, self.period_in_seconds,
                                                  self.publisherCallback.getThreadPointer()))

        return True, ""