Home Ā» PID Tuning: The Power of Python

PID Tuning: The Power of Python

PID Tuning - Simulation Results

Introduction

Ever wondered if there’s a less conventional yet effective way to tune a PID controller? Well, Python can help! As far as you know (even approximately) your plant model, Python has got you covered. This blog post will deep dive into how you can achieve optimal PID tuning by leveraging Python code. Let’s get started!


What is PID Tuning?

PID tuning involves adjusting the parameters of a PID controller to optimize system performance. The PID controller equation with filtered derivative and back-calculation anti-windup is as follows:

     $$U(s) = K_p E(s) + \left( K_i E(s) + K_{aw} (U_{sat}(s) - U(s)) \right) \times \frac{1}{s} + K_d E(s) \times \frac{s}{T_c s + 1} $$

Here,  $U_{sat}(s)$ is the saturated command and  $E(s) = R(s) - Y(s)$.


Finding the Optimal PID Parameters

The objective of tuning is to find the best combination of PID parameters (K_p, K_i, K_d, K_{aw}, T_c) that gives us satisfactory control performance results.

When we know the physics of the system that we want to control we can build a model and turn the tuning problem in an optimisation problem aimed to minimize a specific cost function, for example:

    \[J = \sum_{i=1}^{N-1} \left[ W_e \times (r_i - y_i)^2 + W_u \times u_{sat_i}^2 \right]\]

where:

  • r: setpoint
  • y: controlled variable
  • u_{sat}: saturated command
  • W_e: weighting coefficient related to the control error
  • W_u: weighting coefficient related to u_sat

The Weighting Coefficients

W_e and W_u are the weighting coefficient and their choice determines the compromise between minimizing the error and minimizing the control effort:

  • W_e > W_u: Minimizing the error takes precedence over minimizing control effort.
  • W_e < W_u: Minimizing control effort is more important than minimizing the error.

PID Tuning in Python – A Practical Example

The Plant Model

For our example, let’s consider an object with a mass m = 10 kg, which slides on a surface experiencing a viscous friction of coefficient k = 0.5 Ns/m, and is pushed by a force F (our control input). The dynamic equation is:

    \[\frac{d^2z}{dt^2} m = F - k \times \frac{dz}{dt}\]

Plant Model
Plant Model


The Python Code

The code developed for this example includes:

  1. A “PID” class for PID control with filtered derivative and back-calculation anti-windup.
  2. An “Object” class representing the plant.
  3. A “Simulation” function that runs an 80-second simulation.
  4. A “Cost” function that calculates J using the various PID parameters.

You can find the complete Python code at this GitHub link.

PID Control Diagram
PID Control Diagram

Results

Here are three different configurations with their performance results:

Identical Weights

  • W_e = 1, W_u = 1
  • Result: K_p = 1.65, K_i = 0.228, K_d = 4.87, K_{aw} = 0.301, T_c = 0.506

Focus on Error Minimization

  • W_e = 30, W_u = 1
  • Result: K_p = 1.07, K_i = 0.0119, K_d = 4.99, K_{aw} = 0.0123, T_c = 1.01

Focus on Control Effort Minimization

  • W_e = 1, W_u = 30
  • Result: K_p = 1.04, K_i = 0.0941, K_d = 4.99, K_{aw} = 0.176, T_c = 0.981
PID Tuning - Simulation Results
PID Tuning – Simulation Results

Conclusion

Python offers a versatile and free platform that can be used for PID tuning (amongst many other things), allowing for precise optimization based on your specific needs.

By understanding the role of the cost function J and its weighting coefficients (W_e and W_u in this example), you can find the perfect combination of parameters that suits your needs.

Happy PID tuning!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Menu