]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/FGFCS.cpp
Updated to match changes in radiostack.[ch]xx
[flightgear.git] / src / FDM / JSBSim / FGFCS.cpp
index a16be4006a6020bbf0c049f2e541cb19dce63d47..57e73bf8c0aa7411a16eb6ac214f9361351e4518 100644 (file)
@@ -47,6 +47,7 @@ INCLUDES
 #include "FGPosition.h"
 #include "FGAuxiliary.h"
 #include "FGOutput.h"
+#include "FGPropertyManager.h"
 
 #include "filtersjb/FGFilter.h"
 #include "filtersjb/FGDeadBand.h"
@@ -59,6 +60,10 @@ INCLUDES
 static const char *IdSrc = "$Id$";
 static const char *IdHdr = ID_FCS;
 
+#if defined(WIN32) && !defined(__CYGWIN__)
+#define snprintf _snprintf
+#endif
+
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
@@ -70,12 +75,17 @@ FGFCS::FGFCS(FGFDMExec* fdmex) : FGModel(fdmex)
 
   DaCmd = DeCmd = DrCmd = DfCmd = DsbCmd = DspCmd = 0.0;
   PTrimCmd = YTrimCmd = RTrimCmd = 0.0;
-  DaLPos = DaRPos = DePos = DrPos = DfPos = DsbPos = DspPos = 0.0;
   GearCmd = GearPos = 1; // default to gear down
   LeftBrake = RightBrake = CenterBrake = 0.0;
   DoNormalize=true;
   
-  for(i=0;i<6;i++) { ToNormalize[i]=-1;}
+  bind();
+  for (i=0;i<=NForms;i++) {
+    DePos[i] = DaLPos[i] = DaRPos[i] = DrPos[i] = 0.0;
+    DfPos[i] = DsbPos[i] = DspPos[i] = 0.0;
+  }
+    
+  for (i=0;i<NNorm;i++) { ToNormalize[i]=-1;}
   Debug(0);
 }
 
@@ -91,6 +101,8 @@ FGFCS::~FGFCS()
   PropAdvance.clear();
 
   unsigned int i;
+  
+  unbind();
 
   for (i=0;i<Components.size();i++) delete Components[i];
   Debug(1);
@@ -107,7 +119,7 @@ bool FGFCS::Run(void)
     for (i=0; i<MixturePos.size(); i++) MixturePos[i] = MixtureCmd[i];
     for (i=0; i<PropAdvance.size(); i++) PropAdvance[i] = PropAdvanceCmd[i];
     for (i=0; i<Components.size(); i++)  Components[i]->Run();
-    if(DoNormalize) Normalize();
+    if (DoNormalize) Normalize();
   } else {
   }
 
