Creation of Instance Object

Hi,

This is the script I tried to make use of the "simple-pid" python library to build a PID controller in Ignition.

There are two sections:

  1. import the library and create a object.
from simple_pid import PID
pid = PID(15, 1, 0, setpoint=1.5)
inPV = 1
  1. cyclically run the object, and assign the outcome to a tag.
outCV = pid(inPV)
system.tag.writeAsync("[default]ctrlPID/outCV", outCV)

How do I set it up in Ignition to get it working?
The object should be created only once, but the functions of the object should be running cyclically.

Open to all comments.

This is a new post, split from my last post

Ignition event scripts generally do not hold state--that is, any variables/functions/classes created in the script are discarded at the end of the script.

Ignition project library scripts, on the other hand, are run once at the top level the first time they are referenced elsewhere, and then persist until the project is edited/saved (or an parent project is edited/saved).

Put your import and your initialization of pid in the top level (unindented) in a project library script. Put the call to execute the instance in a function within the project library script. That function would use system.tag.readBlocking() to retrieve the process value into a local python variable for the pid instance to use, and would use system.tag.writeBlocking() to send the control value to an output tag.

Call that function from a gateway timer event at the desired pace.

Edit: Oy! Library script auto-import is racy, and doesn't always run once. :frowning_face: Use the work-around here until it is fixed:

2 Likes

You are legend.

Just tested in your way. The PID loop works like a dream.

Just to follow up on this case,

I can use the gateway timer script to trigger the PID calculation, it works out well.

Also I tried to use a tag now(1000), and perform the PID calculation every 1 second. It also works out well.

To compare these two method, the second method is better in terms of modularizing the code, but I am not sure whether it will impact the gateway performance if I have dozens of the PID running, all at the same 1 second scan rate?

Probably won't matter for dozens or even hundreds of tags, as long as the events execute to completion quickly. But keep in mind that jython has substantial startup overhead--it is best to use a single timer script with all of your loops at a given pace.

1 Like

Thanks.

Here's another issue:

I get it working if I use a value change in a perspective view to trigger the project script.

But if I link the project script directly to a tag "value changed" script, it will show the following error:
global name 'xxxx' is not defined

It's the same issue as mentioned in the two posts below.
It looks like that I still cannot assign project script to a tag value change event.

https://docs.inductiveautomation.com/display/DOC81/Project+Library#ProjectLibrary-GatewayScriptingProject

1 Like

@chaoliang which Lib you used to build PID controller .. is there any guidance for that ?
Thanks ,Ali

I was using this lib to perform PID control. It works well.

1 Like