Added try...except to be more safe after pprzlink api changes. (#2548)

* Updated pprzlink library to the latest version

* Added try...except to crazyradio2ivy.py to be safer after pprzlink API changes. paparazzi/pprzlink#117

* Updated request messages with the new pprzlink API

* Added a #noqa comment so linters won't remove the seemingly unused import.

* Updated the guided_mode_example.py to have a wait timeout.
This commit is contained in:
Mohammad Jafar Mashhadi
2020-07-13 17:01:36 -06:00
committed by GitHub
parent 4575375876
commit eeac31e1e4
5 changed files with 24 additions and 32 deletions
@@ -98,7 +98,12 @@ class RadioBridge:
print("Got message {} from {}".format(msg.name, sender_id))
# Forward message to Ivy bus
if self.is_connected:
self._ivy.send(msg, sender_id=sender_id)
try:
self._ivy.send(msg, sender_id=sender_id)
except RuntimeError as e:
print("Runtime error {}".format(e))
except ValueError as e:
print("Invalid message error {}".format(e))
def _link_quality_cb(self, quality):
pass
@@ -161,17 +161,26 @@ class IvyRequester(object):
self._interface = None
def get_aircrafts(self):
wait_step = 0.1
timeout = 30 / wait_step # 30 seconds
new_answer = False
def aircrafts_cb(ac_id, msg):
global new_answer
self.ac_list = [int(a) for a in msg['ac_list'].split(',') if a]
print("aircrafts: {}".format(self.ac_list))
new_answer = True
self._interface.subscribe(aircrafts_cb, "(.*AIRCRAFTS .*)")
sender = 'get_aircrafts'
request_id = '42_1' # fake request id, should be PID_index
self._interface.send("{} {} AIRCRAFTS_REQ".format(sender, request_id))
self._interface.send_request('ground', "AIRCRAFTS", aircrafts_cb)
# hack: sleep briefly to wait for answer
sleep(0.1)
while not new_answer and timeout > 0:
sleep(wait_step)
timeout -= 1
if not new_answer:
print("WARNING: Getting the list of aircraft timed out. The results might be outdated.")
# Didn't raise an exception or return None in order to not break the API
return self.ac_list
+2 -24
View File
@@ -125,7 +125,6 @@ class PprzConnect(object):
"""
self.verbose = verbose
self._notify = notify
self._req_idx = 0
self._conf_list_by_name = {}
self._conf_list_by_id = {}
@@ -184,27 +183,6 @@ class PprzConnect(object):
"""
return self._ivy
def _get_req_id(self):
req_id = '{}_{}'.format(getpid(), self._req_idx)
self._req_idx += 1
return req_id
def _message_req(self, msg_name, cb, params=None):
bind_id = None
def _cb(sender, msg):
if bind_id is not None:
self._ivy.unsubscribe(bind_id)
cb(sender, msg)
req_id = self._get_req_id()
req_regex = '^{} ([^ ]* +{}( .*|$))'.format(req_id, msg_name)
bind_id = self._ivy.subscribe(_cb, req_regex)
req_msg = PprzMessage('ground','{}_REQ'.format(msg_name))
if params is not None:
req_msg.set_values(params)
#FIXME we shouldn't use directly Ivy, but pprzlink python API is not supporting the request id for now
IvySendMsg('pprz_connect {} {} {}'.format(req_id, req_msg.name, req_msg.payload_to_ivy_string()))
#self._ivy.send(req_msg)
def get_aircrafts(self):
"""
request all aircrafts IDs from a runing server
@@ -217,7 +195,7 @@ class PprzConnect(object):
#ac_list = [int(a) for a in msg['ac_list'].split(',') if a]
if self.verbose:
print("aircrafts: {}".format(ac_list))
self._message_req("AIRCRAFTS", aircrafts_cb)
self._ivy.send_request('ground', "AIRCRAFTS", aircrafts_cb)
def new_ac_cb(sender, msg):
ac_id = msg['ac_id']
@@ -243,7 +221,7 @@ class PprzConnect(object):
self._notify(conf) # user defined general callback
if self.verbose:
print(conf)
self._message_req("CONFIG", conf_cb, [ac_id])
self._ivy.send_request('ground', "CONFIG", conf_cb, ac_id=ac_id)
if __name__ == '__main__':
+1 -1
View File
@@ -124,7 +124,7 @@ def main():
ivy.subscribe(worldenv_cb,'(.* WORLD_ENV_REQ .*)')
# wait for ivy to stop
from ivy.std_api import IvyMainLoop
from ivy.std_api import IvyMainLoop # noqa
signal.pause()