Skip to content

Commit a861004

Browse files
Expose Input enable/disable and implement for additional cases (#1459)
Co-authored-by: Sean McLeod <sean@seanmcleod.com>
1 parent d6fee6b commit a861004

11 files changed

Lines changed: 103 additions & 37 deletions

File tree

python/jsbsim.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ cdef extern from "FGFDMExec.h" namespace "JSBSim":
249249
void DoTrim(int mode) except +convertJSBSimToPyExc
250250
void DisableOutput()
251251
void EnableOutput()
252+
void DisableInput()
253+
void EnableInput()
252254
void Hold()
253255
void EnableIncrementThenHold(int time_steps)
254256
void CheckIncrementalHold()

python/jsbsim.pyx.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,14 @@ cdef class FGFDMExec(FGJSBBase):
10181018
"""@Dox(JSBSim::FGFDMExec::EnableOutput)"""
10191019
self.thisptr.EnableOutput()
10201020

1021+
def disable_input(self) -> None:
1022+
"""@Dox(JSBSim::FGFDMExec::DisableInput)"""
1023+
self.thisptr.DisableInput()
1024+
1025+
def enable_input(self) -> None:
1026+
"""@Dox(JSBSim::FGFDMExec::EnableInput)"""
1027+
self.thisptr.EnableInput()
1028+
10211029
def hold(self) -> None:
10221030
"""@Dox(JSBSim::FGFDMExec::Hold)"""
10231031
self.thisptr.Hold()

src/FGFDMExec.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ INCLUDES
4545

4646
#include "models/FGPropagate.h"
4747
#include "models/FGOutput.h"
48+
#include "models/FGInput.h"
4849
#include "math/FGTemplateFunc.h"
4950

5051
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -66,7 +67,6 @@ class FGExternalReactions;
6667
class FGGroundReactions;
6768
class FGFCS;
6869
class FGInertial;
69-
class FGInput;
7070
class FGPropulsion;
7171
class FGMassBalance;
7272
class FGLogger;
@@ -485,6 +485,10 @@ class JSBSIM_API FGFDMExec : public FGJSBBase
485485
void DisableOutput(void) { Output->Disable(); }
486486
/// Enables data logging to all outputs.
487487
void EnableOutput(void) { Output->Enable(); }
488+
/// Disables input from all inputs.
489+
void DisableInput(void) { Input->Disable(); }
490+
/// Enables input from all inputs.
491+
void EnableInput(void) { Input->Enable(); }
488492
/// Pauses execution by preventing time from incrementing.
489493
void Hold(void) {holding = true;}
490494
/// Turn on hold after increment

src/input_output/FGInputSocket.cpp

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,14 @@ namespace JSBSim {
5353
CLASS IMPLEMENTATION
5454
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
5555

56-
FGInputSocket::FGInputSocket(FGFDMExec* fdmex) :
57-
FGInputType(fdmex), socket(0), SockProtocol(FGfdmSocket::ptTCP),
56+
FGInputSocket::FGInputSocket(FGFDMExec* fdmex, bool isEnabled) :
57+
FGInputType(fdmex, isEnabled), SockProtocol(FGfdmSocket::ptTCP),
5858
BlockingInput(false)
5959
{
6060
}
6161

6262
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6363

64-
FGInputSocket::~FGInputSocket()
65-
{
66-
delete socket;
67-
}
68-
69-
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70-
7164
bool FGInputSocket::Load(Element* el)
7265
{
7366
if (!FGInputType::Load(el))
@@ -92,17 +85,55 @@ bool FGInputSocket::Load(Element* el)
9285

9386
bool FGInputSocket::InitModel(void)
9487
{
95-
if (FGInputType::InitModel()) {
96-
delete socket;
97-
socket = new FGfdmSocket(SockPort, SockProtocol);
88+
if (!FGInputType::InitModel())
89+
return false;
9890

99-
if (socket == 0) return false;
100-
if (!socket->GetConnectStatus()) return false;
91+
if (enabled)
92+
return CreateSocket();
10193

102-
return true;
103-
}
94+
return true;
95+
}
96+
97+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98+
99+
bool FGInputSocket::CreateSocket()
100+
{
101+
socket = std::make_unique<FGfdmSocket>(SockPort, SockProtocol);
102+
103+
if (!socket) return false;
104+
if (!socket->GetConnectStatus()) return false;
105+
106+
return true;
107+
}
108+
109+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110+
111+
void FGInputSocket::Enable()
112+
{
113+
if (enabled) return; // already enabled
114+
FGInputType::Enable();
115+
CreateSocket();
116+
}
117+
118+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119+
120+
void FGInputSocket::Disable()
121+
{
122+
FGInputType::Disable();
123+
socket.reset();
124+
}
125+
126+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127+
128+
bool FGInputSocket::Toggle()
129+
{
130+
bool enabled_status = FGInputType::Toggle();
131+
if (enabled_status)
132+
CreateSocket();
133+
else
134+
socket.reset();
104135

105-
return false;
136+
return enabled_status;
106137
}
107138

108139
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

src/input_output/FGInputSocket.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ class FGInputSocket : public FGInputType
6363
{
6464
public:
6565
/** Constructor. */
66-
FGInputSocket(FGFDMExec* fdmex);
67-
68-
/** Destructor. */
69-
~FGInputSocket() override;
66+
FGInputSocket(FGFDMExec* fdmex, bool isEnabled);
7067

7168
/** Init the input directives from an XML file.
7269
@param element XML Element that is pointing to the input directives
@@ -82,10 +79,17 @@ class FGInputSocket : public FGInputType
8279
/// Generates the input.
8380
void Read(bool Holding) override;
8481

82+
/// Methods to enable, disable and toggle the input
83+
void Enable() override;
84+
void Disable() override;
85+
bool Toggle() override;
86+
8587
protected:
8688

89+
bool CreateSocket();
90+
8791
unsigned int SockPort;
88-
FGfdmSocket* socket;
92+
std::unique_ptr<FGfdmSocket> socket;
8993
FGfdmSocket::ProtocolType SockProtocol;
9094
std::string data;
9195
bool BlockingInput;

src/input_output/FGInputType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ namespace JSBSim {
4949
CLASS IMPLEMENTATION
5050
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
5151

52-
FGInputType::FGInputType(FGFDMExec* fdmex) :
53-
FGModel(fdmex), enabled(true)
52+
FGInputType::FGInputType(FGFDMExec* fdmex, bool isEnabled) :
53+
FGModel(fdmex), enabled(isEnabled)
5454
{
5555
Debug(0);
5656
}

src/input_output/FGInputType.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class FGInputType : public FGModel
7676
/** Constructor (implement the FGModel interface).
7777
@param fdmex a pointer to the parent executive object
7878
*/
79-
FGInputType(FGFDMExec* fdmex);
79+
FGInputType(FGFDMExec* fdmex, bool isEnabled=true);
8080

8181
/// Destructor
8282
~FGInputType() override;
@@ -109,13 +109,13 @@ class FGInputType : public FGModel
109109
virtual void Read(bool Holding) = 0;
110110

111111
/// Enables the input generation.
112-
void Enable(void) { enabled = true; }
112+
virtual void Enable(void) { enabled = true; }
113113
/// Disables the input generation.
114-
void Disable(void) { enabled = false; }
114+
virtual void Disable(void) { enabled = false; }
115115
/** Toggles the input generation.
116116
@result the input generation status i.e. true if the input has been
117117
enabled, false if the input has been disabled. */
118-
bool Toggle(void) {enabled = !enabled; return enabled;}
118+
virtual bool Toggle(void) {enabled = !enabled; return enabled;}
119119

120120
/** Overwrites the name identifier under which the input will be read.
121121
This method is taken into account if it is called before

src/input_output/FGUDPInputSocket.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ namespace JSBSim {
5151
CLASS IMPLEMENTATION
5252
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
5353

54-
FGUDPInputSocket::FGUDPInputSocket(FGFDMExec* fdmex) :
55-
FGInputSocket(fdmex), rate(20), oldTimeStamp(0.0)
54+
FGUDPInputSocket::FGUDPInputSocket(FGFDMExec* fdmex, bool isEnabled) :
55+
FGInputSocket(fdmex, isEnabled), rate(20), oldTimeStamp(0.0)
5656
{
5757
SockPort = 5139;
5858
SockProtocol = FGfdmSocket::ptUDP;
@@ -90,7 +90,7 @@ bool FGUDPInputSocket::Load(Element* el)
9090

9191
void FGUDPInputSocket::Read(bool Holding)
9292
{
93-
if (socket == 0) return;
93+
if (!socket) return;
9494

9595
data = socket->Receive();
9696

src/input_output/FGUDPInputSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class FGUDPInputSocket : public FGInputSocket
6161
{
6262
public:
6363
/** Constructor. */
64-
FGUDPInputSocket(FGFDMExec* fdmex);
64+
FGUDPInputSocket(FGFDMExec* fdmex, bool isEnabled=true);
6565

6666
/** Reads the property names from an XML file.
6767
@param element The root XML Element of the input file.

src/models/FGInput.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ bool FGInput::Load(Element* el)
101101
type = to_upper(type);
102102

103103
if (type.empty() || type == "SOCKET") {
104-
Input = new FGInputSocket(FDMExec);
104+
Input = new FGInputSocket(FDMExec, enabled);
105105
} else if (type == "QTJSBSIM") {
106-
Input = new FGUDPInputSocket(FDMExec);
106+
Input = new FGUDPInputSocket(FDMExec, enabled);
107107
} else if (type != string("NONE")) {
108108
FGXMLLogging log(element, LogLevel::ERROR);
109109
log << "Unknown type of input specified in config file" << endl;
@@ -203,6 +203,23 @@ string FGInput::GetInputName(unsigned int idx) const
203203
return name;
204204
}
205205

206+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207+
208+
/// Enables the input generation for all input instances.
209+
void FGInput::Enable(void)
210+
{
211+
enabled = true;
212+
for (auto input : InputTypes)
213+
input->Enable();
214+
}
215+
216+
/// Disables the input generation for all input instances.
217+
void FGInput::Disable(void)
218+
{
219+
enabled = false;
220+
for (auto input : InputTypes)
221+
input->Disable();
222+
}
206223

207224
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208225
// The bitmasked value choices are as follows:

0 commit comments

Comments
 (0)