PID_Controller.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mvsim/PID_Controller.h>
11 
12 using namespace mvsim;
13 
15 double PID_Controller::compute(double err, double dt)
16 {
17  e_n_2 = e_n_1;
18  e_n_1 = e_n;
19  e_n = err;
20 
21  double output = lastOutput + KP * (e_n - e_n_1) + KI * e_n * dt +
22  KD * (e_n - 2 * e_n_1 + e_n_2) / dt;
23 
24  // prevent integral windup
25  if (max_out != 0.0 && (output < -max_out || output > max_out))
26  {
27  output -= KI * e_n * dt;
28  }
29 
30  lastOutput = output;
31 
32  if (max_out != 0.0)
33  {
34  if (output < -max_out) output = -max_out;
35  if (output > max_out) output = max_out;
36  }
37 
38  return output;
39 }
40 
42 {
43  lastOutput = 0;
44  e_n = 0, e_n_1 = 0, e_n_2 = 0;
45 }
double max_out
For clamping (0=no clamp)
double compute(double err, double dt)


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21