]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/xmlauto.cxx
88c0f99849d6bdf98dc1d500c71723c8970c9341
[flightgear.git] / src / Autopilot / xmlauto.cxx
1 // xmlauto.cxx - a more flexible, generic way to build autopilots
2 //
3 // Written by Curtis Olson, started January 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/misc/sg_path.hxx>
26
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
29
30 #include "xmlauto.hxx"
31
32
33 FGPIDController::FGPIDController( SGPropertyNode *node ):
34     debug( false ),
35     y_n( 0.0 ),
36     r_n( 0.0 ),
37     Kp( 0.0 ),
38     alpha( 0.1 ),
39     beta( 1.0 ),
40     gamma( 0.0 ),
41     Ti( 0.0 ),
42     Td( 0.0 ),
43     u_min( 0.0 ),
44     u_max( 0.0 ),
45     ep_n_1( 0.0 ),
46     edf_n_1( 0.0 ),
47     edf_n_2( 0.0 ),
48     u_n_1( 0.0 )
49 {
50     int i;
51     for ( i = 0; i < node->nChildren(); ++i ) {
52         SGPropertyNode *child = node->getChild(i);
53         string cname = child->getName();
54         string cval = child->getStringValue();
55         if ( cname == "name" ) {
56             name = cval;
57         } else if ( cname == "debug" ) {
58             debug = child->getBoolValue();
59         } else if ( cname == "enable" ) {
60             // cout << "parsing enable" << endl;
61             SGPropertyNode *prop = child->getChild( "prop" );
62             if ( prop != NULL ) {
63                 // cout << "prop = " << prop->getStringValue() << endl;
64                 enable_prop = fgGetNode( prop->getStringValue(), true );
65             } else {
66                 // cout << "no prop child" << endl;
67             }
68             SGPropertyNode *val = child->getChild( "value" );
69             if ( val != NULL ) {
70                 enable_value = val->getStringValue();
71             }
72         } else if ( cname == "input" ) {
73             SGPropertyNode *prop = child->getChild( "prop" );
74             if ( prop != NULL ) {
75                 input_prop = fgGetNode( prop->getStringValue(), true );
76             }
77         } else if ( cname == "reference" ) {
78             SGPropertyNode *prop = child->getChild( "prop" );
79             if ( prop != NULL ) {
80                 r_n_prop = fgGetNode( prop->getStringValue(), true );
81             } else {
82                 prop = child->getChild( "value" );
83                 if ( prop != NULL ) {
84                     r_n = prop->getDoubleValue();
85                 }
86             }
87         } else if ( cname == "output" ) {
88             int i = 0;
89             SGPropertyNode *prop;
90             while ( (prop = child->getChild("prop", i)) != NULL ) {
91                 SGPropertyNode *tmp = fgGetNode( prop->getStringValue(), true );
92                 output_list.push_back( tmp );
93                 i++;
94             }
95         } else if ( cname == "config" ) {
96             SGPropertyNode *prop;
97
98             prop = child->getChild( "Kp" );
99             if ( prop != NULL ) {
100                 Kp = prop->getDoubleValue();
101             }
102
103             prop = child->getChild( "beta" );
104             if ( prop != NULL ) {
105                 beta = prop->getDoubleValue();
106             }
107
108             prop = child->getChild( "alpha" );
109             if ( prop != NULL ) {
110                 alpha = prop->getDoubleValue();
111             }
112
113             prop = child->getChild( "gamma" );
114             if ( prop != NULL ) {
115                 gamma = prop->getDoubleValue();
116             }
117
118             prop = child->getChild( "Ti" );
119             if ( prop != NULL ) {
120                 Ti = prop->getDoubleValue();
121             }
122
123             prop = child->getChild( "Td" );
124             if ( prop != NULL ) {
125                 Td = prop->getDoubleValue();
126             }
127
128             prop = child->getChild( "u_min" );
129             if ( prop != NULL ) {
130                 u_min = prop->getDoubleValue();
131             }
132
133             prop = child->getChild( "u_max" );
134             if ( prop != NULL ) {
135                 u_max = prop->getDoubleValue();
136             }
137         } else {
138             SG_LOG( SG_AUTOPILOT, SG_WARN, "Error in autopilot config logic" );
139             if ( name.length() ) {
140                 SG_LOG( SG_AUTOPILOT, SG_WARN, "Section = " << name );
141             }
142         }
143     }   
144 }
145
146
147 /*
148  * Roy Vegard Ovesen:
149  *
150  * Ok! Here is the PID controller algorithm that I would like to see
151  * implemented:
152  *
153  *   delta_u_n = Kp * [ (ep_n - ep_n-1) + ((Ts/Ti)*e_n)
154  *               + (Td/Ts)*(edf_n - 2*edf_n-1 + edf_n-2) ]
155  *
156  *   u_n = u_n-1 + delta_u_n
157  *
158  * where:
159  *
160  * delta_u : The incremental output
161  * Kp      : Proportional gain
162  * ep      : Proportional error with reference weighing
163  *           ep = beta * r - y
164  *           where:
165  *           beta : Weighing factor
166  *           r    : Reference (setpoint)
167  *           y    : Process value, measured
168  * e       : Error
169  *           e = r - y
170  * Ts      : Sampling interval
171  * Ti      : Integrator time
172  * Td      : Derivator time
173  * edf     : Derivate error with reference weighing and filtering
174  *           edf_n = edf_n-1 / ((Ts/Tf) + 1) + ed_n * (Ts/Tf) / ((Ts/Tf) + 1)
175  *           where:
176  *           Tf : Filter time
177  *           Tf = alpha * Td , where alpha usually is set to 0.1
178  *           ed : Unfiltered derivate error with reference weighing
179  *             ed = gamma * r - y
180  *             where:
181  *             gamma : Weighing factor
182  * 
183  * u       : absolute output
184  * 
185  * Index n means the n'th value.
186  * 
187  * 
188  * Inputs:
189  * enabled ,
190  * y_n , r_n , beta=1 , gamma=0 , alpha=0.1 ,
191  * Kp , Ti , Td , Ts (is the sampling time available?)
192  * u_min , u_max
193  * 
194  * Output:
195  * u_n
196  */
197
198 void FGPIDController::update( double dt ) {
199     double ep_n;         // proportional error with reference weighing
200     double e_n;          // error
201     double ed_n;         // derivative error
202     double edf_n;        // derivative error filter
203     double Tf;           // filter time
204     double delta_u_n;    // incremental output
205     double u_n;          // absolute output
206     double Ts = dt;      // Sampling interval (sec)
207
208     if ( Ts <= 0.0 ) {
209         // do nothing if time step is not positive (i.e. no time has
210         // elapsed)
211         return;
212     }
213
214     if (enable_prop != NULL && enable_prop->getStringValue() == enable_value) {
215         enabled = true;
216     } else {
217         enabled = false;
218     }
219
220     if ( enabled ) {
221         if ( debug ) cout << "Updating " << name << endl;
222
223         double y_n = 0.0;
224         if ( input_prop != NULL ) {
225             y_n = input_prop->getDoubleValue();
226         }
227
228         double r_n = 0.0;
229         if ( r_n_prop != NULL ) {
230             r_n = r_n_prop->getDoubleValue();
231         } else {
232             r_n = r_n_value;
233         }
234                       
235         if ( debug ) cout << "  input = " << y_n << " ref = " << r_n << endl;
236
237         // Calculates proportional error:
238         ep_n = beta * r_n - y_n;
239         if ( debug ) cout << "  ep_n = " << ep_n;
240         if ( debug ) cout << "  ep_n_1 = " << ep_n_1;
241
242         // Calculates error:
243         e_n = r_n - y_n;
244         if ( debug ) cout << " e_n = " << e_n;
245
246         // Calculates derivate error:
247         ed_n = gamma * r_n - y_n;
248         if ( debug ) cout << " ed_n = " << ed_n;
249
250         // Calculates filter time:
251         Tf = alpha * Td;
252         if ( debug ) cout << " Tf = " << Tf;
253
254         // Filters the derivate error:
255         edf_n = edf_n_1 / (Ts/Tf + 1)
256             + ed_n * (Ts/Tf) / (Ts/Tf + 1);
257         if ( debug ) cout << " edf_n = " << edf_n;
258
259         // Calculates the incremental output:
260         delta_u_n = Kp * ( (ep_n - ep_n_1)
261                            + ((Ts/Ti) * e_n)
262                            + ((Td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2)) );
263         if ( debug ) cout << " delta_u_n = " << delta_u_n << endl;
264
265         // Integrator anti-windup logic:
266         if ( delta_u_n > (u_max - u_n_1) ) {
267             delta_u_n = 0;
268         } else if ( delta_u_n < (u_min - u_n_1) ) {
269             delta_u_n = 0;
270         }
271
272         // Calculates absolute output:
273         u_n = u_n_1 + delta_u_n;
274         if ( debug ) cout << "  output = " << u_n << endl;
275
276         // Updates indexed values;
277         u_n_1   = u_n;
278         ep_n_1  = ep_n;
279         edf_n_2 = edf_n_1;
280         edf_n_1 = edf_n;
281
282         unsigned int i;
283         for ( i = 0; i < output_list.size(); ++i ) {
284             output_list[i]->setDoubleValue( u_n );
285         }
286     } else if ( !enabled ) {
287         u_n   = 0.0;
288         ep_n  = 0.0;
289         edf_n = 0.0;
290         // Updates indexed values;
291         u_n_1   = u_n;
292         ep_n_1  = ep_n;
293         edf_n_2 = edf_n_1;
294         edf_n_1 = edf_n;
295     }
296 }
297
298
299 FGPISimpleController::FGPISimpleController( SGPropertyNode *node ):
300     proportional( false ),
301     Kp( 0.0 ),
302     offset_prop( NULL ),
303     offset_value( 0.0 ),
304     integral( false ),
305     Ki( 0.0 ),
306     int_sum( 0.0 ),
307     clamp( false ),
308     debug( false ),
309     y_n( 0.0 ),
310     r_n( 0.0 ),
311     u_min( 0.0 ),
312     u_max( 0.0 )
313 {
314     int i;
315     for ( i = 0; i < node->nChildren(); ++i ) {
316         SGPropertyNode *child = node->getChild(i);
317         string cname = child->getName();
318         string cval = child->getStringValue();
319         if ( cname == "name" ) {
320             name = cval;
321         } else if ( cname == "debug" ) {
322             debug = child->getBoolValue();
323         } else if ( cname == "enable" ) {
324             // cout << "parsing enable" << endl;
325             SGPropertyNode *prop = child->getChild( "prop" );
326             if ( prop != NULL ) {
327                 // cout << "prop = " << prop->getStringValue() << endl;
328                 enable_prop = fgGetNode( prop->getStringValue(), true );
329             } else {
330                 // cout << "no prop child" << endl;
331             }
332             SGPropertyNode *val = child->getChild( "value" );
333             if ( val != NULL ) {
334                 enable_value = val->getStringValue();
335             }
336         } else if ( cname == "input" ) {
337             SGPropertyNode *prop = child->getChild( "prop" );
338             if ( prop != NULL ) {
339                 input_prop = fgGetNode( prop->getStringValue(), true );
340             }
341         } else if ( cname == "reference" ) {
342             SGPropertyNode *prop = child->getChild( "prop" );
343             if ( prop != NULL ) {
344                 r_n_prop = fgGetNode( prop->getStringValue(), true );
345             } else {
346                 prop = child->getChild( "value" );
347                 if ( prop != NULL ) {
348                     r_n = prop->getDoubleValue();
349                 }
350             }
351         } else if ( cname == "output" ) {
352             int i = 0;
353             SGPropertyNode *prop;
354             while ( (prop = child->getChild("prop", i)) != NULL ) {
355                 SGPropertyNode *tmp = fgGetNode( prop->getStringValue(), true );
356                 output_list.push_back( tmp );
357                 i++;
358             }
359         } else if ( cname == "config" ) {
360             SGPropertyNode *prop;
361
362             prop = child->getChild( "Kp" );
363             if ( prop != NULL ) {
364                 Kp = prop->getDoubleValue();
365                 proportional = true;
366             }
367
368             prop = child->getChild( "Ki" );
369             if ( prop != NULL ) {
370                 Ki = prop->getDoubleValue();
371                 integral = true;
372             }
373
374             prop = child->getChild( "u_min" );
375             if ( prop != NULL ) {
376                 u_min = prop->getDoubleValue();
377                 clamp = true;
378             }
379
380             prop = child->getChild( "u_max" );
381             if ( prop != NULL ) {
382                 u_max = prop->getDoubleValue();
383                 clamp = true;
384             }
385         } else {
386             SG_LOG( SG_AUTOPILOT, SG_WARN, "Error in autopilot config logic" );
387             if ( name.length() ) {
388                 SG_LOG( SG_AUTOPILOT, SG_WARN, "Section = " << name );
389             }
390         }
391     }   
392 }
393
394
395 void FGPISimpleController::update( double dt ) {
396     if (enable_prop != NULL && enable_prop->getStringValue() == enable_value) {
397         if ( !enabled ) {
398             // we have just been enabled, zero out int_sum
399             int_sum = 0.0;
400         }
401         enabled = true;
402     } else {
403         enabled = false;
404     }
405
406     if ( enabled ) {
407         if ( debug ) cout << "Updating " << name << endl;
408         double input = 0.0;
409         if ( input_prop != NULL ) {
410             input = input_prop->getDoubleValue();
411         }
412
413         double r_n = 0.0;
414         if ( r_n_prop != NULL ) {
415             r_n = r_n_prop->getDoubleValue();
416         } else {
417             r_n = r_n_value;
418         }
419                       
420         double error = r_n - input;
421         if ( debug ) cout << "input = " << input
422                           << " reference = " << r_n
423                           << " error = " << error
424                           << endl;
425
426         double prop_comp = 0.0;
427         double offset = 0.0;
428         if ( offset_prop != NULL ) {
429             offset = offset_prop->getDoubleValue();
430             if ( debug ) cout << "offset = " << offset << endl;
431         } else {
432             offset = offset_value;
433         }
434
435         if ( proportional ) {
436             prop_comp = error * Kp + offset;
437         }
438
439         if ( integral ) {
440             int_sum += error * Ki * dt;
441         } else {
442             int_sum = 0.0;
443         }
444
445         if ( debug ) cout << "prop_comp = " << prop_comp
446                           << " int_sum = " << int_sum << endl;
447
448         double output = prop_comp + int_sum;
449
450         if ( clamp ) {
451             if ( output < u_min ) {
452                 output = u_min;
453             }
454             if ( output > u_max ) {
455                 output = u_max;
456             }
457         }
458         if ( debug ) cout << "output = " << output << endl;
459
460         unsigned int i;
461         for ( i = 0; i < output_list.size(); ++i ) {
462             output_list[i]->setDoubleValue( output );
463         }
464     }
465 }
466
467
468 FGXMLAutopilot::FGXMLAutopilot() {
469 }
470
471
472 FGXMLAutopilot::~FGXMLAutopilot() {
473 }
474
475  
476 void FGXMLAutopilot::init() {
477     config_props = fgGetNode( "/autopilot/new-config", true );
478
479     SGPropertyNode *path_n = fgGetNode("/sim/systems/autopilot/path");
480
481     if ( path_n ) {
482         SGPath config( globals->get_fg_root() );
483         config.append( path_n->getStringValue() );
484
485         SG_LOG( SG_ALL, SG_INFO, "Reading autopilot configuration from "
486                 << config.str() );
487         try {
488             readProperties( config.str(), config_props );
489
490             if ( ! build() ) {
491                 SG_LOG( SG_ALL, SG_ALERT,
492                         "Detected an internal inconsistancy in the autopilot");
493                 SG_LOG( SG_ALL, SG_ALERT,
494                         " configuration.  See earlier errors for" );
495                 SG_LOG( SG_ALL, SG_ALERT,
496                         " details.");
497                 exit(-1);
498             }        
499         } catch (const sg_exception& exc) {
500             SG_LOG( SG_ALL, SG_ALERT, "Failed to load autopilot configuration: "
501                     << config.str() );
502         }
503
504     } else {
505         SG_LOG( SG_ALL, SG_WARN,
506                 "No autopilot configuration specified for this model!");
507     }
508 }
509
510
511 void FGXMLAutopilot::reinit() {
512     components.clear();
513     init();
514     build();
515 }
516
517
518 void FGXMLAutopilot::bind() {
519 }
520
521 void FGXMLAutopilot::unbind() {
522 }
523
524 bool FGXMLAutopilot::build() {
525     SGPropertyNode *node;
526     int i;
527
528     int count = config_props->nChildren();
529     for ( i = 0; i < count; ++i ) {
530         node = config_props->getChild(i);
531         string name = node->getName();
532         // cout << name << endl;
533         if ( name == "pid-controller" ) {
534             FGXMLAutoComponent *c = new FGPIDController( node );
535             components.push_back( c );
536         } else if ( name == "pi-simple-controller" ) {
537             FGXMLAutoComponent *c = new FGPISimpleController( node );
538             components.push_back( c );
539         } else {
540             SG_LOG( SG_ALL, SG_ALERT, "Unknown top level section: " 
541                     << name );
542             return false;
543         }
544     }
545
546     return true;
547 }
548
549
550 /*
551  * Update helper values
552  */
553 static void update_helper( double dt ) {
554     // Estimate speed in 5,10 seconds
555     static SGPropertyNode *vel = fgGetNode( "/velocities/airspeed-kt", true );
556     static SGPropertyNode *lookahead5
557         = fgGetNode( "/autopilot/internal/lookahead-5-sec-airspeed-kt", true );
558     static SGPropertyNode *lookahead10
559         = fgGetNode( "/autopilot/internal/lookahead-10-sec-airspeed-kt", true );
560
561     static double average = 0.0; // average/filtered prediction
562     static double v_last = 0.0;  // last velocity
563
564     double v = vel->getDoubleValue();
565     double a = 0.0;
566     if ( dt > 0.0 ) {
567         a = (v - v_last) / dt;
568
569         if ( dt < 1.0 ) {
570             average = (1.0 - dt) * average + dt * a;
571         } else {
572             average = a;
573         }
574
575         lookahead5->setDoubleValue( v + average * 5.0 );
576         lookahead10->setDoubleValue( v + average * 10.0 );
577         v_last = v;
578     }
579
580     // Calculate heading bug error normalized to +/- 180.0 (based on
581     // DG indicated heading)
582     static SGPropertyNode *bug
583         = fgGetNode( "/autopilot/settings/heading-bug-deg", true );
584     static SGPropertyNode *ind_hdg
585         = fgGetNode( "/instrumentation/heading-indicator/indicated-heading-deg",
586                      true );
587     static SGPropertyNode *ind_bug_error
588         = fgGetNode( "/autopilot/internal/heading-bug-error-deg", true );
589
590     double diff = bug->getDoubleValue() - ind_hdg->getDoubleValue();
591     if ( diff < -180.0 ) { diff += 360.0; }
592     if ( diff > 180.0 ) { diff -= 360.0; }
593     ind_bug_error->setDoubleValue( diff );
594
595     // Calculate heading bug error normalized to +/- 180.0 (based on
596     // actual/nodrift magnetic-heading, i.e. a DG slaved to magnetic
597     // compass.)
598     static SGPropertyNode *mag_hdg
599         = fgGetNode( "/orientation/heading-magnetic-deg", true );
600     static SGPropertyNode *fdm_bug_error
601         = fgGetNode( "/autopilot/internal/fdm-heading-bug-error-deg", true );
602
603     diff = bug->getDoubleValue() - mag_hdg->getDoubleValue();
604     if ( diff < -180.0 ) { diff += 360.0; }
605     if ( diff > 180.0 ) { diff -= 360.0; }
606     fdm_bug_error->setDoubleValue( diff );
607
608     // Calculate true heading error normalized to +/- 180.0
609     static SGPropertyNode *target_true
610         = fgGetNode( "/autopilot/settings/true-heading-deg", true );
611     static SGPropertyNode *true_hdg
612         = fgGetNode( "/orientation/heading-deg", true );
613     static SGPropertyNode *true_error
614         = fgGetNode( "/autopilot/internal/true-heading-error-deg", true );
615
616     diff = target_true->getDoubleValue() - true_hdg->getDoubleValue();
617     if ( diff < -180.0 ) { diff += 360.0; }
618     if ( diff > 180.0 ) { diff -= 360.0; }
619     true_error->setDoubleValue( diff );
620
621     // Calculate nav1 target heading error normalized to +/- 180.0
622     static SGPropertyNode *target_nav1
623         = fgGetNode( "/radios/nav[0]/radials/target-auto-hdg-deg", true );
624     static SGPropertyNode *true_nav1
625         = fgGetNode( "/autopilot/internal/nav1-heading-error-deg", true );
626
627     diff = target_nav1->getDoubleValue() - true_hdg->getDoubleValue();
628     if ( diff < -180.0 ) { diff += 360.0; }
629     if ( diff > 180.0 ) { diff -= 360.0; }
630     true_nav1->setDoubleValue( diff );
631
632     // Calculate vertical speed in fpm
633     static SGPropertyNode *vs_fps
634         = fgGetNode( "/velocities/vertical-speed-fps", true );
635     static SGPropertyNode *vs_fpm
636         = fgGetNode( "/autopilot/internal/vert-speed-fpm", true );
637
638     vs_fpm->setDoubleValue( vs_fps->getDoubleValue() * 60.0 );  
639 }
640
641
642 /*
643  * Update the list of autopilot components
644  */
645
646 void FGXMLAutopilot::update( double dt ) {
647     update_helper( dt );
648
649     unsigned int i;
650     for ( i = 0; i < components.size(); ++i ) {
651         components[i]->update( dt );
652     }
653 }