HOW TO USE MATLAB/SIMULINK FOR ST CODE GENERATION

3y ago
68 Views
4 Downloads
633.65 KB
13 Pages
Last View : 11d ago
Last Download : 3m ago
Upload by : Isobel Thacker
Transcription

—AP P L I CATI O N NOT EAC500 WITH SIMULINK PLC CODERHOW TO USE MATLAB/SIMULINK FOR STCODE GENERATION

Contents1 Introduction . 31.1Scope of the document .31.2Compatibility .31.3Overview .32 Matlab, Simulink and PLC Coder . 42.1Model creation and simulation . 42.2PLC Code Generation .53 Automation Builder and CoDeSys . 83.1Code Import and Testbench . 83.2Compare Automation Builder & Simulink . 94 Hints and possible issues . 114.1Data Types . 114.2Errors in PLC Code Generation . 114.3Test Bench reports a difference . 124.4Timings between Automation Builder and Simulink are different . 1223ADR010568, 1, en US

AC5 00 WITH S IMU LINK P LC COD ER1Introduction1.1Scope of the documentThis document shows how MATLAB/Simulink with the Simulink PLC Coder can be used for automated ST code generation for AC500 V2 and AC500 V3 PLCs.1.2CompatibilityThe application example explained in this document have been used with the below engineering system versions. They should also work with other versions, nevertheless some small adaptations may be necessary, for future versions.1.3 Automation Builder 2.2.5 or other Matlab R2020a or other MathWork Licenses for at least MATLAB, Simulink and PLC CoderOverviewModel DesignPLC Code implementationModel SimulationTestbench verificationPLC Code generationCompare between Simulation and real SystemAs shown above MATLAB/Simulink is used for a Model design, simulation and optimization. Ifthe model is working as expected the PLC Code in Structured Text can be generated with theSimulink PLC Coder. The generated code can be imported and implemented in an AC500 PLC.To verify the code a testbench is included. Afterwards the PLC can be used to control the realapplication. Differences between the model and the real application are visible and can beused to optimize the system model and correct the system behavior.3ADR010568, 1, en US3

2Matlab, Simulink and PLC CoderThis chapter deals with the model design, simulation and PLC Code generation.NOTICE2.1The document is not describing the MATLAB and Simulink Basics. Elementaryknowledge is required to follow this descriptionModel creation and simulationAs base for this example a closed loop system is used.To discuss and simulate the general example above a car cruise control is used.With the Ramp Block Reference Speed, the user specifies the speed the car should have. Inthe first step the current velocity (feedback) is compared to the desired velocity (input). Thespeed difference is given to the controller. In this example a PID controller is used, which shallbe realized by a PLC.The controller calculates his output values. The output is the percentage how much energythe car engine gets. This value is inputted as Signal to the Car block. The car speed is not onlyinfluenced by the gas but also by the load or the air resistance. These factors are part of theCar model itself and inside the subsystem. External Forces like wind or gradient are summarized as Disturbance Forces. Here nothing is connected. The process itself has the velocity asoutput which is given back to compare. To simulate and test the model some more logic isadded.11Cf. Shreesh Mysore, Richard Murray http://www.cds.caltech.edu/ murray/amwiki/Cruise control (18.05.2020)43ADR010568, 1, en US

AC5 00 WITH S IMU LINK P LC COD ERAs Disturbance Force a hill can be used. To compare the Setpoint and the real velocity a scopeis added, where both signals are inputted. As the velocity is in m/s in the model it is multiplied by 3.6 to get the velocity in km/h.For the model testing a small gradient starts after 5 seconds. After 20 seconds the setpointis increased. Depending on the PID factors in the PLC Controller the behavior of the acceleration process can be changed. The following three little pictures are showing the responsefrom not optimized P, PI and PD controllers.In the simulation the PID Filter can be optimized and the robustness can be checked with thedisturbance force. As visible in the picture below the setpoint changes from 72 km/h to108 km/h. the cruise control accelerates to follow this new speed.As the model in Simulink is now working as it should it can be transferred into PLC Code forthe AC500. As the PLC has a defined cycle but Simulink can work with continues time signalsonly discrete blocks allowed in the subsystem for PLC Code generation. In the next chapterthe steps for PLC Code generation are described.2.2PLC Code GenerationThis chapter describes how ST code for AC500 V2 and AC500 V3 can be created with the Simulink PLC Coder.The Simulink Subsystem PLC PID from the model described in the chapter above shall betransferred into PLC Code. Therefor no continuous block must be used. In case a block has tobe discretized the Model Discretizer which can be found in the Apps can be used.As the application is depending on the PLC cycle time the sample time of the discrete blockscan be set.A subsystem which can be used for code generation has to be an atomic unit. Therefore,open the Subsystem Parameters and select the checkbox Treat as atomic unit as shown inthe picture below.3ADR010568, 1, en US5

