Instantiating and initiating temperature control

For certain applications, only the temperature control should be used from the scope of the TwinCAT 3 Plastic Application. In this, but also in the normal use case, the temperature controller must be capable of being instantiated and initiated individually for this purpose. In the following sample, we will go through this process step by step.

1. Create a new task with a cycle time of 25 ms. This is not mandatory, but - based on experience - recommended.
Instantiating and initiating temperature control 1:
2. Create an instance of FB_Temperature and FB_TemperatureHmi and assign the mapping to the newly created task.
{attribute “qualified_only”}
VAR_GLOBAL
    {attribute “TcHmiSymbol.Hide”}
    {attribute “TcContextName”:=”TempTask”}
    fbTemperature:            FB_Temperature;

    fbTemperatureHmi:         FB_ TemperatureHmi;
END_VAR
3. Initialize the temperature classes. The following options are available for this:
VAR_GLOBAL
    fbRuntime:            FB_BaseRuntime;
END_VAR



// Program with its task call referenced to PlcTask
PROGRAM PlcMain
// Build list of classes that should be called cyclically by the runtime
_Build();             
// Standard call of Cyclic
fbRuntime.Cyclic();   



// Program with its task call referenced to TempTask
PROGRAM TempMain
// Slow call for Temperature
fbRuntime.TemperatureCyclic()



// Method to define the runtime objects
METHOD _Build
VAR_INST
    bBuild:    BOOL;    // Latch the Build only gets executed once
END_VAR


IF NOT bBuild THEN
bBuild := TRUE;

    // Append temperature classes to be initialized and called by the runtime
    fbRuntime.Append(fbTemperature, fbTemperatureHmi);
    // Further objects to append
    fbRuntime.Append(fbOtherObject, fbOtherObjectHmi);

END_IF
VAR
    bInit:        BOOL;
    bInitFailed:  BOOL;
    bParamInit:   BOOL;
    hr:           HRESULT;
END_VAR



IF NOT bInit AND NOT bInitFailed THEN
    hr := fbTempCtrl.SetHMI(fbTempCtrlHmi);
    hr := fbTempCtrl.Init();
    IF SUCCEEDED(hr) THEN
        bInit := TRUE;
    ELSE
        bInitFailed := TRUE;
    END_IF
ELSIF bInit THEN
    IF NOT bParamInit THEN
        hr := fbTempCtrl.ParamInit();
        IF SUCCEEDED(hr) THEN
            bParamInit := TRUE;
        END_IF
    END_IF
    
    fbTempCtrl.Cyclic();
END_IF