]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/models/flight_control/FGPID.cpp
Andreas Gaeb: fix #222 (JSBSIm reset problems)
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGPID.cpp
index 7917d1fedd74c6ad7e06f4c8417b87891f22ffb4..ac265b8ff991275b7cff7e2561a3dc5ce99b44dc 100755 (executable)
@@ -4,7 +4,7 @@
  Author:       Jon S. Berndt
  Date started: 6/17/2006
 
- ------------- Copyright (C) 2006 Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 2006 Jon S. Berndt (jon@jsbsim.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free Software
@@ -36,10 +36,15 @@ INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 #include "FGPID.h"
+#include "input_output/FGXMLElement.h"
+#include <string>
+#include <iostream>
+
+using namespace std;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGPID.cpp,v 1.16 2009/10/24 22:59:30 jberndt Exp $";
 static const char *IdHdr = ID_PID;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -48,16 +53,58 @@ CLASS IMPLEMENTATION
 
 FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
 {
-  dt = fcs->GetState()->Getdt();
+  string kp_string, ki_string, kd_string;
 
   Kp = Ki = Kd = 0.0;
-  P_out = D_out = I_out = 0.0;
+  KpPropertyNode = 0;
+  KiPropertyNode = 0;
+  KdPropertyNode = 0;
+  KpPropertySign = 1.0;
+  KiPropertySign = 1.0;
+  KdPropertySign = 1.0;
+  I_out_total = 0.0;
   Input_prev = Input_prev2 = 0.0;
   Trigger = 0;
 
-  if (element->FindElement("kp")) Kp = element->FindElementValueAsNumber("kp");
-  if (element->FindElement("ki")) Ki = element->FindElementValueAsNumber("ki");
-  if (element->FindElement("kd")) Kd = element->FindElementValueAsNumber("kd");
+  if ( element->FindElement("kp") ) {
+    kp_string = element->FindElementValue("kp");
+    if (!is_number(kp_string)) { // property
+      if (kp_string[0] == '-') {
+       KpPropertySign = -1.0;
+       kp_string.erase(0,1);
+      }
+      KpPropertyNode = PropertyManager->GetNode(kp_string);
+    } else {
+      Kp = element->FindElementValueAsNumber("kp");
+    }
+  }
+
+  if ( element->FindElement("ki") ) {
+    ki_string = element->FindElementValue("ki");
+    if (!is_number(ki_string)) { // property
+      if (ki_string[0] == '-') {
+       KiPropertySign = -1.0;
+       ki_string.erase(0,1);
+      }
+      KiPropertyNode = PropertyManager->GetNode(ki_string);
+    } else {
+      Ki = element->FindElementValueAsNumber("ki");
+    }
+  }
+
+  if ( element->FindElement("kd") ) {
+    kd_string = element->FindElementValue("kd");
+    if (!is_number(kd_string)) { // property
+      if (kd_string[0] == '-') {
+       KdPropertySign = -1.0;
+       kd_string.erase(0,1);
+      }
+      KdPropertyNode = PropertyManager->GetNode(kd_string);
+    } else {
+      Kd = element->FindElementValueAsNumber("kd");
+    }
+  }
+
   if (element->FindElement("trigger")) {
     Trigger =  PropertyManager->GetNode(element->FindElementValue("trigger"));
   }
@@ -78,29 +125,38 @@ FGPID::~FGPID()
 
 bool FGPID::Run(void )
 {
+  double I_out_delta = 0.0;
+  double P_out, D_out;
+
   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
 
-  P_out = Kp * (Input - Input_prev);
-  D_out = (Kd / dt) * (Input - 2*Input_prev + Input_prev2);
+  if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
+  if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
+  if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
+
+  P_out = Kp * Input;
+  D_out = (Kd / dt) * (Input - Input_prev);
 
   // Do not continue to integrate the input to the integrator if a wind-up
   // condition is sensed - that is, if the property pointed to by the trigger
-  // element is non-zero.
+  // element is non-zero. Reset the integrator to 0.0 if the Trigger value
+  // is negative.
 
   if (Trigger != 0) {
     double test = Trigger->getDoubleValue();
-    if (fabs(test) < 0.000001) {
-      I_out = Ki * dt * Input;
-    }
+    if (fabs(test) < 0.000001) I_out_delta = Ki * dt * Input;  // Normal
+    if (test < 0.0)            I_out_total = 0.0;  // Reset integrator to 0.0
   } else { // no anti-wind-up trigger defined
-    I_out = Ki * dt * Input;
+    I_out_delta = Ki * dt * Input;
   }
+  
+  I_out_total += I_out_delta;
 
+  Output = P_out + I_out_total + D_out;
+  
   Input_prev = Input;
   Input_prev2 = Input_prev;
 
-  Output += P_out + I_out + D_out;
-
   Clip();
   if (IsOutput) SetOutput();
 
@@ -137,7 +193,10 @@ void FGPID::Debug(int from)
       else
         cout << "      INPUT: " << InputNodes[0]->getName() << endl;
 
-      if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
+      if (IsOutput) {
+        for (unsigned int i=0; i<OutputNodes.size(); i++)
+          cout << "      OUTPUT: " << OutputNodes[i]->getName() << endl;
+      }
     }
   }
   if (debug_lvl & 2 ) { // Instantiation/Destruction notification