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