diff --git a/droneapi/lib/__init__.py b/droneapi/lib/__init__.py index 26bd20853..99528bee5 100644 --- a/droneapi/lib/__init__.py +++ b/droneapi/lib/__init__.py @@ -704,6 +704,22 @@ def mavrx_debug_handler(message): """ self.mavrx_callback = callback + def unset_mavlink_callback(self): + """ + Clears the asynchronous notification added by set_mavlink_callback. + + The code snippet below shows how to set, then clear, a MAVLink callback function. + + .. code:: python + # Set MAVLink callback handler (after getting Vehicle instance) + vehicle.set_mavlink_callback(mavrx_debug_handler) + + # Remove the MAVLink callback handler. Callback will not be + # called after this point. + vehicle.unset_mavlink_callback() + """ + self.mavrx_callback = None + def flush(self): """ It is important to understand that setting attributes/changing vehicle state may occur over a slow link. diff --git a/droneapi/module/api.py b/droneapi/module/api.py index 0992a1e7a..245d7b0c8 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -489,7 +489,7 @@ def set(chnum, v): self.__on_change('rangefinder') - if (self.vehicle is not None) and hasattr(self.vehicle, 'mavrx_callback'): + if (self.vehicle is not None) and getattr(self.vehicle, 'mavrx_callback', None): self.vehicle.mavrx_callback(m) self.send_to_server(m) diff --git a/tests/sitl/__main__.py b/tests/sitl/__main__.py index c7e6b91ce..d2dc2285f 100755 --- a/tests/sitl/__main__.py +++ b/tests/sitl/__main__.py @@ -184,6 +184,7 @@ def lets_run_a_test(name): # NOTE these are *very inappropriate settings* # to make on a real vehicle. They are leveraged # exclusively for simulation. Take heed!!! + p.stdin.write('set moddebug 3\n') p.stdin.write('param set ARMING_CHECK 0\n') p.stdin.write('param set FS_THR_ENABLE 0\n') p.stdin.write('param set FS_GCS_ENABLE 0\n') diff --git a/tests/sitl/test_115.py b/tests/sitl/test_115.py new file mode 100644 index 000000000..4805e92d9 --- /dev/null +++ b/tests/sitl/test_115.py @@ -0,0 +1,37 @@ +from droneapi.lib import VehicleMode +from pymavlink import mavutil +import time +import sys +import os + +def test_115(local_connect): + api = local_connect() + v = api.get_vehicles()[0] + + # Dummy callback + def noop(*args): + pass + + # Set the callback. + v.set_mavlink_callback(noop) + + # Change the vehicle into STABILIZE mode + v.mode = VehicleMode("STABILIZE") + + # NOTE wait crudely for ACK on mode update + time.sleep(3) + + # Unset the callback. + v.set_mavlink_callback(None) + + # Disarm should not throw errors. + v.armed = False + v.flush() + + time.sleep(3) + + # Re-arm should not throw errors. + v.armed = True + v.flush() + + time.sleep(3)