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. |
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.
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:
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.
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.
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:
To enable ADS access, enable internal block output in the code settings under the TCT Advanced tab:
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.
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.
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. |
Using external mode
The temperature controller model TctSmplTempCtrl.mdl has been preconfigured so that ExternalMode connections are permitted:
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.
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: