Monday, 4 September 2017

Twincat Moving Average


Beckhoff realisiert offene Automatisierungssysteme auf Basis der PC-basierten Steuerungstechnik. Produktspektrum: Industrie-PCs, I / O - und Feldbuskomponenten, Antriebstechnik und Automatisierungssoftware. Die neue Automation Technologydquo von Beckhoff steht für universelle und branchenunabhaumlngige Steuerungs - und Automatisierungslöcher, die weltweit in den verschiedensten Anwendungen, von der CNC-gesteuerten Werkzeugmaschine bis zur intelligenten Gebaumludesteuerung, zum Einsatz kommen. Ihr Browser kann leider keine Frames darstellen. Sie benoumltigen mindestens: MS Internet Explorer 2.1 oder Netscape Navigator 2.01 Wir empfehlen Ihnen fuumlr beide Browser die Versionen 4.x oder houmlher. Ihr Browser kann leider nicht mit Frames arbeiten. Um die BECKHOFF Site zu besuchen, benötigen Sie MS Internet Explorer 2.1 oder höher oder Netscape Navigator 2.01 oder höher. Wir empfehlen für beide Browser die Version 4.x (oder höher).TcPlcLibControllerToolbox TwinCAT PLC Control Toolbox Bibliothek BECHHOFF 2 Inhalt 1. ÜBERBLICK. 4 2. ALLGEMEINE FUNKTION DER FBCTRL. FUNKTIONSBLÖCKE. 7 3. FUNKTIONSBLÖCKE. 11 3.1. AUXILARY. 11 FUNCTIONBLOCK FBCTRLGETSYSTEMTIME (nur auf einem PC-System). 11 FUNCTIONBLOCK FBCTRLGETTASKCYCLETIME (nur auf einem PC-System). 13 FUNCTIONBLOCK FBCTRLLOOPSCHEDULER. 15 3.2. Base. 19 FUNCTIONBLOCK FBCTRLD. 19 FUNKTIONSBLOCK FBCTRLHYSTERESE. 21 FUNCTIONBLOCK FBCTRLI. 23 FUNCTIONBLOCK FBCTRLP. 25 FUNCTIONBLOCK FBCTRLTRANSFERFUNCTION1. 26 FUNCTIONBLOCK FBCTRLTRANSFERFUNCTION2. 3.3. Regler. 35 FUNCTIONBLOCK FBCTRL2POINT. 35 FUNCTIONBLOCK FBCTRL2POINTPWMADAPTIVE. 37 FUNCTIONBLOCK FBCTRL3POINT. 40 FUNCTIONBLOCK FBCTRL3POINTEXT. 43 FUNCTIONBLOCK FBCTRLnPOINT. 46 FUNCTIONBLOCK FBCTRLPARAMETERSCHALTER. 48 FUNCTIONBLOCK FBCTRLPI. 51 FUNCTIONBLOCK FBCTRLPIPID. 54 FUNCTIONBLOCK FBCTRLPID. 58 FUNCTIONBLOCK FBCTRLPIDEXTSPLITRANGE. 63 FUNCTIONBLOCK FBCTRLPIDEXT. 69 FUNCTIONBLOCK FBCTRLPIDSPLITRANGE. 75 3.4. Filter. 81 FUNCTIONBLOCKFBCTRLACTUALVALUEFILTER. 81 FUNKTIONSBLOCK FBCTRLARITHMETICMEAN. 83 FUNCTIONBLOCK FBCTRLDIGITALFILTER. 84 FUNKTIONSBLOCK FBCTRLMOVINGAVERAGE. 88 FUNCTIONBLOCK FBCTRLNOISEGENERATOR (nur auf einem PC-System). 90 FUNCTIONBLOCK FBCTRLPT1. 92 FUNCTIONBLOCK FBCTRLPT2. 94 FUNCTIONBLOCK FBCTRLPT2schwingung. 97 FUNCTIONBLOCK FBCTRLPT3. 99 FUNCTIONBLOCK FBCTRLPTn. 101 FUNCTIONBLOCK FBCTRLPTt. 103 FUNCTIONBLOCK FBCTRLSERVOMOTORSIMULATION (nur auf einem PC-System). 105 FUNCTIONBLOCK FBCTRLTuTg. 108 TwinCAT PLC-Bibliothek: Steuerungs-Toolbox BECHHOFF 3 3.5. Interpolation. 110 FUNCTIONBLOCK FBCTRLLININTERPOLATION. 110 FUNCTIONBLOCK FBCTRLNORMALIZE. 113 3.6. Überwachung / Alarmierung. 116 FUNCTIONBLOCK FBCTRLCHECKIFINBAND. 116 FUNCTIONBLOCK FBCTRLLOGDATA (nur auf einem PC-System). 118 FUNCTIONBLOCK FBCTRLLOGMATFILE (nur auf einem PC-System). 3.7. Ausgang zur Steuerung. 126 FUNCTIONBLOCK FBCTRLDEADBAND. 126 FUNCTIONBLOCK FBCTRLLIMITER. 128 FUNCTIONBLOCK FBCTRLMULTIPLEPWMOUT. 130 FUNCTIONBLOCK FBCTRLPWMOUT. 135 FUNCTIONBLOCK FBCTRLPWMOUTEXT. 137 FUNCTIONBLOCK FBCTRLSCALE. 140 FUNCTIONBLOCK FBCTRLSERVOMOTOROUT. 142 FUNCTIONBLOCK FBCTRLSPLITRANGE. 145 FUNKTIONSBLOCK FBCTRLSTEPPINGMOTOROUT. 148 3.8. Sollwertgenerierung. 151 FUNCTIONBLOCK FBCTRL3PHASESETPOINTGENERATOR (nur auf einem PC-System). 151 FUNKTIONSBLOCK FBCTRLRAMPGENERATOR. 159 FUNKTIONSBLOCK FBCTRLRAMPGENERATOREXT. 161 FUNCTIONBLOCK FBCTRLSETPOINTGENERATOR. 164 FUNCTIONBLOCK FBCTRLSIGNALGENERATOR. 167 ANHANG. 170 Definition der Strukturen und Enums. 170 SCHWIMMER. 170 ECTRLMODE. 170 ECTRLSTATE. 170 ECTRLERRORCODES. 171 ECTRLSIGNALTYPE. 176 BEISPIELPROJEKT. 176 TwinCAT PLC-Bibliothek: Controller-Toolbox BECHHOFF 4 1. Übersicht Diese Bibliothek enthält Funktionsbausteine, die in einem Funktionsdiagramm verschiedene steuerungstechnische Übertragungselemente repräsentieren. Komplexe Steuerungen, die für eine Vielzahl von Anwendungen eingesetzt werden können, sind ebenso enthalten wie Grundbausteine, mit denen einzigartige Steuerungsstrukturen für spezielle Anwendungen implementiert werden können. Funktionsbausteine ​​Name FBCTRL2POINT FBCTRL2POINTPWMADAPTIVE FBCTRL3PHASESETPOINTGENERATOR FBCTRL3POINT FBCTRL3POINTEXT FBCTRLACTUALVALUEFILTER FBCTRLARITHMETICMEAN FBCTRLCHECKIFINBAND FBCTRLD FBCTRLDEADBAND Beschreibung 2-Punkt-Regler Adaptive 2-Punktregler mit PWM-Ausgang 3-Phasen-Sollwertgeber 3-Punkt-Regler Erweiterte 3-Punkt-Regler Istwert Filter arithmetisches Mittel Filterbereichsüberwachung D-Element Tote Band TwinCAT PLC Library: Controller Toolbox BECHHOFF 5 FBCTRLDIGITALFILTER FBCTRLGETSYSTEMTIME FBCTRLGETTASKCYCLETIME FBCTRLHYSTERESIS FBCTRLI FBCTRLLIMITER FBCTRLLININTERPOLATION FBCTRLLOGDATA FBCTRLLOGMATFILE FBCTRLLOOPSCHEDULER FBCTRLMOVINGAVERAGE FBCTRLMULTIPLEPWMOUT FBCTRLNORMALIZE FBCTRLNOISEGENERATOR FBCTRLnPOINT FBCTRLP FBCTRLPARAMETERSWITCH FBCTRLPI FBCTRLPIPID FBCTRLPID FBCTRLPIDEXT FBCTRLPIDEXTSPLITRANGE FBCTRLPIDSPLITRANGE FBCTRLPT1 FBCTRLPT2 FBCTRLPT2oscillation Digitalfilter Ausgabefenster Systemzeit Get Steuerung Task-Zykluszeit Hysterese Element I-Element Wertbegrenzer Lineares Interpolationselement Datenlogger im. csv ASCII-Format Datenlogger im Matlab 5-Format Regelt die Leistung, wenn mehrere Schleifen vorhanden sind. Moving Average Filter PWM-Element mit mehreren Ausgängen Kennlinienlinearisierung Rauschgenerator n-Stellungsregler P-Element Parameterschaltalgorithmus für einen Splitbereichsregler PI-Regler Kaskadierter PI-PID-Regler PID-Regler Erweiterter PID-Regler Erweiterter PID-Regler mit Parameterschaltung PID-Regler mit Parameterumschaltung PT1 Element PT2 Element Oszillierende PT2 Element TwinCAT PLC Library: Controller Toolbox BECHHOFF 6 FBCTRLPT3 FBCTRLPTn FBCTRLPTt FBCTRLPWMOUT FBCTRLPWMOUTEXT FBCTRLRAMPGENERATOR FBCTRLRAMPGENERATOREXT FBCTRLSCALE FBCTRLSERVOMOTOROUT FBCTRLSERVOMOTORSIMULATION FBCTRLSETPOINTGENERATOR PT3 Element PTn Element PTt-Glied PWM Element Erweiterte PWM Element Rampengenerator Erweiterte Einstellung Rampengenerator Bereich Steuerung eines Servomotors Simulation eines Servomotors Sollwertgenerator FBCTRLSIGNALGEC: Bereich TwinCATInfoSystem1 Signalgenerator 033ABCTempHTMLTcPlcLibContollerCTR LSignalGenerator. htm FBCTRLSPLITRANGE FBCTRLSTEPPINGMOTOROUT FBCTRLTRANSFERFUNCTIONI FBCTRLTRANSFERFUNCTIONII FBCTRLTuTg Split in positiver und negativer Teil Ansteuerung von Schrittmotor Übertragungsfunktion gemäß der ersten Standardform Übertragungsfunktion gemäß dem zweiten Standard-Form TuTg Element TwinCAT PLC Library: Regler-Toolbox BECHHOFF 7 2. Allgemeine Funktionsweise der FBCTRL. Funktionsbausteine ​​Die allgemeine Funktionsweise der Bausteine ​​im Feld Controller Tool wird in den folgenden Abschnitten beschrieben. Allgemeine Hinweise Im Allgemeinen kann die TcControllerToolbox auf PC-, BX - oder BC-Systemen verwendet werden. Wenn die Toolbox auf einem BC-System verwendet wird, muss die maximale Anzahl der Bausteine ​​in der SPS-Steuerung unter dem Menüpunkt Projekt, Optionen eingestellt werden. Diskretisierung Die kontinuierlichen Übertragungsfunktionen der in dieser Bibliothek zusammengesetzten Übertragungselemente werden mit Hilfe der Trapezregel (Tustin-Formel) in diskrete Werte transformiert. Die Tustin Formel: TwinCAT PLC Library: Regler-Toolbox BECHHOFF 8 Funktionsbaustein-Eingänge eMode: Mit diesem Eingang kann die Betriebsart der Mehrheit der Bausteine ​​ausgewählt werden. Dies ermöglicht die Auswahl einer der folgenden Betriebsarten: eCTRLMODEPASSIVE eCTRLMODEACTIVE Der Ausgang bzw. die Ausgänge des Bausteins werden auf null gesetzt, die internen Zustände bleiben jedoch erhalten. Der Baustein wird entsprechend seiner Beschreibung ausgeführt und entsprechende Ausgangswerte werden berechnet (Normalbetrieb). In dieser Betriebsart werden alle internen Zustände zurückgesetzt und das Fehlerbit gelöscht. Der Wert des Eingangswertes fManSyncValue wird am Ausgang (Handbetrieb) zur Verfügung gestellt. ECTRLMODERESET eCTRLMODEMANUAL stParams: Mit dieser Struktur werden die notwendigen Parameter an den Funktionsbaustein übergeben. Die Variablen tTaskCycleTime und tCtrlCycleTime sind in allen Parameterstrukturen enthalten. Diese Parameter funktionieren auf folgende Weise: Der Parameter tTaskCycleTime gibt die Zykluszeit an, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Zykluszeit der aufrufenden Task. Wird sie nur in jedem zweiten Zyklus aufgerufen, muss die Zeit entsprechend verdoppelt werden. Der Parameter tCtrlCycleTime gibt die Abtastzeit der Regelkreise an. Diese Zeit muss größer oder gleich dem Parameter tTaskCycleTime sein. Wenn die Abtastzeit gleich tTaskCycleTime ist, wird der Baustein bei jedem Aufruf ausgeführt. Wird ein Faktor 5 größer gewählt, wird der Baustein nur bei jedem 5. Ruf bearbeitet. Dadurch können auch bei einer schnellen Aufgabe langsame Regelkreise realisiert werden. Die Parameter tTaskCycleTime und tCtrlCycleTime sind vom Typ TIME und erlauben daher keine Eingaben von weniger als 1ms. Um die Steuerung in einer schnellen SPS-Task mit einer Zykluszeit von weniger als 1ms zu verwenden, kann als Referenz für die angegebenen Zykluszeiten eine globale Basiszeit angegeben werden. Hinweise zur Verwendung der Basiszeit. Beispiele: Es wird angenommen, dass der Baustein in jedem Task-Zyklus aufgerufen wird. Taskkonfiguration T10ms T10ms T100ms T100ms T100ms Parameter: tTaskCycleTime T10ms T10ms T100ms T100ms T50ms Parameter: tCtrlCycleTime T10ms T50ms T100ms T50ms T50ms Arbeitsweise: Der Regelkreis wird mit einer Abtastzeit von 10 ms verarbeitet. Der Regelkreis wird mit einer Abtastzeit von 50 ms verarbeitet. Der Regelkreis wird mit einer Abtastzeit von 100 ms verarbeitet. ERROR, Ausführung nicht möglich ERROR, obwohl der Baustein ausgeführt wurde, falsche Ausgabewerte wurden berechnet TwinCAT PLC Library: Controller Toolbox BECHHOFF 9 Die Ausgänge der Funktionsbausteine ​​eState: Dieser Ausgang gibt den aktuellen internen Zustand des Bausteins an. ECTRLSTATEIDLE Der Baustein wurde erfolgreich zurückgesetzt und wartet nun auf die Auswahl der Betriebsart. ECTRLSTATEPASSIVE Der Baustein befindet sich im passiven Zustand, in dem keine Berechnungen durchgeführt werden. ECTRLSTATEACTIVE Der Baustein befindet sich im aktiven Zustand, was der normale Betriebszustand ist. ECTRLSTATERESET Eine Rücksetzanforderung wird bearbeitet, der Reset ist jedoch noch nicht abgeschlossen. ECTRLSTATEMANAUL Der Baustein befindet sich im manuellen Zustand und der Ausgang kann manuell am entsprechenden Eingang eingestellt werden. ECTRLSTATE. Wenn es andere interne Zustände gibt, werden sie zusammen mit den entsprechenden Blöcken beschrieben. ECTRLSTATEERROR Ein Fehler ist aufgetreten, wenn der Block nicht in diesem Zustand ausgeführt wird. Siehe eErrorId für weitere Informationen. BError: Ein Fehler im Block wird durch einen TRUE an diesem booleschen Ausgang angezeigt. EErrorId: Die Fehlernummer wird an diesem Ausgang bereitgestellt, wenn der Ausgang bError TRUE ist. Verwenden der globalen Basiszeit (nur auf einem PC-System verfügbar) Um die Funktionsbausteine ​​einer SPS-Task mit einer Zykluszeit von weniger als 1ms verwenden zu können, können die angegebenen Zykluszeiten als Ticks einer Base interpretiert werden Zeit. In dieser speziellen Parametrierung wird die Zeiteinheit von 1ms als 1 Tick interpretiert. Dieser Ansatz entspricht der Einstellung einer SPS-Zykluszeit von weniger als 1ms im TwinCAT System Manager. Die Umschaltung und Deklaration der Basiszeit erfolgt mit der globalen Struktur stCtrlGLOBALCycleTimeInterpretation für alle Funktionsbausteine ​​der Toolbox. VARGLOBAL stCtrlGLOBALCycleTimeInterpretation. STCTRLCYCLETIMEINTERPRETATION ENDVAR TYP STCTRLCYCLETIMEINTERPRETATION. STRUCT bInterpretCycleTimeAsTicks. BOOL (z. B. 2ms - 2ticks) fBaseTime. FLOAT (Basiszeit in Sekunden, zum Beispiel 200s - 200E-6s) ENDSTRUCT EndType Um die angegebenen Zykluszeiten zu interpretieren, wie Zecken, wird die Variable bInterpretCycleTimeAsTicks in der globalen Struktur stCtrlGLOBALCycleTimeInterpretation auf TRUE gesetzt. Innerhalb dieser Struktur muss die Basiszeiteinheit in der Variablen fBaseTime gesetzt werden. TwinCAT PLC Library: Controller Toolbox BECHHOFF 10 durch die Flagge bInterpretCycleTimeAsTicks Einstellung, die Interpretation der Parameter mit den Namen tTaskCycleTime tCtrlCycleTime geändert wird. Die Interpretation und Wirkung aller anderen Parameter vom Typ TIME bleibt unberührt. Beispiel: Die Basiszeiteinheit des TwinCAT-Systems ist 200s. Die SPS-Task und damit die Toolbox-Bausteine ​​werden alle 400s zyklisch aufgerufen. Einstellen der globalen Struktur: stCtrlGLOBALCycleTimeInterpretation. bInterpretCycleTimeAsTicks: TRUE stCtrlGLOBALCycleTimeInterpretation. fBaseTime: 200E-6 Parametrierung eines Funktionsbausteins aus der Toolbox: stParams. tTaskCycleTime: T2ms (2200s400s) stParams. tCtrlCycleTime: T4ms (4200s800s) stParams. Die an den Bausteinen angegebene TaskCycleTime ist 2200E-6s 400s und entspricht somit der eingestellten SPS-Zykluszeit. Die CtrlCycleTime ist auf 800s 4200E-6s eingestellt, so dass die Regelschleife mit einer Zykluszeit von 800s arbeitet, d. h. sie wird während jedes zweiten PLC-Zyklus verarbeitet. TwinCAT PLC Library: Steuerungswerkzeugkasten BECHHOFF 11 3. FUNKTIONSBLÖCKE 3.1. AUXILARY FUNCTIONBLOCK FBCTRLGETSYSTEMTIME (nur auf einem PC-System) Dieser Funktionsbaustein liest die aktuelle Windows-Systemzeit und stellt sie in SystemTimeStruct zur Verfügung. Beschreibung: Dieser Funktionsbaustein stellt die aktuelle Systemzeit in seiner Ausgangsstruktur zur Verfügung. Die Auflösung wird durch den Parameter tCtrlCycleTime angegeben, die maximale Auflösung beträgt 10 ms und es ist notwendig, die Bedingung tCtrlCycleTime 2 tTaskCycleTime zu beobachten. Wenn dies nicht der Fall ist, wird die Auflösung auf 2 tCtrlCycleTime reduziert. VAROUTPUT VAROUTPUT stSystemTime eErrorId bError ENDVAR TYPE TIMESTRUCT STRUCT wYear wMonth wDayOfWeek wday wHour wMinute wSecond. WORT. WORT. WORT. WORT. WORT. WORT. WORT. BOOL. ZEITRAUM. ECTRLERRORCODES wMilliseconds. WORD ENDSTRUCT ENDTYP TwinCAT PLC Bibliothek: Steuerungswerkzeugkasten BECHHOFF 12 stSystemTime. Struktur, in der die Systemzeit ausgegeben wird. WYear. Das Jahr: 1970 2106 wMonth. Der Monat: 1 12 (1. Januar, 2. Februar usw.) wDayOfWeek. Der Tag der Woche: 0 6 (Sonntag 0, Montag 1 usw.) wDay. Der Tag des Monats: 1 31 wHour. Stunde: 0 23 wMinute. Minute: 0 59 wSecond. Zweitens: 0 59 wMillisekunden. Millisekunde: 0 999 eErrorId bError. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald eine Fehlersituation eintritt. VARINOUT VARINOUT stParams: STCTRLGETSYSTEMTIME ENDVAR stParams. Parameterstruktur des Funktionsbausteins. Dieser besteht aus den folgenden Elementen: TYPE STCTRLGETSYSTEMTIME: STRUCT tTaskCycleTime. TIME (Task-Zykluszeit TIME) tCtrlCycleTime. TIME (Reglerzykluszeit TIME) ENDSTRUCT ENDTYPE tCtrlCycleTime. Zykluszeit, mit der der Regelkreis verarbeitet wird. Dieser Wert muss größer oder gleich der TaskCycleTime sein. Der Funktionsbaustein verwendet diese Eingangsvariable, um intern zu berechnen, ob der Zustand und die Ausgangsvariablen im aktuellen Zyklus aktualisiert werden müssen. TTaskCycleTime. Zykluszeit, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Task-Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. Entwicklungsumgebung TwinCAT v2.8 Zielsystemtyp PC (i386) SPS-Bibliotheken verknüpft werden TcControllerToolbox. lib TwinCAT PLC Library: Controller Toolbox BECHHOFF 13 FUNCTION FBCTRLGETTASKCYCLETIME (nur auf einem PC-System) Dieser Funktionsbaustein ermöglicht die Task-Zykluszeit eines Programms Mit einer Auflösung von 1 ms bestimmt werden. Warnung. - Die TaskCycleTime kann nur korrekt ermittelt werden, wenn das Programm keine aktiven Haltepunkte enthält. - Die Task-Zykluszeit wird nur einmal ermittelt. Es werden keine weiteren Messungen durchgeführt, wenn einer der bCycleTimeValid - oder bError-Ausgänge TRUE ist. - Dieser Baustein sollte nicht verwendet werden, wenn Taktzeiten von weniger als 1 ms, die nicht ein Vielfaches von 1 ms sind, verwendet werden. VARINPUT VARINPUT eMode: ECTRLMODE ENDVAR eMode. Eingabe, die die Bausteinbetriebsart angibt. VAROUTPUT VAROUTPUT tTaskCycleTime bCycleTimeValid eState eErrorId bError ENDVAR tTaskCycleTime. Dieser Ausgang zeigt die aktuelle Task-Zykluszeit mit einer Auflösung von 1 ms an. BCycleTimeValid eState eErrorId bError. Die Zeit, die in der Ausgabe tTaskCycleTime enthalten ist, ist gültig, wenn diese Ausgabe TRUE ist. Zustand des Funktionsbausteins. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald ein Fehler auftritt. ZEIT. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL (Auflösung: 1 ms) TwinCAT PLC Library: Controller Toolbox BECHHOFF 14 Beispiel: PROGRAM PRGGETTASKCYCLETIMETEST VAR tTaskCycleTime. TIME bCycleTimeValid. BOOL eState. ECTRLSTATE eErrorId. ECTRLERRORCODES bError. BOOL fbCTRLGETTASKCYCLETIME. FBCTRLGETTASKCYCLETIME (Regelkreis) bInit. BOOL: TRUE fSetpointValue. SCHWIMMER: 45.0 fActualValue. FLOAT fbCTRLPI. FBCTRLPI stCTRLPIParams. STCTRLPIPARAMS fbCTRLPT1. FBCTRLPT1 stCTRLPT1Params. STCTRLPT1PARAMS ENDVAR (Aufruf fb, um den Task-Zykluszeit erhalten) fbCTRLGETTASKCYCLETIME (eMode: eCTRLMODEACTIVE, tTaskCycleTime tTaskCycleTime, bCycleTimeValid bCycleTimeValid, eState eCTRLMODEACTIVE, eErrorId eErrorId, bError bError) (Regelkreis aufrufen, wenn die Zykluszeit gültig ist) IF fbCTRLGETTASKCYCLETIME. bCycleTimeValid THEN IF bInit DANN stCTRLPT1Params. tTaskCycleTime: fbCTRLGETTASKCYCLETIME. tTaskCycleTime stCTRLPT1Params. tCtrlCycleTime: T100ms stCTRLPT1Params. fKp: 1,0 stCTRLPT1Params. tT1: T10s stCTRLPIParams. tTaskCycleTime: fbCTRLGETTASKCYCLETIME. tTaskCycleTime stCTRLPIParams. tCtrlCycleTime: T100ms stCTRLPIParams. fKp: 0,5 stCTRLPIParams. tTn: T5 stCTRLPIParams. fOutMaxLimit: 100,0 stCTRLPIParams. fOutMinLimit: 0.0 bInit: FALSE ENDIF (Call Controller) fbCTRLPI (fActualValue: fbCTRLPT1.fOut, fSetpointValue: fSetpointValue, eMode: eCTRLMODEACTIVE, stParams: stCTRLPIParams) (Call PT1) fbCTRLPT1 (fIn: fbCTRLPI. fOut, eMode: eCTRLMODEACTIVE, stParams: stCTRLPT1Params, TwinCAT PLC Library: Controller Toolbox BECHHOFF 15 fOut) ENDIF fActualValue Entwicklungsumgebung TwinCAT v2.8 Zielsystemtyp PC (i386) SPS-Bibliotheken TcControllerToolbox. lib FUNCTION FBCTRLLOOPSCHEDULER der Baustein ermöglicht die Systembelastung über verknüpft werden verteilt werden eine Reihe von Regelkreisen, die die gleiche tCtrlCycleTime und b), für die die Bedingung tCtrlCycleTime tTaskCycleTime gilt a) parametriert verwenden. Der durch diesen Block berechnete Ausgangsvektor wird verwendet, um die einzelnen Regelkreise zu unterschiedlichen Zeiten zu starten, so daß die Systembelastung verteilt wird. Verhalten des Ausgangsvektors: TwinCAT PLC Library: Regler-Toolbox BECHHOFF In diesem Diagramm werden 6 Regelkreise verwaltet. In diesem Fall tCtrlCycleTime 7 tTaskCycleTime. Der Programmierer muss in der SPS folgendes Array erstellen, wenn dieser Funktionsbaustein verwendet werden soll: arrOutputVector. ARRAY1..nNumberOfControlLoops OF BOOL Der Baustein setzt die Bits in diesem Vektor auf TRUE oder FALSE. Die mit dem Loop-Scheduler verwalteten Regelkreise werden nur dann in den Zustand eCTRLMODEACTIVE geschaltet, wenn das entsprechende Bit im Ausgangsvektor TRUE ist. Siehe Beispielcode unten. VARINPUT VARINPUT nManValue. DWORD eMode: ECTRLMODE ENDVAR nManValue. Dieser Eingang erlaubt die Einstellung der ersten 32 Bits im Ausgangsvektor in eCTRLMODEMANUAL. A 1 setzt das erste Bit, a 2 das zweite Bit, a 3 das erste und das zweite Bit. EMode. Eingabe, die die Bausteinbetriebsart angibt. VAROUTPUT VAROUTPUT eState eErrorId bError ENDVAR eState eErrorId bError. Zustand des Funktionsbausteins. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald ein Fehler auftritt. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameterstruktur für den Loop-Scheduler. Dieser besteht aus den folgenden Elementen: TYPE STCTRLLOOPSCHEDULERPARAMS: STRUCT tCtrlCycleTime. TIME: T0ms (Reglerzykluszeit TIME) tTaskCycleTime. ZEIT: T0ms (Task-Zykluszeit TIME) nNumberOfControlLoops pOutputVectorADR nOutputVectorSIZEOF ENDSTRUCT. UINT. POINTER TO BOOL: 0. UINT: 0. STCTRLLOOPSCHEDULERPARAMS TwinCAT PLC Library: Controller Toolbox BECHHOFF 17 EndType tCtrlCycleTime. Die Zykluszeit, mit der die vom Loop-Scheduler verwalteten Regelkreise verarbeitet werden. Dieser Wert muss größer oder gleich der TaskCycleTime sein. TTaskCycleTime. Die Zykluszeit, mit der der Loop-Scheduler und die den Regelkreisen zugeordneten Funktionsbausteine ​​aufgerufen werden. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. NNumberOfControlLoops pOutputVectorADR nOutputVectorSIZEOF. Die Anzahl der zu steuernden Regelkreise. Adresse des Ausgangsvektors. Größe des Ausgabevektors in Bytes. Beispiel: PROGRAM PRGLoopScheduler VAR arrOutputVector. ARRAY1..5 VON BOOL eMode stParams eErrorId bError. ECTRLMODE. STCTRLLOOPSCHEDULERPARAMS. ECTRLERRORCODES. BOOL fbCTRLLoopScheduler. FBCTRLLOOPSCHEDULER bInit. BOOL: TRUE (Modi der Regelkreise) eModeCtrlLoop1. ECTRLMODE eModeCtrlLoop2. ECTRLMODE eModeCtrlLoop3. ECTRLMODE eModeCtrlLoop4. ECTRLMODE eModeCtrlLoop5. ECTRLMODE ENDVAR IF bInit DANN stParams. tCtrlCycleTime stParams. tTaskCycleTime: T10ms: T2ms stParams. nNumberOfControlLoops: 5 bInit ENDIF: FALSE (Set-Adressen) stParams. nOutputVectorSIZEOF: SIZEOF (arrOutputVector) stParams. pOutputVectorADR: ADR (arrOutputVector) (Call-Scheduler) fbCTRLLoopScheduler ( eMode: eMode, stParams: stParams, eErrorId eErrorId, bError bError) IF arrOutputVector 1 THEN (Regelkreis 1) eModeCtrlLoop1 aktivieren: eCTRLMODEACTIVE ENDIF TwinCAT PLC Library: Controller Toolbox BECHHOFF 18 IF arrOutputVector 2 THEN (aktivieren Regelkreis 2) eModeCtrlLoop2: eCTRLMODEACTIVE ENDIF IF arrOutputVector 3 THEN eModeCtrlLoop3 (Regelkreis 3 aktivieren): eCTRLMODEACTIVE ENDIF IF arrOutputVector 5 THEN (aktivieren Regelkreis 5) eModeCtrlLoop5: eCTRLMODEACTIVE ENDIF Entwicklungsumgebung TwinCAT v2.8 TwinCAT v2 eCTRLMODEACTIVE ENDIF IF arrOutputVector 4 THEN eModeCtrlLoop4 (Regelkreis 4 aktivieren) 0,9, bauen 947 ab v2.9 TwinCAT, bauen 956 ab Zielsystemtyp PC (i386) BC BX SPS-Bibliotheken TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Bibliothek verknüpft werden: Controller Toolbox BECHHOFF 19 3.2. Base FUNCTIONBLOCK FBCTRLD Der Funktionsbaustein stellt in einem Funktionsdiagramm ein DT1-Übertragungselement (ein reales D-Element) zur Verfügung. Übertragungsfunktion (kontinuierlich): VARINPUT VARINPUT fInfManSyncValue bSync eMode ENDVAR fInfManSyncValue Mode. BSync eMode. Eingabe des D-Elements. Eingang, an den das D-Element synchronisiert werden kann oder dessen Wert am Ausgang im Manual liegt. Eine steigende Flanke an diesem Eingang setzt das D-Element auf den Wert fManSyncValue. Eingabe, die die Bausteinbetriebsart angibt. SCHWEBEN. SCHWEBEN. BOOL. ECTRLMODE VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR. SCHWEBEN. ECTRLSTATE. ECTRLERRORCODES. BOOL TwinCAT PLC-Bibliothek: Steuerungs-Toolbox BECHHOFF 20 fOut eState eErrorId bError. Ausgang des D-Elements. Zustand des Funktionsbausteins. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald ein Fehler auftritt. VARINOUT VARINOUT stParams ENDVAR stParams. Parameterstruktur des D-Elements. Diese besteht aus den folgenden Elementen: TYPE STCTRLDPARAMS. STRUCT tCtrlCycleTime. TIME: T0ms (Reglerzykluszeit TIME) tTaskCycleTime. TIME: T0ms (Task-Zykluszeit TIME) tTv. TIME: T0ms (Ableitungszeit Tv) tTd. TIME: T0ms (abgeleitete Dämpfzeit Td) fOutMaxLimit. FLOAT: 1E38 (maximale Ausgangsgrenze) fOutMinLimit. FLOAT: -1E38 (minimale Ausgabegrenze) ENDSTRUCT ENDTYPE tCtrlCycleTime. Zykluszeit, mit der der Regelkreis verarbeitet wird. Dieser Wert muss größer oder gleich der TaskCycleTime sein. Der Funktionsbaustein verwendet diese Eingangsvariable, um intern zu berechnen, ob der Zustand und die Ausgangsvariablen im aktuellen Zyklus aktualisiert werden müssen. TTaskCycleTime. Zykluszeit, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. TTv tTd fOutMaxLimit fOutMinLimit. Differenzierzeitkonstante. Dämpfungszeitkonstante. Obere Grenze, auf die der Ausgang des D-Elements beschränkt ist. Untere Grenze, auf die der Ausgang des D-Elements beschränkt ist. Zielsystem PC (i386) BC BX PLC-Bibliotheken mit TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx. STCTRLDPARAMS Entwicklungsumgebung TwinCAT v2.8 TwinCAT v2.9 aus Build 947 TwinCAT v2.9 aus Build 956 TwinCAT PLC-Bibliothek: Controller-Toolbox BECHHOFF 21 FUNCTIONBLOCK FBCTRLHYSTERESIS Der Funktionsbaustein stellt in einem Funktionsdiagramm ein Hysterese-Übertragungselement zur Verfügung. Übertragungsfunktion: VARINPUT VARINPUT fIn bManSyncValue bSync eMode ENDVAR fIn bManSyncValue bSync eMode. Eingabe des Hystereseelementes. Eingang, durch den das Hystereseelement auf einen der beiden Zweige eingestellt werden kann. Eine steigende Flanke an diesem Eingang setzt das Hystereseelement auf den Wert fManSyncValue. Eingabe, die die Bausteinbetriebsart angibt. SCHWEBEN. BOOL. BOOL. ECTRLMODE VAROUTPUT VAROUTPUT bOut eState eErrorId bError ENDVAR. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL TwinCAT PLC-Bibliothek: Steuerungs-Toolbox BECHHOFF 22 bOut eState eErrorId bError. Ausgang des Hystereseelementes. Zustand des Funktionsbausteins. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald eine Fehlersituation eintritt. VARINOUT VARINOUT stParams ENDVAR stParams. Parameterstruktur des Hystereseelementes. Diese besteht aus folgenden Elementen: TYP STCTRLHYSTERESISPARAMS. STRUCT tCtrlCycleTime. TIME: T0ms (Reglerzykluszeit TIME) tTaskCycleTime. TIME: T0ms (Task-Zykluszeit TIME) fHystereseRange. FLOAT (Bereich der Hystereseschleife) ENDSTRUCT ENDTYPE tCtrlCycleTime. Zykluszeit, mit der der Regelkreis verarbeitet wird. Dieser Wert muss größer oder gleich der TaskCycleTime sein. Der Funktionsbaustein verwendet diese Eingangsvariable, um intern zu berechnen, ob der Zustand und die Ausgangsvariablen im aktuellen Zyklus aktualisiert werden müssen. STCTRLHYSTERESISPARAMS tTaskCycleTime. Zykluszeit, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. FHysteresebereich. Hysteresebereich siehe Diagramm oben. Entwicklungsumgebung TwinCAT v2.8 TwinCAT v2.9 ab Bauen 947 TwinCAT v2.9 ab Bauen 956 Zielsystem PC (i386) BC BX PLC-Bibliotheken mit TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC-Bibliothek: Controller-Toolbox BECHHOFF 23 FUNCTIONBLOCK FBCTRLI Der Funktionsbaustein stellt im Funktionsdiagramm ein I-Übertragungselement zur Verfügung. Übertragungsfunktion: VARINPUT VARINPUT fInfManSyncValue bSync eMode bHalten ENDVAR fInfManSyncValue Modus. BSync eMode bHalten. Eingabe des I-Elements. Eingang, auf den das I-Element synchronisiert werden kann oder dessen Wert am Ausgang in Manual vorliegt. Eine steigende Flanke an diesem Eingang setzt den Integrator auf den Wert fManSyncValue. Eingabe, die die Bausteinbetriebsart angibt. A TRUE an diesem Eingang hält den Integrator fest am aktuellen Wert, unabhängig vom Eingang fIn. SCHWEBEN. SCHWEBEN. BOOL. ECTRLMODE. BOOL VAROUTPUT VAROUTPUT fOut bARWactive eState eErrorId bError. SCHWEBEN. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL TwinCAT PLC-Bibliothek: Steuerungswerkzeugkasten BECHHOFF 24 ENDVAR fOut bARWactive eState eErrorId bError. Ausgang des I-Elements. A TRUE an diesem Ausgang zeigt an, dass der Integrator eingeschränkt wird. Zustand des Funktionsbausteins. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. Wird TRUE, sobald ein Fehler auftritt. VARINOUT VARINOUT stParams ENDVAR stParams. Parameterstruktur des I-Elements. Diese besteht aus den folgenden Elementen: TYPE STCTRLIPARAMS. STRUCT tCtrlCycleTime. TIME: T0ms (Reglerzykluszeit) tTaskCycleTime. TIME: T0ms (Task-Zykluszeit) tTi. TIME: T0ms (Integrationszeit Ti) fOutMaxLimit. FLOAT: 1E38 (maximale Ausgangsgrenze) fOutMinLimit. FLOAT: -1E38 (minimale Ausgangsgrenze) ENDSTRUCT EndType tCtrlCycleTime. Zykluszeit, mit der der Regelkreis verarbeitet wird. Dieser Wert muss größer oder gleich der TaskCycleTime sein. Der Funktionsbaustein verwendet diese Eingangsvariable, um intern zu berechnen, ob der Zustand und die Ausgangsvariablen im aktuellen Zyklus aktualisiert werden müssen. STCTRLIPARAMS tTaskCycleTime. Zykluszeit, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. TTi fOutMaxLimit fOutMinLimit. Integrationszeit des I-Elements. Obergrenze, bei der die Integration angehalten wird (ARW-Maß). Die Erreichung dieses Grenzwertes wird am Ausgang bARWActive mit dem Wert TRUE angezeigt. Untergrenze, bei der die Integration angehalten wird (ARW-Messung). Die Erreichung dieses Grenzwertes wird am Ausgang bARWActive mit dem Wert TRUE angezeigt. Zielsystem PC (i386) BC BX SPS-Bibliotheken TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 enthalten ab Build 947 TwinCAT v2.9 ab Build 956 TwinCAT PLC Library: Controller Toolbox BECHHOFF 25 FUNCTION FBCTRLP Der Funktionsbaustein stellt im Funktionsdiagramm ein P-Übertragungselement zur Verfügung. Übertragungsfunktion: VARINPUT VARINPUT fIn eMode ENDVAR fIn eMode. Eingabe des P-Elements. Eingabe, die die Bausteinbetriebsart angibt. FLOAT: ECTRLMODE VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR fOut eState. Ausgabe des P-Elements. Zustand des Funktionsbausteins. FLOAT: ECTRLSTATE: ECTRLERRORCODES: BOOL eErrorId. Liefert die Fehlernummer, wenn die Ausgabe bError gesetzt ist. BError. Wird TRUE, sobald ein Fehler auftritt. VARINOUT VARINOUT stParams ENDVAR stParams. Parameterstruktur des P-Elements. Diese besteht aus den folgenden Elementen. STCTRLPPARAMS TwinCAT PLC Library: Controller Toolbox BECHHOFF 26 TYPE STCTRLPPARAMS. STRUCT tCtrlCycleTime. TIME: T0ms (Reglerzykluszeit) tTaskCycleTime. TIME: T0ms (Task-Zykluszeit) fKp. FLOAT: 0.0 ENDSTRUCT ENDTYPE tCtrlCycleTime (Proportionalverstärkung Kp). Zykluszeit, mit der der Regelkreis verarbeitet wird. Dieser Wert muss größer oder gleich der TaskCycleTime sein. Der Funktionsbaustein verwendet diese Eingangsvariable, um intern zu berechnen, ob der Zustand und die Ausgangsvariablen im aktuellen Zyklus aktualisiert werden müssen. TTaskCycleTime. Zykluszeit, mit der der Funktionsbaustein aufgerufen wird. Wenn der Baustein in jedem Zyklus aufgerufen wird, entspricht dies der Task-Zykluszeit der aufrufenden Task. FKp. Proportionale Verstärkung des P-Elements. Zielsystem PC (i386) BC BX SPS-Bibliotheken TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 enthalten ab Build 947 TwinCAT v2.9 ab Build 956 FUNCTION FBCTRLTRANSFERFUNCTION1 Dieser Block berechnet eine diskrete Übertragungsfunktion Mit der unten dargestellten ersten Standardform. Die Übertragungsfunktion kann hier beliebig sein, n. Die Koeffizienten für die folgenden Übertragungsfunktionen gespeichert sind in den Parameterfelder: TwinCAT PLC Library: Controller Toolbox BECHHOFF 27 Beschreibung des Übertragungsverhaltens: über die Übertragungsfunktion mit dem ersten Standard-Form, nach einigen Transformationen berechnet wird, in jedem Abtastschritt. Der Programmierer muss die folgenden Arrays in der SPS erstellen, wenn dieser Funktionsbaustein verwendet werden soll: arfNumeratorArray. ARRAY0..nOrderOfTheTransferfunktion von FLOAT arDenominatorArray. ARRAY0..nOrderOfTheTransferfunktion von FLOAT arstTransferfunktion1Daten. ARRAY0..nOrderOfTheTransferfunction OF STCTRLTRANSFERFUNCTION1DATA Die Koeffizienten b0 bis bn werden im Array arfNumeratorArray gespeichert. Dies muss wie folgt organisiert werden: arfNumeratorArray 0 arfNumeratorArray 1. arfNumeratorArray n-1 arfNumeratorArray n : b0 : b1 : bn-1 : bn The coefficients b0 to bn are stored in the array arDenominatorArray. This must be organised as follows: arDenominatorArray 0 : a0 arDenominatorArray 1 : a1. arDenominatorArray n-1 : an-1 arDenomiantorArray n : an The internal data required by the block is stored in the arstTransferfunction1Data array. This data must never be modified from within the PLC program. This procedure is also illustrated in the example program listed below. VARINPUT VARINPUT fIn fManValue eMode ENDVAR fIn fManValue. Input to the transfer function. Input whose value is present at the output in manual mode. FLOAT. FLOAT. ECTRLMODE TwinCAT PLC Library: Controller Toolbox BECHHOFF 28 eMode. Input that specifies the blocks operating mode. VAROUTPUT VAROUTPUT fOut eState bError eErrorId ENDVAR fOut eState eErrorId bError. Output from the transfer function. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. BOOL. ECTRLERRORCODES VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the function block. This consists of the following elements: TYPE STCTRLTRANSFERFUNCTION1PARAMS: STRUCT tTaskCycleTime. TIME ( task cycle time in seconds ) tTaskCtrlTime. TIME: T0ms ( controller cycle time ) nOrderOfTheTransferfunction. USINT pNumeratorArrayADR nNumeratorArraySIZEOF. POINTER TO FLOAT : 0. UINT. STCTRLTRANSFERFUNCTION1PARAMS pDenominatorArrayADR. POINTER TO FLOAT : 0 nDenomiantorArraySIZEOF. UINT pTransferfunction1DataADR. POINTER TO STCTRLTRANSFERFUNCTION1DATA nTransferfunction1DataSIZEOF. UINT ENDTYPE tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. tPWMPeriod nOrderOfTheTransferfunction pNumeratorArrayADR nNumeratorArraySIZEOF pDenominatorArrayADR nDenominatorArraySIZEOF. Period of the PWM signal. Order of the transfer function 0. . Address of the array containing the numerator coefficients. Size of the array containing the numerator coefficients, in bytes. Address of the array containing the denominator coefficients. Size of the array containing the denominator coefficients, in bytes. TwinCAT PLC Library: Controller Toolbox BECHHOFF 29 pTransferfunction1DataADR. Address of the data array. nTransferfunction1DataSIZEOF. Size of the data array in bytes. TYPE STCTRLTRANSFERFUNCTION1DATA: STRUCT Internal structure. This must not be written to. ENDSTRUCT ENDTYPE Example: PROGRAM PRGTRANSFERFUNCTION1TEST VAR CONSTANT nOrderOfTheTransferfunction. USINT : 2 ENDVAR VAR arfNumeratorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arDenominatorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arstTransferfunction1Data. ARRAY0..nOrderOfTheTransferfunction OF STCTRLTRANSFERFUNCTION1DATA eMode stParams eErrorId bError. ECTRLMODE. STCTRLTRANSFERFUNCTION1PARAMS. ECTRLERRORCODES. BOOL fbTansferfunction. FBCTRLTRANSFERFUNCTION1 bInit fIn fOut. BOOL : TRUE. FLOAT : 0. FLOAT b0, b1, b2. FLOAT a0,a1,a2. FLOAT ENDVAR IF bInit THEN ( set values in the local arrays ) arfNumeratorArray0 : 1.24906304658218E-007 arfNumeratorArray1 : 2.49812609316437E-007 arfNumeratorArray2 : 1.24906304658218E-007 arDenominatorArray0 : 0.998501124344101 arDenominatorArray1 : -1.99850062471888 arDenominatorArray2 : 1.0 ( set values in the parameter struct ) stParams. tTaskCycleTime : T2ms stParams. tCtrlCycleTime : T2ms stParams. nOrderOfTheTransferfunction : nOrderOfTheTransferfunction ( set the mode ) eMode : eCTRLMODEACTIVE bInit : FALSE ENDIF TwinCAT PLC Library: Controller Toolbox BECHHOFF 30 ( set the addresses ) stParams. pNumeratorArrayADR stParams. nNumeratorArraySIZEOF stParams. pDenominatorArrayADR stParams. nDenominatorArraySIZEOF : ADR( arfNumeratorArray) : SIZEOF( arfNumeratorArray) : ADR( arDenominatorArray ) : SIZEOF( arDenominatorArray ) stParams. pTransferfunction1DataADR : ADR( arstTransferfunction2Data ) stParams. nTransferfunction1DataSIZEOF : SIZEOF( arstTransferfunction2Data ) ( call fnction block ) fbTansferfunction ( fIn : fIn, eMode : eMode, stParams : stParams, fOut fOut, eErrorId eErrorId, bError bError ) Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLTRANSFERFUNCTION2 This block calculates a discrete transfer function with the second standard form illustrated below. The transfer function here can be of any order, n. The coefficients for the following transfer functions are stored in the parameter arrays: Description of the transfer behaviour: The transfer function above is calculated with the second standard form, after some transformations, in every sampling step. TwinCAT PLC Library: Controller Toolbox BECHHOFF 31 The programmer must create the following arrays in the PLC if this function block is to be used: arfNumeratorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arDenominatorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arstTransferfunction2Data. ARRAY0..nOrderOfTheTransferfunction OF STCTRLTRANSFERFUNCTION2DATA The coefficients b0 to bn are stored in the array arfNumeratorArray. This must be organised as follows: arfNumeratorArray 0 arfNumeratorArray 1 . arfNumeratorArray n-1 arfNumeratorArray n : b0 : b1 : bn-1 : bn The coefficients b0 to bn are stored in the array arDenominatorArray. This must be organised as follows: arDenominatorArray 0 : a0 arDenominatorArray 1 : a1. arDenominatorArray n-1 : an-1 arDenomiantorArray n : an The internal data required by the block is stored in the arstTransferfunction2Data array. This data must never be modified from within the PLC program. This procedure is also illustrated in the example program listed below. VARINPUT VARINPUT fIn fManValue eMode ENDVAR fIn fManValue eMode. Input to the transfer function. Input whose value is present at the output in manual mode. Input that specifies the blocks operating mode. FLOAT. FLOAT. ECTRLMODE TwinCAT PLC Library: Controller Toolbox BECHHOFF 32 VAROUTPUT VAROUTPUT fOut eState bError eErrorId ENDVAR fOut eState eErrorId bError. Output from the transfer function. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. BOOL. ECTRLERRORCODES VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the function block. This consists of the following elements: TYPE STCTRLTRANSFERFUNCTION2PARAMS: STRUCT tTaskCycleTime. TIME ( task cycle time in seconds ) tCrtlCycleTime. TIME: T0ms ( controller cycle ) nOrderOfTheTransferfunction. USINT pNumeratorArrayADR nNumeratorArraySIZEOF pDenominatorArrayADR nDenomiantorArraySIZEOF. POINTER TO FLOAT : 0. UINT. POINTER TO FLOAT : 0. UINT. STCTRLTRANSFERFUNCTION2PARAMS pTransferfunction2DataADR. POINTER TO STCTRLTRANSFERFUNCTION2DATA nTransferfunction2DataSIZEOF. UINT ENDTYPE tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. tPWMPeriod nOrderOfTheTransferfunction pNumeratorArrayADR nNumeratorArraySIZEOF pDenominatorArrayADR nDenominatorArraySIZEOF. Period of the PWM signal. Order of the transfer function 0. . Address of the array containing the numerator coefficients. Size of the array containing the numerator coefficients, in bytes. Address of the array containing the denominator coefficients. Size of the array containing the denominator coefficients, in bytes. TwinCAT PLC Library: Controller Toolbox BECHHOFF 33 pTransferfunction2DataADR. Address of the data array. nTransferfunction2DataSIZEOF. Size of the data array in bytes. TYPE STCTRLTRANSFERFUNCTION2DATA: STRUCT Internal structure. This must not be written to. ENDSTRUCT ENDTYPE Example: PROGRAM PRGTRANSFERFUNCTION2TEST VAR CONSTANT nOrderOfTheTransferfunction. USINT : 2 ENDVAR VAR arfNumeratorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arDenominatorArray. ARRAY0..nOrderOfTheTransferfunction OF FLOAT arstTransferfunction2Data. ARRAY0..nOrderOfTheTransferfunction OF STCTRLTRANSFERFUNCTION2DATA eMode stParams eErrorId bError. ECTRLMODE. STCTRLTRANSFERFUNCTION2PARAMS. ECTRLERRORCODES. BOOL fbTansferfunction. FBCTRLTRANSFERFUNCTION2 bInit fIn fOut. BOOL : TRUE. FLOAT : 0. FLOAT b0, b1, b2. FLOAT a0,a1,a2. FLOAT ENDVAR IF bInit THEN ( set values in the local arrays ) arfNumeratorArray0 : 1.24906304658218E-007 arfNumeratorArray1 : 2.49812609316437E-007 arfNumeratorArray2 : 1.24906304658218E-007 arDenominatorArray0 : 0.998501124344101 arDenominatorArray1 : -1.99850062471888 arDenominatorArray2 : 1.0 ( set values in the parameter struct ) stParams. tTaskCycleTime : T2ms stParams. tCtrlCycleTime : T2ms stParams. nOrderOfTheTransferfunction : nOrderOfTheTransferfunction ( set the mode ) eMode : eCTRLMODEACTIVE bInit : FALSE ENDIF TwinCAT PLC Library: Controller Toolbox BECHHOFF 34 ( set the addresses ) stParams. pNumeratorArrayADR : ADR( arfNumeratorArray) stParams. nNumeratorArraySIZEOF : SIZEOF( arfNumeratorArray) stParams. pDenominatorArrayADR : ADR( arDenominatorArray ) stParams. nDenominatorArraySIZEOF : SIZEOF( arDenominatorArray ) stParams. pTransferfunction2DataADR : ADR( arstTransferfunction2Data ) stParams. nTransferfunction2DataSIZEOF : SIZEOF( arstTransferfunction2Data ) ( call the function block ) fbTansferfunction ( fIn : fIn, eMode : eMode, stParams : stParams, fOut fOut, eErrorId eErrorId, bError bError ) Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Library: Controller Toolbox BECHHOFF 35 3.3. Controller FUNCTIONBLOCK FBCTRL2POINT The function block provides a 2-point transfer element in the functional diagram. Behaviour of the output: VARINPUT VARINPUT fActualValue. FLOAT fSetpointValue. FLOAT bManSyncValue bSync eMode ENDVAR fActualValue fSetpointValue bManSyncValue bSync eMode. Actual value of the controlled variable. Set value of the controlled variable. Input through which the 2-point element can be set to one of the two branches. A rising edge at this input sets the 2-point element to the value bManSyncValue. Input that specifies the blocks operating mode. BOOL. BOOL. ECTRLMODE TwinCAT PLC Library: Controller Toolbox BECHHOFF 36 VAROUTPUT VAROUTPUT bOut eState eErrorId bError ENDVAR bOut eState eErrorId bError. Output of the 2-point element. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the 2-point element. This consists of the following elements: TYPE STCTRL2POINTPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fHysteresisRange. FLOAT ENDSTRUCT ENDTYPE ( range of the hysteresis loop ). STCTRL2POINTPARAMS tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fHysteresisRange: Hysteresis range, see diagram above. Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Library: Controller Toolbox BECHHOFF 37 FUNCTIONBLOCK FBCTRL2POINTPWMADAPTIVE The function block provides an adaptive two-position controller. It is particularly suitable for single-area controllers in which high inlet temperatures are present and which make use of a thermal actuator. Behaviour of the output: TwinCAT PLC Library: Controller Toolbox BECHHOFF 38 Description of the function: Internally, the controller uses a PWM block that is used to drive the thermal actuator. The mark-to-space ratio of the PWM block is adaptively adjusted to the behaviour of the controlled system. The PWM output is switched on as soon as the system deviation, fE, which is the set value minus the actual value, is greater than zero, and is switched off when the system deviation is less than zero. The mark-to-space ratio is not changed as long as the system deviation remains within the range - fOkRange. fOkRange. If fE fOkRange, the mark-to-space ratio is increased by fStepSize. After such an increase, time tWaitTime must elapse before the mark-to-space ratio can be changed again. If fE falls below - fOkRange, the mark-to-space ratio is reduced by fStepSize. The mark-to-space ratio is only modified over the range fMinLimit. fMaxLimit. The period of the PWM signal is specified by the parameter tPWMPeriod. VARINPUT VARINPUT fActualValue. FLOAT fSetpointValue. FLOAT fManSyncValue bSync eMode ENDVAR fActualValue fSetpointValue. Actual value of the controlled variable. Set value of the controlled variable. FLOAT. BOOL. ECTRLMODE fManSyncValue. Input to which the controllers mark-to-space ratio can be set, or with which the output can be set in Manual Mode. The output is set in Manual Mode if fManSyncValue 0.0. bSync. A rising edge at this input will set the mark-to-space ratio of the internal PWM block to the value fManSyncValue. eMode. Input that specifies the blocks operating mode. VAROUTPUT VAROUTPUT bOut fPWMDutyCycle eState eErrorId bError ENDVAR bOut. Output of the controller. fPWMDutyCycle. Current mark-to-space ratio of the internal PWM block. eState eErrorId. State of the function block. Supplies the error number when the bError output is set. BOOL. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL ( debug only ) bError: Becomes TRUE, as soon as an error occurs. TwinCAT PLC Library: Controller Toolbox BECHHOFF 39 VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the 2-point element. This consists of the following elements: TYPE STCTRL2POINTPWMADAPTIVEPARAMS: STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) tPWMPeriod. TIME fOkRange. FLOAT fForceRange. FLOAT fStepSize. FLOAT fMinLimit. FLOAT ( 0. 100 ) fMaxLimit. FLOAT ( 0. 100 ) tWaitTime. TIME ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. tPMWPeriod :Period of the PWM signal. STCTRL2POINTPWMADAPTIVEPARAMS fOkRange :The range of fE over which the mark-to-space ratio will not be modified. fForceRange fStepSize fMaxLimit fMinLimit tWaiTimet :If fE exceeds this range, the output is permanently set to TRUE. Value by which the mark-to-space ratio is varied each time it is adapted. 0. 100. Maximum mark-to-space ratio in percent 0. 100. Minimum mark-to-space ratio in percent 0. 100. Waiting time between individual modifications of the mark-to-space ratio. Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Library: Controller Toolbox BECHHOFF 40 FUNCTIONBLOCK FBCTRL3POINT The function block provides a 3-point transfer element in the functional diagram. Behaviour of the output: VARINPUT VARINPUT fActualValue. FLOAT fSetpointValue. FLOAT nManSyncValue bSync eMode ENDVAR fActualValue fSetpointValue nManSyncValue. Actual value of the controlled variable. Set value of the controlled variable. Input through which the 3-point element can be set to one of the three branches. nManSyncValue 1 bPosOut TRUE, bNegOut FALSE nManSyncValue 0, the output is set to zero when it is within the range of - fDeadBandOutput. fDeadBandOutput . Input dead band If the parameter fDeadBandInput is set 0 then the output is held constant for as long as the system deviation remains within the range of - fDeadBandInput. fDeadBandInput . Step response: TwinCAT PLC Library: Controller Toolbox BECHHOFF 66 ARW: VARINPUT VARINPUT fActualValue fSetpointValue eParameterRecord fManSyncValue bSync eMode bHold ENDVAR fActualValue fSetpointValue eParameterRecord fManSyncValue bSync eMode. Actual value of the controlled variable. Set value of the controlled variable. Index of the active parameter record. Input with which the PI element can be set. A rising edge at this input sets the PI element to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. ECTRLPARAMETERRECORD. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the system deviation. VAROUTPUT VAROUTPUT fOutPos fOutNeg fOut. FLOAT. FLOAT. FLOAT TwinCAT PLC Library: Controller Toolbox BECHHOFF 67 bARWActive. BOOL : FALSE ( ARW active. TRUE/FALSE - freeze I-part ) bParameterChangeActive. BOOL bError eErrorId. BOOL. ECTRLERRORCODES ( error flag ) ( error code ) ENDVAR fOutPos fOutNeg fOut bARWactive. Output of the PID element when the control value is positive. A zero is output otherwise. Output of the PID element when the control value is negative. A zero is output otherwise. Output of the PID element. A TRUE at this output indicates that the PID element is being restricted. bParameterChangeActive. A TRUE at this output indicates that the change from one parameter record to the other is in progress. fCtrlDerivation eErrorId bError. A TRUE at this output indicates that the PID element is being restricted. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PID element. This consists of the following elements: TYPE STCTRLPIDSPLITRANGEPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKpheating tTnheating tTvheating tTdheating fKpcooling tTncooling tTvcooling tTdcooling. FLOAT : 0 ( proportional gain ). TIME : T0ms ( Tn ). TIME : T0ms ( Tv ). TIME : T0ms ( Td ). FLOAT : 0 ( proportional gain ). TIME : T0ms ( Tn ). TIME : T0ms ( Tv ). TIME : T0ms ( Td ). STCTRLPIDEXTPARAMS nParameterChangeCycleTicks. INT fDeadBandInput fDeadBandOutput fInnerWindow. REAL : 0.0 ( ctrl deviation dead band/neutral zone for const output ). REAL : 0.0 ( ctrl output dead band/neutral zone for zero output ). REAL : 0.0 ( inner window for reduced I-part ( dE-window) ) TwinCAT PLC Library: Controller Toolbox BECHHOFF 68 fOuterWindow fOutMaxLimit fOutMinLimit. REAL : 0.0 ( outer window for disabling I-part (dE-window) ). FLOAT : 1E38 ( maximum output limit ). FLOAT : -1E38 ( minimum output limit ). BOOL. BOOL. BOOL bPInTheFeedbackPath bDInTheFeedbackPath bARWOnIPartOnly ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. eCTRLPARAMETERRECORDHEATING region: fKpheating tTnheating tTvheating tTdheating. Controller amplification / controller coefficient. Integral action time. The I component is deactivated if this is parameterised as T0s. Derivative action time. The D component is deactivated if this is parameterised as T0s. Damping time eCTRLPARAMETERRECORDCOOLING region: fKpcooling tTncooling tTvcooling tTdcooling. Controller amplification / controller coefficient. Integral action time. The I component is deactivated if this is parameterised as T0s. Derivative action time. The D component is deactivated if this is parameterised as T0s. Damping time nParameterChangeCycleTicks: The number of task cycles over which the change from one parameter record to the other takes place. fDeadBandInput. See description above fDeadBandOutput: See description above fInnerWindow fOuterWindow. See description above. See description above fOutMaxLimit. Upper limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. fOutMinLimit. Lower limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. bPInTheFeedbackPath. Input value of the P element can be selected with this input (see functional diagram). bDInTheFeedbackPath. Input value of the D element can be selected with this input (see functional diagram). TwinCAT PLC Library: Controller Toolbox BECHHOFF 69 bARWOnIPartOnly: If this parameter is FALSE (the standard setting), the integration of the I component is halted if the complete controller output reaches the upper or lower limit. If it is TRUE, the integration is halted if the I component (the output of the integrator) reaches some limit. (Cf. functional diagram.) Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLPIDEXT The function block provides an extended PID element in the functional diagram. Transfer function: Functional diagram: TwinCAT PLC Library: Controller Toolbox BECHHOFF 70 The standard functional diagram of a PID controller in additive form has been expanded by the two active boolean inputs bPInTheFeedbackPath and bDInTheFeedbackPath (which act as switches), so that a modified functional diagram can be activated. Control background: due to the differential component of the control algorithm, large control values are generated at set value discontinuities, which cause a strain on the control elements and may cause the control system to oscillate. A control algorithm with a differential component that is only applied to the controlled variable ( bDInTheFeedbackPath : TRUE ) can avoid this problem. The bPInTheFeedbackPath and bDInTheFeedbackPath inputs permit the closed control loop to implement the following transfer functions: with. The standard setting for the two bPInTheFeedbackPath and bDInTheFeedbackPath inputs is FALSE. The PID controller then acts as a standard PID controller in additive form. TwinCAT PLC Library: Controller Toolbox BECHHOFF 71 Additional functions: Switching off the I component in the Outer Window Integration of the system deviation is halted if the system deviation is greater than the fOuterWindow parameter. In this way it is possible to prevent an extremely large I component from developing if the system deviation is large, since this could lead to a marked overshoot. If it is not wanted, the function can be disabled by setting fOuterWindow : 0. Linear reduction of the I component in the Inner Window With this function it is possible to drive the I component linearly down to zero in the range specified by the fInnerWindow parameter. If it is not wanted, the function can be disabled by setting fInnerWindow : 0. Output dead band If the parameter fDeadBandOutput is set 0, the output is set to zero when it is within the range of - fDeadBandOutput. fDeadBandOutput . Input dead band If the parameter fDeadBandInput is set 0 then the output is held constant for as long as the system deviation remains within the range of - fDeadBandInput. fDeadBandInput . Step response: TwinCAT PLC Library: Controller Toolbox BECHHOFF 72 ARW: VARINPUT VARINPUT fActualValue fSetpointValue fManSyncValue bSync eMode bHold ENDVAR fActualValue fSetpointValue fManSyncValue bSync eMode. Actual value of the controlled variable. Set value of the controlled variable. Input with which the PID element can be set. A rising edge at this input sets the PID element to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the system deviation. VAROUTPUT VAROUTPUT fOut. FLOAT. BOOL : FALSE ( minimum limitation active. TRUE/FALSE - ARW ) bMinLimitReached. BOOL : FALSE ( maximum limitation active. TRUE/FALSE - ARW ) bMaxLimitReached TwinCAT PLC Library: Controller Toolbox BECHHOFF 73 bARWActive. BOOL : FALSE ( ARW active. TRUE/FALSE - freeze I-part ) fCtrlDerivation. FLOAT ( controller command derivation dy/dt ) eState bError eErrorId. ECTRLSTATE. BOOL. ECTRLERRORCODES ( error flag ) ( error code ) ENDVAR fOut. Output of the PID-element. bMaxLimitReached. The output is TRUE when the block is at its upper limit. bMinLimitReached. The output is TRUE when the block is at its lower limit. bARWactive fCtrlDerivation eState eErrorId bError. A TRUE at this output indicates that the PID element is being restricted. A TRUE at this output indicates that the PID element is being restricted. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PID element. This consists of the following elements: TYPE STCTRLPIDEXTPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKp tTn tTv tTd. FLOAT : 0 ( proportional gain ). TIME : T0ms ( Tn ). TIME : T0ms ( Tv ). TIME : T0ms ( Td ) ( ctrl deviation dead band/neutral zone for const output ) ( ctrl output dead band/neutral zone for zero output ) ( inner window for reduced I-part (dE-window) ) ( outer window for disabling I-part (dE-window) ) ( maximum output limit ) ( minimum output limit ). STCTRLPIDEXTPARAMS fDeadBandInput. REAL : 0.0 fDeadBandOutput. REAL : 0.0 fInnerWindow fOuterWindow fOutMaxLimit fOutMinLimit. REAL. REAL : 0.0 : 0.0. FLOAT : 1E38. FLOAT : -1E38 bPInTheFeedbackPath. BOOL bDInTheFeedbackPath. BOOL TwinCAT PLC Library: Controller Toolbox BECHHOFF 74 bARWOnIPartOnly ENDSTRUCT ENDTYPE. BOOL tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fKp tTn tTv tTd. Controller amplification / controller coefficient. Integral action time. The I component is deactivated if this is parameterised as T0s. Derivative action time. The D component is deactivated if this is parameterised as T0s. Damping time fDeadBandInput. See description above fDeadBandOutput: See description above fInnerWindow fOuterWindow. See description above. See description above fOutMaxLimit. Upper limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. fOutMinLimit. Lower limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. bPInTheFeedbackPath. Input value of the P element can be selected with this input (see functional diagram). bDInTheFeedbackPath. Input value of the D element can be selected with this input (see functional diagram). bARWOnIPartOnly: If this parameter is FALSE (the standard setting), the integration of the I component is halted if the complete controller output reaches the upper or lower limit. If it is TRUE, the integration is halted if the I component (the output of the integrator) reaches some limit. (Cf. functional diagram.) Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Library: Controller Toolbox BECHHOFF 75 FUNCTIONBLOCK FBCTRLPIDSPLITRANGE The function block provides an extended PID transfer element in the functional diagram. With this controller it is possible to switch between two different parameter records while the regulation is active. Description: This function block is an extension of FBCTRLPID, which means that the controller can be used to control systems with two controlled devices for which the transfer behaviours are different. A system with one actuator for heating and another actuator for cooling would be a typical application. To optimise the regulation of such an arrangement, it is possible to switch between two PID parameter records. Switching between the parameter records is implemented in such a way that the control value remains continuous even as the parameter records are changed. The switching algorithm calculates a linear, time-dependent transition between the two parameter records. The nParameterChangeCycleTicks parameter can be used to specify the number of task cycles over which the continuous change between the two parameter records takes place. Transfer function: Functional diagram: The standard functional diagram of a PID controller in additive form has been expanded by the two active boolean inputs bPInTheFeedbackPath and bDInTheFeedbackPath (which act as switches), so that a modified functional diagram can be activated. TwinCAT PLC Library: Controller Toolbox BECHHOFF 76 Control background: due to the differential component of the control algorithm, large control values are generated at set value discontinuities, which cause a strain on the control elements and may cause the control system to oscillate. A control algorithm with a differential component that is only applied to the controlled variable ( bDInTheFeedbackPath : TRUE ) can avoid this problem. The bPInTheFeedbackPath and bDInTheFeedbackPath inputs permit the closed control loop to implement the following transfer functions: with. The standard setting for the two bPInTheFeedbackPath and bDInTheFeedbackPath inputs is FALSE. The PID controller then acts as a standard PID controller in additive form. TwinCAT PLC Library: Controller Toolbox BECHHOFF 77 Step response: ARW: TwinCAT PLC Library: Controller Toolbox BECHHOFF 78 VARINPUT VARINPUT fActualValue fSetpointValue eParameterRecord fManSyncValue bSync eMode bHold ENDVAR fActualValue fSetpointValue eParameterRecord fManSyncValue bSync eMode. Actual value of the controlled variable. Set value of the controlled variable. Index of the active parameter record. Input with which the PI element can be set. A rising edge at this input sets the PI element to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. ECTRLPARAMETERRECORD. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the system deviation. VAROUTPUT VAROUTPUT fOutPos fOutNeg fOut. FLOAT. FLOAT. FLOAT bARWActive freeze I-part ) bParameterChangeActive. BOOL : FALSE. BOOL ( ARW active. TRUE/FALSE - eState bError eErrorId. ECTRLSTATE. BOOL. ECTRLERRORCODES ( error flag ) ( error code ) ENDVAR fOutPos fOutNeg. Output of the PID element when the control value is positive. A zero is output otherwise. Output of the PID element when the control value is negative. A zero is output otherwise. TwinCAT PLC Library: Controller Toolbox BECHHOFF 79 fOut bARWactive. Output of the PID element. A TRUE at this output indicates that the PID element is being restricted. bParameterChangeActive. A TRUE at this output indicates that the change from one parameter record to the other is in progress. fCtrlDerivation eState eErrorId bError. A TRUE at this output indicates that the PID element is being restricted. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PID element. This consists of the following elements: TYPE STCTRLPIDSPLITRANGEPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKpheating tTnheating tTvheating tTdheating fKpcooling tTncooling tTvcooling tTdcooling. FLOAT : 0 ( proportional gain ). TIME : T0ms ( Tn ). TIME : T0ms ( Tv ). TIME : T0ms ( Td ). FLOAT : 0 ( proportional gain ). TIME : T0ms ( Tn ). TIME : T0ms ( Tv ). TIME : T0ms ( Td ). STCTRLPIDEXTPARAMS nParameterChangeCycleTicks. INT fOutMaxLimit fOutMinLimit. FLOAT : 1E38 ( maximum output limit ). FLOAT : -1E38 ( minimum output limit ). BOOL. BOOL. BOOL bPInTheFeedbackPath bDInTheFeedbackPath bARWOnIPartOnly ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. eCTRLPARAMETERRECORDHEATING region: fKpheating. Controller amplification / controller coefficient TwinCAT PLC Library: Controller Toolbox BECHHOFF 80 tTnheating tTvheating tTdheating. Integral action time. The I component is deactivated if this is parameterised as T0s. Derivative action time. The D component is deactivated if this is parameterised as T0s. Damping time eCTRLPARAMETERRECORDCOOLING region: fKpcooling tTncooling tTvcooling tTdcooling. Controller amplification / controller coefficient. Integral action time. The I component is deactivated if this is parameterised as T0s. Derivative action time. The D component is deactivated if this is parameterised as T0s. Damping time nParameterChangeCycleTicks: The number of task cycles over which the change from one parameter record to the other takes place. fOutMaxLimit. Upper limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. fOutMinLimit. Lower limit at which integration is halted and to which the output is limited (ARW measure). Reaching this limit is indicated by a TRUE at the bARWActive output. bPInTheFeedbackPath. Input value of the P element can be selected with this input (see functional diagram). bDInTheFeedbackPath. Input value of the D element can be selected with this input (see functional diagram). bARWOnIPartOnly: If this parameter is FALSE (the standard setting), the integration of the I component is halted if the complete controller output reaches the upper or lower limit. If it is TRUE, the integration is halted if the I component (the output of the integrator) reaches some limit. (Cf. functional diagram.) Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx TwinCAT PLC Library: Controller Toolbox BECHHOFF 81 3.4. Filter FUNCTIONBLOCKFBCTRLACTUALVALUEFILTER The function block allows a measured input variable to be checked for plausibility and filtered. Behaviour of the output: This block allows a plausibility check to be carried out on a measured input variable. If the difference between two sampling values in sequence (measurements) is larger than the specified window, fDeltaMax, then the current input value is suppressed for a maximum of three cycles. During this time the output value is extrapolated from the previous input values. If the specified window is exceeded for more than three cycles, the output will again follow the input variable. The behaviour of the output is illustrated in the diagram above. VARINPUT VARINPUT fActualValue. FLOAT fSetpointValue. FLOAT fManValue eMode ENDVAR fActualValue fSetpointValue fManValue eMode. Actual value of the controlled variable. Set value of the controlled variable. Input variable whose value is output in manual mode. Input that specifies the blocks operating mode. FLOAT. ECTRLMODE VAROUTPUT VAROUTPUT fOut eState. FLOAT. ECTRLSTATE TwinCAT PLC Library: Controller Toolbox BECHHOFF 82 eErrorId bError ENDVAR fOut eState eErrorId bError. ECTRLERRORCODES. BOOL. Output of the function block. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the actual value filter element. This consists of the following elements: TYPE FBCTRLACTUALVALUEFILTER: STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fDeltaMax ENDSTRUCT ENDTYPE :FLOAT. FBCTRLACTUALVALUEFILTER tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fDeltaMax. Maximum difference between two input values in sequence. See description above. Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 TwinCAT PLC Library: Controller Toolbox BECHHOFF 83 FUNCTIONBLOCK FBCTRLARITHMETICMEAN Calculating the mean value: VARINPUT VARINPUT fIn fManSyncValue bSync eMode bHold ENDVAR fIn fManSyncValue bSync eMode. Input value for the mean value filter. Input value to which the mean value filter can be set, or that is issued at the output in manual mode. A rising edge at this input sets the mean value filter to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL TwinCAT PLC Library: Controller Toolbox BECHHOFF 84 fOut eState eErrorId bError. Output of the mean value filter. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the mean value filter. This consists of the following elements: TYPE STCTRLARITHMETICMEANPARAMS: STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx. STCTRLARITHMETICMEANPARAMS FUNCTIONBLOCK FBCTRLDIGITALFILTER This block calculates a discrete transfer function with the structure described below. This structure allows either an FIR filter (Finite Impulse Response) or an IIR filter (Infinite Impulse Response) to be implemented. The transfer function here can be of any order, n. The coefficients for the following transfer structure are stored in the parameter arrays: TwinCAT PLC Library: Controller Toolbox BECHHOFF 85 The programmer must create the following arrays in the PLC if this function block is to be used: arCoefficientsArraya. ARRAY1..nFilterOrder1 OF FLOAT arCoefficientsArrayb. ARRAY1..nFilterOrder1 OF FLOAT arstDigitalFilterData. ARRAY1..nFilterOrder OF STCTRLDIGITALFILTERDATA The coefficients b1 to bn are stored in the array arCoefficientsArrayb. This must be organised as follows: arfNumeratorArray 1 arfNumeratorArray 2 . arfNumeratorArray n-1 arfNumeratorArray n : b1 : b2 : bn-1 : bn The coefficients a1 to an are stored in the array arCoefficientsArraya. This must be organised as follows: arDenominatorArray 1 : xxx ( not being evaluated ) arDenominatorArray 2 : a2. arDenominatorArray n-1 : an-1 arDenominatorArray n : an The internal data required by the block is stored in the arstDigitalFilterData array. This data must never be modified from within the PLC program. This procedure is also illustrated in the example program listed below. VARINPUT VARINPUT fIn fManValue eMode ENDVAR. FLOAT. FLOAT. ECTRLMODE TwinCAT PLC Library: Controller Toolbox BECHHOFF 86 fIn fManValue eMode. Input of the digital filter. Input whose value is present at the output in manual mode. Input that specifies the blocks operating mode. VAROUTPUT VAROUTPUT fOut eState bError eErrorId ENDVAR fOut eState eErrorId bError. Output of the digital filter. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. BOOL. ECTRLERRORCODES VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the function block. This consists of the following elements: TYPE STCTRLDIGITALFILTERPARAMS. STRUCT tTaskCycleTime. TIME ( task cycle time ) tCtrlCycleTime. TIME : T0ms ( controller cycle time ) nFilterOrder. USINT. STCTRLDIGITALFILTERPARAMS pCoefficientsArrayaADR. POINTER TO FLOAT : 0 nCoefficientsArrayaSIZEOF. UINT pCoefficientsArraybADR. POINTER TO FLOAT : 0 nCoefficientsArraybSIZEOF. UINT pDigitalFilterDataADR. POINTER TO STCTRLDIGITALFILTERDATA nDigitalFilterDataSIZEOF. UINT ENDSTRUCT ENDTYPE tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. Order of the digital filter 0. nFilterOrder TwinCAT PLC Library: Controller Toolbox BECHHOFF 87 pCoefficientsArrayaADR nCoefficientsArrayaSIZEOF pCoefficientsArraybADR nCoefficientsArraybSIZEOF pDigitalFilterDataADR nDigitalFilterDataSIZEOF. Address of the array containing the a coefficients. Size of the array containing the a coefficients, in bytes. Address of the array containing the b coefficients. Size of the array containing the b coefficients, in bytes. Address of the data array. Size of the data array in bytes. TYPE STCTRLDIGITALFILTERDATA STRUCT Internal structure. Write access to this structure is not permitted. ENDSTRUCT ENDTYPE Example: PROGRAM PRGDIGITALFILTERTEST VAR fbDigitalFilter. FBCTRLDIGITALFILTER arCoefficientsArraya. ARRAY1..3 OF FLOAT arCoefficientsArrayb. ARRAY1..3 OF FLOAT arstDigitalFilterData. ARRAY1..2 OF STCTRLDIGITALFILTERDATA eMode. ECTRLMODE stParams. STCTRLDIGITALFILTERPARAMS eErrorId. ECTRLERRORCODES bError. BOOL fIn fOut. FLOAT : 0. FLOAT bInit. BOOL : TRUE ENDVAR IF bInit THEN ( set values in the local arrays ) arCoefficientsArraya1 : 0.0 ( not used ) arCoefficientsArraya2 : 0.2 arCoefficientsArraya3 : 0.1 arCoefficientsArrayb1 : 0.6 arCoefficientsArrayb2 : 0.4 arCoefficientsArrayb3 : 0.2 ( set values in the parameter struct ) stParams. tTaskCycleTime : T2ms stParams. tCtrlCycleTime : T2ms stParams. nFilterOrder : 2 ( set the mode ) eMode : eCTRLMODEACTIVE bInit : FALSE ENDIF TwinCAT PLC Library: Controller Toolbox BECHHOFF 88 ( set addresses ) stParams. pCoefficientsArrayaADR : ADR( arCoefficientsArraya) stParams. nCoefficientsArrayaSIZEOF : SIZEOF( arCoefficientsArraya) stParams. pCoefficientsArraybADR : ADR( arCoefficientsArrayb) stParams. nCoefficientsArraybSIZEOF : SIZEOF( arCoefficientsArrayb) stParams. pDigitalFilterDataADR : ADR( arstDigitalFilterData ) stParams. nDigitalFilterDataSIZEOF : SIZEOF( arstDigitalFilterData ) fbDigitalFilter ( fIn : fIn, eMode : eMode, stParams : stParams, fOut fOut, eErrorId eErrorId, bError bError) Development environment TwinCAT v2.8 TwinCAT v2.9, build 947 onwards TwinCAT v2.9, build 956 onwards Target system type PC (i386) BC BX PLC libraries to be linked TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLMOVINGAVERAGE The function block provides a moving mean value filter in the functional diagram. Description: The arithmetic mean of the last n values is calculated. The programmer must create an array, ARRAY 1. n of FLOAT, in which the function block can store the data that it requires internally. TwinCAT PLC Library: Controller Toolbox BECHHOFF 89 VARINPUT VARINPUT fIn fManSyncValue bSync eMode bHold ENDVAR fIn fManSyncValue bSync eMode. Input variable for the moving average filter. Input value to which the moving average filter can be set, or that is issued at the output in manual mode. A rising edge at this input sets the moving average filter to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR fOut eState eErrorId bError. Output of the moving average filter. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the moving average filter. This consists of the following elements: TYPE STCTRLMOVINGAVERAGEPARAMS: STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) nSamplesToFilter. UINT. STCTRLMOVINGAVERAGEPARAMS TwinCAT PLC Library: Controller Toolbox BECHHOFF 90 pWorkArrayADR. POINTER TO FLOAT : 0 nWorkArraySIZEOF. UINT : 0 ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. nSamplesToFilter pWorkArrayADR. The number of values, n, whose arithmetic mean is calculated. The address of the array where the input values are temporarily stored. nWorkArraySIZEOF: The size of the work array. Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLNOISEGENERATOR(only on a PC system) This function block generates a noise signal on the basis of the pseudo-random number in the range - fAmplitude/2. fAmplitude/2 . Output signal: Output signal with an amplitude of 5.0. TwinCAT PLC Library: Controller Toolbox BECHHOFF 91 VARINPUT VARINPUT fManSyncValue eMode ENDVAR fManSyncValue eMode. Input magnitude whose value is sent to the output in manual mode. Input that specifies the blocks operating mode. FLOAT. ECTRLMODE VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR fOut eState eErrorId bError. Output of the noise generator. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the noise generator. This consists of the following elements: TYPE STCTRLNOISEGENERATORPARAMS: STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fAmplitude ENDTYPE tCtrlCycleTime. FLOAT : 0. STCTRLNOISEGENERATORPARAMS. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fAmplitude. Amplitude of the output signal. A noise signal extending over the range - fAmplitude/2.0. fAmplitude/2.0 is created at the function blocks output. TwinCAT PLC Library: Controller Toolbox BECHHOFF 92 Development environment TwinCAT v2.8 Target system type PC (i386) PLC libraries to be linked TcControllerToolbox. lib FUNCTIONBLOCK FBCTRLPT1 The function block provides a PT1 transfer element in the functional diagram. Transfer function: Step response: TwinCAT PLC Library: Controller Toolbox BECHHOFF 93 VARINPUT VARINPUT fIn fManSyncValue bSync eMode bHold ENDVAR fIn fManSyncValue bSync eMode. Input value of the PT1 element. Input value to which the PT1 element can be set, or that is issued at the output in manual mode. A rising edge at this input sets the PT1 element to the value fManSyncValue. Input that specifies the blocks operating mode. FLOAT. FLOAT. BOOL. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR fOut eState eErrorId bError. Output of the PT1 element. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PT1 element. This consists of the following elements. STCTRLPT1PARAMS TYPE STCTRLPT1PARAMS. STRUCT TwinCAT PLC Library: Controller Toolbox BECHHOFF 94 tCtrlCycleTime tTaskCycleTime. TIME. TIME : T0ms ( controller cycle time TIME ) : T0ms ( task cycle time TIME ) fKp tT1 ENDSTRUCT ENDTYPE. FLOAT. TIME : 0 ( proportional gain ) : T0ms ( T1 ) tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fKp tT1. Controller amplification / transfer coefficient. Time constant Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 FUNCTIONBLOCK FBCTRLPT2 The function block provides a non-oscillating PT2 transfer element in the functional diagram. Transfer function: TwinCAT PLC Library: Controller Toolbox BECHHOFF 95 Step response: VARINPUT VARINPUT fIn fManValue eMode bHold ENDVAR fIn fManValue eMode. Input value of the PT2 element. Input value that is output in manual mode. Input that specifies the blocks operating mode. FLOAT. FLOAT. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut eState. FLOAT. ECTRLSTATE eErrorId. ECTRLERRORCODES bError. BOOL ENDVAR TwinCAT PLC Library: Controller Toolbox BECHHOFF 96 fOut eState eErrorId bError. Output of the PT2 element. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PT2 element. This consists of the following elements: TYPE STCTRLPT2PARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKp. FLOAT : 0 ( proportional gain ) tT1. TIME : T0ms ( T1 ) tT2. TIME : T0ms ( T2 ) ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fKp tT1 tT2. Controller amplification / transfer coefficient. Time constant T1. Time constant T2 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx. STCTRLPT2PARAMS Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 TwinCAT PLC Library: Controller Toolbox BECHHOFF 97 FUNCTIONBLOCK FBCTRLPT2oscillation The function block provides an oscillating PT2 transfer element in the functional diagram. Transfer function: Step response: VARINPUT VARINPUT fIn fManValue eMode bHold ENDVAR. FLOAT. FLOAT. ECTRLMODE. BOOL TwinCAT PLC Library: Controller Toolbox BECHHOFF 98 fIn fManValue eState eMode. Input variable of the oscillating PT2 element. Input value that is output in manual mode. State of the function block. Input that specifies the blocks operating mode. bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut. FLOAT eState. ECTRLSTATE eErrorId. ECTRLERRORCODES bError. BOOL ENDVAR fOut eErrorId bError. Output of the PT2 element. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the oscillating PT2 element. This consists of the following elements: TYPE STCTRLPT2oscillationPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKp fTheta tT0 ENDSTRUCT ENDTYPE. FLOAT : 0 ( proportional gain ). FLOAT : 0 ( damping ratio ). TIME : T0ms ( T0 ). STCTRLPT2oscillationPARAMS tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fKp fTheta tT0. Proportional gain. Damping ratio. Characteristic time TwinCAT PLC Library: Controller Toolbox BECHHOFF 99 Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLPT3 The function block provides a non-oscillating PT3 transfer element in the functional diagram. Transfer function: Step response: TwinCAT PLC Library: Controller Toolbox BECHHOFF 100 VARINPUT VARINPUT fIn fManValue eMode bHold ENDVAR fIn fManValue eMode. Input value of the PT3 element. Input value that is output in manual mode. Input that specifies the blocks operating mode. FLOAT. FLOAT. ECTRLMODE. BOOL bHold. A TRUE at this input will hold the internal state (and therefore also the output) constant at its current value, independently of the input value. VAROUTPUT VAROUTPUT fOut. FLOAT eState. ECTRLSTATE eErrorId. ECTRLERRORCODES bError. BOOL ENDVAR fOut eState eErrorId bError. Output of the PT3 element. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the PT3 element. This consists of the following elements: TYPE STCTRLPT3PARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time TIME ) tTaskCycleTime. TIME : T0ms ( task cycle time TIME ) fKp tT1. FLOAT : 0 ( proportional gain ). TIME : T0ms ( T1 ). STCTRLPT3PARAMS TwinCAT PLC Library: Controller Toolbox BECHHOFF 101 tT2. TIME tT3. TIME ENDSTRUCT ENDTYPE : T0ms ( T2 ) : T0ms ( T3 ) tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. fKp tT1 tT2 tT3. Controller amplification / transfer coefficient. Time constant T1. Time constant T2. Time constant T3 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 FUNCTIONBLOCK FBCTRLPTn This function block provides a non-oscillating PTn transfer element with (n 0.0. PWM signal, when fPwmInput 0.0. PWM signal, when fPwmInput 0.0. TRUE indicates that fIn movement direction is positive 0 -- generator is inactive -1 -- movement direction is negative. Indicates whether the generator is active. bGeneratorActive bCommandBuffered. When this output is TRUE it indicates that the buffer store contains at a transport instruction that will begin when the current instruction has completed. An instruction in the buffer store is cleared if the following special case is specified as parameter set: fAcceleration : 0.0 fDeceleration : 0.0 fStartPos : 0.0 fStartVelo : 0.0 fTargetPos : 0.0 fTargetVelo : 0.0 fVelocity : 0.0 tCtrlCycleTime : T0s tTaskCycleTime : T0s eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant bDone has been reached. This output becomes TRUE when the movement has been completed and the destination position TwinCAT PLC Library: Controller Toolbox BECHHOFF 153 bCommandAborted. This output becomes TRUE when the current movement is interrupted. This can be caused, for instance, through a rising edge at the bStop input. eErrorId bError. Supplies the error number when the bError output is set. Becomes TRUE as soon as an error situation occurs. VARINOUT VARINOUT stParams. STCTRL3PHASESETPOINTGENERATORPARAMS ENDVAR stParams. Parameter structure of the set value generator. This consists of the following elements: TYPE STCTRLRAMPGENERATORPARAMS. STRUCT tTaskCycleTime. TIME ( task cycle time TIME ) tCtrlCycleTime. TIME ( controller cycle time TIME ) fStartPos fStartVelo fVelocity fTargetPos fTargetVelo fAcceleration fDeceleration. LREAL. LREAL. LREAL ( 0.0 ). LREAL. LREAL. LREAL ( 0.0 ). LREAL ( 0.0 ) eNewParameterType. ECTRLNEWPARAMETERTYPE ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every task cycle this corresponds to the task cycle time of the calling task. fStartPos fStartVelo fVelocity fTargetPos fTargetVelo. Starting position for the movement profile. Starting velocity for the movement profile. Velocity in units/second. Destination position for the movement profile. Target velocity of the movement profile. Warning:The target velocity is retained when the destination position has been reached (the bDone flag is set), but after this point the position is no longer calculated (constant position at velocity. 0.0). Acceleration in units / second2. Deceleration in units / second2. fAcceleration fDeceleration eNewParameterType TYPE ECTRLNEWPARAMETERTYPE. ( eCTRLNEWPARAMETERTYPENotInstant : 0, eCTRLNEWPARAMETERTYPEInstant : 1 ) TwinCAT PLC Library: Controller Toolbox BECHHOFF 154 ENDTYPE eCTRLNEWPARAMETERTYPEInstant: When a restart instruction is issued with a new parameter set, this set is adopted immediately. In other words a transition from the current state of movement to the data represented by the new parameter set is calculated, and the old parameters are discarded. eCTRLNEWPARAMETERTYPENotInstant: When a restart instruction is issued with a new parameter set, the new set is not adopted immediately. In other words the current movement is first executed to completion, after which positioning to the new destination is carried out with the new parameters. A TRUE at the bCommandBuffered output indicates that the buffer store contains a non-instantaneous restart instruction. An instruction that has already been stored can be overwritten or deleted by a further new, non-instantaneous parameter set. Positioning examples Positioning example 1: stParams. fStartPos : -500.0 stParams. fTargetPos : 2500.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1000.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 fOverride : 100.0 TwinCAT PLC Library: Controller Toolbox BECHHOFF 155 Positioning example 2: stParams. fStartPos : 1000.0 stParams. fTargetPos : -2500.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1000.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 fOverride : 100.0 Positioning example 3: stParams. fStartPos : -500.0 stParams. fTargetPos : 2500.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1000.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant fOverride : 100.0 Parameter change if fSetPos 1000.0, eNewPosType : eCTRLNEWPOSTYPENotInstant stParams. fTargetPos : 0.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1300.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 fOverride : 100.0 TwinCAT PLC Library: Controller Toolbox BECHHOFF 156 Positioning example 4: stParams. fStartPos : -500.0 stParams. fTargetPos : 2500.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1000.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant fOverride : 100.0 Parameter change if fSetPos 1000.0, eNewPosType : eCTRLNEWPOSTYPEInstant stParams. fTargetPos : 0.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 1300.0 stParams. fTargetVelo : 0.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 2000.0 fOverride : 100.0 TwinCAT PLC Library: Controller Toolbox BECHHOFF 157 Positioning example 5: Start at point 1: stParams. fStartPos : -100.0 stParams. fTargetPos : 200.0 stParams. fStartVelo : 0.0 stParams. fVelocity : 250.0 stParams. fTargetVelo : 150.0 stParams. fAcceleration : 500.0 stParams. fDeceleration : 400.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPEInstant Restart at point 2: stParams. fTargetPos : 400.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant Restart at point 3: stParams. fTargetPos : 200.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant Restart at point 4: stParams. fTargetPos : 600.0 stParams. fTargetVelo : 0.0 stParams. eNewParameterType : eCTRLNEWPARAMETERTYPENotInstant TwinCAT PLC Library: Controller Toolbox BECHHOFF 158 Warning: If a new parameter set of type eCTRLNEWPOSTYPEInstant in which the deceleration is reduced is given to the block it is possible that the old destination position will be exceeded. Example: stParams. fTargetPos : 1000.0 stParams. fStartPos : 0.0 stParams. fVelocity : 500.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 1000.0 IF fSetPos 800.0 THEN stParams. fTargetPos : 400.0 stParams. fVelocity : 500.0 stParams. fAcceleration : 1000.0 stParams. fDeceleration : 500.0 stParams. eNewPosType : eCTRLNEWPOSTYPEInstant ENDIF It can clearly be seen in the following scope trace that the original destination position of 1000 mm is exceeded, the reason being that the new parameter set had a reduced deceleration. TwinCAT PLC Library: Controller Toolbox BECHHOFF 159 Development environment TwinCAT v2.8 Target system type PC (i386) PLC libraries to be linked TcControllerToolbox. lib FUNCTIONBLOCK FBCTRLRAMPGENERATOR The function block provides a parameterisable ramp generator. Description: This function block generates a ramp connecting the starting value fStart and the final value fTarget. The slope of the ramp (i. e. the velocity) is given in units/s by means of the fVeloPos and fVeloNeg parameters. The starting value is adopted when a rising edge appears at bEnable calculation of the ramp then begins. As long as the signal bEnable remains TRUE the final value can be changed, and the output value changes, taking the form of a ramp as it moves from the current value to the presently active final value. Behaviour of the output value: TwinCAT PLC Library: Controller Toolbox BECHHOFF 160 VARINPUT VARINPUT bEnable. BOOL fStart. FLOAT fTarget ENDVAR bEnable fStart fTarget. Start of ramp generation. Starting value for the ramp. Finishing value for the ramp. FLOAT ( target value ) VAROUTPUT VAROUTPUT fOut bValueReached eState eErrorId bError ENDVAR fOut. Output of the ramp generator. bValueReached. This output indicates by going TRUE that the output fOut has reached the value fTarget. eState eErrorId bError. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE as soon as an error situation occurs. FLOAT. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL TwinCAT PLC Library: Controller Toolbox BECHHOFF 161 VARINOUT VARINOUT stParams. STCTRLRAMPGENERATORPARAMS ( RAMPGenerator parameter struct ) ENDVAR stParams. Parameter structure of the ramp generator. This consists of the following elements: TYPE STCTRLRAMPGENERATORPARAMS. STRUCT tTaskCycleTime. TIME ( task cycle time TIME ) tCtrlCycleTime. TIME ( controller cycle time TIME ) fVeloPos fVeloNeg ENDSTRUCT ENDTYPE. FLOAT ( velocity ramp by time range: 0.0 ). FLOAT ( velocity ramp by time range: 0.0 ) tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every task cycle this corresponds to the task cycle time of the calling task. fVeloPos fVeloNeg. Velocity in unit/s, with which the output is changed from a lower value to a higher one. Velocity in unit/s, with which the output is changed from a higher value to a lower one. Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 FUNCTIONBLOCK FBCTRLRAMPGENERATOREXT This function block represents a parameterisable ramp generator. In contrast to FBCTRLRAMPGENERATOR it supports the ECTRLMODEs. TwinCAT PLC Library: Controller Toolbox BECHHOFF 162 Description: This function block generates a ramp connecting the starting value fStartValue and the final value fTargetValue. The slope of the ramp (i. e. the velocity) is given in units/s by means of the fVeloPos and fVeloNeg parameters. The starting value is adopted when eCTRLMODERESET changes to eCTRLMODEACTIVE, and calculation of the ramp begins. As long as the block remains in eCTRLMODEACTIVE the target value can be changed, and the output value changes, taking the form of a ramp as it moves from the current value to the presently active target value. The current velocity is output at fVeloOut. It is possible to use this for feed forward in the control loop. Behaviour of the output value: VARINPUT VARINPUT fStartValue fTargetValue fManValue bHold eMode ENDVAR fStartValue fTargetValue fManValue bHold eMode. Starting value for the ramp. Finishing value for the ramp. Input magnitude to which the output in eCTRLMODEMANUAL is set. Calculation of the ramp is halted at the current value. Input that specifies the blocks operating mode. FLOAT. FLOAT. FLOAT. BOOL. ECTRLMODE TwinCAT PLC Library: Controller Toolbox BECHHOFF 163 VAROUTPUT VAROUTPUT fOut fVeloOut bValueReached eState eErrorId bError ENDVAR fOut fVeloOut. Output of the ramp generator. Current velocity of the ramp generator. FLOAT. FLOAT. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL bValueReached. This output indicates by going TRUE that the output fOut has reached the value fTargetValue. eState eErrorId bError. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE as soon as an error situation occurs. VARINOUT VARINOUT stParams: STCTRLRAMPGENERATOREXTPARAMS ENDVAR stParams. Parameter structure of the ramp generator. This consists of the following elements: TYPE STCTRLRAMPGENERATOREXTPARAMS. STRUCT tTaskCycleTime. TIME ( task cycle time TIME ) tCtrlCycleTime. TIME ( controller cycle time TIME ) fVeloPos fVeloNeg ENDSTRUCT ENDTYPE tCtrlCycleTime. FLOAT ( velocity ramp by time range: 0.0 ). FLOAT ( velocity ramp by time range: 0.0 ). Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every task cycle this corresponds to the task cycle time of the calling task. fVeloPos fVeloNeg. Velocity (0.0) in unit/s, with which the output is changed from a lower value to a higher one. Velocity (0.0) in unit/s, with which the output is changed from a higher value to a lower one. TwinCAT PLC Library: Controller Toolbox BECHHOFF 164 Development environment TwinCAT v2.8 TwinCAT v2.9, build 947 onwards TwinCAT v2.9, build 956 onwards Target system type PC (i386) BC BX PLC libraries to be linked TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx FUNCTIONBLOCK FBCTRLSETPOINTGENERATOR The function block provides a set value generator that outputs the set value selected from a table. Changing from one set value to another can be implemented continuously or discontinuously. Behaviour of the output value: Example table: 18 20 22 24 The first line of the table corresponds to Index 1, at the second to Index 2, and so forth. TwinCAT PLC Library: Controller Toolbox BECHHOFF 165 Description: The individual set values are stored in the array. The array must be made known to the block through the appropriate parameters. One of the set values stored in the table is selected by means of the nSetpointIndex input. This is then made available at the output, and can be used as the set value for the controller. Changing from one value to another can be implemented in a linear fashion or as a jump. The velocity of a continuous transition is specified by the fVeloPos and fVeloNeg parameters. The bValueReached output indicates that the chosen set value has been reached. VARINPUT VARINPUT nSetpointIndex. INT fManValue eMode ENDVAR nSetpointIndex fManValue eMode. Index of the selected set value. Input whose value is output in manual mode. Input that specifies the blocks operating mode. (1. n ). FLOAT. ECTRLMODE VAROUTPUT VAROUTPUT fOut bValueReached eState eErrorId bError ENDVAR fOut. Output of the set value generator. bValueReached. The output is TRUE when the selected set value has been reached, i. e. the ramp leading to the selected value has finished. eState eErrorId bError. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. SETPOINTTABLEELEMENT. BOOL. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams. STCTRLSETPOINTGENERATORPARAMS ( parameters ) ENDVAR stParams. Parameter structure of the ramp generator. This consists of the following elements: TYPE STCTRLSETPOINTGENERATORPARAMS: TwinCAT PLC Library: Controller Toolbox BECHHOFF 166 STRUCT tCtrlCycleTime tTaskCycleTime. TIME : T0ms ( controller cycle time TIME ). TIME : T0ms ( task cycle time TIME ) pDataTableADR. POINTER TO INTERPOLATIONTABLEELEMENT : 0 nDataTableSIZEOF. UINT : 0 nDataTableNumberOfRows. UINT : 0 fVeloPos fVeloNeg bDisableRamping ENDSTRUCT ENDTYPE. FLOAT ( velocity ramp by time range 0.0 ). FLOAT ( velocity ramp by time range 0.0 ). BOOL : FALSE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. pDataTableADR. Address of the data array. pDataTableSIZEOF. Size of the data array. pDataTableNumberOfRows. Number of lines in the data array. fVeloPos fVeloNeg. Velocity in unit/s, with which the output is changed from a lower value to a higher one. Velocity in unit/s, with which the output is changed from a higher value to a lower one. bDisableRamping: A continuous output value is not calculated if this parameter is TRUE. The output simply jumps between the two values. TwinCAT PLC Library: Controller Toolbox BECHHOFF 167 FUNCTIONBLOCK FBCTRLSIGNALGENERATOR The function block provides a signal generator offering triangular, sine and sawtooth signal forms. Output signals: TwinCAT PLC Library: Controller Toolbox BECHHOFF 168 VARINPUT VARINPUT fManValue eMode ENDVAR fManValue eMode. Input whose value is present at the output in manual mode. Input that specifies the blocks operating mode. FLOAT. ECTRLMODE VAROUTPUT VAROUTPUT fOut eState eErrorId bError ENDVAR fOut eState eErrorId bError. Output of the signal generator. State of the function block. Supplies the error number when the bError output is set. Becomes TRUE, as soon as an error occurs. FLOAT. ECTRLSTATE. ECTRLERRORCODES. BOOL VARINOUT VARINOUT stParams ENDVAR stParams. Parameter structure of the function block. This consists of the following elements: TYPE STCTRLIPARAMS. STRUCT tCtrlCycleTime. TIME : T0ms ( controller cycle time ) tTaskCycleTime. TIME : T0ms ( task cycle time ) eSignalType. ECTRLSIGNALTYPE. STCTRLSIGNALGENERATORPARAMS tCylceDuration. TIME fAmplitude. FLOAT fOffset. FLOAT : 0.0 tStart. TIME : T0s ENDSTRUCT ENDTYPE tCtrlCycleTime. Cycle time with which the control loop is processed. This must be greater than or equal to the TwinCAT PLC Library: Controller Toolbox BECHHOFF 169 TaskCycleTime. The function block uses this input variable to calculate internally whether the state and the output variables have to be updated in the current cycle. tTaskCycleTime. Cycle time with which the function block is called. If the block is called in every cycle this corresponds to the task cycle time of the calling task. eSignalType tCycleDuration fAmplitude fOffset. Selection of the type of signal. See ECTRLSIGNALTYPE below. Period of the generated signal curve. Amplitude of the generated signal curve. Offset to be added to the signal curve. tStart. The moment of time within a period at which following the signal curve will begin when switching into eCTRLMODEACTIVE occurs. TYPE ECTRLSIGNALTYPE. ( eCTRLTRIANGLE eCTRLSINUS eCTRLSAWTOOTH ) ENDTYPE Development Environment TwinCAT v2.8 TwinCAT v2.9 from Build 947 TwinCAT v2.9 from Build 956 Target System PC (i386) BC BX PLC libraries to include TcControllerToolbox. lib TcControllerToolbox. lb6 TcControllerToolbox. lbx : 0. 1. 2 TwinCAT PLC Library: Controller Toolbox BECHHOFF 170 Appendix Definition of the structures and enums All the structures and enums used in the Controller Toolbox are described in this appendix. FLOAT: The library is structured in such a way that it can run either on a PC, BX or on a BC system. To achieve this portability, the function blocks in the library only work with the FLOAT data type. This data type is defined as LREAL or as REAL in a supplementary library. The supplementary library TcFloatPC. lib is linked for PC systems. TYPE FLOAT. LREAL ENDTYPE The supplementary library TcFloatBC. lib is linked for BC systems. TYPE FLOAT. REAL ENDTYPE The supplementary library TcFloatBX. lib is linked for BX systems. TYPE FLOAT. REAL ENDTYPE ECTRLMODE: TYPE ECTRLMODE. ( eCTRLMODEIDLE : 0, ( mode idle ) eCTRLMODEPASSIVE : 1, ( mode passive ) eCTRLMODEACTIVE : 2, ( mode active ) eCTRLMODERESET : 3, ( mode reset ) eCTRLMODEMANUAL : 4, ( mode manual ) eCTRLMODETUNE : 5, ( mode tuning ) eCTRLMODESELFTEST : 6, ( mode selftest ) eCTRLMODESYNCMOVEMENT : 7 ( mode synchronize ) ) ENDTYPE ECTRLSTATE. TYPE ECTRLSTATE. ( eCTRLSTATEIDLE eCTRLSTATEPASSIVE eCTRLSTATEACTIVE eCTRLSTATERESET : 0, ( state idle ) : 1, ( state passive ) : 2, ( state active ) : 3, ( state reset ) TwinCAT PLC Library: Controller Toolbox BECHHOFF 171 eCTRLSTATEMANUAL eCTRLSTATETUNING eCTRLSTATESELFTEST : 4, ( state manual ) : 5, ( state tuning ) : 6, ( state tuning ) : 7, ( identification ) : 8 : 9 ( state error ) ( state synchronizing movement ) eCTRLSTATEIDENTIFICATION eCTRLSTATEERROR eCTRLSTATESYNCMOVEMENT ) ENDTYPE ECTRLERRORCODES: TYPE ECTRLERRORCODES. ( eCTRLERRORNOERROR eCTRLERRORINVALIDTASKCYCLETIME eCTRLERRORINVALIDCTRLCYCLETIME eCTRLERRORINVALIDPARAM eCTRLERRORINVALIDPARAMTv eCTRLERRORINVALIDPARAMTd eCTRLERRORINVALIDPARAMTn eCTRLERRORINVALIDPARAMTi : 0, ( no error ) : 1, ( invalid task cycle time ) : 2, ( invalid ctrl cycle time ) : 3, ( invalid parameter ) : 4, ( invalid parameter ) : 5, ( invalid parameter ) : 6, ( invalid parameter ) : 7, ( invalid parameter ) : 8, ( invalid parameter ) : 9, ( invalid parameter ) : 10, ( invalid parameter ) : 11, ( invalid parameter ) : 12, ( invalid parameter ) : 13, ( invalid parameter ) : 14, ( invalid parameter ) : 15, ( invalid parameter ) : 16, ( invalid parameter ) : 17, ( invalid parameter ) : 18, ( invalid parameter ) : 19, ( invalid parameter ) : 20, ( invalid parameter ) : 21, ( invalid parameter ) eCTRLERRORINVALIDPARAMfHystereisisRange eCTRLERRORINVALIDPARAMfPosOutOnOff eCTRLERRORINVALIDPARAMfNegOutOnOff eCTRLERRORINVALIDPARAMTableDescription eCTRLERRORINVALIDPARAMTableData eCTRLERRORINVALIDPARAMDataTableADR eCTRLERRORINVALIDPARAMT0 eCTRLERRORINVALIDPARAMT1 eCTRLERRORINVALIDPARAMT2 eCTRLERRORINVALIDPARAMT3 eCTRLERRORINVALIDPARAMTheta eCTRLERRORINVALIDPARAMnOrder eCTRLERRORINVALIDPARAMTt eCTRLERRORINVALIDPARAMTu TwinCAT PLC Library: Controller Toolbox BECHHOFF 172 eCTRLERRORINVALIDPARAMTg eCTRLERRORINVALIDPARAMinfiniteslope eCTRLERRORINVALIDPARAMfMaxIsLessThanfMin : 22, ( invalid parameter ) : 23, ( invalid parameter ) : 24, ( invalid parameter ) : 25, eCTRLERRORINVALIDPARAMfOutMaxLimitIsLessThanfOutMinLimit ( invalid parameter ) eCTRLERRORINVALIDPARAMfOuterWindow eCTRLERRORINVALIDPARAMfInnerWindow : 26, ( invalid parameter ) : 27, ( invalid parameter ) : 28, eCTRLERRORINVALIDPARAMfOuterWindowIsLessThanfInnerWindow ( invalid parameter ) eCTRLERRORINVALIDPARAMfDeadBandInput eCTRLERRORINVALIDPARAMfDeadBandOutput eCTRLERRORINVALIDPARAMPWMCycletime eCTRLERRORINVALIDPARAMnoParameterset eCTRLERRORINVALIDPARAMfOutOn eCTRLERRORINVALIDPARAMfOutOff eCTRLERRORINVALIDPARAMfGain eCTRLERRORINVALIDPARAMfOffset eCTRLERRORMODENOTSUPPORTED : 29, ( invalid parameter ) : 30, ( invalid parameter ) : 31, ( invalid parameter ) : 32, ( invalid parameter ) : 33, ( invalid parameter ) : 34, ( invalid parameter ) : 35, ( invalid parameter ) : 36, ( invalid parameter ) : 37, ( invalid mode: mode not supported ) eCTRLERRORINVALIDPARAMTvheating eCTRLERRORINVALIDPARAMTdheating eCTRLERRORINVALIDPARAMTnheating eCTRLERRORINVALIDPARAMTvcooling eCTRLERRORINVALIDPARAMTdcooling eCTRLERRORINVALIDPARAMTncooling eCTRLERRORRANGENOTSUPPORTED : 38, ( invalid parameter ) : 39, ( invalid parameter ) : 40, ( invalid parameter ) : 41, ( invalid parameter ) : 42, ( invalid parameter ) : 43, ( invalid parameter ) : 44, ( invalid parameter ) : 45, ( invalid parameter ) eCTRLERRORParameterEstimationFailed eCTRLERRORNoiseLevelToHigh eCTRLERRORINTERNALERROR0 eCTRLERRORINTERNALERROR1 eCTRLERRORINTERNALERROR2 eCTRLERRORINTERNALERROR3 eCTRLERRORINTERNALERROR4 : 46, ( invalid parameter ) : 47, ( invalid parameter ) : 48, ( internal error ) : 49, ( internal error ) : 50, ( internal error ) : 51, ( internal error ) : 52, ( internal error ) eCTRLERRORINVALIDPARAMnParameterChangeCycleTicks TwinCAT PLC Library: Controller Toolbox BECHHOFF 173 eCTRLERRORINTERNALERROR5 eCTRLERRORINTERNALERROR6 eCTRLERRORINTERNALERROR7 eCTRLERRORINTERNALERROR8 eCTRLERRORINTERNALERROR9 eCTRLERRORINVALIDPARAMWorkArrayADR eCTRLERRORINVALIDPARAMtOnTiime eCTRLERRORINVALIDPARAMtOffTiime eCTRLERRORINVALIDPARAMnMaxMovingPulses : 53, ( internal error ) : 54, ( internal error ) : 55, ( internal error ) : 56, ( internal error ) : 57, ( internal error ) : 58, ( invalid parameter ) : 59, ( invalid parameter ) : 60, ( invalid parameter ) : 61, ( invalid parameter ) : 62, ( invalid parameter ) eCTRLERRORINVALIDPARAMnAdditionalPulsesAtLimits eCTRLERRORINVALIDPARAMfCtrlOutMaxMin eCTRLERRORINVALIDPARAMfDeltaMax eCTRLERRORINVALIDPARAMtMovingTime eCTRLERRORINVALIDPARAMtDeadTime : 63, ( invalid parameter ) : 64, ( invalid parameter ) : 65, ( invalid parameter ) : 66, ( invalid parameter ) eCTRLERRORINVALIDPARAMtAdditionalMoveTimeAtLimits : 67, ( invalid parameter ) eCTRLERRORINVALIDPARAMfThreshold eCTRLERRORMEMCPY eCTRLERRORMEMSET eCTRLERRORINVALIDPARAMnNumberOfColumns eCTRLERRORFileClose eCTRLERRORFileOpen eCTRLERRORFileWrite eCTRLERRORINVALIDPARAMfVeloNeg eCTRLERRORINVALIDPARAMfVeloPos eCTRLERRORINVALIDPARAMDeadBandInput eCTRLERRORINVALIDPARAMDeadBandOutput eCTRLERRORINVALIDPARAMCycleDuration eCTRLERRORINVALIDPARAMtStart : 68, ( invalid parameter ) : 69, ( MEMCPY failed ) : 70, ( MEMSET failed ) : 71, ( invalid parameter ) : 72, ( File Close failed ) : 73, ( File Open failed ) : 74, ( File Write failed ) : 75, ( invalid parameter ) : 76, ( invalid parameter ) : 77, ( invalid parameter ) : 78, ( invalid parameter ) : 79, ( invalid parameter ) : 80, ( invalid parameter ) eCTRLERRORINVALIDPARAMStepHeigthTuningToLow: 81, ( invalid parameter ) eCTRLERRORINVALIDPARAMfMinLimitIsLessThanZero: 82, ( invalid parameter ) eCTRLERRORINVALIDPARAMfMaxLimitIsGreaterThan100 : 83, (invalid parameter) eCTRLERRORINVALIDPARAMfStepSize : 84, ( invalid parameter ) TwinCAT PLC Library: Controller Toolbox BECHHOFF 174 eCTRLERRORINVALIDPARAMfOkRangeIsLessOrEqualZero : 85, ( invalid parameter ) eCTRLERRORINVALIDPARAMfForceRangeIsLessOrEqualfOkRange : 86, ( invalid parameter ) eCTRLERRORINVALIDPWMPeriod eCTRLERRORINVALIDPARAMtMinimumPulseTime eCTRLERRORFileDelete eCTRLERRORINVALIDPARAMnNumberOfPwmOutputs : 87, ( invalid parameter ) : 88, ( invalid parameter ) : 89, ( invalid parameter ) : 90, ( File Delete failed ) eCTRLERRORINVALIDPARAMnPwmInputArraySIZEOF: 91, ( invalid parameter ) eCTRLERRORINVALIDPARAMnPwmOutputArraySIZEOF: 92, ( invalid parameter ) eCTRLERRORINVALIDPARAMnPwmWaitTimesConfigSIZEOF : 93, ( invalid parameter ) eCTRLERRORINVALIDPARAMnPwmInternalDataSIZEOF : 94, ( invalid parameter ) eCTRLERRORSIZEOF : 95, ( SIZEOF failed ) eCTRLERRORINVALIDPARAMnOrderOfTheTransferfunction : 96, ( invalid parameter ) eCTRLERRORINVALIDPARAMnNumeratorArraySIZEOF : 97, ( invalid parameter ) eCTRLERRORINVALIDPARAMnDenominatorArraySIZEOF : 98, ( invalid parameter ) eCTRLERRORINVALIDPARAManIsZero eCTRLERRORINVALIDPARAMWorkArraySIZEOF eCTRLERRORINVALIDPARAMMOVINGRANGEMINMAX eCTRLERRORINVALIDPARAMMOVINGTIME eCTRLERRORINVALIDPARAMDEADTIME : 99, ( invalid parameter ) : 100,( invalid parameter ) : 101, ( invalid parameter ) : 102, ( invalid parameter ) : 103, ( invalid parameter ) : 104, eCTRLERRORINVALIDPARAMfMinLimitIsGreaterThanfMaxLimit ( invalid parameter ) eCTRLERRORINVALIDPARAMDataTableSIZEOF : 105, ( invalid parameter ) : 106, ( invalid parameter ) eCTRLERRORTaskCycleTimeChanged eCTRLERRORINVALIDPARAMnMinMovingPulses eCTRLERRORINVALIDPARAMfAcceleration eCTRLERRORINVALIDPARAMfDeceleration : 107, ( invalid parameter ) : 108, ( invalid parameter ) : 109, ( invalid parameter ) : 110, ( invalid parameter ) eCTRLERRORINVALIDPARAMOutputVectorDescription TwinCAT PLC Library: Controller Toolbox BECHHOFF 175 eCTRLERRORINVALIDPARAMStartAndTargetPos eCTRLERRORINVALIDPARAMfVelocity eCTRLERRORINVALIDPARAMfTargetPos eCTRLERRORINVALIDPARAMfStartPos eCTRLERRORINVALIDPARAMfMovingLength eCTRLERRORNTGetTime : 111, ( invalid parameter ) : 112, ( invalid parameter ) : 113, ( invalid parameter ) : 114, ( invalid parameter ) : 115, ( invalid parameter ) : 116, ( internal error NTGetTime ) eCTRLERRORINVALIDPARAMNo3PhaseSolutionPossible: 117, ( invalid parameter ) eCTRLERRORINVALIDPARAMfStartVelo eCTRLERRORINVALIDPARAMfTargetVelo eCTRLERRORINVALIDNEWPARAMETERTYPE eCTRLERRORINVALIDPARAMfBaseTime : 118, ( invalid parameter ) : 119, ( invalid parameter ) : 120 ( invalid parameter ) : 121, ( invalid parameter ) : 122, eCTRLERRORINVALIDPARAMnOrderOfTheTransferfunctionSIZEOF ( invalid parameter ) eCTRLERRORINVALIDPARAMnFilterOrder : 124, ( invalid parameter ) eCTRLERRORINVALIDPARAMnCoefficientsArrayaSIZEOF : 125, ( invalid parameter ) eCTRLERRORINVALIDPARAMnCoefficientsArraybSIZEOF : 126, ( invalid parameter ) eCTRLERRORINVALIDPARAMnDigitalFiterDataSIZEOF : 127, ( invalid parameter ) eCTRLERRORINVALIDPARAMnLogBufferSIZEOF eCTRLERRORLogBufferOverflow eCTRLERRORINVALIDPARAMnLogBufferADR : 128, ( invalid parameter ) : 129, ( invalid parameter ) : 130, ( invalid parameter ) : 131, ( invalid parameter ) eCTRLERRORINVALIDPARAMnCoefficientsArraybADR : 132, ( invalid parameter ) eCTRLERRORINVALIDPARAMnPwmInputArrayADR eCTRLERRORINVALIDPARAMnPwmOutputArrayADR : 133, ( invalid parameter ) : 134, ( invalid parameter ) : 135, ( invalid parameter ) eCTRLERRORINVALIDPARAMnPwmInternalDataADR : 136, ( invalid parameter ) eCTRLERRORINVALIDPARAMnDigitalFiterDataADR: 137, ( invalid parameter ) eCTRLERRORINVALIDPARAMnCoefficientsArrayaADR eCTRLERRORINVALIDPARAMnPwmWaitTimesConfigADR TwinCAT PLC Library: Controller Toolbox BECHHOFF 176 eCTRLERRORINVALIDPARAMnNumeratorArrayADR : 138, ( invalid parameter ) eCTRLERRORINVALIDPARAMnDenominatorArrayADR: 139, ( invalid parameter ) eCTRLERRORINVALIDPARAMnTransferfunction1DataADR : 140, ( invalid parameter ) eCTRLERRORINVALIDPARAMnTransferfunction2DataADR : 141 ( invalid parameter ) ) ENDTYPE ECTRLSIGNALTYPE: TYPE ECTRLSIGNALTYPE. ( eCTRLTRIANGLE : 0, eCTRLSINUS : 1, eCTRLSAWTOOTH : 2 ) ENDTYPE Example project The ways in which the blocks in the TcControllerToolbox function are illustrated in an example project. This project contains programs using the individual function blocks, with which the basic functionality is illustrated. The illustrations include some examples and also the construction of more complex control systems. The control system is simulated for all of these programs, so that no hardware is necessary for the project. Install and start the example project The TcControllerToolbox offers the programmer transfer elements with which a very wide variety of controllers can be implemented. Save and unpack the example program from TcControllerToolboxExamples. zip. The example program uses simulations of controlled systems, and can thus be run on any Windows NT PC without additional hardware. The TwinCAT system is started through the TwinCAT icon on the taskbar. TwinCAT PLC Library: Controller Toolbox BECHHOFF 177 The TcControllerToolboxExamples. pro example program is loaded into TwinCAT PLC Control, translated and started. Load the PRO file Compile the PLC project via the menu Project - Rebuild All. Load the PLC project via the menu Online - Login into the runtime system Start the program via the menu Online - Run TwinCAT ScopeView allows the signal curves from the individual examples to be displayed graphically. The settings supplied in TcControllerToolboxExamplesScopex. scp can be used for this purpose. This specification of the Scope file to be used for each particular example can be found in the comments in the projects MAIN program. Load the SCP file into TwinCAT ScopeView Start recording via the menu or via the F5 key. Because the example project contains a number of different examples, it is necessary to set the variable nExampleSelector to the appropriate example number in the MAIN program. Double-click on the nExampleSelector variable Enter the number of the example. OK. Press the F7 key, or click Force Values in the online menu. TwinCAT PLC Library: Controller Toolbox BECHHOFF 178 Assigning the program numbers The programs are selected through the nExampleSelector variable in MAIN. Function block to which the example relates Base FBCTRLP FBCTRLI FBCTRLD FBCTRLHYSTERESIS FBCTRLTRANSFERFUNCTION1 FBCTRLTRANSFERFUNCTION2 Controller FBCTRL2POINT FBCTRL3POINT FBCTRL3POINTEXT FBCTRLnPOINT FBCTRLPI FBCTRLPID FBCTRLPIDEXT Filter / Controlled System Simulation FBCTRLACTUALVALUEFILTE R FBCTRLARITHMETICMEAN FBCTRLMOVINGAVERAGE FBCTRLPT1 FBCTRLPT2 FBCTRLPT2oscillation FBCTRLPT3 nExampleSelecto Scope File r 1 2 3 4 5 6 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope2.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp 10 11 12 13 14 15 16 TcControllerToolboxExamplesScope3.scp TcControllerToolboxExamplesScope4.scp TcControllerToolboxExamplesScope5.scp TcControllerToolboxExamplesScope5.scp TcControllerToolboxExamplesScope5.scp TcControllerToolboxExamplesScope5.scp TcControllerToolboxExamplesScope5.scp 20 TcControllerToolboxExamplesScope1.scp 21 22 23 24 25 26 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TwinCAT PLC Library: Controller Toolbox BECHHOFF 179 FBCTRLPTn FBCTRLPTt FBCTRLTuTg Interpolation FBCTRLINTERPOLATION FBCTRLNORMALIZE Monitoring / Alarming FBCTRLCHECKIFINBAND FBCTRLLOGDATA Output to Controlling Equipment FBCTRLDEADBAND FBCTRLLIMITER FBCTRLPWMOUT FBCTRLPWMOUTEXT FBCTRLMULTIPLEPWMOUT FBCTRLSCALE Setpointgeneration FBCTRLRAMPGENERATOR FBCTRLSETPOINTGENERATO R FBCTRLSIGNALGENERATOR Controlled System Examples FBCTRLPIDSPLITRANGE FBCTRLPIPID 27 28 29 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp 30 31 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope7.scp 40 41 TcControllerToolboxExamplesScope8.scp 50 51 52 53 54 55 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope9.scp TcControllerToolboxExamplesScope9.scp TcControllerToolboxExamplesScope10.scp TcControllerToolboxExamplesScope1.scp 60 61 TcControllerToolboxExamplesScope1.scp TcControllerToolboxExamplesScope1.scp 62 TcControllerToolboxExamplesScope1.scp 80 81 TcControllerToolboxExamplesScope6.scp TcControllerToolboxExamplesScope11.scp TwinCAT PLC Library: Controller Toolbox BECHHOFF 180 Program Structure The Main module calls up the corresponding example program in accordance with the variable nExampleSelector. The comments in the individual programs should make them clear and easy to understand. TwinCAT PLC Library: Controller ToolboxContact and Coil Nearly In Control TwinCAT 3 Tutorial: Structured Text TwinCAT 3 includes all five IEC-61131-3 languages: Ladder Diagram, Structured Text, Function Block Diagram, Sequential Function Chart, and Instruction List. If you8217re coming from the Allen-Bradley world then obviously Ladder Diagram is going to be your most comfortable language, but I expect you8217ll also want to make use of Structured Text. In fact, Beckhoff themselves typically present Structured Text as the go-to language for programming in TwinCAT 3. I prefer writing most of my programs in Ladder Diagram for the obvious reasons: ease of troubleshooting, and the ability of electricians to go online with the program and debug it. However, we can8217t forget that old adage, 8220use the right tool for the job,8221 and there are times when Structured Text is the right tool, and Ladder Diagram is not. Structured Text has similarities to Pascal or BASIC (at least after they removed the concept of line numbers from BASIC). The most applicable feature of Structured Text for us are LOOP s. The FOR Loop Imagine for a moment that you have an array of a thousand REAL data values and you want to compute the average of those values. The formula is pretty simple: just add them up and divide by 1000. Obviously this presents some difficulty in Ladder Diagram, but in Structured Text, we can just use a FOR loop. Start by creating a new function. Call it AverageOf1000 and make sure you select a function with the return type of REAL. and Structured Text (ST) in the Implementation Language drop-down box: Click Open . Now you8217ll have an empty Structured Text function: We could pass the array in as an input, but if you remember from the last section, that would mean copying the entire array every time this function is called, which could negatively impact the scan time. It8217s better to pass large data structures like this by reference, which means we declare it as a VARINOUT variable: Next declare some local variables: one to store the sum of the values, and another to be an index to hold where we8217re pointing to in the array. Now we can write our logic, which consists of a FOR loop and a division operation: On line 1 it initializes the value of variable Sum to 0. Note that the : operator means assignment. It computes the expression on the right (the RValue) and stores it in the variable on the left (the LValue). Also note that each statement ends with a semi-colon. This is important and you8217ll get a syntax error if you don8217t include it (the exception is the semi-colon at the end of line 4, which is optional, but frequently included in many Structured Text examples). Lines 2 and 4 define the FOR loop. Line 2 defines a loop index variable (called Index in this case), followed by an assignment symbol ( : ). This means the Index variable will take on the values from 1 to 1000 and BY 1 means it will count by 1. The lines between 2 and 4 are what will be executed with each value of Index . If you were to watch the runtime execute this logic, what you8217d see is (roughly): Set Index to 1 Execute line 3 Set Index to 2 Execute line 3 Set Index to 3 Execute line 3 Set Index to 4 Execute line 3 Set Index to 5 Execute line 3 8230 Set Index to 999 Execute line 3 Set Index to 1000 Execute line 3 As you can see, loops can have a significant impact on scan time, especially as the number of iterations becomes high. If you8217re running TwinCAT 3 on a modern PC, then 1000 iterations isn8217t too bad, but executing a million iterations on a 2 GHz PC is likely going to take a minimum of 0.5 milliseconds, and that8217s without doing anything in the loop. You have to be aware of this and program accordingly. If you8217re averaging the list of the last 100 sensor readings, don8217t even worry about it, but if you8217re doing math-heavy computation on thousands of data points, be aware that it might be too much work to do in one scan time. Line 5 takes the Sum and divides by 1000, assigning the result to the return value of the function. Note that I added a decimal point to the value 1000.0 and I did this to remind the reader that I8217m dealing with floating point numbers here. This is a style choice. You don8217t have to do it. The WHILE Loop (and IF/THEN/ELSE Blocks) Another type of loop is the WHILE loop. Instead of executing a fixed number of times like a FOR loop, it can execute as long as some condition is true. For instance, let8217s say we want to find the first index in an array where the value is greater than some value: The purpose of this function is to search an array of 1000 values and return the first index where the value is greater than some Threshold. If it doesn8217t find any values greater than the Threshold then it returns 0, which is an invalid index. Line 1 initializes a boolean flag, Found. to FALSE. Since this is a function, it8217s not really necessary because the value would be initialized to false every time you call the function, but if this was a function block, then you8217d want to include that line because the value would be retained from call to call. Line 2 initializes the Index variable to the first array index (1). Lines 3 and 9 define the WHILE loop. Lines 4 through 8 will be executed repeatedly as long as the expression in line 3 returns true. As you can see, we loop until either we find it, or the Index passes the upper bound of the array. Lines 4 through 8 comprises an IF/THEN/ELSE block. If the expression in line 4 is true, then it executes line 5. If the expression on line 4 is false, then it executes line 7 instead. To demonstrate how this works, assume the values in the array are 25, 50, 75, 100, 125, etc. Also assume Threshold is 80. We would expect the function to return a value of 4. Here8217s how the function executes: Line 2 sets Index to 1 Line 3 evaluates to true because Found is false and Index is 1 Line 4 evaluates to false (25 is not greater than 80) Line 7 sets Index to 2 Line 3 evaluates to true because Found is false and Index is 2 Line 4 evaluates to false (50 is not greater than 80) Line 7 sets Index to 3 Line 3 evaluates to true because Found is false and Index is 3 Line 4 evaluates to false (75 is not greater than 80) Line 7 sets Index to 4 Line 3 evaluates to true because Found is false and Index is 4 Line 4 evaluates to true (100 is greater than 80) Line 5 sets Found to true Line 3 evaluates to false because Found is true Line 11 evaluates to true Line 12 sets the return value of the function to 4 (because Index has the value 4) While this is a perfectly reasonable function, there are also some problems with it. First of all, the scan time is quite variable. The worst case scan time is when the value isn8217t found, and it returns 0. In that case it iterates through the entire array. In the best case it returns 1. Variable scan times can lead to problems if the worst case is never tested, or if you have a lot of functions like this and there8217s some diabolical case where all of them have to execute the worst case on the same scan, and you exceed your allowable scan time. Secondly, the logic is complex. Some of you might be laughing at me for saying that. If you8217re a PC programmer writing code in C or BASIC then the function above is actually quite simple, yet in PLC programming we have an abnormal emphasis on simplicity. We want logic that is obviously correct when we look at it, and the above function isn8217t obviously correct unless you give it a significant amount of analysis. To analyse it you really have to 8220play computer8221 and walk through at least 2 different scenarios: one where the value is found, and one where it8217s not found. Earlier in this section I talked about expecting electricians to go online with our programs and do troubleshooting. An electrician can understand Ladder Diagram, and with a little bit of work they can probably understand the FOR loop example above, but there are going to be a lot of people who won8217t be able to understand this example of a WHILE loop with IF/THEN/ELSE blocks. If you believe these people don8217t have any business going online with a PLC, then suggest you should change your attitude. Automation is a team sport and we have no room on the team for big egos. Use the simplest logic you possibly can (not the shortest). If the machine you8217re programming has 10 motors, don8217t try to write the motor start/stop logic in Structured Text with a FOR loop. Don8217t even make a function block and re-use it 10 times. Just write 10 different programs in Ladder Diagram and copy the logic. Sure they might share some common logic, like an OkToRunMotors coil that gets set in another program. Remember that these are 10 physically different motors and the conditions for starting and stopping them are likely to change over time. Recognize that and keep the logic separate. On the other hand, Structured Text is the right tool for the event-logging and recipe-handling logic of a program. An electrician logging into the PLC to understand why a motor isn8217t starting isn8217t going to be concerned with the event-logging module. Structured Text is also the right tool for manipulating data, such as a scan received from a barcode scanner or an RFID reader. Complex math is also more easily expressed in Structured Text. Using the right tool for the job means taking more than the problem itself into account. Make sure you take the capabilities of your team and the customer8217s capabilities into account too. Don8217t Loop on an Input A novice programmer will write this: Notice how we8217re looping on an input. An input is a real-world physical input. It only changes when an I/O scan happens. When the runtime executes this logic, it will enter the loop and potentially never exit, and none of the rest of your program will execute again. The machine will appear to freeze, outputs will stay in their last state, and bad things will happen. Simply, if you8217re using an input as the conditional in a WHILE loop, then you don8217t have a good understanding of how the PLC runtime works, and you need to stop and go back to the beginning. Most PLCs work by reading the physical inputs into memory, running the program logic, and copying the new values of the outputs to the actual physical outputs (that8217s a simplification and not true of all PLCs, but it8217s a good model to start with), and then doing it again and again. The amount of time it takes to do all that is your scan time, and we want the scan time to be as short as possible. Causing the program to enter a loop that waits for an input to turn on will essentially stop the program. In some cases it will also prevent the I/O scan from happening, so it8217s impossible for that input to change state again. The machine will freeze forever. Ladder Diagram doesn8217t give you the option of shooting yourself in the foot like this, but Structured Text does. Stay away from infinite loops. Mixing Ladder Diagram and Structured Text I8217ve shown you how you can write programs, functions, and function blocks in Structured Text, but sometimes it8217s nice to add a little Structured Text in the middle of your Ladder Diagram program. It turns out that a program can include something called an Action (which is like a mini local sub-program that you can call from your program) and the Action can be written in a different implementation language than the parent program. To add an Action . right click on an existing (Ladder Diagram) program and choose Add-Action8230 from the context menu. All you have to enter is a Name and choose an Implementation Language . Choose Structured Text. The new Action will show up in the Solution Explorer under your program. The Action has access to all the declarations (inputs, outputs, and local variables) of the parent program or POU. You can call the Action like any other program: just add a block and enter the Action name. String Functions String functions can be used in both Ladder Diagram and Structured Text, but when you start to do complicated string manipulation then I suggest moving into Structured Text because it can be easier to understand. Here are your typical string functions and what they do: LEN(s) 8211 returns the number of characters in string s LEFT(s, n) 8211 returns the n left-most characters from string s, or returns s if n LEN(s) RIGHT(s, n) 8211 returns the n right-most characters from string s, or returns s if n LEN(s) MID(s, n, p) 8211 returns n characters from string s, starting at position p (first character number is 1, not 0) CONCAT(s1, s2) 8211 returns strings s1 and s2 joined (concatenated) together INSERT(s1, s2, p) 8211 returns a new string formed by inserting s2 into s1 at position p DELETE(s, n, p) 8211 the opposite of MID. returns string s with the n characters starting at position p removed REPLACE(s1, s2, n, p) 8211 combines DELETE and INSERT 8211 removes n characters from s1 starting at position p, and replaces them with s2 FIND(s1, s2) 8211 returns the position of string s2 in string s1, or 0 if not found, and is case sensitive You can, of course, create your own string functions. For instance, it might be useful to have a different replace function that takes a 3 strings: a string to search, a string to find, a string to replace all instances of the found string with: Notice how the variables are declared as TMaxString instead of STRING 8230 STRING Limitations Since variables are statically allocated in TwinCAT 3, when you define a STRING variable you have to declare the length. Implicitly this is 80 characters, and it uses up 81 bytes of memory (80 for the data and one byte for a null terminator). Strings are limited to a length of 255 characters. There is a specific type called TMaxString which is an alias for STRING(255) . Be careful because TwinCAT 3 will silently truncate a string to the maximum defined length of the destination string when you do an assignment. When you make your own string functions, you should use TMaxString as the variable type to make sure they work with any string passed to them. If you don8217t, the input and output variables will be silently truncated to the length you specify. Conclusions Structured Text is a powerful tool. In some PLCs, like the Allen-Bradley ControlLogix line, you have to pay extra for the Structured Text editor, but with TwinCAT 3 you get it for free. (Actually, you get the Ladder Diagram editor for free too8230) With great power comes great responsibility. Use your new powers wisely and sparingly. When programming PLCs, the first priority is correctness and the second priority is readability. Nobody gets points for writing fewer lines of code. Remember that. Comments are closed.

No comments:

Post a Comment