sensor.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 }
49 
50 
52  double _samplingTime
53  ) : TransferDevice( _nY,BN_SENSOR,_samplingTime )
54 {
55 }
56 
57 
59 {
60 }
61 
62 
64 {
65 }
66 
67 
69 {
70  if ( this != &rhs )
71  {
73 
74 
75  }
76 
77  return *this;
78 }
79 
80 
82  double _noiseSamplingTime
83  )
84 {
85  if ( _noise.getDim( ) != getNY( ) )
87 
88  for( uint i=0; i<getNY( ); ++i )
89  {
90  if ( additiveNoise[i] != 0 )
91  delete additiveNoise[i];
92 
93  additiveNoise[i] = _noise.clone( i );
94  }
95 
96  noiseSamplingTimes.setAll( _noiseSamplingTime );
97 
98  return SUCCESSFUL_RETURN;
99 }
100 
101 
103  const Noise& _noise,
104  double _noiseSamplingTime
105  )
106 {
107  if ( ( idx >= getNY( ) ) || ( _noise.getDim( ) != 1 ) )
109 
110  if ( additiveNoise[idx] != 0 )
111  delete additiveNoise[idx];
112 
113  additiveNoise[idx] = _noise.clone( );
114 
115  if ( ( idx > 0 ) && ( acadoIsEqual( _noiseSamplingTime, noiseSamplingTimes(0) ) == BT_FALSE ) )
117 
118  noiseSamplingTimes.setAll( _noiseSamplingTime ); // should be changed later
119 
120  return SUCCESSFUL_RETURN;
121 }
122 
123 
124 
126  )
127 {
128  if ( _deadTimes.getDim( ) != getNY( ) )
130 
131  if ( _deadTimes.getMin( ) < 0.0 )
133 
134  if ( deadTimes.getDim( ) == 0 )
136 
137  for( uint i=0; i<getNY(); ++i )
138  deadTimes( i ) = _deadTimes( i );
139 
140  return SUCCESSFUL_RETURN;
141 }
142 
143 
145  )
146 {
147  if ( _deadTime < 0.0 )
149 
150  if ( deadTimes.getDim( ) == 0 )
152 
153  for( uint i=0; i<getNY(); ++i )
154  deadTimes( i ) = _deadTime;
155 
156  return SUCCESSFUL_RETURN;
157 }
158 
159 
161  double _deadTime
162  )
163 {
164  if ( idx >= getNY( ) )
166 
167  if ( _deadTime < 0.0 )
169 
170  if ( deadTimes.getDim( ) == 0 )
172 
173  deadTimes( idx ) = _deadTime;
174 
175  return SUCCESSFUL_RETURN;
176 }
177 
178 
179 
180 returnValue Sensor::init( double _startTime,
181  const DVector& _startValue
182  )
183 {
184  if ( TransferDevice::init( _startTime,_startValue ) != SUCCESSFUL_RETURN )
186 
187  return SUCCESSFUL_RETURN;
188 }
189 
190 
191 
193  )
194 {
195  // consistency checks
196  if ( getStatus( ) != BS_READY )
198 
199  if ( ( _y.getNumPoints( ) < 2 ) || ( _y.getNumRows( ) != getNY( ) ) )
201 
204 
205 
206  // delay inputs and store last signal
207  if ( delaySensorOutput( _y ) != SUCCESSFUL_RETURN )
209 
210  // add sensor noise
211  if ( addSensorNoise( _y ) != SUCCESSFUL_RETURN )
213 
214  return SUCCESSFUL_RETURN;
215 }
216 
217 
218 
219 //
220 // PROTECTED MEMBER FUNCTIONS:
221 //
222 
223 
225  )
226 {
227  if ( hasDeadTime( ) == BT_FALSE )
228  {
229  // store last signal
230  DVector tmp = _y.getLastVector( );
231 
232  lastSignal.init( tmp );
233  lastSignal.setTime( 0,_y.getLastTime( ) );
234 
235  return SUCCESSFUL_RETURN;
236  }
237  else
238  {
239  double startTime = _y.getFirstTime( );
240  double endTime = _y.getLastTime( );
241 
242  // determine variables grid of delayed input
243  VariablesGrid yDelayed;
244  if ( getDelayedOutputGrid( _y, yDelayed ) != SUCCESSFUL_RETURN )
246 
247  // store last signal
248  lastSignal = yDelayed.getTimeSubGrid( yDelayed.getFloorIndex( endTime ),yDelayed.getLastIndex( ) );
249 
250  // crop delayed signal to current horizon
251  _y = yDelayed.getTimeSubGrid( yDelayed.getFloorIndex( startTime ),yDelayed.getFloorIndex( endTime ) );
252 
253  return SUCCESSFUL_RETURN;
254  }
255 }
256 
257 
259  VariablesGrid& _yDelayed
260  ) const
261 {
262  // determine common time grid for delayed outputs:
263  Grid delayedOutputTimeGrid = lastSignal.getTimePoints( );
264 
265  // make sure that last time instant of horizon lies within the grid
267  delayedOutputTimeGrid.addTime( _y.getLastTime( ) );
268 
269  // add grids of all delayed output components
270  for( uint i=0; i<getNY( ); ++i )
271  delayedOutputTimeGrid.merge( _y.getTimePoints( ).shiftTimes( deadTimes(i) ),MM_REPLACE );
272 
273  VariablesGrid tmp;
274 
275  // setup common variables grid for delayed inputs
276  _yDelayed.init( );
277 
278  for( uint i=0; i<getNY( ); ++i )
279  {
280  tmp = lastSignal( i );
281  tmp.merge( _y( i ).shiftTimes( deadTimes(i) ),MM_REPLACE,BT_FALSE );
282  tmp.refineGrid( delayedOutputTimeGrid );
283 
284  _yDelayed.appendValues( tmp );
285  }
286 
287  return SUCCESSFUL_RETURN;
288 }
289 
290 
291 
293  ) const
294 {
295  if ( hasNoise( ) == BT_FALSE )
296  return SUCCESSFUL_RETURN;
297 
298  // generate current noise
299  VariablesGrid currentNoise;
300 
301  if ( generateNoise( _y.getFirstTime(),_y.getLastTime(),currentNoise ) != SUCCESSFUL_RETURN )
303 
304  // determine common grid
305  Grid commonGrid, tmpGrid;
306  _y.getGrid( commonGrid );
307  currentNoise.getGrid( tmpGrid );
308  commonGrid.merge( tmpGrid,MM_KEEP );
309 
310  // adapt input grids and add noise
311  _y.refineGrid( commonGrid );
312  currentNoise.refineGrid( commonGrid );
313  _y += currentNoise.getValuesSubGrid( 0,getNY()-1 );
314 
315  return SUCCESSFUL_RETURN;
316 }
317 
318 
319 
321 
322 /*
323  * end of file
324  */
T getMin() const
Definition: vector.hpp:184
returnValue setTime(double _time)
Definition: grid.cpp:307
returnValue merge(const Grid &arg, MergeMethod _mergeMethod=MM_DUPLICATE, BooleanType keepOverlap=BT_TRUE)
Definition: grid.cpp:358
virtual returnValue step(VariablesGrid &_y)
Definition: sensor.cpp:192
double getFirstTime() const
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
Sensor & operator=(const Sensor &rhs)
Definition: sensor.cpp:68
VariablesGrid getValuesSubGrid(uint startIdx, uint endIdx) const
Base class for generating pseudo-random noise for simulating the Process.
Definition: noise.hpp:56
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
Allows to conveniently handle (one-dimensional) grids consisting of time points.
Definition: grid.hpp:58
Allows to simulate the behaviour of sensors within the Process.
Definition: sensor.hpp:54
uint getLastIndex() const
#define CLOSE_NAMESPACE_ACADO
BooleanType hasDeadTime() const
uint getNY() const
uint getFloorIndex(double time) const
Definition: grid.cpp:593
BooleanType hasNoise() const
BlockStatus getStatus() const
returnValue appendValues(const VariablesGrid &arg)
returnValue setOutputDeadTime(uint idx, double _deadTime)
Definition: sensor.cpp:160
returnValue addSensorNoise(VariablesGrid &_y) const
Definition: sensor.cpp:292
returnValue merge(const VariablesGrid &arg, MergeMethod _mergeMethod=MM_DUPLICATE, BooleanType keepOverlap=BT_TRUE)
VariablesGrid lastSignal
unsigned getDim() const
Definition: vector.hpp:172
virtual returnValue generateNoise(double startTime, double endTime, VariablesGrid &currentNoise) const
returnValue getGrid(Grid &_grid) const
virtual returnValue init(double _startTime=0.0, const DVector &_startValue=emptyConstVector)
DVector getLastVector() const
returnValue refineGrid(const Grid &arg, InterpolationMode mode=IM_CONSTANT)
VariablesGrid getTimeSubGrid(uint startIdx, uint endIdx) const
returnValue init()
returnValue setOutputNoise(const Noise &_noise, double _noiseSamplingTime)
Definition: sensor.cpp:81
void rhs(const real_t *x, real_t *f)
virtual Noise * clone() const =0
Grid & shiftTimes(double timeShift)
Definition: grid.cpp:449
virtual ~Sensor()
Definition: sensor.cpp:63
returnValue addTime(double _time)
Definition: grid.cpp:339
uint getNumPoints() const
void setAll(const T &_value)
Definition: vector.hpp:160
returnValue getDelayedOutputGrid(const VariablesGrid &_y, VariablesGrid &_yDelayed) const
Definition: sensor.cpp:258
double getLastTime() const
#define ACADOWARNING(retval)
DVector noiseSamplingTimes
#define BEGIN_NAMESPACE_ACADO
virtual returnValue init(double _startTime=0.0, const DVector &_startValue=emptyConstVector)
Definition: sensor.cpp:180
#define BT_FALSE
Definition: acado_types.hpp:49
Sensor()
Definition: sensor.cpp:46
uint getDim() const
returnValue delaySensorOutput(VariablesGrid &_y)
Definition: sensor.cpp:224
returnValue setOutputDeadTimes(const DVector &_deadTimes)
Definition: sensor.cpp:125
TransferDevice & operator=(const TransferDevice &rhs)
#define ACADOERROR(retval)
Base class for simulating Actuator and Sensor behaviour wihtin the Process.


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