After the steps above the subsystem is ready for code generation. By right clicking: PLC Code,Check Subsystem Compatibility the user can check if there are any issues in the subsystemwhich prevent the code generation.The next step is to define the options for PLC Code generation.Depending on the used PLC the Target IDE can be selected. For AC500 V2 PLCs 3S CoDeSys2.3 has to be selected. For AC500 V3 PLCs 3S CoDeSys 3.5 or PLC Open XML has to be selected. In this application note the code generation for a V3 PLC is described. The steps for aV2 are nearly the same. The differences are short commented in this note.63ADR010568, 1, en US

AC5 00 WITH S IMU LINK P LC COD ERThe Code Output Directory is the path where the created PLC Code is saved.Generate testbench for subsystem and Include testbench diagnostic code can be used to create a testbench. Simulink creates an array with input variables for the function block and anarray with the output variables. The testbench loops through all input variables and compares the calculated PLC output with the output from the Simulink simulation. The user cancheck if the behavior of this function block is different in the PLC and the simulation.After defining the options, the PLC Code can be generated. Therefor right click on the Subsystem again, select PLC Code and Generate Code for Subsystem. This might take severalminutes.A report for the code generation is created in addition to the PLC Code. The progress can befollowed in the Diagnostic Viewer.3ADR010568, 1, en US7

3Automation Builder and CoDeSys3.1Code Import and TestbenchIn Automation Builder the generated code can be imported. To import the code, select theApplication and navigate Project – Import – PLC open XML to import the generated code. TheGlobal Variable List GVL CONSTS and GVL VARS as well as the POUs MainTB, PLC PID andTestBench are added to the project.To run the Testbench the MainTB has to be called by a task or inside the PLC PRG. Pleasemake sure, that the task which is calling this function block has the corresponding cycle timeto sample distance in Simulink. Here a sample distance of 0.01 s was configured so a 10 mscyclic task is used.83ADR010568, 1, en US

AC5 00 WITH S IMU LINK P LC COD ERWhen changing to run the testbench is executed. As visible from the screenshot below the instance from TestBench stepped through the 6000 array entries. As the variable testVerify isstill true the test was successful.3.2Compare Automation Builder & SimulinkIn the last chapter was shown that the output from the PLC PID block is equal in Simulinkand Automation Builder.In Simulink the set Velocity is compared to the real velocity. To have the same layout in Automation Builder the Car block in Simulink is also converted into ST Code and the closed loopsystem is rebuild in CFCSimilar to the Scope in Simulink the Trace can be used to watch and compare the two velocitysignals. The two pictures below show the compare between the trace in the AC500 and theScope in Simulink. As visible the two graphs are identical.3ADR010568, 1, en US9

103ADR010568, 1, en US

AC5 00 WITH S IMU LINK P LC COD ER4Hints and possible issues4.1Data TypesBy default, Simulink sources are using double values. This is corresponding with LREAL Valuesin the AC500. Calculations with floating point values take more CPU time than calculationswith fixed point values especially when using a PM57X, PM58X or an ECO CPU. If possible, useInteger values instead to optimize the execution speed and reduce the CPU Load.The data types are by default inherit. As the sources are double all Signal inputs and outputswill be double.The data type in Simulink has to be defined depending on the data types which shall be usedin CoDeSys. Following table shows the corresponding data types in CoDeSys and Simulink.4.2Errors in PLC Code GenerationThere are many different reasons why a code generation is not possible. At first please makesure that the Simulink model can be simulated by pressing run.The most common issue is using not allowed blocks. For PLC Code generation continuousblocks are not allowed. These blocks need to be replaced by a discrete block. Therefor theModel Discretizer in the section apps can be used.3ADR010568, 1, en US11

