author | Edouard Tisserant |
Fri, 12 Apr 2013 15:41:22 +0900 | |
changeset 2121 | 11102245e1d4 |
parent 2118 | c317b1aaf920 |
child 2136 | 71fdfd4a12a3 |
permissions | -rw-r--r-- |
2111 | 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 |
||
2118
c317b1aaf920
Fixed bug when using real axis and simulated axis at the same time (got the same index)
Laurent Bessard
parents:
2111
diff
changeset
|
54 |
BLOCK_FUNTION_DEFINITION_TEMPLATE = " __CIA402Node_%(location)s.axis->__mcl_func_MC_%(blocktype)s = __%(blocktype)s_%(location)s;" |
2111 | 55 |
|
56 |
GLOBAL_INSTANCES = [ |
|
57 |
{"blocktype": "GetTorqueLimit", |
|
58 |
"inputs": [], |
|
59 |
"outputs": [{"name": "TorqueLimitPos", "type": "UINT"}, |
|
60 |
{"name": "TorqueLimitNeg", "type": "UINT"}]}, |
|
61 |
{"blocktype": "SetTorqueLimit", |
|
62 |
"inputs": [{"name": "TorqueLimitPos", "type": "UINT"}, |
|
63 |
{"name": "TorqueLimitNeg", "type": "UINT"}], |
|
64 |
"outputs": []}, |
|
65 |
] |
|
66 |
||
67 |
#-------------------------------------------------- |
|
68 |
# Ethercat CIA402 Node |
|
69 |
#-------------------------------------------------- |
|
70 |
||
71 |
class _EthercatCIA402SlaveCTN(_EthercatSlaveCTN): |
|
72 |
XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
73 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
74 |
<xsd:element name="CIA402SlaveParams"> |
|
75 |
<xsd:complexType> |
|
76 |
%s |
|
77 |
</xsd:complexType> |
|
78 |
</xsd:element> |
|
79 |
</xsd:schema> |
|
80 |
""" % ("\n".join(['<xsd:attribute name="Enable%s" type="xsd:boolean" use="optional" default="false"/>' % category |
|
81 |
for category, variables in EXTRA_NODE_VARIABLES]) + AxisXSD) |
|
82 |
||
83 |
NODE_PROFILE = 402 |
|
84 |
EditorType = CIA402NodeEditor |
|
85 |
||
86 |
ConfNodeMethods = [ |
|
87 |
{"bitmap" : "CIA402AxisRef", |
|
88 |
"name" : _("Axis Ref"), |
|
89 |
"tooltip" : _("Initiate Drag'n drop of Axis ref located variable"), |
|
90 |
"method" : "_getCIA402AxisRef", |
|
91 |
"push": True}, |
|
92 |
] |
|
93 |
||
94 |
def GetIconName(self): |
|
95 |
return "CIA402Slave" |
|
96 |
||
97 |
def SetParamsAttribute(self, path, value): |
|
98 |
if path == "CIA402SlaveParams.Type": |
|
99 |
path = "SlaveParams.Type" |
|
100 |
elif path == "CIA402SlaveParams.Alias": |
|
101 |
path = "SlaveParams.Alias" |
|
102 |
return _EthercatSlaveCTN.SetParamsAttribute(self, path, value) |
|
103 |
||
104 |
def GetVariableLocationTree(self): |
|
105 |
axis_name = self.CTNName() |
|
106 |
current_location = self.GetCurrentLocation() |
|
107 |
children = [{"name": "%s Axis Ref" % (axis_name), |
|
108 |
"type": LOCATION_VAR_INPUT, |
|
109 |
"size": "W", |
|
110 |
"IEC_type": "AXIS_REF", |
|
111 |
"var_name": axis_name, |
|
112 |
"location": "%%IW%s.0" % (".".join(map(str, current_location))), |
|
113 |
"description": "", |
|
114 |
"children": []}] |
|
115 |
children.extend(self.CTNParent.GetDeviceLocationTree(self.GetSlavePos(), current_location, axis_name)) |
|
116 |
return {"name": axis_name, |
|
117 |
"type": LOCATION_CONFNODE, |
|
118 |
"location": self.GetFullIEC_Channel(), |
|
119 |
"children": children, |
|
120 |
} |
|
121 |
||
122 |
def CTNGlobalInstances(self): |
|
123 |
current_location = self.GetCurrentLocation() |
|
124 |
return [("%s_%s" % (block_infos["blocktype"], "_".join(map(str, current_location))), |
|
125 |
"EtherLab%s" % block_infos["blocktype"]) for block_infos in GLOBAL_INSTANCES] |
|
126 |
||
127 |
def _getCIA402AxisRef(self): |
|
128 |
data = wx.TextDataObject(str(("%%IW%s.0" % ".".join(map(str, self.GetCurrentLocation())), |
|
129 |
"location", "AXIS_REF", self.CTNName(), ""))) |
|
130 |
dragSource = wx.DropSource(self.GetCTRoot().AppFrame) |
|
131 |
dragSource.SetData(data) |
|
132 |
dragSource.DoDragDrop() |
|
133 |
||
134 |
def CTNGenerate_C(self, buildpath, locations): |
|
135 |
current_location = self.GetCurrentLocation() |
|
136 |
||
137 |
location_str = "_".join(map(lambda x:str(x), current_location)) |
|
138 |
||
139 |
plc_cia402node_filepath = os.path.join(os.path.split(__file__)[0], "plc_cia402node.c") |
|
140 |
plc_cia402node_file = open(plc_cia402node_filepath, 'r') |
|
141 |
plc_cia402node_code = plc_cia402node_file.read() |
|
142 |
plc_cia402node_file.close() |
|
143 |
||
144 |
str_completion = { |
|
145 |
"slave_pos": self.GetSlavePos(), |
|
146 |
"location": location_str, |
|
147 |
"MCL_headers": Headers, |
|
148 |
"extern_located_variables_declaration": [], |
|
149 |
"fieldbus_interface_declaration": [], |
|
150 |
"fieldbus_interface_definition": [], |
|
151 |
"entry_variables": [], |
|
152 |
"init_axis_params": [], |
|
153 |
"init_entry_variables": [], |
|
154 |
"extra_variables_retrieve": [], |
|
155 |
"extra_variables_publish": [] |
|
156 |
} |
|
157 |
||
158 |
for blocktype_infos in GLOBAL_INSTANCES: |
|
159 |
texts = { |
|
160 |
"blocktype": blocktype_infos["blocktype"], |
|
161 |
"ucase_blocktype": blocktype_infos["blocktype"].upper(), |
|
162 |
"location": "_".join(map(str, current_location)) |
|
163 |
} |
|
164 |
texts["blockname"] = "%(ucase_blocktype)s_%(location)s" % texts |
|
165 |
||
166 |
inputs = [{"input_name": "POS", "input_value": str(self.GetSlavePos())}, |
|
167 |
{"input_name": "EXECUTE", "input_value": "__GET_VAR(data__->EXECUTE)"}] +\ |
|
168 |
[{"input_name": input["name"].upper(), |
|
169 |
"input_value": "__GET_VAR(data__->%s)" % input["name"].upper()} |
|
170 |
for input in blocktype_infos["inputs"]] |
|
171 |
input_texts = [] |
|
172 |
for input_infos in inputs: |
|
173 |
input_infos.update(texts) |
|
174 |
input_texts.append(BLOCK_INPUT_TEMPLATE % input_infos) |
|
175 |
texts["extract_inputs"] = "\n".join(input_texts) |
|
176 |
||
177 |
outputs = [{"output_name": output} for output in ["DONE", "BUSY", "ERROR"]] + \ |
|
178 |
[{"output_name": output["name"].upper()} for output in blocktype_infos["outputs"]] |
|
179 |
output_texts = [] |
|
180 |
for output_infos in outputs: |
|
181 |
output_infos.update(texts) |
|
182 |
output_texts.append(BLOCK_OUTPUT_TEMPLATE % output_infos) |
|
183 |
texts["return_outputs"] = "\n".join(output_texts) |
|
184 |
||
185 |
str_completion["fieldbus_interface_declaration"].append( |
|
186 |
BLOCK_FUNCTION_TEMPLATE % texts) |
|
187 |
||
188 |
str_completion["fieldbus_interface_definition"].append( |
|
189 |
BLOCK_FUNTION_DEFINITION_TEMPLATE % texts) |
|
190 |
||
191 |
variables = NODE_VARIABLES[:] |
|
192 |
||
193 |
params = self.CTNParams[1].getElementInfos(self.CTNParams[0]) |
|
194 |
for param in params["children"]: |
|
195 |
if param["name"] in EXTRA_NODE_VARIABLES_DICT: |
|
196 |
if param["value"]: |
|
197 |
extra_variables = EXTRA_NODE_VARIABLES_DICT.get(param["name"]) |
|
198 |
for variable_infos in extra_variables: |
|
199 |
var_infos = { |
|
200 |
"location": location_str, |
|
201 |
"name": variable_infos["description"][0] |
|
202 |
} |
|
203 |
variables.append(variable_infos["description"]) |
|
204 |
retrieve_template = variable_infos.get("retrieve", DEFAULT_RETRIEVE) |
|
205 |
publish_template = variable_infos.get("publish", DEFAULT_PUBLISH) |
|
206 |
||
207 |
if retrieve_template is not None: |
|
208 |
str_completion["extra_variables_retrieve"].append( |
|
209 |
retrieve_template % var_infos) |
|
210 |
if publish_template is not None: |
|
211 |
str_completion["extra_variables_publish"].append( |
|
212 |
publish_template % var_infos) |
|
213 |
elif param["value"] is not None: |
|
214 |
param_infos = { |
|
215 |
"location": location_str, |
|
216 |
"param_name": param["name"], |
|
217 |
} |
|
218 |
if param["type"] == "boolean": |
|
219 |
param_infos["param_value"] = {True: "true", False: "false"}[param["value"]] |
|
220 |
else: |
|
221 |
param_infos["param_value"] = str(param["value"]) |
|
222 |
str_completion["init_axis_params"].append( |
|
223 |
" __CIA402Node_%(location)s.axis->%(param_name)s = %(param_value)s;" % param_infos) |
|
224 |
||
225 |
for variable in variables: |
|
226 |
var_infos = dict(zip(["name", "index", "subindex", "var_type", "dir"], variable)) |
|
227 |
var_infos["location"] = location_str |
|
228 |
var_infos["var_size"] = self.GetSizeOfType(var_infos["var_type"]) |
|
229 |
var_infos["var_name"] = "__%(dir)s%(var_size)s%(location)s_%(index)d_%(subindex)d" % var_infos |
|
230 |
||
231 |
str_completion["extern_located_variables_declaration"].append( |
|
232 |
"IEC_%(var_type)s *%(var_name)s;" % var_infos) |
|
233 |
str_completion["entry_variables"].append( |
|
234 |
" IEC_%(var_type)s *%(name)s;" % var_infos) |
|
235 |
str_completion["init_entry_variables"].append( |
|
236 |
" __CIA402Node_%(location)s.%(name)s = %(var_name)s;" % var_infos) |
|
237 |
||
238 |
self.CTNParent.FileGenerator.DeclareVariable( |
|
239 |
self.GetSlavePos(), var_infos["index"], var_infos["subindex"], |
|
240 |
var_infos["var_type"], var_infos["dir"], var_infos["var_name"]) |
|
241 |
||
242 |
for element in ["extern_located_variables_declaration", |
|
243 |
"fieldbus_interface_declaration", |
|
244 |
"fieldbus_interface_definition", |
|
245 |
"entry_variables", |
|
246 |
"init_axis_params", |
|
247 |
"init_entry_variables", |
|
248 |
"extra_variables_retrieve", |
|
249 |
"extra_variables_publish"]: |
|
250 |
str_completion[element] = "\n".join(str_completion[element]) |
|
251 |
||
252 |
Gen_CIA402Nodefile_path = os.path.join(buildpath, "cia402node_%s.c"%location_str) |
|
253 |
cia402nodefile = open(Gen_CIA402Nodefile_path, 'w') |
|
254 |
cia402nodefile.write(plc_cia402node_code % str_completion) |
|
255 |
cia402nodefile.close() |
|
256 |
||
257 |
return [(Gen_CIA402Nodefile_path, '"-I%s"'%os.path.abspath(self.GetCTRoot().GetIECLibPath()))],"",True |