Model-Based Design of Production Machines – Part 3: Automatic Code Generation for PLCs - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 32:11
Loaded: 0.51%
Stream Type LIVE
Remaining Time 32:11
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 32:11

    Model-Based Design of Production Machines – Part 3: Automatic Code Generation for PLCs

    From the series: Model-Based Design of Production Machines

    Overview

    From food packaging to metal cutting and injection molding, leading industrial equipment and machinery OEMs use Model-Based Design with MATLAB® and Simulink® to address the increasing complexity of their products.

    In the 3rd part of this webinar series, you will learn how to deploy your algorithm on a PLC using automatic code generation.

    Highlights

    • Automatic code generation targeting a Siemens PLC
    • Integration of the generated code into the TIA Portal

    About the Presenter

    Conrado Ramirez Garcia is an application engineer at MathWorks. He specialized in supporting customers of different areas including machine builders, industrial robotics, wind turbines, automation, and energy production. His focus area includes the code generation for embedded devices and industrial controllers.

    Before joining MathWorks, he worked for 5 years at GE Aviation and GE Power as a Controls Engineer. Conrado holds a master’s degree on Power Engineering from the Technische Universität München, where he specialized on Control Systems, Energy Systems and Power Electronics.

    Recorded: 10 Oct 2023

    Hello, and welcome to the Model-Based Design of Production Machines webinar series. This is the third part of a four-part series where we are going to talk about code generation for PLCs. My name is Raresh Curatu, and I'm an industry manager at MathWorks. Joining us today is my colleague Conrado Ramirez Garcia from the Application Engineering Department who will guide you through the technical part.

    Thank you, Raresh. My name is Conrado Ramirez Garcia. I'm an application engineer at MathWorks and I focus on the topics for code generation for embedded devices and PLCs, and in this specific webinar I will be talking about Simulink PLC Coder and how you can deploy your models into a PLC.

    As a brief recap, here is a system overview of the example that we are using throughout the webinar series. This model has five components that form the assembly line. The first robot is a Comau Racer V3. This robot picks up the cups and puts them in the shuttles. The second robot is a Mitsubishi RV-4F. The camera sensor in this robot's work cell detects the balls and then this robot picks up the balls and places each of them in a separate cup.

    The shuttle track moves four shuttles to robot 1, then to robot 2, and then to the conveyor belt before returning to robot 1. The conveyor belt carries the cups away from the track and into a container. The remaining non-moving model parts comprise the static machine frame which serves as the base for the assembly. It includes two work cells housing the robots as well as the base for the center assembly.

    In this webinar series, we have started with modeling and desktop simulation and we are moving on to using hardware. We are following the typical steps taken when developing machinery software using model-based design. After simulating the plant in the desktop environment, designing and simulating the controller, and visualizing the simulations, we are showing you the steps that you need to take in order to deploy the control algorithm on a Siemens PLC using Simulink PLC Coder and the simatic target for Simulink. This way, the control algorithm that you have been developing in Simulink will be equivalent to the code running on the PLC.

    To continue the validation and verification of the software on the PLC, we will next deploy the simulation model of the plant on a real-time computer using Simulink Real-Time-- this in a hardware-in-the-loop configuration. The real-time simulation will then communicate with the PLC through PROFINET and also with a 3D visualization of the simulation.

    On the screen you now see the agenda for the entire webinar series and how the content is distributed throughout the series. In part 1 we have talked about how to import CAD models into MATLAB to then later use them within Simulink. In part 2 you have seen how to design supervisory logic for your PLCs using state flow. These two sessions have already been recorded and will be made available for you.

    Today in part 3, we will talk about how to bring this logic to any PLC or industrial controller, and in part 4 we will round up the design process by testing the PLC in a hardware-in-the-loop simulation. During the presentation, if you have any questions, please enter them in the Q&A section. We will gladly answer them at the end of the presentation. Now I would like to hand it over to Conrado for the technical content.

    This is the agenda for today's webinar. In the first part I will be talking about the key features of the Simulink PLC Coder and the code generation workflow. The second part is about the code generation itself. That means what you need to consider in your Simulink model for exporting your algorithms into structured text.

    The third part is about testbench generation. That means how you can make sure that the generated code is doing exactly the same as the model. So this is a way to verify that the generated code is actually working properly. And last but not least is how you can use these algorithms or these generated code in TIA portals.

    So we're going to import that in TIA portal and see how we can deploy it. So first let me start showing you very quickly the Simulink model that we will be using for this example. As you saw in the previous webinars, my colleague was telling you about Simulink 3D animation and visualization stuff. So now in this part, I will be talking about the code generation for the actor motion control.

    So this is the block that we are interested in, and this block contains different substates. So this is a state machine. This containing different substates for controlling the robot number 1, number 2 shuttles that are moving the particles around the simulation. And this is a quite complex logic. It's difficult to do it by hand, so that's why we profit from the code generation so that you can automate this process.

    For doing that-- so we need to look for the PLC Coder and click the Generate PLC Code button. So I already generated the code, so I am just opening the report and I am showing you the files that we get. So what we get is an SEL file. This SEL file is the one that you need to import it in TIA portal and deploy the algorithm there. So I will explain that in the fourth section-- so how you can import this code there. But let me continue with the slides where we left.

    So with PLC Coder, as I mentioned before, you can deploy it or you can automatically generate the code from your algorithm. And there are already some customers that are using these workflows. One example is ENGEL that use MATLAB and Simulink in a model-based design workflow for developing injection modeling and machine controllers. That means doing the whole control design and also planned modeling in Simulink, and at the end using PLC Coder for deploying the algorithms in structured text. So there are plenty of examples of customers using our tools. You can just visit our links if you want to know more.

    So this is the integration workflow. That means typically we start from a Simulink model that we want deployed. What we do is with the PLC Coder, we generate structured text. And this structured text needs to be imported into this development environment, and this code will be compiled there. And once compiled, you can deploy it in the PLC of your choice.

    So the key features that the PLC Coder offers are it obviously supports Simulink blocks. That means blocks that are typically used in control design, gain blocks, PID blocks, mathematical operations, but it also allows you to use Stateflow. That means if you are using state machines, this is also supported with the structured text of a PLC Coder and for sure MATLAB. So if you are using MATLAB functions, those are as well supported.

    The main value of using the PLC Coder is that it gives you the freedom to choose freely between different vendors. That means once you have designed your algorithm, you can deploy it to any vendor that you want-- so any different PLC because at the end you have one single model that contains your know-how or your IP.

    Also the coder offers you some optimizations that you can use for optimizing your code. As I mentioned before, you can also create a testbench for validating the test and model-to-code traceability. That is also really important for making sure that the code is implemented and also to understand it better.

    So in this table I'm showing you what are the vendors that we support out of the box. If you don't find your vendor in this list, you can reach out to us. We'll provide support. Or we also provide with the PLC Coder XML support. That means you can also generate code for other platforms as well.

    In Simulink you can define-- let's say this is your design and you want to validate or be able to tune parameters at a later stage. What you can do is define some Simulink parameters, and in there, modifying the storage classes. That means you can say that different parameters should be declared as a local variable, as a global variable, or as a constant. You have this freedom to integrate and how the code looks like. You can also influence it.

    If you are using complex structures, in this case bus objects that are hierarchically arranged, you have different elements on that. You can generate code out of that-- so using the PLC Coder, and what you get at the end is a structure containing all the elements the same way as you have in your simulation as well. We can also support alias data types. That means if you are using a certain data type that is based in a base type, you can also generate code from that, and those are supported as well.

    You can also-- before even generate code or you can prove that your model is compliant against different checks-- so something like industry standards, check that the blocks have the right settings, and the model have the right configuration. So that will give you a hint-- so how you can improve the quality of your models. And if you want also to define your own checks, you can also do it using Simulink checks-- so to create different checks across your organization, you can also do that.

    As I mentioned before, traceability. This is possible so that you can, once you generate code-- so from one single block you can navigate to the specific line of code where this was implemented, and also the other way around. So once you generated the code, there are some hyperlinks that if you click there, they are bringing you to the model where this was implemented. So those are quite useful in the debugging phase.

    Also for understanding your models, you can export them into a web view. That means if all their colleagues don't have a Simulink license or are not using Simulink but you still want to show the schematics, you can profit by that using the web view.

    Regarding optimizations, there are certain options that you can profit from. For instance, if you have an array you can define what is the maximum threshold before it is generating a FOR loop. So in this example what we have is an array of 10 elements and we are on the right-hand side, I am defining this threshold to be 5. That means that if it's bigger than five elements, it's giving me a FOR loop as you can see here on the right-hand side. And on the left-hand side, I modify this value to be something bigger than 10 and we are getting all the elements or the operations done one after another. So those are some of the optimizations that you can do in the PLC Coder.

    There are also protections for overflow embedded-- the PLC Coder provides that. So if you click this option, it is generating on the left-hand side overflow protection code. So that means this is proofing, is checking that the values are not exceeding the maximum allowed values for this specific data type, and it's giving some protections. Once this is disabled as you see on the right-hand side, you see all these lines of code. And this is just-- the operation is happening there.

    It's also possible to define signal storage reuse. That means that by clicking this option, what you get is on the left-hand side is a mode-optimized code. That means in line 38 you are getting three different operations in one single line while, on the right-hand hand side without this optimization, we are getting more lines of code and also more internal variables which are easier to debug and also to understand the code for readability. But it's giving you more operations at the end. So it's up to you how you can play with those optimizations.

    Regarding optimization as well-- so we can, if you are using MATLAB functions and you are using nested functions for instance, it's possible to inline all of them. That means that MATLAB is looking-- or the PLC Coder is looking how everything could be optimized into one single function. And if this option is disabled, you are getting three different functions and Yeah this is creating at the end more functions. It's up to how you want to do your design.

    Also something important is if you are using temporal logic, that means-- let's imagine that we have a state flow diagram which is going from one state to another after three seconds or four seconds-- those transitions after some time. With the PLC Coder it is possible that you define TON functions that are specific to the development environment that you are working on. Or you can keep it generic saying-- just a generic counter, and what it does is just generating a counter variable for doing these transitions.

    In the case that you are using global variables that are already in your program and you plan to use them in Simulink, but now you want to generate code out of that and you don't want to do the declaration again from these global variables, what you can do is using identifiers-- that means with identifiers we let the coder know that those variables no longer need an initialization or declaration. That means that the declaration is omitted. And those are used normally in the generated code.

    I also have an example. So let's imagine that there is a function block that you already have implemented. It's called Magnitude, and you want to include this block into a Simulink model. So once you generate code you for sure don't want to do the implementation again of this function, so using identifiers, the implementation of this function is not generated. But what we see is that we are getting still the instantiation, or creating a new instance, and also this instance is used in the generated code.

    Now we come to the part of the testbench diagnostics, or testbench creation. So there is one option that generate test bench for the subsystem and also include a testbench diagnostics. What is giving you is, it's creating a whole program in that case, and this program is-- it contains the inputs from the simulation. And in the generated code the program is using this log inputs from the simulation and is doing a comparison from this output versus the values from the simulation. I can explain that in more detail in the third part.

    So now I'm going to show you the code generation itself. So for that I'm going to switch back to Simulink again. So this is the model that we were using, as we saw before. So it is a quite complex model that we need to generate, or we need to export into structured text. So from this subsystem, in order to be able to use the PLC Coder we need to go in the block parameters and define it as an atomic unit.

    So with atomic unit, you make sure that Simulink is treating that as a separate subsystem, I would say, that is not optimized with the rest of the model. So for that it is by itself. And once it's there, you will be able to find under Settings-- you will be able to find a PLC code generation option. So in there, so what you need to consider is under Target ID, you can look for a specific platform you are working with. And if you don't find it here, you can also use PLC Open XML as a generic format for exported in other platforms.

    For this specific case I will be using TIA portal, so that's why I select that. As we saw before-- so under optimizations I am trying to profit from all the optimizations possible. So signal storage reuse. Remove also floating point integer conversion-- so in the case that there are some. I am also using the variables as inlined, and for the report I can create a traceability report. I can also create a model web view. I'm not doing that, so I will just create a traceability report and that's it.

    So once I do that, I can generate the code. So as I mentioned before, I generated the code already so I will just open the report and explain that a little bit more. So in this traceability section, we get a compilation of all the objects and also the links that are present there in the design. So this is just a very long list with all the links to the specific objects and the parts of the code where this is implemented in Code Metrics. This is also important.

    We see the name of the file that we have is called nameofmymodel.sel, and we get here the function block information. So what we are getting is just a function of the subsystem that I am using, and we see what are the stack size, how many lines are used, how many inputs, how many amp outputs, how many local variables. Everything you need to know is in this overview.

    In the generated files itself, we find the declaration of my function block. And this is called state_machine-- so the name of my subsystem. And there is a declaration of all inputs and outputs, as well as internal variables. So for my example I am using an array of 700 elements, so that's what we see here. So those values are initialized. And then on line 791, we see the method type.

    That means that for that-- so you need to develop or you need to include in your model-- or you include that in TIA portal or in the platform that you're working on, use 0 if you are initializing this or use 1 when it is running in steady state, so as in a normal operation. So this is also something that you need to consider. And besides that, this is the code that you get.

    As I mentioned before, there are some links that are providing you or enabling you to understand better the code. So if we want to see what exactly means this transition, we can click on that and it's bringing me back to the specific part of main design where this is happening. So this also works the other way around. If I want to look into a specific transition, I can do right-click on that PLC code and navigate to the code where this is implemented. So it's telling me this transition is happening exactly in this line.

    And if I navigate to the bottom of this report of my file I get-- so at the end of the function, and that's everything that it's containing. So this is the file that I imported in TIA portal for doing a real-time simulation. This will be in part 4. But anyway, so I'm just giving you an insight about that.

    So I will go back to the slides and I will continue with next part of the agenda, which is testbench generation. So for that I have an example in which I have the same model, and in that-- so under the settings the first thing that I need to do is to select Generate testbench and include testbench diagnostics. So once that I hit the Generate PLC Code button, what is happening in the background before even generating code-- it's running the simulation. Everything at once.

    That means that the simulation is running now, and what Simulink is doing is logging all the input values that are coming to my subsystem and also logging all of the outputs that this system is producing. So that's why you see that the simulation is running now. I did a speed-up because the simulation is taking a hundred seconds for simulation.

    So once the simulation is done, now it starts the code generation process. So the code generation process started, and that's what we get. So we get again the same report as before. Again with Metrics, it's generating exactly the same function as before, but now there is something different in the SEL file. So we see that there is also in line 22 the declaration of my function. So this is still right. So this is, as before, the same.

    But if we scroll a little bit, I need to go down until the end of the function declaration. So it is right there. So in that point, this is the end of the function that we had before, but what is happening now is also generating another function. So this function is called testbench, and this function is declaring all of the logged outputs that we have. You can also see here-- so the array length is 1,000.

    So this is because we are using hundred milliseconds execution rate and the simulation is run in just hundred seconds. So that's why we get this rate of thousand. So that means the thousand values in the simulation are logged, are stored, and those values will be used for my program. So I'm scrolling a little bit, and what you see here is the first input. So it's actually hard-coding all of the values in the simulation for input number 1, 2, 3, and 4, and all of them the same as outputs.

    So if I scroll a little bit further, we come to the part where the program is actually doing these comparisons. So it's a little bit up. There you go. So it's here. So in this part what we have is, again-- so this is verifying in all these hundred elements-- so that means in these thousand steps it's doing a validation or initializing all of these values with the previously logged inputs.

    And in this part it's doing an instantiation. So this is for the initialization, so that means that here the method type needs to be 0 for doing all the initialization. And when this is running on steady state it needs to be 1 so that it represents the normal operation mode, which is called also step function. So that's what I am also showing here.

    So this with number 1, and you can see here also in that part that all variables are being reassignated, so that when we run the instance of this model, we can do this comparison and see if there are some errors on the outputs. So as you can see-- so in this part the variables that were stored in Simulink versus the one that are produced in the model. So once the code is executed, those are compared against a certain threshold to see if there is some kind of discrepancies.

    So for verifying that there is a variable called Test Verify, and in the case that there is some discrepancies bigger than that, this value is changing from true to false. And when you run the test, this will be an indicator to see if your test failed. So this is the way how you can validate that the code is actually working the same way as it was in the simulation.

    So this also doing the comparison for each of the variables. So for output number 1, output number 2, number 3, 4-- all of them are compared. And if one of them wants-- a single value or variable is having some discrepancies, this will change the whole output to false. So this is the end of the function. And then at the end, you see here that it's creating a testbench instantiation for that. So that means it's creating a program for that. So this is the one that I'm going to export in TIA portal, and I'm going to show you how this works. So I will show you that in a minute.

    Now that we generated our testbench, the next step is to know how to run it on the PLC in TIA portal. So that's why I have here with me an open controller from Siemens, and I'm going to show you a live demonstration how this is actually done. So I'm switch back to TIA portal, and the first thing you need to do is to navigate to External Sources and in there find the SEL file that was the output of the PLC Coder.

    So once this is done, right-click on that and generate block from source. I already did that, and what I got was of course the name of my state machine. So that was the function that I originally had. And as well I got a testbench. So this testbench is including, as I mentioned before, all the inputs and outputs of the simulation. So by drag and drop I just bring it here into a cyclic interrupt. And I already compiled the model here, so what I do is I'm just going to execute. It and I will go online and start the PLC.

    So what we see here is once I hit Run, the PLC is checking, does this test variable is not changing to false? That means that as long as everything is OK and between range, and the code is behaving the same as in the model, we are getting this value true.

    What we also see here is that test cycle number. So that means how many iterations are happening. So as I mentioned before, that was a hundred seconds that I simulated with a hundred-milliseconds sample rate. That means we are getting thousand iterations and this counter will go until thousand. And in case that this variable is set to false, we see here in Test Variable Name what is the variable that is causing the issues.

    So let's wait until this goes until thousand. I am going to speed it up a little bit on the video. So we are at the end, and after these thousand iterations the value remained as true. That means that we are getting the same results as in the simulation, and that's the way to verify it. So that's the way how we can make sure that our design-- in this case, our state machine is behaving the same and we're getting a fully functional code that we can trust and we can run on a real hardware. So that's all from this webinar series number 3. Now I will hand over to Raresh. And thanks for your attention

    Thank you, Conrado. Great example of model-based design targeting industrial controllers. Thank you. This is the end of today's presentation, But before we end today's session we will be answering your questions. So if you haven't already, please type your questions in the Q&A section. Don't forget to register and attend the next webinar. The next webinar will take place in two weeks from now, and it will also be the last one of the series. The recordings of the webinars that you have been attending will be made available to you.

    If you plan to attend Forum Industria Digitale in Italy, SPS in Germany, or the Precision Fair in the Netherlands this November, we would be happy to meet you there in person and talk about your projects. In the meanwhile, if you want to take a look at what else industrial automation and machinery engineers are using MATLAB and Simulink for, please visit mathworks.com/industrial-automation-machinery. Thank you for your attention and for your interest. And now let's take a look at the Q&A section.

    Related Products