Davka

Tips for keeping code clean?

5 posts in this topic

Hello!

I am fairly new to the PLC world and I have worked mostly with Mitsubishi products. Right now I am doing some work with an FX5U and a GS2110 touchscreen. The code is a mix of LD for the meat and potatoes stuff and ST for more math-intensive parts.

My question is how you guys generally organize your code with respect to inputs from HMI, stuff happening in the PLC and outputs to machines? I have made state machines for the sequential processes such as weighing in materials and more direct "stuff goes in, stuff comes out" code for the manual modes.

The flow from HMI out to machine is sort of like this: HMI Inputs -> PLC -> PLC decides whether this is an automated process or a manually controlled one -> Execute the corresponding code -> Activate proper outputs, use sensor feedback if it's an automated process

I hope this makes sense. Would be nice to get a feel for general workflow and organization from other people who work with this.

Best regards,

David

 

 

Share this post


Link to post
Share on other sites

Hello,

My personal point of view: I normally use separate POU's for different logical parts of the program:
1.Inputs (Physical - it's own POU so that you can invert digital signals if nescessary, and scale analog signals)
2.Alarms (Software generated alarms to trigger bits and so on)
3.HMI (Stuff to/from the screens)
4.All different logically separated program blocks (divide in "as many as possible blocks") like:
    -PIDs
    -Sequence A
    -Sequence B
    -Sequence C
    -Frequency Inverters
    -Servos
    -...
5. Communication
    -Ethernet
    -Profibus
    -Modbus
    -...
6. Outputs (Physical - it's own POU so that you can invert digital signals if nescessary, and scale analog signals)

When it comes to global variable lists, I normally go with:
1.Constants (all constants used in the code)
2.GlobalAssigned (all assigned global variables)
3.GlobalUnassigned (all unassigned global variables)

Then, local variable lists as far as possible.

As a general note I normally spearate in "as many as possible" logical units, including making Function Blocks if possible for repeatable code. The reason for this is to segment the code for easier bugfixing and decoding. My idea is to create logical units of code, like an object oriented approach of developing code.

I also use different casings depending on the variable type:
1.Local variables: lowerCaseFirstLetter
2.Global variables: UpperCaseFirstLetter
4.In/Out FB variables: UpperCaseFirstLetter
5.Constants: UpperFirstCaseLetter

Use local variables and unassigned variables as far as possible (avoid assigning them if possible).

In addition I never use the variable type/device in the naming. By using Tooltip you can show which type and device a specific name is assigned to, hence using this kind of information in the variable name is pointless and confusing for anyone else decoding a program.

Share this post


Link to post
Share on other sites

K I S S

Share this post


Link to post
Share on other sites

I agree @RussB, but could you maybe elaborate? Saying KISS is maybe a bit to super simple ;-)

Share this post


Link to post
Share on other sites

I get accused of being simplistic rather often.

~ Instead of making a multi branched complex rung make 3 or more straight forward simple rungs. Modern PLC/PAC's do not lack discretes to use as internal coils.

~ Whenever possible scale analogs in the hardware instead of in logic.

~ Use INTEGER instead of REAL when a REAL makes no sense such as raw values from an encoder or other counter.

~ Keep any and all HMI controlled variables properly labeled so future troubleshooters know where to look for them.

~ If the Programming software allows, let it assign Addresses and you program all variables with Clear & Concise Names. This too helps future changes to the logic made by others.

~ Document all code

~ Document all Variables

~ Document everything, include internet links when available. Hopefully they will not change in the near future, even when they do at least you tried.

~ Most important is to HAVE FUN doing this.

 

Edited by RussB
SP

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now