Automatic code generation using Python and Automation Studio
Python script to fast track Automation Studio project development

Automatic code generation using Python and Automation Studio

In this article, I'll explain how you can use Python alongside Automation Studio to generate code automatically and get to the next level of your industrial automation journey. Without further ado, let's talk about some of the advantages and uses case.

Advantages

  • Reduce time required to perform certain tasks by an order of magnitude
  • Eliminate copy-pasting errors
  • Enforce coding and naming guideline

Use cases

There are many other use cases, but here are the ones I'm using the most on a day-to-day basis

  • Creation of new software modules: I use this one in order to start the development of a specific equipment on the machine.
  • Instantiating a module: I use this one when a module is completed but there are many physical instances of it on the machine (ex: multiple conveyors, multiple labelling head, multiple robots performing the same tasks, etc)
  • Creation of new function blocks: This script creates a new function block based on a template. It changes all the files/variables names based on the name provided.

Below will be an example of me creating a Conveyor module based of my Template module and instantiating it 2 times. I'll click on most of the files that the scripts generate. The intent is not to explain everything that's happening, but just to notice how much work is done automatically in seconds.

No alt text provided for this image

How to develop your script

It's important to understand that almost every file in Automation Studio is text-based. That means you can quite easily make changes to any of them using Python.

In my opinion, your script should have at least the following class in order to be efficient

  1. Project Path: This class will give you all the relevant path for the files you want to access (ex: logical view path is C:/Repo/Template/Logical)
  2. Naming Guideline: This class will give you all the relevant names for the different files. (ex: the task name of a module should be <Module name>_task, the alarm file name for a module should be <Module name truncated to 4 character>AlarmX.mpalarmxcore)
  3. Directory: This class allows you to perform actions on directory (ex: copying folders, renaming folders, deleting folders, etc)
  4. File: This class allows you to perform actions on files(ex: copying files, renaming files, deleting files, etc)
  5. Parser: This class allows you to perform operations inside a file (ex: replacing some words, finding line number where a word is found, inserting text at a given line number, etc)

With the 5 classes above, you'll be able to navigate automatically anywhere in your project and perform changes to the files you want easily.

Please note that you'll need to have strong coding guidelines. You can't make a script if the files structure you're parsing are always different.

Disclaimer, I'm not a python programmer. I'm probably bad at best, but this skill level is sufficient in order to produce useful script.

End of post! Please leave a comment if you use automatic code generation already or if you found that topic interesting.

If you're an innovative machine builders or an end-user that wants to do complicated project involving AcoposTrak, Acopos6D along with some robots for flexible manufacturing, please reach out. We've got the software engineering skills to get you up and running in no time!


Interesting! I’ve always pondered the idea of scripting up a tool that would convert Python language to rslogix xml. Allowing you to write PLC programs with Python language. Would be really cool!

Like
Reply

And if you want to add a GUI quickly to the python script, you can use the DelphiVCL or DelphiFMX python module which bridges the power of #Delphi and its rock solid UI frameworks with the flexibility of python and the python4Delphi bridge: https://www.embarcadero.com/new-tools/python/delphi-4-python

Brillant. Compare to the old fashion with for example macros in Excel, this class looks like really more modern.👍👍👍

Excellent use case for bringing python into the automation workflow! I also use it to translate excel based alarm lists into the mappAlarmX format!

To view or add a comment, sign in

More articles by Francis Chillis Pion

Others also viewed

Explore content categories