etherlab/EthercatCIA402Slave.py
changeset 2111 f2cffda17d00
child 2118 c317b1aaf920
equal deleted inserted replaced
2110:e8c43f542eb1 2111:f2cffda17d00
       
     1 import os
       
     2 
       
     3 import wx
       
     4 
       
     5 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
       
     6 
       
     7 from MotionLibrary import Headers, AxisXSD
       
     8 from EthercatSlave import _EthercatSlaveCTN
       
     9 from ConfigEditor import CIA402NodeEditor
       
    10 
       
    11 NODE_VARIABLES = [
       
    12     ("ControlWord", 0x6040, 0x00, "UINT", "Q"),
       
    13     ("TargetPosition", 0x607a, 0x00, "DINT", "Q"),
       
    14     ("ModesOfOperation", 0x06060, 0x00, "SINT", "Q"),
       
    15     ("StatusWord", 0x6041, 0x00, "UINT", "I"),
       
    16     ("ModesOfOperationDisplay", 0x06061, 0x00, "SINT", "I"),
       
    17     ("ActualPosition", 0x6064, 0x00, "DINT", "I"),
       
    18     ("ActualVelocity", 0x606C, 0x00, "DINT", "I"),
       
    19 ]
       
    20 
       
    21 DEFAULT_RETRIEVE = "    __CIA402Node_%(location)s.axis->%(name)s = *(__CIA402Node_%(location)s.%(name)s);"
       
    22 DEFAULT_PUBLISH = "    *(__CIA402Node_%(location)s.%(name)s) = __CIA402Node_%(location)s.axis->%(name)s;"
       
    23 
       
    24 EXTRA_NODE_VARIABLES = [
       
    25     ("ErrorCode", [
       
    26         {"description": ("ErrorCode", 0x603F, 0x00, "UINT", "I"),
       
    27          "publish": None}
       
    28         ]),
       
    29     ("DigitalInputs", [
       
    30         {"description": ("DigitalInputs", 0x60FD, 0x00, "UDINT", "I"),
       
    31          "publish": None}
       
    32         ]),
       
    33     ("DigitalOutputs", [
       
    34         {"description": ("DigitalOutputs", 0x60FE, 0x00, "UDINT", "Q"),
       
    35          "retrieve": None}
       
    36         ])
       
    37 ]
       
    38 EXTRA_NODE_VARIABLES_DICT = dict([("Enable" + name, value) for name, value in EXTRA_NODE_VARIABLES])
       
    39 
       
    40 BLOCK_INPUT_TEMPLATE = "    __SET_VAR(%(blockname)s->,%(input_name)s, %(input_value)s);"
       
    41 BLOCK_OUTPUT_TEMPLATE = "    __SET_VAR(data__->,%(output_name)s, __GET_VAR(%(blockname)s->%(output_name)s));"
       
    42 
       
    43 BLOCK_FUNCTION_TEMPLATE = """
       
    44 extern void ETHERLAB%(ucase_blocktype)s_body__(ETHERLAB%(ucase_blocktype)s* data__);
       
    45 void __%(blocktype)s_%(location)s(MC_%(ucase_blocktype)s *data__) {
       
    46 __DECLARE_GLOBAL_PROTOTYPE(ETHERLAB%(ucase_blocktype)s, %(blockname)s);
       
    47 ETHERLAB%(ucase_blocktype)s* %(blockname)s = __GET_GLOBAL_%(blockname)s();
       
    48 %(extract_inputs)s
       
    49 ETHERLAB%(ucase_blocktype)s_body__(%(blockname)s);
       
    50 %(return_outputs)s
       
    51 }
       
    52 """
       
    53 
       
    54 BLOCK_FUNTION_DEFINITION_TEMPLATE = """    if (!__CIA402Node_%(location)s.axis->__mcl_func_MC_%(blocktype)s)
       
    55 __CIA402Node_%(location)s.axis->__mcl_func_MC_%(blocktype)s = __%(blocktype)s_%(location)s;"""
       
    56 
       
    57 GLOBAL_INSTANCES = [
       
    58     {"blocktype": "GetTorqueLimit", 
       
    59      "inputs": [],
       
    60      "outputs": [{"name": "TorqueLimitPos", "type": "UINT"},
       
    61                  {"name": "TorqueLimitNeg", "type": "UINT"}]},
       
    62     {"blocktype": "SetTorqueLimit", 
       
    63      "inputs": [{"name": "TorqueLimitPos", "type": "UINT"},
       
    64                 {"name": "TorqueLimitNeg", "type": "UINT"}],
       
    65      "outputs": []},
       
    66 ]
       
    67 
       
    68 #--------------------------------------------------
       
    69 #                 Ethercat CIA402 Node
       
    70 #--------------------------------------------------
       
    71 
       
    72 class _EthercatCIA402SlaveCTN(_EthercatSlaveCTN):
       
    73     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
       
    74     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       
    75       <xsd:element name="CIA402SlaveParams">
       
    76         <xsd:complexType>
       
    77           %s
       
    78         </xsd:complexType>
       
    79       </xsd:element>
       
    80     </xsd:schema>
       
    81     """ % ("\n".join(['<xsd:attribute name="Enable%s" type="xsd:boolean" use="optional" default="false"/>' % category 
       
    82                       for category, variables in EXTRA_NODE_VARIABLES]) + AxisXSD)
       
    83     
       
    84     NODE_PROFILE = 402
       
    85     EditorType = CIA402NodeEditor
       
    86     
       
    87     ConfNodeMethods = [
       
    88         {"bitmap" : "CIA402AxisRef",
       
    89          "name" : _("Axis Ref"),
       
    90          "tooltip" : _("Initiate Drag'n drop of Axis ref located variable"),
       
    91          "method" : "_getCIA402AxisRef",
       
    92          "push": True},
       
    93     ]
       
    94     
       
    95     def GetIconName(self):
       
    96         return "CIA402Slave"
       
    97     
       
    98     def SetParamsAttribute(self, path, value):
       
    99         if path == "CIA402SlaveParams.Type":
       
   100             path = "SlaveParams.Type"
       
   101         elif path == "CIA402SlaveParams.Alias":
       
   102             path = "SlaveParams.Alias"
       
   103         return _EthercatSlaveCTN.SetParamsAttribute(self, path, value)
       
   104     
       
   105     def GetVariableLocationTree(self):
       
   106         axis_name = self.CTNName()
       
   107         current_location = self.GetCurrentLocation()
       
   108         children = [{"name": "%s Axis Ref" % (axis_name),
       
   109                      "type": LOCATION_VAR_INPUT,
       
   110                      "size": "W",
       
   111                      "IEC_type": "AXIS_REF",
       
   112                      "var_name": axis_name,
       
   113                      "location": "%%IW%s.0" % (".".join(map(str, current_location))),
       
   114                      "description": "",
       
   115                      "children": []}]
       
   116         children.extend(self.CTNParent.GetDeviceLocationTree(self.GetSlavePos(), current_location, axis_name))
       
   117         return  {"name": axis_name,
       
   118                  "type": LOCATION_CONFNODE,
       
   119                  "location": self.GetFullIEC_Channel(),
       
   120                  "children": children,
       
   121         }
       
   122     
       
   123     def CTNGlobalInstances(self):
       
   124         current_location = self.GetCurrentLocation()
       
   125         return [("%s_%s" % (block_infos["blocktype"], "_".join(map(str, current_location))),
       
   126                  "EtherLab%s" % block_infos["blocktype"]) for block_infos in GLOBAL_INSTANCES]
       
   127     
       
   128     def _getCIA402AxisRef(self):
       
   129         data = wx.TextDataObject(str(("%%IW%s.0" % ".".join(map(str, self.GetCurrentLocation())), 
       
   130                                       "location", "AXIS_REF", self.CTNName(), "")))
       
   131         dragSource = wx.DropSource(self.GetCTRoot().AppFrame)
       
   132         dragSource.SetData(data)
       
   133         dragSource.DoDragDrop()
       
   134     
       
   135     def CTNGenerate_C(self, buildpath, locations):
       
   136         current_location = self.GetCurrentLocation()
       
   137         
       
   138         location_str = "_".join(map(lambda x:str(x), current_location))
       
   139         
       
   140         plc_cia402node_filepath = os.path.join(os.path.split(__file__)[0], "plc_cia402node.c")
       
   141         plc_cia402node_file = open(plc_cia402node_filepath, 'r')
       
   142         plc_cia402node_code = plc_cia402node_file.read()
       
   143         plc_cia402node_file.close()
       
   144         
       
   145         str_completion = {
       
   146             "slave_pos": self.GetSlavePos(),
       
   147             "location": location_str,
       
   148             "MCL_headers": Headers,
       
   149             "extern_located_variables_declaration": [],
       
   150             "fieldbus_interface_declaration": [],
       
   151             "fieldbus_interface_definition": [],
       
   152             "entry_variables": [],
       
   153             "init_axis_params": [],
       
   154             "init_entry_variables": [],
       
   155             "extra_variables_retrieve": [],
       
   156             "extra_variables_publish": []
       
   157         }
       
   158         
       
   159         for blocktype_infos in GLOBAL_INSTANCES:
       
   160             texts = {
       
   161                 "blocktype": blocktype_infos["blocktype"],
       
   162                 "ucase_blocktype": blocktype_infos["blocktype"].upper(),
       
   163                 "location": "_".join(map(str, current_location))
       
   164             }
       
   165             texts["blockname"] = "%(ucase_blocktype)s_%(location)s" % texts
       
   166             
       
   167             inputs = [{"input_name": "POS", "input_value": str(self.GetSlavePos())},
       
   168                       {"input_name": "EXECUTE", "input_value": "__GET_VAR(data__->EXECUTE)"}] +\
       
   169                      [{"input_name": input["name"].upper(), 
       
   170                        "input_value": "__GET_VAR(data__->%s)" % input["name"].upper()}
       
   171                       for input in blocktype_infos["inputs"]]
       
   172             input_texts = []
       
   173             for input_infos in inputs:
       
   174                 input_infos.update(texts)
       
   175                 input_texts.append(BLOCK_INPUT_TEMPLATE % input_infos)
       
   176             texts["extract_inputs"] = "\n".join(input_texts)
       
   177             
       
   178             outputs = [{"output_name": output} for output in ["DONE", "BUSY", "ERROR"]] + \
       
   179                       [{"output_name": output["name"].upper()} for output in blocktype_infos["outputs"]]
       
   180             output_texts = []
       
   181             for output_infos in outputs:
       
   182                 output_infos.update(texts)
       
   183                 output_texts.append(BLOCK_OUTPUT_TEMPLATE % output_infos)
       
   184             texts["return_outputs"] = "\n".join(output_texts)
       
   185             
       
   186             str_completion["fieldbus_interface_declaration"].append(
       
   187                     BLOCK_FUNCTION_TEMPLATE % texts)
       
   188             
       
   189             str_completion["fieldbus_interface_definition"].append(
       
   190                     BLOCK_FUNTION_DEFINITION_TEMPLATE % texts)
       
   191             
       
   192         variables = NODE_VARIABLES[:]
       
   193         
       
   194         params = self.CTNParams[1].getElementInfos(self.CTNParams[0])
       
   195         for param in params["children"]:
       
   196             if param["name"] in EXTRA_NODE_VARIABLES_DICT:
       
   197                 if param["value"]:
       
   198                     extra_variables = EXTRA_NODE_VARIABLES_DICT.get(param["name"])
       
   199                     for variable_infos in extra_variables:
       
   200                         var_infos = {
       
   201                             "location": location_str,
       
   202                             "name": variable_infos["description"][0]
       
   203                         }
       
   204                         variables.append(variable_infos["description"])
       
   205                         retrieve_template = variable_infos.get("retrieve", DEFAULT_RETRIEVE)
       
   206                         publish_template = variable_infos.get("publish", DEFAULT_PUBLISH)
       
   207                         
       
   208                         if retrieve_template is not None:
       
   209                             str_completion["extra_variables_retrieve"].append(
       
   210                                 retrieve_template % var_infos)
       
   211                         if publish_template is not None:
       
   212                             str_completion["extra_variables_publish"].append(
       
   213                                 publish_template % var_infos)
       
   214             elif param["value"] is not None:
       
   215                 param_infos = {
       
   216                     "location": location_str,
       
   217                     "param_name": param["name"],
       
   218                 }
       
   219                 if param["type"] == "boolean":
       
   220                     param_infos["param_value"] = {True: "true", False: "false"}[param["value"]]
       
   221                 else:
       
   222                     param_infos["param_value"] = str(param["value"])
       
   223                 str_completion["init_axis_params"].append(
       
   224                     "        __CIA402Node_%(location)s.axis->%(param_name)s = %(param_value)s;" % param_infos)
       
   225         
       
   226         for variable in variables:
       
   227             var_infos = dict(zip(["name", "index", "subindex", "var_type", "dir"], variable))
       
   228             var_infos["location"] = location_str
       
   229             var_infos["var_size"] = self.GetSizeOfType(var_infos["var_type"])
       
   230             var_infos["var_name"] = "__%(dir)s%(var_size)s%(location)s_%(index)d_%(subindex)d" % var_infos
       
   231             
       
   232             str_completion["extern_located_variables_declaration"].append(
       
   233                     "IEC_%(var_type)s *%(var_name)s;" % var_infos)
       
   234             str_completion["entry_variables"].append(
       
   235                     "    IEC_%(var_type)s *%(name)s;" % var_infos)
       
   236             str_completion["init_entry_variables"].append(
       
   237                     "    __CIA402Node_%(location)s.%(name)s = %(var_name)s;" % var_infos)
       
   238             
       
   239             self.CTNParent.FileGenerator.DeclareVariable(
       
   240                     self.GetSlavePos(), var_infos["index"], var_infos["subindex"], 
       
   241                     var_infos["var_type"], var_infos["dir"], var_infos["var_name"])
       
   242         
       
   243         for element in ["extern_located_variables_declaration", 
       
   244                         "fieldbus_interface_declaration",
       
   245                         "fieldbus_interface_definition",
       
   246                         "entry_variables", 
       
   247                         "init_axis_params", 
       
   248                         "init_entry_variables",
       
   249                         "extra_variables_retrieve",
       
   250                         "extra_variables_publish"]:
       
   251             str_completion[element] = "\n".join(str_completion[element])
       
   252         
       
   253         Gen_CIA402Nodefile_path = os.path.join(buildpath, "cia402node_%s.c"%location_str)
       
   254         cia402nodefile = open(Gen_CIA402Nodefile_path, 'w')
       
   255         cia402nodefile.write(plc_cia402node_code % str_completion)
       
   256         cia402nodefile.close()
       
   257         
       
   258         return [(Gen_CIA402Nodefile_path, '"-I%s"'%os.path.abspath(self.GetCTRoot().GetIECLibPath()))],"",True