PLC code generation | Drilling Systems Modeling & Automation, Part 7
From the series: Drilling Systems Modeling & Automation
The control of the drilling machinery, consisting of drawworks, mud pumps and top drive requires a very short response time. The control algorithms are therefore implemented in PLCs, providing a response time in the range of milliseconds. However, manually translating our validated control strategy to PLC code is an arduous task because this must be rewritten in a new language and must reflect the PLC architecture.
Hence, we use automatic code generation which is an integral part of Model-Based Design. This helps eliminate errors associated with traditional hand coding and reduces overall development and validation time. Simulink PLC Coder generates source code in structured text format from the Simulink model and Stateflow chart. In this case, we generate a Structured Text and Ladder Diagram in a format that is compatible with Rockwell Automation’s Studio 5000. Bidirectional links enable us to navigate and trace between Simulink model components and the generated code. Finally, we use Studio 5000 Logix Designer® as an IDE to compile the code and run it on the PLC.
Published: 2 Jul 2020
Hi, and welcome back. My name is Jonathan LeSage, and I'm an application engineer with MathWorks.
In this video, we're going to start to take a look at how we can take a lot of the work that we've already done in that state flowchart that we've created, and actually to look at what we've been working on. Take the state flowchart that we've been working on where we've built kind of an energy limiter and autodriller logic, and take that and actually deploy it outside of the simulation environment.
And the way that we're going to do that is through code generation. We're actually going to take that state flowchart and convert it over to structured text that can be used on a PLC. We could just as easily convert this over to embedded C code and take that to PLCs that support compiled C code, or to other platforms as well.
So let's hop over to Simulink. And I have a model here, the one we've been working with, the autodriller. We have inputs and outputs. And what we want to do is convert this whole chart, including the energy limiter logic here, this kind of mathematical routine. We want to convert all of this over to a structured text.
Now, the way that we can do that, let's go over to our Apps tab here, and then drop it down to look at code generation. So you'll see here's a whole section for code generation. We can go to C code through embedded coder. If we want to go to VHDL or Verilog for FPGA-based targets. We do have an HDL coder tool as well.
But today we're going to be using PLC Coder. Now what PLC Coder is going to do is it's going to convert this chart over to structured text. I'm going to go ahead and go to the Settings. So we can take a look at what the options are here.
Now, one of the important things when you're generating PLC code is to choose the target IDE. I'm going to be targeting Rockwell Automation Studio 5000 today with an add-on instruction. But you'll see that there's numerous other vendors that we directly link to.
Now, if you don't see the vendor that you're working with, keep in mind you can always choose generic structured text. This generates the IEC 61131-3 structured text, which as long as your PLC vendor is compliant with that standard, which most are, you should be good to go.
Now, once we've chosen that, I want to make sure I've got a report selected as well. Open that automatically. And go back to our model.
I can select this, and go ahead and say, Generate PLC Code. This is going to go through the this block diagram here, and convert it over into the structured text, which can be used in the PLC environment.
Once the process is done, we also get this traceability report. I really like these traceability reports, because we can take a look at the actual structured text file here, and then trace back and forth between the code itself. So if I'm curious kind of what this is implementing right here, I can click on these hyperlinks. This will take me to the state chart.
And then I can also dive in here, for example. Maybe I want to see where this block is implemented. In the code itself, I can right-click, and say, navigate to code. And it will take me to that line of structured text code. It gives me kind of a way to kind of go back and forth between the two.
Now, this is structured text that I could directly use in my PLC. This is an add-on instruction. So I'm going to have kind of an-- it's going to appear as a block in my diagram. I can-- basically, I have the inputs, the outputs, just as I've defined in Simulink, and then internal variables.
Now that we've seen how to generate code from this chart, let's see how we can take the generated code and bring it into our PLC development environment. Now, keep in mind whenever you generate code of any type, you're going to get a source folder here, typically in your current folder.
Within that, for the PLC code, you'll notice that we have two files. We have this .st. This is the structured text. This is kind of the raw file.
And then, depending on the target IDE that you select, you're going to-- you potentially will have another file type. In this case, we have a .L5X. This is specific to the Rockwell Automation Tools, to Studio 5000, for example. I also have this HTML folder. This is that report that we generated.
So now let's take a look at how we can actually use this in our PLC project directly. So over here, I have a Studio 5000. This is not a MathWorks tool. This is developed by a PLC vendor.
I have this, basically, ladder diagram here for controlling the drawworks. And now I want to basically add that structured text that I've generated into this chart so I can use that as well. So the way I can do that is go to add-on instructions here. I can right-click. Go ahead and say, Import That.
And you'll see here I've got in my folder here the autoDrillingLogic.L5X. Go ahead and select that. Go through the import process here. Say OK. This is going to import that structured text directly into our project. And you'll see here we get a few add-on instructions, the main one here being the energy speed limiter. And if I look into the logic, you'll see that this is that same code that we took a look at in the generated report.
So this can now be used in our program. So I can maybe go to the bottom of my ladder here. Add a new ladder element. And we can just start to search for energy limiter, energy speed. There it is. Say, OK. And now we have this block up here.
All of the inputs and outputs right now are unmapped. So we could go to our parameters and local tags, maybe create a new local tag just for demonstration purposes. And we also create data types for these. So there's like an overall data type structure.
So I could give this a name. Maybe just call this-- we'll call it just test. Create this. You'll see down here at the bottom we have test. And this has kind of all of the inputs.
And then it's also got this important SS method type. SS method type is essentially the flag that says if this is the first time this logic is being called, you want this to be zero. Then from then on, it will be a one. It's basically the initialization state, but we could start to populate all of these right here.
And we'll put Test up here. So on and so forth. It's a Test.w. And we could, of course, map these directly to some other variables if we had those defined somewhere else already.
So now this chart is fully filled out. And we could actually start to potentially run this. So that's the basic way to bring the chart over. In general, it's pretty straightforward. Generate as an add-on instruction. You can import it in. Use it directly into your existing code, or just use it as kind of your overall framework for your program.
In our next and final video, we'll be talking about how we can take our plant model and actually have that run on the Speedgoat in real time for testing, like we saw in the very beginning. Thank you.