pendulum_dae_nmpc_test.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 #include <iostream>
27 #include <fstream>
28 #include <vector>
29 #include <string>
30 #include <iomanip>
31 #include <cstring>
32 #include <cstdlib>
33 #include <cmath>
34 
35 using namespace std;
36 
37 #include "acado_common.h"
38 #include "acado_auxiliary_functions.h"
39 
40 #define NX ACADO_NX /* number of differential states */
41 #define NXA ACADO_NXA /* number of alg. states */
42 #define NU ACADO_NU /* number of control inputs */
43 #define N ACADO_N /* number of control intervals */
44 #define NY ACADO_NY /* number of references, nodes 0..N - 1 */
45 #define NYN ACADO_NYN
46 #define NUM_STEPS 100 /* number of simulation steps */
47 #define VERBOSE 1 /* show iterations: 1, silent: 0 */
48 
49 ACADOvariables acadoVariables;
51 
52 int main()
53 {
54  unsigned i, j, iter;
55  acado_timer t;
56  real_t t1, t2;
57  real_t fdbSum = 0.0;
58  real_t prepSum = 0.0;
59  int status;
60 
61  t1 = t2 = 0;
62 
63  // Reset all solver memory
64  memset(&acadoWorkspace, 0, sizeof( acadoWorkspace ));
65  memset(&acadoVariables, 0, sizeof( acadoVariables ));
66 
67  //
68  // Initialize the solver
69  //
70  acado_initializeSolver();
71 
72  //
73  // Prepare a consistent initial guess
74  //
75 
76  for (i = 0; i < N + 1; ++i)
77  {
78  acadoVariables.x[i * NX + 0] = 1;
79  acadoVariables.x[i * NX + 1] = sqrt(1.0 - 0.1 * 0.1);
80  acadoVariables.x[i * NX + 2] = 0.9;
81  acadoVariables.x[i * NX + 3] = 0;
82  acadoVariables.x[i * NX + 4] = 0;
83  acadoVariables.x[i * NX + 5] = 0;
84  }
85 
86  //
87  // Prepare references
88  //
89 
90  for (i = 0; i < N; ++i)
91  {
92  acadoVariables.y[i * NY + 0] = 0; // x
93  acadoVariables.y[i * NY + 1] = 1.0; // y
94  acadoVariables.y[i * NY + 2] = 0; // w
95  acadoVariables.y[i * NY + 3] = 0;
96  acadoVariables.y[i * NY + 4] = 0;
97  acadoVariables.y[i * NY + 5] = 0;
98  acadoVariables.y[i * NY + 6] = 0; // u
99  }
100 
101  acadoVariables.yN[ 0 ] = 0; // x
102  acadoVariables.yN[ 1 ] = 1.0; // y
103  acadoVariables.yN[ 2 ] = 0; // w
104  acadoVariables.yN[ 3 ] = 0;
105  acadoVariables.yN[ 4 ] = 0;
106  acadoVariables.yN[ 5 ] = 0;
107 
108  //
109  // Current state feedback
110  //
111  for (i = 0; i < NX; ++i)
112  acadoVariables.x0[ i ] = acadoVariables.x[ i ];
113 
114  //
115  // Logger initialization
116  //
117  vector< vector< double > > log;
118 
119  log.resize(NUM_STEPS);
120  for (i = 0; i < log.size(); ++i)
121  log[ i ].resize(NX + NXA + NU + 5, 0.0);
122 
123 
124  //
125  // Warm-up the solver
126  //
127  acado_preparationStep();
128 
129  //
130  // Real-time iterations loop
131  //
132  for( iter = 0; iter < NUM_STEPS; iter++ )
133  {
134  acado_tic( &t );
135  status = acado_feedbackStep( );
136  t2 = acado_toc( &t );
137 
138 #if VERBOSE
139 // printDifferentialVariables();
140 // printControlVariables();
141 #endif // VERBOSE
142 
143  if ( status )
144  {
145  cout << "Iteration:" << iter << ", QP problem! QP status: " << status << endl;
146 
147  break;
148  }
149 
150  //
151  // Logging
152  //
153 
154  for(i = 0; i < NX; i++)
155  log[ iter ][ i ] = acadoVariables.x[ i ];
156  for(j = 0; i < NX + NXA; i++, j++)
157  log[ iter ][ i ] = acadoVariables.z[ j ];
158  for(j = 0; i < NX + NXA + NU; i++, j++)
159  log[ iter ][ i ] = acadoVariables.u[ j ];
160 
161  log[ iter ][ i++ ] = t1;
162  log[ iter ][ i++ ] = t2;
163  log[ iter ][ i++ ] = acado_getObjective();
164  log[ iter ][ i++ ] = acado_getKKT();
165  log[ iter ][ i++ ] = acado_getNWSR();
166 
167 #if VERBOSE
168  cout << "Iteration #" << setw( 4 ) << iter
169  << ", KKT value: " << scientific << acado_getKKT()
170  << ", objective value: " << scientific << acado_getObjective()
171  << endl;
172 #endif // VERBOSE
173 
174  //
175  // Prepare for the next iteration
176  //
177 
178  // In this simple example, we feed the NMPC with an ideal feedback signal
179  // i.e. what NMPC really expects in the next sampling interval
180  for (i = 0; i < NX; ++i)
181  acadoVariables.x0[ i ] = acadoVariables.x[NX + i];
182 
183  // Shift states and control and prepare for the next iteration
184  acado_shiftStates(2, 0, 0);
185  acado_shiftControls( 0 );
186 
187  acado_tic( &t );
188  acado_preparationStep();
189  t1 = acado_toc( &t );
190 
191  //
192  // More logging...
193  //
194  prepSum += t1;
195  fdbSum += t2;
196  }
197 
198 #if VERBOSE
199  cout << "Average feedback time: " << scientific << fdbSum / NUM_STEPS * 1e6 << " microseconds" << endl;
200  cout << "Average preparation time: " << scientific << prepSum / NUM_STEPS * 1e6 << " microseconds" << endl;
201 #endif // VERBOSE
202 
203  //
204  // Save log to a file
205  //
206  ofstream dataLog( "./pendulum_dae_nmpc_test_sim_log.txt" );
207  if ( dataLog.is_open() )
208  {
209  for (i = 0; i < log.size(); i++)
210  {
211  for (j = 0; j < log[ i ].size(); j++)
212  dataLog << log[ i ][ j ] << " ";
213  dataLog << endl;
214  }
215 
216  dataLog.close();
217  }
218  else
219  {
220  cout << "Log file could not be opened" << endl;
221 
222  return 1;
223  }
224 
225  // For debugging
226  if ( status )
227  {
228  cout << "Solver failed!" << endl;
229  return EXIT_FAILURE;
230  }
231 
232  return EXIT_SUCCESS;
233 }
#define NY
IntermediateState sqrt(const Expression &arg)
#define NU
#define NUM_STEPS
#define NX
int main()
#define NXA
ACADOvariables acadoVariables
#define N
double real_t
Definition: AD_test.c:10
ACADOworkspace acadoWorkspace
IntermediateState log(const Expression &arg)


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