pid_controller.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
35 
36 
37 
39 
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
47 {
48  nInputs = 0;
49  nOutputs = 0;
50 
52 }
53 
54 
56  uint _nOutputs,
57  double _samplingTime
58  ) : ControlLaw( _samplingTime ), ClippingFunctionality( _nOutputs )
59 {
60  if ( ( _nOutputs != _nInputs ) && ( _nOutputs != 1 ) )
61  {
62  _nOutputs = 1;
64  }
65 
66  nInputs = _nInputs;
67  nOutputs = _nOutputs;
68 
70  pWeights.setZero( );
71 
73  iWeights.setZero( );
74 
76  dWeights.setZero( );
77 
78  iValue.init( nInputs );
79  iValue.setZero( );
80 
82  lastError.setZero( );
83 
85 }
86 
87 
89 {
90  nInputs = rhs.nInputs;
91  nOutputs = rhs.nOutputs;
92 
93  pWeights = rhs.pWeights;
94  iWeights = rhs.iWeights;
95  dWeights = rhs.dWeights;
96 
97  iValue = rhs.iValue;
98  lastError = rhs.lastError;
99 }
100 
101 
103 {
104 }
105 
106 
108 {
109  if ( this != &rhs )
110  {
111  ControlLaw::operator=( rhs );
113 
114  nInputs = rhs.nInputs;
115  nOutputs = rhs.nOutputs;
116 
117  pWeights = rhs.pWeights;
118  iWeights = rhs.iWeights;
119  dWeights = rhs.dWeights;
120 
121  iValue = rhs.iValue;
122  lastError = rhs.lastError;
123  }
124 
125  return *this;
126 }
127 
128 
130 {
131  return new PIDcontroller( *this );
132 }
133 
134 
135 
137  )
138 {
139  if ( _pWeights.getDim() != getNumInputs( ) )
141 
142  pWeights = _pWeights;
143 
144  return SUCCESSFUL_RETURN;
145 }
146 
147 
149  )
150 {
151  if ( _iWeights.getDim() != getNumInputs( ) )
153 
154  iWeights = _iWeights;
155 
156  return SUCCESSFUL_RETURN;
157 }
158 
159 
161  )
162 
163 {
164  if ( _dWeights.getDim() != getNumInputs( ) )
166 
167  dWeights = _dWeights;
168 
169  return SUCCESSFUL_RETURN;
170 }
171 
172 
174  const DVector &x0_,
175  const DVector &p_,
176  const VariablesGrid& _yRef
177  )
178 {
179  if ( x0_.getDim( ) != getNumInputs( ) )
181 
182  // Use reference trajectory if it is defined,
183  // otherwise set default reference to zero
184  DVector xRef( x0_.getDim() );
185 
186  if ( _yRef.getNumPoints( ) > 0 )
187  {
188  if ( _yRef.getNumValues( ) != getNumInputs( ) )
190 
191  xRef = _yRef.getVector( 0 );
192  }
193  else
194  {
195  xRef.setZero( );
196  }
197 
198 
199  // initialize control and parameter signals
200  u.init( getNumOutputs() );
201  u.setZero( );
202 
203  p = p_;
204 
205 
206  lastError = xRef - x0_;
207 
208  setStatus( BS_READY );
209 
210  return SUCCESSFUL_RETURN;
211 }
212 
213 
214 returnValue PIDcontroller::step( double currentTime,
215  const DVector& _x,
216  const DVector& _p,
217  const VariablesGrid& _yRef
218  )
219 {
220  if ( getStatus( ) != BS_READY )
222 
223  if ( _x.getDim( ) != getNumInputs( ) )
225 
226 
227  /* 1) Use reference trajectory if it is defined */
228  // set default reference to zero
229  DVector xRef( _x.getDim() );
230 
231  if ( _yRef.getNumPoints( ) > 0 )
232  {
233  if ( _yRef.getNumValues( ) != getNumInputs( ) )
235 
236  xRef = _yRef.getVector( 0 );
237  }
238  else
239  {
240  xRef.setZero( );
241  }
242 
243 
244  /* 2) Determine PID control action. */
245  if ( getNumOutputs( ) > 0 )
246  {
247  if ( determineControlAction( xRef-_x,u ) != SUCCESSFUL_RETURN )
249  }
250  else
251  u.init();
252 
253  p = _p;
254 
255 
256  /* 3) Call output transformator. */
257  if ( clipSignals( u,p ) != SUCCESSFUL_RETURN )
259 
260  return SUCCESSFUL_RETURN;
261 }
262 
263 
264 
266 {
267  return getNumInputs( );
268 }
269 
270 
272 {
273  return 0;
274 }
275 
276 
278 {
279  return getNumOutputs( );
280 }
281 
282 
284 {
285  return 0;
286 }
287 
288 
290 {
291  return 0;
292 }
293 
294 
296 {
297  return getNX( );
298 }
299 
300 
302 {
303  return BT_FALSE;
304 }
305 
306 
308 {
309  if ( isDynamic() == BT_TRUE )
310  return BT_FALSE;
311  else
312  return BT_TRUE;
313 }
314 
315 
316 
317 //
318 // PROTECTED MEMBER FUNCTIONS:
319 //
320 
322  DVector& output
323  )
324 {
325  uint i;
326  double tmp;
327 
328  output.init( getNumOutputs() );
329  output.setZero( );
330 
331  // update integral value
332  for( i=0; i<getNumInputs(); ++i )
333  iValue(i) += error(i) * getSamplingTime( );
334 
335  // determine outputs
336  for( i=0; i<getNumInputs(); ++i )
337  {
338  tmp = pWeights(i) * error(i);
339  tmp += iWeights(i) * iValue(i);
340  tmp += dWeights(i) * (error(i) - lastError(i)) / getSamplingTime( );
341 
342  if ( getNumOutputs( ) > 1 )
343  output(i) = tmp;
344  else
345  output(0) += tmp;
346  }
347 
348  // update last error
349  lastError = error;
350 
351  return SUCCESSFUL_RETURN;
352 }
353 
354 
355 
357 
358 // end of file.
uint getNumOutputs() const
ControlLaw & operator=(const ControlLaw &rhs)
Definition: control_law.cpp:68
virtual BooleanType isStatic() const
virtual returnValue step(double currentTime, const DVector &_x, const DVector &_p=emptyConstVector, const VariablesGrid &_yRef=emptyConstVariablesGrid)
virtual uint getNXA() const
returnValue setStatus(BlockStatus _status)
void init(unsigned _dim=0)
Definition: vector.hpp:155
Provides a time grid consisting of vector-valued optimization variables at each grid point...
virtual uint getNX() const
Allows to pass back messages to the calling function.
returnValue setDerivativeWeights(const DVector &_dWeights)
ClippingFunctionality & operator=(const ClippingFunctionality &rhs)
virtual ~PIDcontroller()
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
returnValue setProportionalWeights(const DVector &_pWeights)
virtual uint getNY() const
returnValue determineControlAction(const DVector &error, DVector &output)
PIDcontroller & operator=(const PIDcontroller &rhs)
virtual uint getNU() const
#define CLOSE_NAMESPACE_ACADO
virtual uint getNP() const
virtual ControlLaw * clone() const
returnValue setIntegralWeights(const DVector &_iWeights)
BlockStatus getStatus() const
unsigned getDim() const
Definition: vector.hpp:172
Derived & setZero(Index size)
void rhs(const real_t *x, real_t *f)
double getSamplingTime() const
#define BT_TRUE
Definition: acado_types.hpp:47
uint getNumPoints() const
Allows to transform the output of the ControlLaw before passing it to the Process.
virtual returnValue init(double startTime=0.0, const DVector &x0_=emptyConstVector, const DVector &p_=emptyConstVector, const VariablesGrid &_yRef=emptyConstVariablesGrid)
DVector getVector(uint pointIdx) const
uint getNumInputs() const
#define BEGIN_NAMESPACE_ACADO
virtual BooleanType isDynamic() const
#define BT_FALSE
Definition: acado_types.hpp:49
USING_NAMESPACE_ACADO void output(const char *name, const Expression &e)
Implements a PID control law to be used within a Controller.
Base class for interfacing online feedback laws to be used within a Controller.
Definition: control_law.hpp:64
returnValue clipSignals(VariablesGrid &_u, VariablesGrid &_p=emptyVariablesGrid)
virtual uint getNW() const
#define ACADOERROR(retval)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:59