Temperature Controller

Description

The following example extends the basics, shown in example "TemperatureController_minimal" by the following subjects:

Overview of project directory

TE1400Sample_TemperatureController contains all the files for this example:

TctSmplTempCtrl.mdl

More advanced (but still very simple) temperature controller.
Temperature Controller 1:

TctSmplCtrlSysPT2.mdl

Simple PT2 model for the controlled system.

TctSmplClosedLoopCtrl.mdl

Model of a closed control loop, which was implemented through referencing of the controller models and the controlled system.

TctSmplTempCtrlParameters.mat

Contains all the necessary model parameters.

TctSmplTempCtrlBusObjects.mat

Contains all the required Simulink BusObjects (structure definitions).

_PrecompiledTcComModules

This subdirectory contains readily compiled TwinCAT modules that were generated from the enclosed Simulink models. They enable the integration of a module in TwinCAT to be tested, without having to generate the module first. They can be used in situations where a MATLAB license is not yet available, for example. A quick reference guide for module installation on the development PC is also enclosed.

Info: To start the module on an x64 target system, the system must be switched to test mode!

_PreviousSimulinkVersions

The MDL files described above are stored in the file format of the current Simulink version. This subdirectory contains the models in the file format of elder Simulink versions.

Parameter access

TctSmplTempCtrl.mdl has no embedded parameter values (inline parameters), i.e. the parameter values are stored in the corresponding model parameter structure. In addition, under the tab TCT Advanced of the coder settings, the module generator is configured such that ADS access to the parameters and generation of ADS symbols is allowed. ADS access is then possible from TwinCAT Scope View or other ADS clients. The Block diagram tab in TwinCAT XAE is an ADS client. Access to its parameter depends on these settings.
Temperature Controller 2:

If the option Inline parameters is activated without further configurations, all parameter values in the generated module codes are fixed. The Configure... button next to Inline parameters can be used to open a configurator, in which you can select the variables of the MATLAB working area that are to remain configurable in the generated module:

Temperature Controller 3:

In the sample shown, only the workspace variables Kp and Tn remain configurable, which means that only the Simlulink block parameters that depend on these parameters are configurable. The parameter structure is reduced to these two elements.

For further information on parameter inlining, see
Simulink documentation.

Using bus objects

Simulink BusObjects enable access to TwinCAT modules generated in Simulink via structured symbols. This sample contains a predefined BusObject called MonitoringSignalsType. It is an output structure, i.e. it assigns the signals it contains to a PLC module.
The configuration of a BusObject is started by double-clicking on the BusCreator block. To start the Bus Editor, click the Edit button in the Welcome screen, as shown in the figure below. Further information on using BusObjects can be found in the Simulink documentation.

Temperature Controller 4:

During instantiation of the generated module in a TwinCAT project, the specified BusObject is imported into the TwinCAT project as a global TwinCAT data type. This data type is used by the generated module itself for displaying the output structure, although it can also be used by other modules, such as a PLC, for example, which are linked to this output structure.
Temperature Controller 5:

Using test points

In Simulink you can specify test points on signals for monitoring by Simulink "Floating Scope", for example. If the TwinCAT Target module generator is used, signals with such test points are invariably declared as member variable for the generated TwinCAT module. This enables ADS access to the signal. For more information on test points, see Simulink documentation.
In this sample, the Model Explorer is used to define a test point for the control difference e:

Temperature Controller 6:


To enable ADS access, enable internal block output in the code settings under the TCT Advanced tab:

Temperature Controller 7:


In this way you can use TwinCAT Scope View to access the signal with test points and some other block output variables when the generated TwinCAT module is executed.

Temperature Controller 8:

Using referenced models

Open the model TctSmplClosedLoopCtrl.mdl, which contains two model references. Referenced models are the temperature controller described above and a simple P-T2 model of a temperature control system.

Temperature Controller 9:

Such model referencing has several advantages, both in general and in combination with TwinCAT Target. Two basic options for structured modelling and, particularly in this example, for controller design are:

Simulation for optimizing the controller:

Optimization of the controller design based on simulation of the control loop with MATLAB/Simulink, followed by transfer of the optimized controller into the real-time environment of TwinCAT 3. Thanks to the use of standard Simulink input and output blocks for the definition of the TwinCAT module process images, no changes in the controller model are required before module generation commences.

Reuse and faster creation of models:

A model can be referenced several times in one or several higher-level models. In this way, the models can be divided into reusable functional units, similar to text programming languages, where the code is structured into functions or methods. This improves the readability of complex models.
The generated code of referenced models is compiled into static libraries, which are only updated if the referenced model was modified since the last code generation. This can speed up the development of complex models, if parts that are only rarely modified are stored in referenced models.
In this example, model generation can be started for a control loop model, and a real-time control loop simulation can be executed in the TwinCAT runtime.

Note on licenses:

The control loop model of this example can only be compiled into a TwinCAT module with a valid TwinCAT Target license (TE1400). Otherwise, this model exceeds the limits for unlicensed models.

Using external mode

The temperature controller model TctSmplTempCtrl.mdl has been preconfigured so that ExternalMode connections are permitted:

Temperature Controller 10:

Because of these configurations, you can use the Connect to Target icon in the Simulink toolbar to establish a connection with the generated temperature controller via ExternalMode. The module must have been previously generated and started on a TwinCAT system and an ADS route must have been configured between your development system and the corresponding target system. A number of dialogs are displayed to help you navigate to the desired module instance.

Temperature Controller 11:

You can now use the Scope block in Simulink to monitor the real-time signals of the generated and now connected TwinCAT module. You can also change the value of the Internal Setpoint block, for example. As soon as the parameter change is confirmed, it is downloaded directly to the target module. This is only possible for adjustable parameters if the model parameters are not inlined (see "Parameter access").

Generating TwinCAT modules from subsystems

Creating a TwinCAT module in a Simulink subsystem, instead of the entire model, via the subsystem context menu:

Temperature Controller 12: