Roald87 / roald87.github.io

Mostly cooking and coding in TwinCAT.
https://cookncode.com
MIT License
4 stars 2 forks source link

twincat/2021/08/17/tc-simulation #55

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Machine simulation with a digital twin

When you’re developing PLC code it can be difficult to test the behavior of the whole code, due to all the disconnected in and outputs. A colleague of mine found a nice solution how you can create a digital twin of the real machine. The digital twin then simulates the behavior of the real one. In this article I’ll show you how you can do this with a simple oven project.

https://cookncode.com/twincat/2021/08/17/tc-simulation.html

r2k-in-the-vortex commented 1 year ago

It's unnecessary to make an entire separate project and manage the linking between them vs different linking in real machine. Just build the simulation code right into the main project itself and enable/disable it with a single setting. For any input you can make a function block where you query value via method or property, if simulation is enabled it returns simulated value otherwise a real one.

Also one simple way to manage digital inputs is to make methods On() and Off() which in real use return input value, or its inverted value, but in simulation always TRUE. And then you write your code using those methods such that unless you override something all input checks pass and continue in simulation and the entire code runs as normal.

Roald87 commented 1 year ago

Thanks for sharing your ideas! There are always more ways to achieve something. I'm not a big fan of putting the simulation code into the real one, but if it works for you that is great.

PatrickSt90 commented 1 year ago

Being able to simulate your plant is in my opinion an extremely important and underestimated Task in PLC programming.

I already used several different methods, but for my tasks the simplest ones performed best.

Using a CAD-Model (for example with Siemens NX-MCD) , adding kinematics, physics and all that stuff is not required in 99% of all cases. You need a lot of time to implement it and while doing it you add a lot of bugs you don't want to (the goal is to fix the bugs in the software, not the ones in your simulation).

Most of the features you need in the model can be expressed by simple formulas (like the temperature example) or logic conjunctions.

Using a seperate project is a possible solution for this, but not my preffered way, because this forces you to handle all the bus communication, which can be sensor specific. For example an analog pressure sensor is connected by a DWORD, with an value that first has to be converted to an LREAL to get a physical unit like [bar]. To do this, and check the sensor state and stuff like this i use an input block. This block is not a part of my Virtual-Commissioning, because this has to be tested with real hardware, and doesn't change from project to project.

So when I test my program I use the PLC variables before they go to the output-interface, do some calculations with them and replace the values of the input-interface. This has to be done in the original project an can be enabled or disabled by one single DEBUG-variable

Roald87 commented 1 year ago

@PatrickSt90 thanks for your inputs! It's good to share methods so we can all learn and use what we need.

Ali-ciit commented 3 months ago

Many thanks; much appreciated.