code_generation/empc/powerkite.cpp
Go to the documentation of this file.
1 
3 
5 
6 int main( ){
7 
8  // DIFFERENTIAL STATES :
9 // -------------------------
10  DifferentialState r; // the length r of the cable
11  DifferentialState phi; // the angle phi
12  DifferentialState theta; // the angle theta
13 // ------------------------- // -------------------------------------------
14  DifferentialState dr; // first derivative of r0 with respect to t
15  DifferentialState dphi; // first derivative of phi with respect to t
16  DifferentialState dtheta; // first derivative of theta with respect to t
17 // ------------------------- // -------------------------------------------
18  DifferentialState n; // winding number
19 // ------------------------- // -------------------------------------------
20  DifferentialState Psi; // the roll angle Psi
21  DifferentialState CL; // the aerodynamic lift coefficient
22 // ------------------------- // -------------------------------------------
23  DifferentialState W; // integral over the power at the generator
24  // ( = ENERGY )
25 
26 
27 // CONTROL :
28 // -------------------------
29  Control ddr0; // second derivative of r0 with respect to t
30  Control dPsi; // first derivative of Psi with respect to t
31  Control dCL; // first derivative of CL with respect to t
32 
33 
34 // PARAMETERS :
35 // ------------------------
36  // PARAMETERS OF THE KITE :
37  // -----------------------------
38  double mk = 850.00; // mass of the kite // [ kg ]
39  double A = 500.00; // effective area // [ m^2 ]
40  double V = 720.00; // volume // [ m^3 ]
41  double cd0 = 0.04; // aerodynamic drag coefficient // [ ]
42  // ( cd0: without downwash )
43  double K = 0.04; // induced drag constant // [ ]
44 
45 
46  // PHYSICAL CONSTANTS :
47  // -----------------------------
48  double g = 9.81; // gravitational constant // [ m /s^2]
49  double rho = 1.23; // density of the air // [ kg/m^3]
50 
51  // PARAMETERS OF THE CABLE :
52  // -----------------------------
53  double rhoc = 1450.00; // density of the cable // [ kg/m^3]
54  double cc = 1.00; // frictional constant // [ ]
55  double dc = 0.05614; // diameter // [ m ]
56 
57 
58  // PARAMETERS OF THE WIND :
59  // -----------------------------
60  double w0 = 10.00; // wind velocity at altitude h0 // [ m/s ]
61  double h0 = 100.00; // the altitude h0 // [ m ]
62  double hr = 0.10; // roughness length // [ m ]
63 
64 
65 
66 // OTHER VARIABLES :
67 // ------------------------
68 
69  double AQ ; // cross sectional area
70 
71  IntermediateState mc; // mass of the cable
72  IntermediateState m ; // effective inertial mass
73  IntermediateState m_; // effective gravitational mass
74 
76 
77  IntermediateState dm; // first derivative of m with respect to t
78 
79 
80 // DEFINITION OF PI :
81 // ------------------------
82 
83  double PI = 3.1415926535897932;
84 
85 
86 // ORIENTATION AND FORCES :
87 // ------------------------
88 
89  IntermediateState h ; // altitude of the kite
90  IntermediateState w ; // the wind at altitude h
91  IntermediateState we [ 3]; // effective wind vector
92  IntermediateState nwe ; // norm of the effective wind vector
93  IntermediateState nwep ; // -
94  IntermediateState ewep [ 3]; // projection of ewe
95  IntermediateState eta ; // angle eta: cf. [2]
96  IntermediateState et [ 3]; // unit vector from the left to the right wing tip
97  IntermediateState Caer ;
98  IntermediateState Cf ; // simple constants
99  IntermediateState CD ; // the aerodynamic drag coefficient
100  IntermediateState Fg [ 3]; // the gravitational force
101  IntermediateState Faer [ 3]; // the aerodynamic force
102  IntermediateState Ff [ 3]; // the frictional force
103  IntermediateState F [ 3]; // the total force
104 
105 
106 // TERMS ON RIGHT-HAND-SIDE
107 // OF THE DIFFERENTIAL
108 // EQUATIONS :
109 // ------------------------
110 
111  IntermediateState a_pseudo [ 3]; // the pseudo accelaration
112  IntermediateState dn ; // the time derivate of the kite's winding number
113  IntermediateState ddr ; // second derivative of s with respect to t
114  IntermediateState ddphi ; // second derivative of phi with respect to t
115  IntermediateState ddtheta ; // second derivative of theta with respect to t
116  IntermediateState power ; // the power at the generator
117 // ------------------------ // ----------------------------------------------
118  IntermediateState regularisation ; // regularisation of controls
119 
120 
121 
122 // MODEL EQUATIONS :
123 // ===============================================================
124 
125 
126 
127 // CROSS AREA OF THE CABLE :
128 // ---------------------------------------------------------------
129 
130  AQ = PI*dc*dc/4.0 ;
131 
132 // THE EFECTIVE MASS' :
133 // ---------------------------------------------------------------
134 
135  mc = rhoc*AQ*r ; // mass of the cable
136  m = mk + mc / 3.0; // effective inertial mass
137  m_ = mk + mc / 2.0; // effective gravitational mass
138 // ----------------------------- // ----------------------------
139  dm = (rhoc*AQ/ 3.0)*dr; // time derivative of the mass
140 
141 
142 // WIND SHEAR MODEL :
143 // ---------------------------------------------------------------
144 
145  h = r*cos(theta) ;
146  w = log(h/hr) / log(h0/hr) *w0 ;
147 
148 
149 // EFFECTIVE WIND IN THE KITE`S SYSTEM :
150 // ---------------------------------------------------------------
151 
152  we[0] = w*sin(theta)*cos(phi) - dr ;
153  we[1] = -w*sin(phi) - r*sin(theta)*dphi ;
154  we[2] = -w*cos(theta)*cos(phi) + r *dtheta;
155 
156 
157 // CALCULATION OF THE KITE`S TRANSVERSAL AXIS :
158 // ---------------------------------------------------------------
159 
160  nwep = pow( we[1]*we[1] + we[2]*we[2], 0.5 );
161  nwe = pow( we[0]*we[0] + we[1]*we[1] + we[2]*we[2], 0.5 );
162  eta = asin( we[0]*tan(Psi)/ nwep ) ;
163 
164 // ---------------------------------------------------------------
165 
166 // ewep[0] = 0.0 ;
167  ewep[1] = we[1] / nwep ;
168  ewep[2] = we[2] / nwep ;
169 
170 // ---------------------------------------------------------------
171 
172  et [0] = sin(Psi) ;
173  et [1] = (-cos(Psi)*sin(eta))*ewep[1] - (cos(Psi)*cos(eta))*ewep[2];
174  et [2] = (-cos(Psi)*sin(eta))*ewep[2] + (cos(Psi)*cos(eta))*ewep[1];
175 
176 
177 
178 
179 // CONSTANTS FOR SEVERAL FORCES :
180 // ---------------------------------------------------------------
181 
182  Cg = (V*rho-m_)*g ;
183  Caer = (rho*A/2.0 )*nwe ;
184  Cf = (rho*dc/8.0)*r*nwe ;
185 
186 
187 // THE DRAG-COEFFICIENT :
188 // ---------------------------------------------------------------
189 
190  CD = cd0 + K*CL*CL ;
191 
192 
193 
194 // SUM OF GRAVITATIONAL AND LIFTING FORCE :
195 // ---------------------------------------------------------------
196 
197  Fg [0] = Cg * cos(theta) ;
198 // Fg [1] = Cg * 0.0 ;
199  Fg [2] = Cg * sin(theta) ;
200 
201 
202 // SUM OF THE AERODYNAMIC FORCES :
203 // ---------------------------------------------------------------
204 
205  Faer[0] = Caer*( CL*(we[1]*et[2]-we[2]*et[1]) + CD*we[0] ) ;
206  Faer[1] = Caer*( CL*(we[2]*et[0]-we[0]*et[2]) + CD*we[1] ) ;
207  Faer[2] = Caer*( CL*(we[0]*et[1]-we[1]*et[0]) + CD*we[2] ) ;
208 
209 
210 // THE FRICTION OF THE CABLE :
211 // ---------------------------------------------------------------
212 
213 // Ff [0] = Cf * cc* we[0] ;
214  Ff [1] = Cf * cc* we[1] ;
215  Ff [2] = Cf * cc* we[2] ;
216 
217 
218 
219 // THE TOTAL FORCE :
220 // ---------------------------------------------------------------
221 
222  F [0] = Fg[0] + Faer[0] ;
223  F [1] = Faer[1] + Ff[1] ;
224  F [2] = Fg[2] + Faer[2] + Ff[2] ;
225 
226 
227 
228 // THE PSEUDO ACCELERATION:
229 // ---------------------------------------------------------------
230 
231  a_pseudo [0] = - ddr0
232  + r*( dtheta*dtheta
233  + sin(theta)*sin(theta)*dphi *dphi )
234  - dm/m*dr ;
235 
236  a_pseudo [1] = - 2.0*cos(theta)/sin(theta)*dphi*dtheta
237  - 2.0*dr/r*dphi
238  - dm/m*dphi ;
239 
240  a_pseudo [2] = cos(theta)*sin(theta)*dphi*dphi
241  - 2.0*dr/r*dtheta
242  - dm/m*dtheta ;
243 
244 
245 
246 
247 // THE EQUATIONS OF MOTION:
248 // ---------------------------------------------------------------
249 
250  ddr = F[0]/m + a_pseudo[0] ;
251  ddphi = F[1]/(m*r*sin(theta)) + a_pseudo[1] ;
252  ddtheta = -F[2]/(m*r ) + a_pseudo[2] ;
253 
254 
255 
256 
257 
258 // THE DERIVATIVE OF THE WINDING NUMBER :
259 // ---------------------------------------------------------------
260 
261  dn = ( dphi*ddtheta - dtheta*ddphi ) /
262  (2.0*PI*(dphi*dphi + dtheta*dtheta) ) ;
263 
264 
265 
266 // THE POWER AT THE GENERATOR :
267 // ---------------------------------------------------------------
268 
269  power = m*ddr*dr ;
270 
271 
272 
273 // REGULARISATION TERMS :
274 // ---------------------------------------------------------------
275 
276 
277  regularisation = 5.0e2 * ddr0 * ddr0
278  + 1.0e8 * dPsi * dPsi
279  + 1.0e5 * dCL * dCL
280  + 2.5e5 * dn * dn
281  + 2.5e7 * ddphi * ddphi;
282  + 2.5e7 * ddtheta * ddtheta;
283  + 2.5e6 * dtheta * dtheta;
284 // ---------------------------
285 
286 
287 
288 // THE "RIGHT-HAND-SIDE" OF THE ODE:
289 // ---------------------------------------------------------------
291 
292  f << dot(r) == dr ;
293  f << dot(phi) == dphi ;
294  f << dot(theta) == dtheta ;
295  f << dot(dr) == ddr0 ;
296  f << dot(dphi) == ddphi ;
297  f << dot(dtheta) == ddtheta ;
298  f << dot(n) == dn ;
299  f << dot(Psi) == dPsi ;
300  f << dot(CL) == dCL ;
301  f << dot(W) == (-power + regularisation)*1.0e-6;
302 
303  // DEFINE THE WEIGHTING MATRICES:
304  // ----------------------------------------------------------
305  Expression states;
306  states << r;
307  states << phi;
308  states << theta;
309  states << dr;
310  states << dphi;
311  states << dtheta;
312  states << n;
313  states << Psi;
314  states << CL;
315  states << W;
316 
317  Expression controls;
318  controls << ddr0;
319  controls << dPsi;
320  controls << dCL;
321 
322  Function rf, rfN;
323  rf << states;
324  rf << controls;
325 
326  rfN << states;
327 
328  BMatrix Wmat(rf.getDim(), rf.getDim()); Wmat.setAll( true );
329  BMatrix WNmat(rfN.getDim(), rfN.getDim()); WNmat.setAll( true );
330  // ----------------------------------------------------------
331 
332 
333  // SET UP THE MPC - OPTIMAL CONTROL PROBLEM:
334  // ----------------------------------------------------------
335 
336  const double t_start = 0.0;
337  const double t_end = 18.0;
338  const uint N = 18;
339 
340  OCP ocp( t_start, t_end, N );
341  ocp.minimizeMayerTerm( W );
342 
343  ocp.setModel( f );
344 
345  ocp.subjectTo( -0.34 <= phi <= 0.34 );
346  ocp.subjectTo( 0.85 <= theta <= 1.45 );
347  ocp.subjectTo( -40.0 <= dr <= 10.0 );
348  ocp.subjectTo( -0.7 <= n <= 0.90 );
349  ocp.subjectTo( -0.29 <= Psi <= 0.29 );
350  ocp.subjectTo( 0.1 <= CL <= 1.50 );
351 
352  ocp.subjectTo( -25.0 <= ddr0 <= 25.0 );
353  ocp.subjectTo( -0.065 <= dPsi <= 0.065 );
354  ocp.subjectTo( -3.5 <= dCL <= 3.5 );
355 
356  ocp.subjectTo( AT_END, 0 <= r <= 0 );
357  ocp.subjectTo( AT_END, 0 <= phi <= 0 );
358  ocp.subjectTo( AT_END, 0 <= theta <= 0 );
359  ocp.subjectTo( AT_END, 0 <= dr <= 0 );
360  ocp.subjectTo( AT_END, 0 <= dphi <= 0 );
361  ocp.subjectTo( AT_END, 0 <= dtheta <= 0 );
362  ocp.subjectTo( AT_END, -0.7 <= n <= 0.90 );
363  ocp.subjectTo( AT_END, 0 <= Psi <= 0 );
364  ocp.subjectTo( AT_END, 0 <= CL <= 0 );
365  ocp.subjectTo( AT_END, 0 <= W <= 1e6 );
366 
367  // ----------------------------------------------------------
368 
369  // DEFINE AN MPC EXPORT MODULE AND GENERATE THE CODE:
370  // ----------------------------------------------------------
371  OCPexport mpc( ocp );
372 
375  mpc.set( INTEGRATOR_TYPE, INT_RK4 );
376  mpc.set( NUM_INTEGRATOR_STEPS, 18 );
377  mpc.set( QP_SOLVER, QP_QPOASES );
378  mpc.set( HOTSTART_QP, NO );
379  mpc.set( GENERATE_TEST_FILE, NO );
380  mpc.set( GENERATE_MAKE_FILE, NO );
386 
387  mpc.exportCode( "mpc_export" );
388  mpc.printDimensionsQP( );
389  // ----------------------------------------------------------
390 
391 
392  // DEFINE A SIM EXPORT MODULE AND GENERATE THE CODE:
393  // ----------------------------------------------------------
394  SIMexport sim( 1, t_end/(5*N) );
395 
396  sim.setModel( f );
397 
398  sim.set( INTEGRATOR_TYPE, INT_RK4 );
399  sim.set( NUM_INTEGRATOR_STEPS, 2 );
402  sim.exportCode( "sim_export" );
403  // ----------------------------------------------------------
404 
405  return 0;
406 }
407 
408 
409 
#define N
USING_NAMESPACE_ACADO IntermediateState sin(const Expression &arg)
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
USING_NAMESPACE_ACADO int main()
#define USING_NAMESPACE_ACADO
IntermediateState asin(const Expression &arg)
IntermediateState pow(const Expression &arg1, const Expression &arg2)
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
virtual returnValue exportCode(const std::string &dirName, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16)
Definition: ocp_export.cpp:68
void setAll(const T &_value)
Definition: matrix.hpp:141
IntermediateState tan(const Expression &arg)
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue minimizeMayerTerm(const Expression &arg)
Definition: ocp.cpp:238
returnValue setModel(const DifferentialEquation &_f)
IntermediateState cos(const Expression &arg)
returnValue set(OptionsName name, int value)
Definition: options.cpp:126
User-interface to automatically generate simulation algorithms for fast optimal control.
Definition: sim_export.hpp:60
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
#define YES
Definition: acado_types.hpp:51
int getDim() const
virtual returnValue exportCode(const std::string &dirName, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16)
Definition: sim_export.cpp:104
#define NO
Definition: acado_types.hpp:53
Data class for defining optimal control problems.
Definition: ocp.hpp:89
Expression dot(const Expression &arg)
const double t_end
const double t_start
A user class for auto-generation of OCP solvers.
Definition: ocp_export.hpp:57
returnValue printDimensionsQP()
Definition: ocp_export.cpp:464
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.
IntermediateState log(const Expression &arg)


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