-
Notifications
You must be signed in to change notification settings - Fork 2
/
EncoderFM350.scl
445 lines (422 loc) · 15 KB
/
EncoderFM350.scl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
FUNCTION_BLOCK FB127
TITLE ='Input driver for FM350'
AUTHOR : EMC
FAMILY : EMC2
NAME : EncFM350
VERSION : '2.1'
VAR_INPUT
EncErr : BOOL ; //1=Encoder error
END_VAR
VAR_OUTPUT
DI_0 : BOOL ; //Digital input 0
DI_1 : BOOL ; //Digital input 1
DI_2 : BOOL ; //Digital input 2
END_VAR
VAR_IN_OUT
Axis : UDT1 ; //Global axis data
Init : BOOL := TRUE; //1=Initialize block
ReadDiagErr : BOOL := TRUE; //1=Read data record 1 (DS1)
END_VAR
VAR
Koord : STRUCT
DA_ERR_W : WORD ; //Data error word
OT_ERR_B : BYTE ; //Operating error byte
bit0 : BOOL ;
STS_TFB : BOOL ; //Status test free
DIAG : BOOL ; //internal use
OT_ERR : BOOL ; //internal use
DATA_ERR : BOOL ; //Data error bit
FM_NEUSTQ : BOOL ; //internal use
FM_NEUST : BOOL ; //internal use
PARA : BOOL ; //1=Module configured
END_STRUCT ;
Koord_dw AT Koord : DWORD ;
Status : STRUCT
byte0 : BYTE ;
STS_RUN : BOOL ; //Status counter working
STS_DIR : BOOL ; //Status count direction
STS_ZERO : BOOL ; //Status zero pass
STS_OFLW : BOOL ; //Status overflow
STS_UFLW : BOOL ; //Status underflow
STS_SYNC : BOOL ; //Status counter synchronized
STS_GATE : BOOL ; //Status internal gate
STS_SW_G : BOOL ; //Status software gate
STS_SET : BOOL ; //Status software gate
bit9 : BOOL ;
STS_STA : BOOL ; //Status digital input START
STS_STP : BOOL ; //Status digital input STOP
STS_CMP1 : BOOL ; //Status output comparison value 1
STS_CMP2 : BOOL ; //Status output comparison value 2
bit14 : BOOL ;
bit15 : BOOL ;
LoadInCounter : BOOL ; //internal use
LoadInLoad : BOOL ; //internal use
Comp1Set : BOOL ; //internal use
Comp2Set : BOOL ; //internal use
NoSync : BOOL ; //internal use
NoZero : BOOL ; //internal use
bit6 : BOOL ;
bit7 : BOOL ;
END_STRUCT ;
Status_dw AT Status : DWORD ;
JobErr : INT ; //Error reported while reading DS1
DiagStatus : STRUCT
MDL_DEFECT : BOOL ; //Module defective
INT_FAULT : BOOL ; //Internal fault
EXT_FAULT : BOOL ; //External fault
PNT_INFO : BOOL ; //Point information
EXT_VOLTAGE : BOOL ; //External voltage low
FLD_CONNCTR : BOOL ; //Field wiring connector missing
NO_CONFIG : BOOL ; //Module has no configuration data
CONFIG_ERR : BOOL ; //Module has configuration error
MDL_TYPE : BYTE ; //Type of module
SUB_NDL_ERR : BOOL ; //Sub-Module is missing or has error
COMM_FAULT : BOOL ; //Communication fault
MDL_STOP : BOOL ; //Module is stopped
WTCH_DOG_FLT : BOOL ; //Watch dog timer stopped module
INT_PS_FLT : BOOL ; //Internal power supply fault
PRIM_BATT_FLT : BOOL ; //Primary battery is in fault
BCKUP_BATT_FLT : BOOL ; //Backup battery is in fault
RESERVED_2 : BOOL ; //Reserved for system
RACK_FLT : BOOL ; //Rack fault, only for bus interface module
PROC_FLT : BOOL ; //Processor fault
EPROM_FLT : BOOL ; //EPROM fault
RAM_FLT : BOOL ; //RAM fault
ADU_FLT : BOOL ; //ADU fault
FUSE_FLT : BOOL ; //Fuse fault
HW_INTR_FLT : BOOL ; //Hardware interupt input in fault
RESERVED_3 : BOOL ; //Reserved for system
CH_TYPE : BYTE ; //Channel type
LGTH_DIA : BYTE ; //Diagnostics data length per channel
CH_NO : BYTE ; //Channel number
GRP_ERR1 : BOOL ; //Group error channel 1
bit7_1 : BOOL ;
bit7_2 : BOOL ;
bit7_3 : BOOL ;
bit7_4 : BOOL ;
bit7_5 : BOOL ;
bit7_6 : BOOL ;
bit7_7 : BOOL ;
CH1_SIGA : BOOL ; //Error signal A
CH1_SIGB : BOOL ; //Error signal B
CH1_SIGZ : BOOL ; //Error signal zero
CH1_BETW : BOOL ; //Error between channels
CH1_5V2 : BOOL ; //Error in 5.2 V encoder supply
byte9 : BYTE ;
byte10 : BYTE ;
byte11 : BYTE ;
dword12 : DWORD ;
END_STRUCT ;
DiagStatus_dw AT DiagStatus : DWORD;
GeberAlt_DI : DINT ; //internal use
Addr_Status : INT ; //internal use
Addr_Koord : INT ; //internal use
Addr_ActPos_DI : INT ; //internal use
Addr_LoadVal_DI : INT ; //internal use
Addr_Steuer : INT ; //internal use
v_Faktor : REAL ; //internal use
PASteuer : DWORD ; //internal use
PASteuer_b AT PASteuer : ARRAY[0..31] OF BOOL;
RefPointInt : REAL ; //internal use
Richtungsanpassung : INT := 1; //internal use
LoadVal_DW : DWORD ; //internal use
RefZustand : INT ; //internal use
DiagFehler : BOOL ; //internal use
EncErr_alt : BOOL ; //internal use
Diag_alt : BOOL ; //internal use
Taktsynchron : BOOL ; //internal use
sRDREC : SFB52;
END_VAR
VAR_TEMP
Geber_DI : DINT ; //internal use
LageDiff_DI : DINT ; //internal use
Addr_InChannel : INT ; //internal use
TruncKorr : REAL ; //internal use
TruncKorr_DI : DINT ; //internal use
Err_DW : DWORD ; //internal use
Err_DW_str AT Err_DW : STRUCT
SWLimitMinExceeded : BOOL ; //1=Min. SW limit switch exceeded
SWLimitMaxExceeded : BOOL ; //1=Max. SW limit switch exceeded
TargetErr : BOOL ; //1=Target beyond travel range
NoSync : BOOL ; //1=Axis not synchronized
DirectionErr : BOOL ; //1=Motion not allowed in direction entered
DataErr : BOOL ; //1=Invalid motion FB parameter
StartErr : BOOL ; //1=Start not possible in current axis state
DistanceErr : BOOL ; //1=Motion is too far
MasterErr : BOOL ; //1=Master axis error with hard stop or wrong axis status
bit09 : BOOL ;
bit10 : BOOL ;
bit11 : BOOL ;
bit12 : BOOL ;
bit13 : BOOL ;
bit14 : BOOL ;
bit15 : BOOL ;
StoppedMotion : BOOL ; //1=Axis is in stop and must be acknowledged
EnableDriveErr : BOOL ; //1=Drive enable missing
FollowingDistErr : BOOL ; //1=Max. following distance exceeded
StandstillErr : BOOL ; //1=Outside standstill range
TargetApproachErr : BOOL ; //1=Target approach error
EncoderErr : BOOL ; //1=Encoder error
OutputErr : BOOL ; //1=Output driver error
ConfigErr : BOOL ; //1=Axis data incorrectly configured
DriveErr : BOOL ; //1=Drive error
bit25 : BOOL ;
bit26 : BOOL ;
bit27 : BOOL ;
bit28 : BOOL ;
bit29 : BOOL ;
bit30 : BOOL ;
bit31 : BOOL ;
END_STRUCT ;
SFCErr : INT ; //Rьckgabewert des SFC6 (RD_SINFO) lokal, da keine relevante Info
TOP_SI : STRUCT
EV_CLASS : BYTE ;
EV_NUM : BYTE ;
PRIORITY : BYTE ;
NUM : BYTE ;
TYP2_3 : BYTE ;
TYP1 : BYTE ;
ZI1 : WORD ;
ZI2_3 : DWORD ;
END_STRUCT ;
START_UP_SI : STRUCT
EV_CLASS : BYTE ;
EV_NUM : BYTE ;
PRIORITY : BYTE ;
NUM : BYTE ;
TYP2_3 : BYTE ;
TYP1 : BYTE ;
ZI1 : WORD ;
ZI2_3 : DWORD ;
END_STRUCT ;
Sim : BOOL ; //internal use
END_VAR
BEGIN
IF NOT Init AND NOT EncErr AND EncErr_alt THEN
Init:=true;
Axis.Err.EncoderErr:=true;
Axis.Error:=true;
END_IF;
IF Init THEN
Addr_InChannel:=AXis.InputModuleInAddr;
Addr_Status:=Addr_InChannel+12;
Addr_ActPos_DI:=Addr_InChannel+4;
Addr_Koord:=Addr_InChannel+8;
Addr_LoadVal_DI:=Addr_InChannel;
Addr_Steuer:=Addr_LoadVal_DI+12;
v_Faktor:=Axis.aufl/Axis.Sample_T;
Koord_dw:=DW#16#0;
Status_dw:=DW#16#0;
PASteuer:=DW#16#0;
DiagFehler:=false;
ReadDiagErr:=true;
Axis.EinInitLaeuft:=true;
Axis.EintreiberFahrbereit:=false;
EncErr_alt:=false;
Diag_alt:=false;
Axis.DoneRef:=false;
RefPointInt:=Axis.RefPoint;
Axis.ExecRef:=0;
Axis.Sync:=false;
Axis.EncoderType:=false;
Richtungsanpassung:=Axis.PolarityEncoder;
RefZustand:=0;
SFCErr:=RD_SINFO(TOP_SI := TOP_SI
,START_UP_SI := START_UP_SI
);
SFCErr:=BYTE_TO_INT(TOP_SI.NUM);
Taktsynchron:=SFCErr>=61 AND SFCErr<=64;
END_IF;
Sim:=Axis.Sim;
IF Sim OR EncErr THEN
Koord_dw:=DW#16#80;
PASteuer:=DW#16#0;
Axis.EinInitLaeuft:=false;
ReadDiagErr:=false;
ELSE
IF Taktsynchron THEN
Status_dw:=ID[Addr_Status];
Koord_dw:=ID[Addr_Koord];
ELSE
Status_dw:=PID[Addr_Status];
Koord_dw:=PID[Addr_Koord];
END_IF;
IF (Koord_dw AND DW#16#60) = DW#16#40 THEN
PASteuer:=DW#16#40000000;
Init:=true;
Axis.EinInitLaeuft:=true;
Axis.Sync:=false;
Axis.EintreiberFahrbereit:=false;
ELSE
PASteuer_b[6]:=false;
IF Taktsynchron THEN
Axis.EncoderValue:=DWORD_TO_DINT(ID[Addr_ActPos_DI]);
ELSE
Axis.EncoderValue:=DWORD_TO_DINT(PID[Addr_ActPos_DI]);
END_IF;
IF Koord.DIAG XOR Diag_alt THEN
ReadDiagErr:=true;
Diag_alt:=Koord.DIAG;
END_IF;
IF ReadDiagErr THEN
sRDREC(REQ := true
,ID := INT_TO_DWORD(Axis.InputModuleInAddr)
,INDEX := 1
,MLEN := 16
,RECORD := DiagStatus
);
ReadDiagErr:=sRDREC.BUSY;
JobErr:=DWORD_TO_INT(SHR(IN:=sRDREC.STATUS,N:=8));
IF (JobErr>=32930 AND JobErr<=32932) OR (JobErr>=32960 AND JobErr<=32975)
THEN
ReadDiagErr:=true;
END_IF;
IF NOT ReadDiagErr THEN
IF JobErr>=0 THEN
IF DiagStatus.MDL_DEFECT THEN
IF (DW#16#F000FFFF AND DiagStatus_dw)<>0 THEN
DiagFehler:=true;
ELSE
DiagFehler:=DiagStatus.GRP_ERR1;
END_IF;
ELSE
DiagFehler:=false;
END_IF;
ELSE
DiagFehler:=true;
END_IF;
END_IF;
END_IF;
END_IF;
END_IF;
Geber_DI:=Axis.EncoderValue;
DI_0:=Status.STS_STA;
DI_1:=Status.STS_STP;
DI_2:=Status.STS_SET;
IF EncErr THEN
Axis.Err.EncoderErr:=true;
Axis.Error:=true;
EncErr_alt:=true;
Axis.Sync:=false;
ELSE
IF Init THEN
GeberAlt_DI:=Geber_DI;
Axis.LageIstwert_DI:=Geber_DI;
Axis.PositionOffset:=Geber_DI;
END_IF;
IF (Koord.OT_ERR OR Koord.DATA_ERR) OR
(NOT Koord.PARA AND Koord.FM_NEUSTQ) OR
DiagFehler
THEN
Axis.Err.EncoderErr:=true;
Axis.Error:=true;
Axis.Sync:=false;
ELSE
IF Axis.ExecRef=2 THEN
IF RefZustand=0 THEN
LoadVal_DW:=DINT_TO_DWORD(Geber_DI) XOR DW#16#50000000;
LoadVal_DW:=LoadVal_DW AND DW#16#F0000000;
LoadVal_DW:=LoadVal_DW OR DW#16#2000000;
IF Status.STS_SET THEN
RefZustand:=5;
LoadVal_DW:=DINT_TO_DWORD(Geber_DI);
ELSE
IF Sim THEN
RefZustand:=5;
ELSE
IF Taktsynchron THEN
QD[Addr_LoadVal_DI]:=LoadVal_DW;
ELSE
PQD[Addr_LoadVal_DI]:=LoadVal_DW;
END_IF;
PASteuer_b[25]:=true;
RefZustand:=3;
END_IF;
END_IF;
ELSIF RefZustand=3 THEN
IF Status.LoadInLoad THEN
PASteuer_b[25]:=false;
RefZustand:=4;
END_IF;
ELSIF RefZustand=4 THEN
IF Taktsynchron THEN
IF ID[Addr_LoadVal_DI]=LoadVal_DW THEN
RefZustand:=5;
END_IF;
ELSE
IF PID[Addr_LoadVal_DI]=LoadVal_DW THEN
RefZustand:=5;
END_IF;
END_IF;
ELSIF RefZustand=5 THEN
IF (LoadVal_DW AND DW#16#F0000000)=(DINT_TO_DWORD(Geber_DI) AND DW#16#F0000000) THEN
Axis.DoneRef:=true;
Axis.InternalPosOffset:=(Axis.InternalPosOffset-DINT_TO_REAL(Axis.LageIstwert_DI-Axis.PositionOffset)*Axis.aufl)
-RefPointInt+Axis.RefPoint;
RefPointInt:=Axis.RefPoint;
Axis.PositionOffset:=DWORD_TO_DINT(LoadVal_DW);
Axis.LageIstwert_DI:=DWORD_TO_DINT(LoadVal_DW);
GeberAlt_DI:=DWORD_TO_DINT(LoadVal_DW);
PASteuer:=PASteuer AND DW#16#FFFCFFFF;
RefZustand:=0;
ELSE
PASteuer:=PASteuer OR DW#16#30000;
Axis.EintreiberFahrbereit:=true;
END_IF;
END_IF;
ELSIF Axis.ExecRef=1 THEN
RefPointInt:=Axis.RefPoint;
Axis.InternalPosOffset:=RefPointInt;
Axis.InternalNomPosition:=0.0;
Axis.LastNomPosition:=0.0;
Axis.LageIstwert_DI:=Geber_DI;
Axis.PositionOffset:=Geber_DI;
Axis.DoneRef:=true;
ELSE
Axis.DoneRef:=false;
Axis.EintreiberFahrbereit:=false;
PASteuer:=PASteuer AND DW#16#FFFCFFFF;
END_IF;
LageDiff_DI:=(Geber_DI-GeberAlt_DI)*INT_TO_DINT(Richtungsanpassung);
Axis.LageIstwert_DI:=LageDiff_DI+Axis.LageIstwert_DI;
Axis.ActPosition:=DINT_TO_REAL(Axis.LageIstwert_DI-Axis.PositionOffset)
*Axis.aufl+RefPointInt;
Axis.InternalActPos:=Axis.ActPosition-Axis.InternalPosOffset;
GeberAlt_DI:=Geber_DI;
Axis.ActVelocity:=DINT_TO_REAL(LageDiff_DI)*v_Faktor;
IF Axis.AxisType THEN
TruncKorr:=(Axis.ActPosition-Axis.AxisLimitMin)/Axis.AchsLaenge;
TruncKorr_DI:=TRUNC(TruncKorr);
IF TruncKorr<0.0 AND DINT_TO_REAL(TruncKorr_DI)<>TruncKorr THEN
TruncKorr_DI:=TruncKorr_DI-1;
END_IF;
Axis.ActPosition:=Axis.ActPosition-DINT_TO_REAL(TruncKorr_DI)*Axis.AchsLaenge;
ELSE
Axis.Err.SWLimitMinExceeded:=Axis.SWLimitEnable and Axis.Sync AND
Axis.ActPosition<=Axis.AxisLimitMin;
Axis.Err.SWLimitMaxExceeded:=Axis.SWLimitEnable and Axis.Sync AND
Axis.ActPosition>=Axis.AxisLimitMax;
Err_DW_str:=Axis.Err;
Axis.Error:=Err_DW<>0;
END_IF;
END_IF;
END_IF;
IF Init THEN
Axis.InternalNomPosition:=Axis.InternalActPos;
Axis.LastNomPosition:=Axis.InternalActPos;
Axis.VLastNomPosition:=Axis.InternalActPos;
IF PASteuer=DW#16#0 THEN
IF NOT ReadDiagErr THEN
Init:=false;
Axis.EinInitLaeuft:=false;
END_IF;
END_IF;
END_IF;
IF NOT (Sim OR EncErr) THEN
IF Taktsynchron THEN
QD[Addr_Steuer]:=PASteuer;
ELSE
PQD[Addr_Steuer]:=PASteuer;
END_IF;
END_IF;
END_FUNCTION_BLOCK