Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Created November 29, 2017 23:57
Show Gist options
  • Save patrickelectric/d5d74b5a7a32d6a41bc88f9d9be77c29 to your computer and use it in GitHub Desktop.
Save patrickelectric/d5d74b5a7a32d6a41bc88f9d9be77c29 to your computer and use it in GitHub Desktop.
reboot python script to pixhawk
#!/usr/bin/env python
from pymavlink import mavutil
import sys
import time
class Rebooter(object):
def __init__(self, connection_string, baudrate=None):
self.master = None
self.source_system = 13
self.baudrate = baudrate
self.connection_string = connection_string
def connect(self):
self.master = mavutil.mavlink_connection(
self.connection_string,
autoreconnect=False,
source_system=self.source_system,
baud=self.baudrate)
def debug(self, *args, **kwargs):
print(args, kwargs)
def run(self):
self.connect()
acked = False
start = time.time()
while time.time() - start < 5:
self.debug("Sending reboot")
self.master.reboot_autopilot()
m = self.master.recv_match(type='COMMAND_ACK',
blocking=True,
timeout=1)
if m is not None:
# FIXME: check for true ack here
acked = True
print("Got ack: %s" % (str(m)))
break
if not acked:
# FIXME: narrow
raise(Exception("Failed to reboot"))
if __name__ == '__main__':
def print_usage():
print("preflight-reboot.py [--baudrate RATE] DEVICE")
from optparse import OptionParser
parser = OptionParser("preflight-reboot.py [--baudrate RATE] DEVICE")
parser.add_option("--baudrate",
type='int',
help="default serial baud rate",
default=57600)
(opts, args) = parser.parse_args()
if len(args) == 0:
print_usage()
sys.exit(1)
rebooter = Rebooter(args[0], baudrate=opts.baudrate)
rebooter.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment