# Creation of Instance Object

**URL:** <https://forum.inductiveautomation.com/t/creation-of-instance-object/72444>\
**Category:** Ignition\
**Created:** [April 14, 2023, 4:50am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444 "2023-04-14T04:50:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [April 14, 2023, 4:50am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/1 "2023-04-14T04:50:29Z")

</div>

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.

```auto
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.

```auto
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_

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 14, 2023, 11:18am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/2 "2023-04-14T11:18:05Z")

</div>

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. ☹ Use the work-around here until it is fixed:

> [@Project Library Script Initialization Races](https://forum.inductiveautomation.com/t/project-library-script-initialization-races/98636):
>
> Are you annoyed by pseudo-random breakage in Ignition project library scripting? Odd top-level constants being garbled, or operations in the top level that have side effects running twice? Or more? Not happening much, but always around project edits, and sometimes even on gateway restart? Yeah, me too. frowning_face Poking around under the hood back in the spring, for a module development task, I ran across some problematic IA bytecode and pinged some people. But it went nowhere, as I co…

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [April 14, 2023, 12:56pm UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/3 "2023-04-14T12:56:28Z")

</div>

You are legend.

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

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [April 15, 2023, 1:27am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/4 "2023-04-15T01:27:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 15, 2023, 1:42am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/5 "2023-04-15T01:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [April 15, 2023, 2:46am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/6 "2023-04-15T02:46:36Z")

</div>

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.

> [@Gateway Script global name not defined](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/4):
>
> Define functions in a project script module. Call those functions as project.someName.functionName(). In the designer, navigate to Project =\> Scripts =\> Script Library.

> [@Call project script from tag change](https://forum.inductiveautomation.com/t/call-project-script-from-tag-change/12086):
>
> Hi, I’m trying to create a simple logger for maintenance troubleshooting. Whenever I call from actionPerformed on a button or the scripting console everything works fine, but when I try to tie it to a tag change nothing wants to cooperate. Am I calling this right or missing a step somewhere? Any help would be greatly appreciated. My setup is Ignition 7.8.2, Java 1.8.0\_77, 2012 R2 with a win7 client Thanks Project Script (project.testLog): def opsLog(label,valueOld,valueNew): #Log sep. by …

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 15, 2023, 4:59am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/7 "2023-04-15T04:59:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yousef\_Ali](https://avatars.discourse-cdn.com/v4/letter/y/87869e/32.png) [@Yousef\_Ali](https://forum.inductiveautomation.com/u/Yousef_Ali)\
**Post date:** [October 15, 2024, 11:22am UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/8 "2024-10-15T11:22:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [October 15, 2024, 12:08pm UTC](https://forum.inductiveautomation.com/t/creation-of-instance-object/72444/9 "2024-10-15T12:08:30Z")

</div>

> [@chaoliang](#):
>
> ```python
> from simple_pid import PID
> pid = PID(15, 1, 0, setpoint=1.5)
> inPV = 1
> 
> ```

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