Skip to content

Commit

Permalink
Merge pull request #65 from JSBSim-Team/master
Browse files Browse the repository at this point in the history
Check the number of <input> nodes (issue JSBSim-Team#497).
  • Loading branch information
sthagen authored Sep 19, 2021
2 parents ca2be4d + d53cb46 commit 0a15ccb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/models/flight_control/FGActuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ FGActuator::FGActuator(FGFCS* fcs, Element* element)
initialized = 0;
saturated = false;

CheckInputNodes(1, 1, element);

if ( element->FindElement("deadband_width") ) {
deadband_width = element->FindElementValueAsNumber("deadband_width");
}
if ( element->FindElement("hysteresis_width") ) {
hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
}

// There can be a single rate limit specified, or increasing and
// There can be a single rate limit specified, or increasing and
// decreasing rate limits specified, and rate limits can be numeric, or
// a property.
auto PropertyManager = fcs->GetPropertyManager();
Expand Down Expand Up @@ -134,7 +136,7 @@ void FGActuator::ResetPastStates(void)
FGFCSComponent::ResetPastStates();

PreviousOutput = PreviousHystOutput = PreviousRateLimOutput
= PreviousLagInput = PreviousLagOutput = Output = 0.0;
= PreviousLagInput = PreviousLagOutput = Output = 0.0;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -167,7 +169,7 @@ bool FGActuator::Run(void )
}

PreviousOutput = Output; // previous value needed for "stuck" malfunction

initialized = 1;

Clip();
Expand Down Expand Up @@ -224,7 +226,7 @@ void FGActuator::Hysteresis(void)
// "Output" is - for the purposes of this Hysteresis method - really the input
// to the method.
double input = Output;

if ( initialized ) {
if (input > PreviousHystOutput)
Output = max(PreviousHystOutput, input-0.5*hysteresis_width);
Expand Down
2 changes: 2 additions & 0 deletions src/models/flight_control/FGDeadBand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ FGDeadBand::FGDeadBand(FGFCS* fcs, Element* element)
Width = nullptr;
gain = 1.0;

CheckInputNodes(1, 1, element);

auto PropertyManager = fcs->GetPropertyManager();
Element* width_element = element->FindElement("width");
if (width_element)
Expand Down
28 changes: 26 additions & 2 deletions src/models/flight_control/FGFCSComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ FGFCSComponent::FGFCSComponent(FGFCS* _fcs, Element* element) : fcs(_fcs)
PropertyManager, init_element));
init_element = element->FindNextElement("init");
}

Element *input_element = element->FindElement("input");
while (input_element) {
InputNodes.push_back(new FGPropertyValue(input_element->GetDataLine(),
Expand Down Expand Up @@ -214,6 +214,30 @@ void FGFCSComponent::ResetPastStates(void)

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGFCSComponent::CheckInputNodes(size_t MinNodes, size_t MaxNodes, Element* el)
{
size_t num = InputNodes.size();

if (num < MinNodes) {
cerr << el->ReadFrom()
<< " Not enough <input> nodes are provided" << endl
<< " Expecting " << MinNodes << " while " << num
<< " are provided." << endl;
throw("Some inputs are missing.");
}

if (num > MaxNodes) {
cerr << el->ReadFrom()
<< " Too many <input> nodes are provided" << endl
<< " Expecting " << MaxNodes << " while " << num
<< " are provided." << endl
<< " The last " << num-MaxNodes << " input nodes will be ignored."
<< endl;
}
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGFCSComponent::SetOutput(void)
{
for (auto node: OutputNodes)
Expand Down Expand Up @@ -331,7 +355,7 @@ void FGFCSComponent::Debug(int from)
if (clip) {
cout << " Minimum limit: " << ClipMin->GetName() << endl;
cout << " Maximum limit: " << ClipMax->GetName() << endl;
}
}
if (delay > 0) cout <<" Frame delay: " << delay
<< " frames (" << delay*dt << " sec)" << endl;
}
Expand Down
1 change: 1 addition & 0 deletions src/models/flight_control/FGFCSComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class FGFCSComponent : public FGJSBBase

void Delay(void);
void Clip(void);
void CheckInputNodes(size_t MinNodes, size_t MaxNodes, Element* el);
virtual void bind(Element* el, FGPropertyManager* pm);
virtual void Debug(int from);
};
Expand Down
11 changes: 7 additions & 4 deletions src/models/flight_control/FGFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ FGFilter::FGFilter(FGFCS* fcs, Element* element)
: FGFCSComponent(fcs, element), DynamicFilter(false), Initialize(true)
{
C[1] = C[2] = C[3] = C[4] = C[5] = C[6] = nullptr;

CheckInputNodes(1, 1, element);

auto PropertyManager = fcs->GetPropertyManager();
for (int i=1; i<7; i++)
ReadFilterCoefficients(element, i, PropertyManager);
Expand Down Expand Up @@ -91,11 +94,11 @@ void FGFilter::ResetPastStates(void)
void FGFilter::ReadFilterCoefficients(Element* element, int index,
std::shared_ptr<FGPropertyManager> PropertyManager)
{
// index is known to be 1-7.
// index is known to be 1-7.
// A stringstream would be overkill, but also trying to avoid sprintf
string coefficient = "c0";
coefficient[1] += index;

if ( element->FindElement(coefficient) ) {
C[index] = new FGParameterValue(element->FindElement(coefficient),
PropertyManager);
Expand Down Expand Up @@ -154,9 +157,9 @@ bool FGFilter::Run(void)
} else {

Input = InputNodes[0]->getDoubleValue();

if (DynamicFilter) CalculateDynamicFilters();

switch (FilterType) {
case eLag:
Output = (Input + PreviousInput1) * ca + PreviousOutput1 * cb;
Expand Down
2 changes: 2 additions & 0 deletions src/models/flight_control/FGGain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ FGGain::FGGain(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
InMax = 1.0;
OutMin = OutMax = 0.0;

CheckInputNodes(1, 1, element);

if (Type == "PURE_GAIN") {
if ( !element->FindElement("gain") ) {
cerr << element->ReadFrom()
Expand Down
2 changes: 2 additions & 0 deletions src/models/flight_control/FGKinemat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ FGKinemat::FGKinemat(FGFCS* fcs, Element* element)
double tmpDetent;
double tmpTime;

CheckInputNodes(1, 1, element);

Detents.clear();
TransitionTimes.clear();

Expand Down
2 changes: 2 additions & 0 deletions src/models/flight_control/FGLinearActuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ CLASS IMPLEMENTATION
FGLinearActuator::FGLinearActuator(FGFCS* fcs, Element* element)
: FGFCSComponent(fcs, element)
{
CheckInputNodes(1, 1, element);

ptrSet = nullptr;
auto PropertyManager = fcs->GetPropertyManager();
if (element->FindElement("set")) {
Expand Down
6 changes: 4 additions & 2 deletions src/models/flight_control/FGPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
ProcessVariableDot = nullptr;
IsStandard = false;
IntType = eNone; // No integrator initially defined.
auto PropertyManager = fcs->GetPropertyManager();

CheckInputNodes(1, 1, element);

auto PropertyManager = fcs->GetPropertyManager();
string pid_type = element->GetAttributeValue("type");

if (pid_type == "standard") IsStandard = true;
Expand Down Expand Up @@ -192,7 +194,7 @@ bool FGPID::Run(void )
}

if (test < 0.0) I_out_total = 0.0; // Reset integrator to 0.0

I_out_total += Ki->GetValue() * dt * I_out_delta;

if (IsStandard)
Expand Down

0 comments on commit 0a15ccb

Please sign in to comment.