How to add a PID-controller in AX?

0
I have found the github page on how to implement a PID-controller in AX, but i'm having issues. I want to initialise a PID-controller in a class that i'm creating but when i try to instantiate the object in the class i get that 'elapsedTimeProvied' needs to be a global variable. (see attached photo)Then i try to invoke the object in the VAR_GLOBAL ... END_VAR and that works, but when i try to set the parameters i get the next error (see the attached photo). My first question is, what is the right way to invoke the object. My second question is, how can i invoke the object in a certain class, so not in VAR_GLOBAL but in VAR ... END_VAR in a class. Because i'm making a class to maken multiple objects and all those need to have a PID-controller to control them.
asked
2 answers
2

Only declare the variables first, then connect them in an initialization method.


The idea is the following:

In the VAR section you should only have declarations like:

elapsedTimeProvider : MeasuredTimeProvider;
dynamic : PID_Dynamic;
controller : DynamicController;


Then in a method of the class, for example Init, you link them together:

dynamic.elapsedTimeProvider := elapsedTimeProvider;
controller.openDynamic := dynamic;


You placed something like dynamic.setK(1.0) inside VAR_GLOBAL. Code cannot be executed there. That block only allows variable declarations, so the parser expects ; and throws an error when it encounters ..


Where to set the parameters: Calls such as setK, Ki, Kd, or limits should also be placed in an initialization method like Init, or in the cyclic execution logic. For example you can configure the PID parameters in Init and then call the controller cyclically with something like controller.Next().


How to create multiple objects: Instead of using globals, put the PID logic inside a class or FB and keep dynamic and elapsedTimeProvider as members. Each instance will then have its own controller, allowing you to create multiple PID controllers from the same implementation.


If the cycle time is constant, a lighter option is to use ConstantTimeProvider instead of MeasuredTimeProvider and simply set the cycle time in seconds.


If this resolves your issue, please mark the answer as accepted.

answered
1

So this configuration should work inside the class.


answered