@@ -154,7 +166,7 @@ void FGFCS::SetThrottlePos(int engineNum, double setting)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGFCS::GetThrottleCmd(int engineNum)
+double FGFCS::GetThrottleCmd(int engineNum) const
 {
   if (engineNum < (int)ThrottlePos.size()) {
     if (engineNum < 0) {
@@ -172,7 +184,7 @@ double FGFCS::GetThrottleCmd(int engineNum)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGFCS::GetThrottlePos(int engineNum)
+double FGFCS::GetThrottlePos(int engineNum) const
 {
   if (engineNum < (int)ThrottlePos.size()) {
     if (engineNum < 0) {
@@ -257,7 +269,7 @@ bool FGFCS::Load(FGConfigFile* AC_cfg)
   
   Name = Name + ":" + AC_cfg->GetValue("NAME");
   if (debug_lvl > 0) cout << "    Control System Name: " << Name << endl;
-  if( AC_cfg->GetValue("NORMALIZE") == "FALSE") {
+  if ( AC_cfg->GetValue("NORMALIZE") == "FALSE") {
       DoNormalize=false;
       cout << "    Automatic Control Surface Normalization Disabled" << endl;
   }    
@@ -298,36 +310,40 @@ bool FGFCS::Load(FGConfigFile* AC_cfg)
     }
   }
   //collect information for normalizing control surfaces
-  
-  for(i=0;i<Components.size();i++) {
+  string nodeName;
+  for (i=0;i<Components.size();i++) {
     
-    if(Components[i]->GetType() == "AEROSURFACE_SCALE" 
-        || Components[i]->GetType() == "KINEMAT"  ) {
-      if( Components[i]->GetOutputIdx() == FG_ELEVATOR_POS ) {
-        ToNormalize[iNDe]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_LEFT_AILERON_POS 
-                      || Components[i]->GetOutputIdx() == FG_AILERON_POS ) {
-        ToNormalize[iNDaL]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_RIGHT_AILERON_POS ) {
-        ToNormalize[iNDaR]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_RUDDER_POS ) {
-        ToNormalize[iNDr]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_SPDBRAKE_POS ) {
-        ToNormalize[iNDsb]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_SPOILERS_POS ) {
-        ToNormalize[iNDsp]=i;
-      } else if ( Components[i]->GetOutputIdx() == FG_FLAPS_POS ) {
-        ToNormalize[iNDf]=i;
+    if ( (Components[i]->GetType() == "AEROSURFACE_SCALE" 
+          || Components[i]->GetType() == "KINEMAT")  
+                    && Components[i]->GetOutputNode() ) { 
+      nodeName= Components[i]->GetOutputNode()->GetName();  
+      if ( nodeName == "elevator-pos-rad" ) {
+        ToNormalize[iDe]=i;
+      } else if ( nodeName  == "left-aileron-pos-rad" 
+                   || nodeName == "aileron-pos-rad" ) {
+        ToNormalize[iDaL]=i;
+      } else if ( nodeName == "right-aileron-pos-rad" ) {
+        ToNormalize[iDaR]=i;
+      } else if ( nodeName == "rudder-pos-rad" ) {
+        ToNormalize[iDr]=i;
+      } else if ( nodeName == "speedbrake-pos-rad" ) {
+        ToNormalize[iDsb]=i;
+      } else if ( nodeName == "spoiler-pos-rad" ) {
+        ToNormalize[iDsp]=i;
+      } else if ( nodeName == "flap-pos-deg" ) {
+        ToNormalize[iDf]=i;
       }
     }
   }     
   
+  bindModel();
+  
   return true;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGFCS::GetComponentOutput(eParam idx) {
+double FGFCS::GetComponentOutput(int idx) {
   return Components[idx]->GetOutput();
 }
 
@@ -407,29 +423,269 @@ void FGFCS::AddThrottle(void)
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGFCS::Normalize(void) {
-  if( ToNormalize[iNDe] > -1 ) 
-    DePosN = Components[ToNormalize[iNDe]]->GetOutputPct();
   
-  if( ToNormalize[iNDaL] > -1 ) 
-    DaLPosN = Components[ToNormalize[iNDaL]]->GetOutputPct();
+  //not all of these are guaranteed to be defined for every model
+  //those that are have an index >=0 in the ToNormalize array
+  //ToNormalize is filled in Load()
   
-  if( ToNormalize[iNDaR] > -1 ) 
-    DaRPosN = Components[ToNormalize[iNDaR]]->GetOutputPct();
+  if ( ToNormalize[iDe] > -1 ) {
+    DePos[ofNorm] = Components[ToNormalize[iDe]]->GetOutputPct();
+  }
   
-  if( ToNormalize[iNDr] > -1 ) 
-    DrPosN = Components[ToNormalize[iNDr]]->GetOutputPct();
+  if ( ToNormalize[iDaL] > -1 ) {
+    DaLPos[ofNorm] = Components[ToNormalize[iDaL]]->GetOutputPct();
+  }
+  
+  if ( ToNormalize[iDaR] > -1 ) {
+    DaRPos[ofNorm] = Components[ToNormalize[iDaR]]->GetOutputPct();
+  }
+
+  if ( ToNormalize[iDr] > -1 ) {
+    DrPos[ofNorm] = Components[ToNormalize[iDr]]->GetOutputPct();
+  }
        
-  if( ToNormalize[iNDsb] > -1 ) 
-    DsbPosN = Components[ToNormalize[iNDsb]]->GetOutputPct();
+  if ( ToNormalize[iDsb] > -1 ) { 
+    DsbPos[ofNorm] = Components[ToNormalize[iDsb]]->GetOutputPct();
+  }
+  
+  if ( ToNormalize[iDsp] > -1 ) {
+    DspPos[ofNorm] = Components[ToNormalize[iDsp]]->GetOutputPct();
+  }
   
-  if( ToNormalize[iNDsp] > -1 ) 
-    DspPosN = Components[ToNormalize[iNDsp]]->GetOutputPct();
+  if ( ToNormalize[iDf] > -1 ) {
+    DfPos[ofNorm] = Components[ToNormalize[iDf]]->GetOutputPct();
+  }
   
-  if( ToNormalize[iNDf] > -1 ) 
-    DfPosN = Components[ToNormalize[iNDf]]->GetOutputPct();
+  DePos[ofMag]  = fabs(DePos[ofRad]);
+  DaLPos[ofMag] = fabs(DaLPos[ofRad]);
+  DaRPos[ofMag] = fabs(DaRPos[ofRad]);
+  DrPos[ofMag]  = fabs(DrPos[ofRad]);
+  DsbPos[ofMag] = fabs(DsbPos[ofRad]);
+  DspPos[ofMag] = fabs(DspPos[ofRad]);
+  DfPos[ofMag]  = fabs(DfPos[ofRad]);
    
 }  
     
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGFCS::bind(void)
+{
+  PropertyManager->Tie("fcs/aileron-cmd-norm", this,
+                       &FGFCS::GetDaCmd,
+                       &FGFCS::SetDaCmd,
+                       true);
+  PropertyManager->Tie("fcs/elevator-cmd-norm", this,
+                       &FGFCS::GetDeCmd,
+                       &FGFCS::SetDeCmd,
+                       true);
+  PropertyManager->Tie("fcs/rudder-cmd-norm", this,
+                       &FGFCS::GetDrCmd,
+                       &FGFCS::SetDrCmd,
+                       true);
+  PropertyManager->Tie("fcs/flap-cmd-norm", this,
+                       &FGFCS::GetDfCmd,
+                       &FGFCS::SetDfCmd,
+                       true);
+  PropertyManager->Tie("fcs/speedbrake-cmd-norm", this,
+                       &FGFCS::GetDsbCmd,
+                       &FGFCS::SetDsbCmd,
+                       true);
+  PropertyManager->Tie("fcs/spoiler-cmd-norm", this,
+                       &FGFCS::GetDspCmd,
+                       &FGFCS::SetDspCmd,
+                       true);
+  PropertyManager->Tie("fcs/pitch-trim-cmd-norm", this,
+                       &FGFCS::GetPitchTrimCmd,
+                       &FGFCS::SetPitchTrimCmd,
+                       true);
+  PropertyManager->Tie("fcs/roll-trim-cmd-norm", this,
+                       &FGFCS::GetYawTrimCmd,
+                       &FGFCS::SetYawTrimCmd,
+                       true);
+  PropertyManager->Tie("fcs/yaw-trim-cmd-norm", this,
+                       &FGFCS::GetRollTrimCmd,
+                       &FGFCS::SetRollTrimCmd,
+                       true);
+  PropertyManager->Tie("gear/gear-cmd-norm", this,
+                       &FGFCS::GetGearCmd,
+                       &FGFCS::SetGearCmd,
+                       true);
+  
+  PropertyManager->Tie("fcs/left-aileron-pos-rad", this,ofRad,
+                       &FGFCS::GetDaLPos,
+                       &FGFCS::SetDaLPos,
+                       true);
+  PropertyManager->Tie("fcs/left-aileron-pos-norm", this,ofNorm,
+                       &FGFCS::GetDaLPos,
+                       &FGFCS::SetDaLPos,
+                       true);
+  PropertyManager->Tie("fcs/mag-left-aileron-pos-rad", this,ofMag,
+                       &FGFCS::GetDaLPos,
+                       &FGFCS::SetDaLPos,
+                       true);
+  PropertyManager->Tie("fcs/right-aileron-pos-rad", this,ofRad,
+                       &FGFCS::GetDaRPos,
+                       &FGFCS::SetDaRPos,
+                       true);
+  PropertyManager->Tie("fcs/right-aileron-pos-norm", this,ofNorm,
+                       &FGFCS::GetDaRPos,
+                       &FGFCS::SetDaRPos,
+                       true);
+  PropertyManager->Tie("fcs/mag-right-aileron-pos-rad", this,ofMag,
+                       &FGFCS::GetDaRPos,
+                       &FGFCS::SetDaRPos,
+                       true);
+  
+  PropertyManager->Tie("fcs/elevator-pos-rad", this, ofRad,
+                       &FGFCS::GetDePos,
+                       &FGFCS::SetDePos,
+                       true );
+  PropertyManager->Tie("fcs/elevator-pos-norm", this,ofNorm,
+                       &FGFCS::GetDePos,                       
+                       &FGFCS::SetDePos,
+                       true );
+  PropertyManager->Tie("fcs/mag-elevator-pos-rad", this,ofMag,
+                       &FGFCS::GetDePos,
+                       &FGFCS::SetDePos,
+                       true );
+  
+  PropertyManager->Tie("fcs/rudder-pos-rad", this,ofRad,
+                       &FGFCS::GetDrPos,
+                       &FGFCS::SetDrPos,
+                       true);
+  PropertyManager->Tie("fcs/rudder-pos-norm", this,ofNorm,
+                       &FGFCS::GetDrPos,
+                       &FGFCS::SetDrPos,
+                       true);
+  PropertyManager->Tie("fcs/mag-rudder-pos-rad", this,ofMag,
+                       &FGFCS::GetDrPos,
+                       &FGFCS::SetDrPos,
+                       true);
+                       
+  PropertyManager->Tie("fcs/flap-pos-deg", this,ofRad,
+                       &FGFCS::GetDfPos,
+                       &FGFCS::SetDfPos,
+                       true);
+  PropertyManager->Tie("fcs/flap-pos-norm", this,ofNorm,
+                       &FGFCS::GetDfPos,
+                       &FGFCS::SetDfPos,
+                       true);
+  
+  PropertyManager->Tie("fcs/speedbrake-pos-rad", this,ofRad,
+                       &FGFCS::GetDsbPos,
+                       &FGFCS::SetDsbPos,
+                       true);
+  PropertyManager->Tie("fcs/speedbrake-pos-norm", this,ofNorm,
+                       &FGFCS::GetDsbPos,
+                       &FGFCS::SetDsbPos,
+                       true);
+  PropertyManager->Tie("fcs/mag-speedbrake-pos-rad", this,ofMag,
+                       &FGFCS::GetDsbPos,
+                       &FGFCS::SetDsbPos,
+                       true);
+                       
+  PropertyManager->Tie("fcs/spoiler-pos-rad", this,ofRad,
+                       &FGFCS::GetDspPos,
+                       &FGFCS::SetDspPos,
+                       true);
+  PropertyManager->Tie("fcs/spoiler-pos-norm", this,ofNorm,
+                       &FGFCS::GetDspPos,
+                       &FGFCS::SetDspPos,
+                       true);
+  PropertyManager->Tie("fcs/mag-spoiler-pos-rad", this,ofMag,
+                       &FGFCS::GetDspPos,
+                       &FGFCS::SetDspPos,
+                       true);
+                       
+  PropertyManager->Tie("gear/gear-pos-norm", this,
+                       &FGFCS::GetGearPos,
+                       &FGFCS::SetGearPos,
+                       true);
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGFCS::bindModel(void)
+{
+  unsigned i;
+  char tmp[80];
+  
+  for (i=0; i<ThrottleCmd.size(); i++) {
+    snprintf(tmp,80,"fcs/throttle-cmd-norm[%u]",i);
+    PropertyManager->Tie( tmp,this,i,
+                          &FGFCS::GetThrottleCmd,
+                          &FGFCS::SetThrottleCmd,
+                          true );
+    snprintf(tmp,80,"fcs/throttle-pos-norm[%u]",i);                      
+    PropertyManager->Tie( tmp,this,i,
+                          &FGFCS::GetThrottlePos,
+                          &FGFCS::SetThrottlePos,
+                          true );
+    if ( MixtureCmd.size() > i ) {
+      snprintf(tmp,80,"fcs/mixture-cmd-norm[%u]",i); 
+      PropertyManager->Tie( tmp,this,i,
+                            &FGFCS::GetMixtureCmd,
+                            &FGFCS::SetMixtureCmd,
+                            true );
+      snprintf(tmp,80,"fcs/mixture-pos-norm[%u]",i);                    
+      PropertyManager->Tie( tmp,this,i,
+                            &FGFCS::GetMixturePos,
+                            &FGFCS::SetMixturePos,
+                            true );
+    }
+    if ( PropAdvanceCmd.size() > i ) {
+      snprintf(tmp,80,"fcs/advance-cmd-norm[%u]",i); 
+      PropertyManager->Tie( tmp,this,i,
+                            &FGFCS::GetPropAdvanceCmd,
+                            &FGFCS::SetPropAdvanceCmd,
+                            true );
+      snprintf(tmp,80,"fcs/advance-pos-norm[%u]",i);                       
+      PropertyManager->Tie( tmp,this,i,
+                            &FGFCS::GetPropAdvance,
+                            &FGFCS::SetPropAdvance,
+                            true );
+    }
+  }
+}                            
+                          
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGFCS::unbind(void)
+{
+  PropertyManager->Untie("fcs/aileron-cmd-norm");
+  PropertyManager->Untie("fcs/elevator-cmd-norm");
+  PropertyManager->Untie("fcs/rudder-cmd-norm");
+  PropertyManager->Untie("fcs/flap-cmd-norm");
+  PropertyManager->Untie("fcs/speedbrake-cmd-norm");
+  PropertyManager->Untie("fcs/spoiler-cmd-norm");
+  PropertyManager->Untie("fcs/pitch-trim-cmd-norm");
+  PropertyManager->Untie("fcs/roll-trim-cmd-norm");
+  PropertyManager->Untie("fcs/yaw-trim-cmd-norm");
+  PropertyManager->Untie("gear/gear-cmd-norm");
+  PropertyManager->Untie("fcs/left-aileron-pos-rad");
+  PropertyManager->Untie("fcs/mag-left-aileron-pos-rad");
+  PropertyManager->Untie("fcs/left-aileron-pos-norm");
+  PropertyManager->Untie("fcs/right-aileron-pos-rad");
+  PropertyManager->Untie("fcs/mag-right-aileron-pos-rad");
+  PropertyManager->Untie("fcs/right-aileron-pos-norm");
+  PropertyManager->Untie("fcs/elevator-pos-rad");
+  PropertyManager->Untie("fcs/mag-elevator-pos-rad");
+  PropertyManager->Untie("fcs/elevator-pos-norm");
+  PropertyManager->Untie("fcs/rudder-pos-rad");
+  PropertyManager->Untie("fcs/mag-rudder-pos-rad");
+  PropertyManager->Untie("fcs/rudder-pos-norm");
+  PropertyManager->Untie("fcs/flap-pos-deg");
+  PropertyManager->Untie("fcs/flap-pos-norm");
+  PropertyManager->Untie("fcs/speedbrake-pos-rad");
+  PropertyManager->Untie("fcs/mag-speedbrake-pos-rad");
+  PropertyManager->Untie("fcs/speedbrake-pos-norm");
+  PropertyManager->Untie("fcs/spoiler-pos-rad");
+  PropertyManager->Untie("fcs/mag-spoiler-pos-rad");
+  PropertyManager->Untie("fcs/spoiler-pos-norm");
+  PropertyManager->Untie("gear/gear-pos-norm");
+}
+
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //    The bitmasked value choices are as follows:
 //    unset: In this case (the default) JSBSim would only print