Skip to content

Commit

Permalink
Allow RX only in the FDM loop
Browse files Browse the repository at this point in the history
  • Loading branch information
julianneswinoga committed Oct 26, 2022
1 parent b504477 commit d65840e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
22 changes: 10 additions & 12 deletions flightgear_python/fg_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
from .general_util import EventPipe, strip_end
from .fg_util import FGConnectionError, FGCommunicationError, fix_fg_radian_parsing

rx_callback_type = Callable[[Struct, EventPipe], Struct]
rx_callback_type = Callable[[Struct, EventPipe], Optional[Struct]]
"""
RX callback function type, signature should be:
.. code-block:: python
def rx_cb(fdm_data: Construct.Struct, event_pipe: EventPipe):
def rx_cb(fdm_data: Construct.Struct, event_pipe: EventPipe) -> Optional[Construct.Struct]:
"""


Expand Down Expand Up @@ -93,20 +93,18 @@ def _fg_packet_roundtrip(self):
s = fix_fg_radian_parsing(s)

# Call user method
if self.event_pipe.is_set() and self.event_pipe.child_poll():
# only update when we have data to send
s = self.fg_rx_cb(s, self.event_pipe)
else:
print('Receiving FG updates but no data to send', flush=True)
sys.stdout.flush()
tx_msg = self.fg_net_struct.build(dict(**s))
s = self.fg_rx_cb(s, self.event_pipe)
sys.stdout.flush() # flush so that `print()` works

# Send data back to FG
if self.fg_tx_sock is not None:
if self.fg_tx_sock is not None and s is not None:
tx_msg = self.fg_net_struct.build(dict(**s))
self.fg_tx_sock.sendto(tx_msg, self.fg_tx_addr)
else:
print(f'Warning: TX not connected, not sending updates to FG for RX {self.fg_rx_sock.getsockname()}')

def _rx_process(self):
if self.fg_tx_sock is None:
print(f'Warning: TX not connected, not sending updates to FG for RX {self.fg_rx_sock.getsockname()}')

self.fg_rx_sock.settimeout(self.rx_timeout_s)
while True:
self._fg_packet_roundtrip()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fdm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ def rx_cb(fdm_data, event_pipe):
run_idx, callback_version = fdm_c.event_pipe.parent_recv()
assert run_idx == i
assert callback_version == fdm_version


@pytest.mark.parametrize('fdm_version', supported_fdm_versions)
def test_fdm_only_rx(mocker, fdm_version):
def rx_cb(fdm_data, event_pipe):
callback_version = fdm_data['version']
event_pipe.child_send((callback_version,))

fdm_c = FDMConnection(fdm_version)

setup_fdm_mock(mocker, fdm_version, fdm_c.fg_net_struct.sizeof())

fdm_c.connect_rx('localhost', 55550, rx_cb)

for i in range(5):
# manually call the process instead of having the process spawn
fdm_c._fg_packet_roundtrip()
callback_version, = fdm_c.event_pipe.parent_recv()
assert callback_version == fdm_version

0 comments on commit d65840e

Please sign in to comment.