According to my understanding, the best way to implement a PID handler is inside an Interrupt Service Routine which is usually a timer ISR with configurable frequency rate. Here we calculate the process output as a function of setpoint and process input, along with other calculations.
I have the following confusions about what needs to be included inside the ISR and main function.
1) Since the frequency of some PID loops is very high, it is a good idea to read the inputs, setpoint and other values from outside the ISR and calculate only the output inside ISR. In this case what will happen if some of my debug functions(usually a printf) delay the reading of an input or setpoint from the main function? What if the ISR calculated a perfect output value but the main program delays the output to the outside world because of the same debug functions? Here the program behaves in a slightly different manner(Sometimes noticeable differences) with and without the debug code.
2) Another option is to implement input and setpoint reading functions inside the ISR. In this case, both input and output may take a significant amount of time to read the values which will affect the ISR timing. For instance, setpoint can be a serial packet which can become a problem for the ISR execution time. Same happens when we output the calculated value to the outside world.
I am a little bit confused which approach to follow while designing a PID controller. What needs to be included inside the ISR(input, setpoint or output routines) and what needs to be included in the main function?
Implementation Details :- The experimentation setup consists of a 60RPM geared DC motor, an H bridge(VNH5019) and a magnetic rotary encoder(AS5048B). The H bridge has a PWM input for speed control and 2 other pins for direction control. AS5048B is an absolute encoder which gives a 14Bit output corresponding to 0 - 360-degree rotation of the motor shaft and the same act as a feedback signal for the plant. I am trying to implement a PID position control setup for this motor. Right now the PID Update function is written inside a timer ISR which runs at a frequency of 10KHz(may be an overkill for this particular application). The input comes from a rotary encoder which will be replaced by a serial packet in future. My plan is to read input and setpoint from a main function, access the values from PID Update ISR , calculate the output PWM value corresponding to the error just like the arduino PID library implementation as shown below
The variables are shared with the PID Update ISR using pointers. So all the inputs read from within the main is available in the ISR and the output value calculated is available in the main function. The PWM value thus calculated is given to the input of the H bridge IC.
Note : Reading input, setpoint and output to H-bridge is not a part of any ISR right now. Only the calculation is done inside PID Update ISR.