- 3.0.2 core module.
PIDController-impl.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 namespace ct {
9 namespace core {
10 
11 template <typename SCALAR>
13  : statePrevious_(SCALAR(0.0)), I_(SCALAR(0.0)), parameters_(parameters), setpoint_(setpoint)
14 {
15 }
16 
17 template <typename SCALAR>
20 {
21 }
22 
23 template <typename SCALAR>
25 {
26 }
27 
28 template <typename SCALAR>
30 {
31  return new PIDController<SCALAR>(*this);
32 }
33 
34 template <typename SCALAR>
36 {
37  statePrevious_ = state;
38 }
39 
40 template <typename SCALAR>
42 {
43  setpoint_.stateDesired_ = state;
44 }
45 
46 template <typename SCALAR>
47 void PIDController<SCALAR>::setDesiredState(const SCALAR& state, const SCALAR& stateDerivative)
48 {
49  setpoint_.stateDesired_ = state;
50  setpoint_.stateDerivativeDesired_ = stateDerivative;
51 }
52 
53 template <typename SCALAR>
55 {
56  SCALAR error = setpoint_.stateDesired_ - state;
57 
58  // ** P-part **
59  // ============
60  SCALAR P = parameters_.k_p * error;
61 
62  // ** I-part **
63  // ============
64  computeI(error);
65 
66  // ** D-Part **
67  // ============
69 
70  // ** Controller Output **
71  // =======================
72  SCALAR u = (P + I_ + D);
73  saturateControl(u);
74 
75  statePrevious_ = state;
76 
77  return u;
78 }
79 
80 template <typename SCALAR>
81 SCALAR PIDController<SCALAR>::computeControl(const SCALAR& state, const SCALAR& stateDerivative, const Time& t)
82 {
83  SCALAR error = setpoint_.stateDesired_ - state;
84 
85  // ** P-part **
86  // ============
87  SCALAR P = parameters_.k_p * error;
88 
89  // ** I-part **
90  // ============
91  computeI(error);
92 
93  // ** D-Part **
94  // ============
95  SCALAR D = parameters_.k_d * (setpoint_.stateDerivativeDesired_ - stateDerivative);
96 
97  // ** Controller Output **
98  // =======================
99  SCALAR u = (P + I_ + D);
100 
101  saturateControl(u);
102 
103  statePrevious_ = state;
104 
105  return u;
106 }
107 
108 template <typename SCALAR>
110 {
111  if (u > parameters_.uMax)
112  u = parameters_.uMax;
113  if (u < parameters_.uMin)
114  u = parameters_.uMin;
115 }
116 
117 template <typename SCALAR>
119 {
120  parameters_ = parameters;
121 }
122 
123 template <typename SCALAR>
125 {
126  return parameters_;
127 }
128 
129 template <typename SCALAR>
131 {
132  statePrevious_ = SCALAR(0.0);
133  I_ = SCALAR(0.0);
134 }
135 
136 template <typename SCALAR>
137 void PIDController<SCALAR>::computeI(const SCALAR& error)
138 {
139  I_ += parameters_.k_i * error * parameters_.dt;
140 
141  // anti wind-up
142  if (I_ > parameters_.Imax)
143  {
144  I_ = parameters_.Imax;
145  }
146  if (I_ < -parameters_.Imax)
147  {
148  I_ = -parameters_.Imax;
149  }
150 }
151 
152 } // namespace core
153 } // namespace ct
setpoint_t setpoint_
the parameters of the PID controller
Definition: PIDController.h:150
parameters_t & Parameters()
returns a reference to the parameters
Definition: PIDController-impl.h:124
ct::core::ControlVector< control_dim > u
parameters_t parameters_
the current I part (integrated)
Definition: PIDController.h:149
SCALAR dt
differential gain
Definition: PIDController.h:53
void reset()
resets the controller
Definition: PIDController-impl.h:130
SCALAR k_d
integral gain
Definition: PIDController.h:52
PIDController * clone() const
clone operator
Definition: PIDController-impl.h:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual ~PIDController()
destructor
Definition: PIDController-impl.h:24
void setInitialState(const SCALAR &state)
set the initial state
Definition: PIDController-impl.h:35
SCALAR computeControl(const SCALAR &state, const Time &t)
computes the control input based on the current state and time
Definition: PIDController-impl.h:54
SCALAR uMax
anti-windup I-part saturation
Definition: PIDController.h:55
SCALAR stateDesired_
Definition: PIDController.h:70
void setDesiredState(const SCALAR &state)
set the state
Definition: PIDController-impl.h:41
SCALAR stateDerivativeDesired_
desired state
Definition: PIDController.h:71
A standard PIDController.
Definition: PIDController.h:31
SCALAR k_p
Definition: PIDController.h:50
setpoint for the PID controller
Definition: PIDController.h:63
PIDController(const parameters_t &parameters=parameters_t(), const setpoint_t &setpoint=setpoint_t())
default constructor
Definition: PIDController-impl.h:12
SCALAR k_i
proportional gain
Definition: PIDController.h:51
SCALAR uMin
max u output
Definition: PIDController.h:56
SCALAR Imax
timestep for I-part
Definition: PIDController.h:54
SCALAR statePrevious_
Definition: PIDController.h:146
Parameters of a PID Controller.
Definition: PIDController.h:38
void changeParameters(const parameters_t &parameters)
change the control parameters
Definition: PIDController-impl.h:118
SCALAR I_
the previous state used for finite differences
Definition: PIDController.h:147
double Time
Definition: Time.h:11