1 // xmlauto.cxx - a more flexible, generic way to build autopilots
3 // Written by Curtis Olson, started January 2004.
5 // Copyright (C) 2004 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/misc/sg_path.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
30 #include "xmlauto.hxx"
33 FGPIDController::FGPIDController( SGPropertyNode *node, bool old ):
34 proportional( false ),
60 for ( i = 0; i < node->nChildren(); ++i ) {
61 SGPropertyNode *child = node->getChild(i);
62 string cname = child->getName();
63 string cval = child->getStringValue();
64 if ( cname == "name" ) {
66 } else if ( cname == "enable" ) {
67 cout << "parsing enable" << endl;
68 SGPropertyNode *prop = child->getChild( "prop" );
70 cout << "prop = " << prop->getStringValue() << endl;
71 enable_prop = fgGetNode( prop->getStringValue(), true );
73 cout << "no prop child" << endl;
75 SGPropertyNode *val = child->getChild( "value" );
77 enable_value = val->getStringValue();
79 } else if ( cname == "debug" ) {
80 debug = child->getBoolValue();
81 } else if ( cname == "input" ) {
82 SGPropertyNode *prop = child->getChild( "prop" );
84 input_prop = fgGetNode( prop->getStringValue(), true );
86 } else if ( cname == "reference" ) {
87 SGPropertyNode *prop = child->getChild( "prop" );
89 r_n_prop = fgGetNode( prop->getStringValue(), true );
91 prop = child->getChild( "value" );
93 r_n_value = prop->getDoubleValue();
96 } else if ( cname == "output" ) {
99 while ( (prop = child->getChild("prop", i)) != NULL ) {
100 SGPropertyNode *tmp = fgGetNode( prop->getStringValue(), true );
101 output_list.push_back( tmp );
104 prop = child->getChild( "clamp" );
105 if ( prop != NULL ) {
110 tmp = prop->getChild( "min" );
112 u_min = tmp->getDoubleValue();
113 cout << "min = " << u_min << endl;
116 tmp = prop->getChild( "max" );
118 u_max = tmp->getDoubleValue();
119 cout << "max = " << u_max << endl;
122 } else if ( cname == "proportional" ) {
125 SGPropertyNode *prop;
127 prop = child->getChild( "pre" );
128 if ( prop != NULL ) {
129 prop = prop->getChild( "one-eighty" );
130 if ( prop != NULL && prop->getBoolValue() ) {
135 prop = child->getChild( "factor" );
136 if ( prop != NULL ) {
137 factor = prop->getDoubleValue();
140 prop = child->getChild( "offset" );
141 if ( prop != NULL ) {
142 SGPropertyNode *sub = prop->getChild( "prop" );
144 offset_prop = fgGetNode( sub->getStringValue(), true );
145 cout << "offset prop = " << sub->getStringValue() << endl;
147 sub = prop->getChild( "value" );
149 offset_value = sub->getDoubleValue();
150 cout << "offset value = " << offset_value << endl;
154 } else if ( cname == "integral" ) {
157 SGPropertyNode *prop;
158 prop = child->getChild( "gain" );
159 if ( prop != NULL ) {
160 gain = prop->getDoubleValue();
163 SG_LOG( SG_AUTOPILOT, SG_WARN, "Error in autopilot config logic" );
169 FGPIDController::FGPIDController( SGPropertyNode *node ):
170 proportional( false ),
196 for ( i = 0; i < node->nChildren(); ++i ) {
197 SGPropertyNode *child = node->getChild(i);
198 string cname = child->getName();
199 string cval = child->getStringValue();
200 if ( cname == "name" ) {
202 } else if ( cname == "debug" ) {
203 debug = child->getBoolValue();
204 } else if ( cname == "enable" ) {
205 cout << "parsing enable" << endl;
206 SGPropertyNode *prop = child->getChild( "prop" );
207 if ( prop != NULL ) {
208 cout << "prop = " << prop->getStringValue() << endl;
209 enable_prop = fgGetNode( prop->getStringValue(), true );
211 cout << "no prop child" << endl;
213 SGPropertyNode *val = child->getChild( "value" );
215 enable_value = val->getStringValue();
217 } else if ( cname == "input" ) {
218 SGPropertyNode *prop = child->getChild( "prop" );
219 if ( prop != NULL ) {
220 input_prop = fgGetNode( prop->getStringValue(), true );
222 } else if ( cname == "reference" ) {
223 SGPropertyNode *prop = child->getChild( "prop" );
224 if ( prop != NULL ) {
225 r_n_prop = fgGetNode( prop->getStringValue(), true );
227 prop = child->getChild( "value" );
228 if ( prop != NULL ) {
229 r_n = prop->getDoubleValue();
232 } else if ( cname == "output" ) {
234 SGPropertyNode *prop;
235 while ( (prop = child->getChild("prop", i)) != NULL ) {
236 SGPropertyNode *tmp = fgGetNode( prop->getStringValue(), true );
237 output_list.push_back( tmp );
240 } else if ( cname == "config" ) {
241 SGPropertyNode *prop;
243 prop = child->getChild( "Kp" );
244 if ( prop != NULL ) {
245 Kp = prop->getDoubleValue();
248 prop = child->getChild( "beta" );
249 if ( prop != NULL ) {
250 beta = prop->getDoubleValue();
253 prop = child->getChild( "alpha" );
254 if ( prop != NULL ) {
255 alpha = prop->getDoubleValue();
258 prop = child->getChild( "gamma" );
259 if ( prop != NULL ) {
260 gamma = prop->getDoubleValue();
263 prop = child->getChild( "Ti" );
264 if ( prop != NULL ) {
265 Ti = prop->getDoubleValue();
268 prop = child->getChild( "Td" );
269 if ( prop != NULL ) {
270 Td = prop->getDoubleValue();
273 prop = child->getChild( "u_min" );
274 if ( prop != NULL ) {
275 u_min = prop->getDoubleValue();
278 prop = child->getChild( "u_max" );
279 if ( prop != NULL ) {
280 u_max = prop->getDoubleValue();
283 SG_LOG( SG_AUTOPILOT, SG_WARN, "Error in autopilot config logic" );
289 void FGPIDController::update_old( double dt ) {
290 if (enable_prop != NULL && enable_prop->getStringValue() == enable_value) {
292 // we have just been enabled, zero out int_sum
301 if ( debug ) cout << "Updating " << name << endl;
303 if ( input_prop != NULL ) {
304 input = input_prop->getDoubleValue();
308 if ( r_n_prop != NULL ) {
309 r_n = r_n_prop->getDoubleValue();
314 double error = r_n - input;
316 while ( error < -180.0 ) { error += 360.0; }
317 while ( error > 180.0 ) { error -= 360.0; }
319 if ( debug ) cout << "input = " << input
320 << " reference = " << r_n
321 << " error = " << error
324 double prop_comp = 0.0;
326 if ( offset_prop != NULL ) {
327 offset = offset_prop->getDoubleValue();
328 if ( debug ) cout << "offset = " << offset << endl;
330 offset = offset_value;
333 if ( proportional ) {
334 prop_comp = error * factor + offset;
338 int_sum += error * gain * dt;
343 if ( debug ) cout << "prop_comp = " << prop_comp
344 << " int_sum = " << int_sum << endl;
346 double output = prop_comp + int_sum;
349 if ( output < u_min ) {
352 if ( output > u_max ) {
356 if ( debug ) cout << "output = " << output << endl;
359 for ( i = 0; i < output_list.size(); ++i ) {
360 output_list[i]->setDoubleValue( output );
369 * Ok! Here is the PID controller algorithm that I would like to see
372 * delta_u_n = Kp * [ (ep_n - ep_n-1) + ((Ts/Ti)*e_n)
373 * + (Td/Ts)*(edf_n - 2*edf_n-1 + edf_n-2) ]
375 * u_n = u_n-1 + delta_u_n
379 * delta_u : The incremental output
380 * Kp : Proportional gain
381 * ep : Proportional error with reference weighing
384 * beta : Weighing factor
385 * r : Reference (setpoint)
386 * y : Process value, measured
389 * Ts : Sampling interval
390 * Ti : Integrator time
391 * Td : Derivator time
392 * edf : Derivate error with reference weighing and filtering
393 * edf_n = edf_n-1 / ((Ts/Tf) + 1) + ed_n * (Ts/Tf) / ((Ts/Tf) + 1)
396 * Tf = alpha * Td , where alpha usually is set to 0.1
397 * ed : Unfiltered derivate error with reference weighing
400 * gamma : Weighing factor
402 * u : absolute output
404 * Index n means the n'th value.
409 * y_n , r_n , beta=1 , gamma=0 , alpha=0.1 ,
410 * Kp , Ti , Td , Ts (is the sampling time available?)
417 void FGPIDController::update( double dt ) {
418 double ep_n; // proportional error with reference weighing
420 double ed_n; // derivative error
421 double edf_n; // derivative error filter
422 double Tf; // filter time
423 double delta_u_n; // incremental output
424 double u_n; // absolute output
425 double Ts = dt; // Sampling interval (sec)
428 // do nothing if time step is not positive (i.e. no time has
433 if (enable_prop != NULL && enable_prop->getStringValue() == enable_value) {
440 if ( debug ) cout << "Updating " << name << endl;
443 if ( input_prop != NULL ) {
444 y_n = input_prop->getDoubleValue();
448 if ( r_n_prop != NULL ) {
449 r_n = r_n_prop->getDoubleValue();
454 if ( debug ) cout << " input = " << y_n << " ref = " << r_n << endl;
456 // Calculates proportional error:
457 ep_n = beta * r_n - y_n;
458 if ( debug ) cout << " ep_n = " << ep_n;
459 if ( debug ) cout << " ep_n_1 = " << ep_n_1;
463 if ( debug ) cout << " e_n = " << e_n;
465 // Calculates derivate error:
466 ed_n = gamma * r_n - y_n;
467 if ( debug ) cout << " ed_n = " << ed_n;
469 // Calculates filter time:
471 if ( debug ) cout << " Tf = " << Tf;
473 // Filters the derivate error:
474 edf_n = edf_n_1 / (Ts/Tf + 1)
475 + ed_n * (Ts/Tf) / (Ts/Tf + 1);
476 if ( debug ) cout << " edf_n = " << edf_n;
478 // Calculates the incremental output:
479 delta_u_n = Kp * ( (ep_n - ep_n_1)
481 + ((Td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2)) );
482 if ( debug ) cout << " delta_u_n = " << delta_u_n << endl;
484 // Integrator anti-windup logic:
485 if ( delta_u_n > (u_max - u_n_1) ) {
487 } else if ( delta_u_n < (u_min - u_n_1) ) {
491 // Calculates absolute output:
492 u_n = u_n_1 + delta_u_n;
493 if ( debug ) cout << " output = " << u_n << endl;
495 // Updates indexed values;
502 for ( i = 0; i < output_list.size(); ++i ) {
503 output_list[i]->setDoubleValue( u_n );
505 } else if ( !enabled ) {
509 // Updates indexed values;
518 FGXMLAutopilot::FGXMLAutopilot() {
522 FGXMLAutopilot::~FGXMLAutopilot() {
526 void FGXMLAutopilot::init() {
527 config_props = fgGetNode( "/autopilot/new-config", true );
529 SGPropertyNode *path_n = fgGetNode("/sim/systems/autopilot/path");
532 SGPath config( globals->get_fg_root() );
533 config.append( path_n->getStringValue() );
535 SG_LOG( SG_ALL, SG_INFO, "Reading autopilot configuration from "
538 readProperties( config.str(), config_props );
541 SG_LOG( SG_ALL, SG_ALERT,
542 "Detected an internal inconsistancy in the autopilot");
543 SG_LOG( SG_ALL, SG_ALERT,
544 " configuration. See earlier errors for" );
545 SG_LOG( SG_ALL, SG_ALERT,
549 } catch (const sg_exception& exc) {
550 SG_LOG( SG_ALL, SG_ALERT, "Failed to load autopilot configuration: "
555 SG_LOG( SG_ALL, SG_WARN,
556 "No autopilot configuration specified for this model!");
561 void FGXMLAutopilot::reinit() {
567 void FGXMLAutopilot::bind() {
570 void FGXMLAutopilot::unbind() {
573 bool FGXMLAutopilot::build() {
574 SGPropertyNode *node;
577 int count = config_props->nChildren();
578 for ( i = 0; i < count; ++i ) {
579 node = config_props->getChild(i);
580 string name = node->getName();
581 // cout << name << endl;
582 if ( name == "pid-controller" ) {
583 FGXMLAutoComponent *c = new FGPIDController( node );
584 components.push_back( c );
586 SG_LOG( SG_ALL, SG_ALERT, "Unknown top level section: "
597 * Update helper values
599 static void update_helper( double dt ) {
600 // Estimate speed in 5,10 seconds
601 static SGPropertyNode *vel = fgGetNode( "/velocities/airspeed-kt", true );
602 static SGPropertyNode *lookahead5
603 = fgGetNode( "/autopilot/internal/lookahead-5-sec-airspeed-kt", true );
604 static SGPropertyNode *lookahead10
605 = fgGetNode( "/autopilot/internal/lookahead-10-sec-airspeed-kt", true );
607 static double average = 0.0; // average/filtered prediction
608 static double v_last = 0.0; // last velocity
610 double v = vel->getDoubleValue();
613 a = (v - v_last) / dt;
616 average = (1.0 - dt) * average + dt * a;
621 lookahead5->setDoubleValue( v + average * 5.0 );
622 lookahead10->setDoubleValue( v + average * 10.0 );
626 // Calculate heading bug error normalized to +/- 180.0
627 static SGPropertyNode *bug
628 = fgGetNode( "/autopilot/settings/heading-bug-deg", true );
629 static SGPropertyNode *ind_hdg
630 = fgGetNode( "/instrumentation/heading-indicator/indicated-heading-deg",
632 static SGPropertyNode *bug_error
633 = fgGetNode( "/autopilot/internal/heading-bug-error-deg", true );
635 double diff = bug->getDoubleValue() - ind_hdg->getDoubleValue();
636 if ( diff < -180.0 ) { diff += 360.0; }
637 if ( diff > 180.0 ) { diff -= 360.0; }
638 bug_error->setDoubleValue( diff );
640 // Calculate true heading error normalized to +/- 180.0
641 static SGPropertyNode *target_true
642 = fgGetNode( "/autopilot/settings/true-heading-deg", true );
643 static SGPropertyNode *true_hdg
644 = fgGetNode( "/orientation/heading-deg", true );
645 static SGPropertyNode *true_error
646 = fgGetNode( "/autopilot/internal/true-heading-error-deg", true );
648 diff = target_true->getDoubleValue() - true_hdg->getDoubleValue();
649 if ( diff < -180.0 ) { diff += 360.0; }
650 if ( diff > 180.0 ) { diff -= 360.0; }
651 true_error->setDoubleValue( diff );
653 // Calculate nav1 target heading error normalized to +/- 180.0
654 static SGPropertyNode *target_nav1
655 = fgGetNode( "/radios/nav[0]/radials/target-auto-hdg-deg", true );
656 static SGPropertyNode *true_nav1
657 = fgGetNode( "/autopilot/internal/nav1-heading-error-deg", true );
659 diff = target_nav1->getDoubleValue() - true_hdg->getDoubleValue();
660 if ( diff < -180.0 ) { diff += 360.0; }
661 if ( diff > 180.0 ) { diff -= 360.0; }
662 true_nav1->setDoubleValue( diff );
667 * Update the list of autopilot components
670 void FGXMLAutopilot::update( double dt ) {
674 for ( i = 0; i < components.size(); ++i ) {
675 components[i]->update( dt );