Repository navigation
Description
By @hamishwillee's suggestion, I'm opening this because I was trying to figure out what happens after a connection abort (i.e. heartbeat_timeout is exceeded), and the documentation does not make it clear. After looking through dronekit/init.py, I see on lines 1099-1102:
if self._heartbeat_error and self._heartbeat_error > 0 and time.time(
) - self._heartbeat_lastreceived > self._heartbeat_error:
raise APIException('No heartbeat in %s seconds, aborting.' %
self._heartbeat_error)Looking at dronekit/mavlink.py, I can see that the exception will cause the handler thread to exit:
try:
while True:
# Downtime
time.sleep(0.05)
# Loop listeners.
for fn in self.loop_listeners:
fn(self)...
except APIException as e:
errprinter('>>> ' + str(e.message))
self._alive = False
self.master.close()
self._death_error = e
returnI think the documentation could be improved by stating that the abort will cause the connection to permanently close (while a heartbeat_warning will not), and that if this behavior is not desired, to set the timeout to a large value. If an abort occurs, @hamishwillee suggested that the only recourse would be a reconnect, and the documentation should state this. Also, in the case of an abort, I'm not sure how to detect it from a script (to go about doing a reconnect).
One other issue, is that a reconnect would issue a new vehicle object, but all the decorated message/attribute listeners on the top level (say in a module) would have already been initialized, and would not be updated. Meaning if you expect a reconnect, you would need to re-declare all your decorated handlers with the new vehicle object (probably inside an initialization function).
Edit to illustrate my point:
# Is only called when the module is imported, cannot be updated...
@vehicle.on_attribute('location')
def listener(...)
...def initialize(vehicle):
# Called each time we call initialize. Sets up listeners on new vehicle
@vehicle.on_attribute('location')
def listener(...)
...It might also be worth considering if it should be possible to disable it completely (say, pass in a timeout of -1).
Activity
mikerob commentedon Nov 24, 2015
I'll post an example of how I'm handling a timeout. It is problematic having the connection drop completely - MAVProxy will continue to listed in the relevant port until something comes back.
Being able to separate initial connection timeout from mid-connection timeout would be useful. In my use case, I'd like to disable the mid-connection timeout completely but still have a short initial one for detecting connections.
*** EDIT: https://github.com/mikerob/dronekit-python/blob/master/pigs/mavmaster.py
atomictom commentedon Nov 24, 2015
@mikerob That's a good point. I would also like to be able to have the initial connect call timeout more quickly, but then either not have connection abort or have the abort occur after a much longer period of time. (It might be good to still have it abort after some time so you can try connecting in an alternate way.)
hamishwillee commentedon Nov 24, 2015
@tcr3dr Some feedback on our connection API here. The requests (in summary) are:
There are other points in there about continuing to listen for the port until something comes back - though obviously it isn't MAVProxy :-)
I'm not sure about the point about decorators/initialisation at module level requiring re-initialisation. Yes it is true, but having the decorators at that level sounds like a design problem with the app, not our architecture.
There is an action on me here to document what happens after abort - but I'd like your feedback on the above before I write it.
atomictom commentedon Nov 25, 2015
@hamishwillee
Mostly that there should probably be an explicit mention of this in the documentation. I actually use Flask for my web server and the idiom there is to have url routes as decorators on module level functions like so:
I imagine others may think that is the natural approach, for example, they may have a package where the init.py creates the vehicle object, then imports the attribute/message listeners which are top-level functions in a sub-module. Perhaps:
drone/init.py
drone/script.py
However, if aborts require calling
connect()again, then that wouldn't work. Obviously, this is an architecture problem that would need to be solved by the user, but it strikes me as a problem that could come up a lot.mikerob commentedon Nov 25, 2015
Is it possible to create a "vehicle.reconnect("/dev/tty...etc")" function that you would use to revive a connection, while keeping everything else intact?
atomictom commentedon Nov 30, 2015
@mikerob While I like the idea of being able to keep the connection from timing out at all, I like this idea as well. The nice thing about what you're suggesting is you can try to connect on another channel than whatever the first connection used.
Evidlo commentedon Jul 25, 2017
Has anything changed here? I haven't found a decent way to detect an abort so that I can reconnect to the Pixhawk if it loses power. Ideally there would be a
vehicle.abortedattribute that I could attach a listener to.Here's one failed attempt at a solution:
But I get
on
vehicle.close(), probably because I'm trying to closevehiclefrom inside its own callback.Evidlo commentedon Jul 25, 2017
Here's a working solution, but too much of a hack in my opinion: