etherlab/EthercatCIA402Slave.py
changeset 2406 ffd091b51cd0
parent 2405 af050469fc5c
child 2407 5f2b1bb464a0
equal deleted inserted replaced
2405:af050469fc5c 2406:ffd091b51cd0
    14 
    14 
    15 import wx
    15 import wx
    16 
    16 
    17 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT
    17 from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT
    18 
    18 
    19 from MotionLibrary import Headers, AxisXSD
    19 from MotionLibrary import AxisXSD
    20 from etherlab.EthercatSlave import _EthercatSlaveCTN, _CommonSlave
    20 from etherlab.EthercatSlave import _EthercatSlaveCTN, _CommonSlave
    21 from etherlab.ConfigEditor import CIA402NodeEditor
    21 from etherlab.ConfigEditor import CIA402NodeEditor
    22 
    22 
    23 # Definition of node variables that have to be mapped in PDO
    23 # Definition of node variables that have to be mapped in PDO
    24 # [(name, index, subindex, type,
    24 # [(name, index, subindex, type,
   203     def CTNGenerate_C(self, buildpath, locations):
   203     def CTNGenerate_C(self, buildpath, locations):
   204         current_location = self.GetCurrentLocation()
   204         current_location = self.GetCurrentLocation()
   205 
   205 
   206         location_str = "_".join(map(str, current_location))
   206         location_str = "_".join(map(str, current_location))
   207         slave_pos = self.GetSlavePos()
   207         slave_pos = self.GetSlavePos()
   208         MCL_headers = Headers
       
   209 
   208 
   210         # Open CIA402 node code template file
   209         # Open CIA402 node code template file
   211         plc_cia402node_filepath = os.path.join(os.path.split(__file__)[0],
   210         plc_cia402node_filepath = os.path.join(os.path.split(__file__)[0],
   212                                                "plc_cia402node.c")
   211                                                "plc_cia402node.c")
   213         plc_cia402node_file = open(plc_cia402node_filepath, 'r')
   212         plc_cia402node_file = open(plc_cia402node_filepath, 'r')
   239                     for input in blocktype_infos["inputs"]]
   238                     for input in blocktype_infos["inputs"]]
   240                 ])
   239                 ])
   241 
   240 
   242             return_outputs = "\n".join([
   241             return_outputs = "\n".join([
   243                 """\
   242                 """\
   244     __SET_VAR(data__->,%(output_name)s,,
   243                 __SET_VAR(data__->,%(output_name)s,,
   245               __GET_VAR(%(blockname)s->%(output_name)s));""" % locals()
   244                 __GET_VAR(%(blockname)s->%(output_name)s));""" % {
       
   245                     "output_name": output_name,
       
   246                     "blockname": blockname
       
   247                 }
   246                 for output_name in ["DONE", "BUSY", "ERROR"] + [
   248                 for output_name in ["DONE", "BUSY", "ERROR"] + [
   247                         output["name"].upper()
   249                         output["name"].upper()
   248                         for output in blocktype_infos["outputs"]]
   250                         for output in blocktype_infos["outputs"]]
   249             ])
   251             ])
       
   252 
       
   253             loc_dict = {
       
   254                 "ucase_blocktype": ucase_blocktype,
       
   255                 "blocktype": blocktype,
       
   256                 "blockname": blockname,
       
   257                 "location_str": location_str,
       
   258                 "extract_inputs": extract_inputs,
       
   259                 "return_outputs": return_outputs,
       
   260             }
   250 
   261 
   251             fieldbus_interface_declaration.append("""
   262             fieldbus_interface_declaration.append("""
   252 extern void ETHERLAB%(ucase_blocktype)s_body__(ETHERLAB%(ucase_blocktype)s* data__);
   263 extern void ETHERLAB%(ucase_blocktype)s_body__(ETHERLAB%(ucase_blocktype)s* data__);
   253 void __%(blocktype)s_%(location_str)s(MC_%(ucase_blocktype)s *data__) {
   264 void __%(blocktype)s_%(location_str)s(MC_%(ucase_blocktype)s *data__) {
   254 __DECLARE_GLOBAL_PROTOTYPE(ETHERLAB%(ucase_blocktype)s, %(blockname)s);
   265 __DECLARE_GLOBAL_PROTOTYPE(ETHERLAB%(ucase_blocktype)s, %(blockname)s);
   255 ETHERLAB%(ucase_blocktype)s* %(blockname)s = __GET_GLOBAL_%(blockname)s();
   266 ETHERLAB%(ucase_blocktype)s* %(blockname)s = __GET_GLOBAL_%(blockname)s();
   256 __SET_VAR(%(blockname)s->, POS,, AxsPub.axis->NetworkPosition);
   267 __SET_VAR(%(blockname)s->, POS,, AxsPub.axis->NetworkPosition);
   257 %(extract_inputs)s
   268 %(extract_inputs)s
   258 ETHERLAB%(ucase_blocktype)s_body__(%(blockname)s);
   269 ETHERLAB%(ucase_blocktype)s_body__(%(blockname)s);
   259 %(return_outputs)s
   270 %(return_outputs)s
   260 }""" % locals())
   271 }""" % loc_dict)
   261 
   272 
   262             fieldbus_interface_definition.append("""\
   273             fieldbus_interface_definition.append("""\
   263         AxsPub.axis->__mcl_func_MC_%(blocktype)s = __%(blocktype)s_%(location_str)s;\
   274         AxsPub.axis->__mcl_func_MC_%(blocktype)s = __%(blocktype)s_%(location_str)s;\
   264 """ % locals())
   275 """ % loc_dict)
   265 
   276 
   266         # Get a copy list of default variables to map
   277         # Get a copy list of default variables to map
   267         variables = NODE_VARIABLES[:]
   278         variables = NODE_VARIABLES[:]
   268 
   279 
   269         # Set AxisRef public struct members value
   280         # Set AxisRef public struct members value
   286                     # Add each variables defined in section description to the
   297                     # Add each variables defined in section description to the
   287                     # list of variables to map
   298                     # list of variables to map
   288                     variables.append(variable_infos["description"])
   299                     variables.append(variable_infos["description"])
   289 
   300 
   290                     # Add code to publish or retrive variable
   301                     # Add code to publish or retrive variable
   291                     for var_exchange_dir, str_list, default_template in [
   302                     coded = [
   292                          ("retrieve", extra_variables_retrieve,
   303                         ("retrieve",
   293                           "    AxsPub.axis->%(var_name)s = *(AxsPub.%(var_name)s);"),
   304                          extra_variables_retrieve,
   294                          ("publish", extra_variables_publish,
   305                          "    AxsPub.axis->%(var_name)s = *(AxsPub.%(var_name)s);"),
   295                           "    *(AxsPub.%(var_name)s) = AxsPub.axis->%(var_name)s;")]:
   306                         ("publish",
   296 
   307                          extra_variables_publish,
   297                         template = variable_infos.get(var_exchange_dir,
   308                          "    *(AxsPub.%(var_name)s) = AxsPub.axis->%(var_name)s;")
   298                                                       default_template)
   309                     ]
       
   310                     for var_exchange_dir, _str_list, default_template in coded:
       
   311                         template = variable_infos.get(var_exchange_dir, default_template)
   299                         if template is not None:
   312                         if template is not None:
   300                             extra_variables_publish.append(template % locals())
   313                             extra_variables_publish.append(template % locals())
   301 
   314 
   302             # Set AxisRef public struct member value if defined
   315             # Set AxisRef public struct member value if defined
   303             if param["value"] is not None:
   316             if param["value"] is not None:
   304                 param_value = ({True: "1", False: "0"}[param["value"]]
   317                 param_value = ({True: "1", False: "0"}[param["value"]]
   305                                if param["type"] == "boolean"
   318                                if param["type"] == "boolean"
   306                                else str(param["value"]))
   319                                else str(param["value"]))
   307 
   320 
   308                 init_axis_params.append("""\
   321                 init_axis_params.append("""\
   309         AxsPub.axis->%(param_name)s = %(param_value)s;""" % locals())
   322         AxsPub.axis->%(param_name)s = %(param_value)s;""" % {"param_value": param_value})
   310 
   323 
   311         # Add each variable in list of variables to map to master list of
   324         # Add each variable in list of variables to map to master list of
   312         # variables to add to network configuration
   325         # variables to add to network configuration
   313         for name, index, subindex, var_type, dir in variables:
   326         for name, index, subindex, var_type, dir in variables:
   314             var_size = self.GetSizeOfType(var_type)
   327             var_size = self.GetSizeOfType(var_type)
       
   328             loc_dict = {
       
   329                 "var_size": var_size,
       
   330                 "var_type": var_type,
       
   331                 "name:": name,
       
   332                 "location_str": location_str,
       
   333                 "index": index,
       
   334                 "subindex": subindex,
       
   335             }
   315             var_name = """\
   336             var_name = """\
   316 __%(dir)s%(var_size)s%(location_str)s_%(index)d_%(subindex)d""" % locals()
   337 __%(dir)s%(var_size)s%(location_str)s_%(index)d_%(subindex)d""" % loc_dict
       
   338             loc_dict["var_name"] = var_name
   317 
   339 
   318             extern_located_variables_declaration.append(
   340             extern_located_variables_declaration.append(
   319                     "IEC_%(var_type)s *%(var_name)s;" % locals())
   341                     "IEC_%(var_type)s *%(var_name)s;" % loc_dict)
   320             entry_variables.append(
   342             entry_variables.append(
   321                     "    IEC_%(var_type)s *%(name)s;" % locals())
   343                     "    IEC_%(var_type)s *%(name)s;" % loc_dict)
   322             init_entry_variables.append(
   344             init_entry_variables.append(
   323                     "    AxsPub.%(name)s = %(var_name)s;" % locals())
   345                     "    AxsPub.%(name)s = %(var_name)s;" % loc_dict)
   324 
   346 
   325             self.CTNParent.FileGenerator.DeclareVariable(
   347             self.CTNParent.FileGenerator.DeclareVariable(
   326                     slave_pos, index, subindex, var_type, dir, var_name)
   348                     slave_pos, index, subindex, var_type, dir, var_name)
   327 
   349 
   328         # Add newline between string in list of generated strings for sections
   350         # Add newline between string in list of generated strings for sections