Search code examples
cmicrocontrollertransfer-function

How to implement a process regulator in C/C++?


The question is quite simple and I don't know where and how to implement the transfer functions.

Let's say that I have a classic system

enter image description here

where P has a transfer function of the second order with zita=0.7, F=1 and C is the process regulator.

I would like to implement this regulator on a controller like ATMega128P in C or C++.

The transfer function of the process is identified by experimental methods and the regulator may vary depending on the process transfer function.

Where should I start?


Solution

  • If you have your transfer function it in continuous time (Laplace), you will need to transform it in discrete time (Z-Transform), using one of the discretization methods (forward difference, backward difference, trapezoidal).

    Once you have the discrete transfer function, you will need to apply inverse Z transform to obtain the system's equation in the time domain. Next, you will need to detrmine the discretization step.

    When if you have this data, you can implement this system on the microcontroller quite easily, since practically you will only implement a simple equation which will probably read a sensor periodically through an ADC input and according to that and previous input (y[k], y[k-1], ...) values will generate the control value (u[k]) according to its reference (r[k]).

    The ADC (y[k]) can be read using in a timer interrupt, set to fire according to your discretization step. Once the value is read, you can compute u[k] and set the execution element accordingly.

    For implementation, I would recommend C, since C++ would probably be a little bit of an overkill in this case (most embedded systems which implement a system such as this one are programmed using ANSI C or MISRA C - especially in automotive).

    Before jumping to C, I would first try to see if I got the calculations correctly and I would simulate the system in Simulink (MATLAB), or Scilab.

    For tuning the real embedded system, I would recommend reading about the Ziegler-Nichols method.

    http://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method

    Info on discretization:

    http://www-verimag.imag.fr/~tdang/DocumentsCours/Discretization.pdf