4.3Test Bench reports a differenceIf the test bench reports an error, there is a difference between the PLC Code and the Simulink Model. The root case for this difference can for example be differences in calculations,behavior in error cases, Please check if already the calculations in the first cycle fails. This indicates a general difference. To check the difference the step function in Simulink can be used to find the initial values and the values after the first cycle. These values can be compared to the PLC Values tofind the difference.In special calculations cases the two systems might have differences. This can be data typeconversations, integer overflow, dividing by 0, 4.4Timings between Automation Builder and Simulink aredifferentIf the testbench runs successful but the PLC output is faster or slower as the simulation, it isan indication that the plc cycle time is different from the simulation sample time.As visible in the screenshot above the cycle time in Simulink (0.01 s) and in AutomationBuilder (10 ms) are the same.123ADR010568, 1, en US

ABB Automation Products GmbHEppelheimer Straße 8269123 Heidelberg, GermanyPhone: 49 62 21 701 1444Fax: 49 62 21 701 1382E-Mail: plc.support@de.abb.comwww.abb.com/plcWe reserve the right to make technicalchanges or modify the contents of thisdocument without prior notice. With regard to purchase orders, the agreed particulars shall prevail. ABB AG does not accept any responsibility whatsoever forpotential errors or possible lack of information in this document.We reserve all rights in this document andin the subject matter and illustrations contained therein. Any reproduction, disclosure to third parties or utilization of itscontents – in whole or in parts – is forbidden without prior written consent of ABBAG.Copyright 2020 ABB. All rights reserved

3ADR010568, 1, en_US 3 1 Introduction 1.1 Scope of the document This document shows how MATLAB/Simulink with the Simulink PLC Coder can be used for au-tomated ST code generation for AC500 V2 and AC500 V3 PLCs. 1.2 Compatibility The application example explained in this document have been used with the below engineer-ing system versions.

Related Documents:

ES360 Introduction to Controls Engineering MATLAB and SIMULINK Help Page 2 of 6 Starting SIMULINK SIMULINK can be started by: 1) Opening a SIMULINK model file (model files use the .mdl extension). 2) Starting MATLAB and clicking on the icon in the tool bar. The SIMULINK Library Browser SIMULINK

4) Simulink . Simulink is a program for simulating signals and dynamic systems. As an extension of Matlab, Simulink adds many features specific to the simulation of dynamic systems while retaining all of Matlab’s general purpose functionality. Simulink has two phases of use: mode

Test Driven Development powered by MATLAB and Simulink 45 Model-Based Design –Simulink and Stateflow Manage Requirements –Simulink Requirements Author and Execute Tests –Simulink Test Measure Test Completeness –Simulink Coverage Refactor and Verify Compliance –Simulink Check

6. Vision implementation in Matlab/Simulink 16 6.1 Matlab 16 6.1.1 Results 16 6.2 Simulink 17 7. Conclusion 19 8. References 20 Appendix A: Explanation of Matlab functions 21 Appendix B: Matlab code for color definition 22 Appendix C: Matlab code for pixel labeling 23 Appendix D: Matlab code for object recognition 24

Introduction to Simulink Todd Atkins tatkins@mathworks.com. 4 Outline What is Simulink? Working with Simulink. How Simulink works. Continuous and discrete models Componentizing models. 5 Simulink Applications. 6 Simulink

Simulink and LEGO MINDSTORMS EV3 9 P a g e Project 1: Explore Simulink and LEGO MINDSTORMS EV3 P1.1 Get Started: Program EV3 Status Light with Simulink Motivation At the end of this project you will be able to program an EV3 brick from Simulink. Objective Create first model in Simulink Check hardware and software installation

MATLAB and Simulink Automatically generate C and HDL Verify hardware and software implementations against the system and algorithm models C MATLAB and Simulink Algorithm and System Design Real-Time Workshop Embedded Coder, Targets, Links V e r i f y Simulink HDL Coder Link for ModelSim Li

Luigi Biagiotti Systems and Control Theory Introduction to Simulink-- 2 Simulink introduction Simulink (Simulation and Link) is an extension of MATLAB that offers modeling, simulation, and analysis of dynamical systems under a graphical user interface (GUI) environment. Simulink is based on block diagrams of Dynamic SystemsFile Size: 1MB