Last active
May 3, 2019 13:17
-
-
Save ghostface/67de1998af68e18212a9ede03666fd3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re, argparse, sys, time | |
import shutil, os | |
from serial import Serial | |
# call like this ./serial_test.py /dev/ttyACM0 115200 labeloflog | |
# note: edit paths in script accordingly - could/should be cleaned up a bit | |
CLI_DELIMITER = '# ' | |
class TimeoutException(Exception): | |
pass | |
def recv_until(ser, delimiter): | |
recv_buf = '' | |
while not delimiter in recv_buf: | |
b = ser.read(1) | |
if not b: | |
raise TimeoutException('Timeout while waiting for a reponse from FC. Powercycle the FC and retry.') | |
recv_buf += b.decode() | |
return recv_buf | |
def drawProgressBar(percent, speed, eta, barLen = 50): | |
sys.stdout.write("\r") | |
progress = "" | |
for i in range(barLen): | |
if i < int(barLen * percent): | |
progress += "=" | |
else: | |
progress += " " | |
sys.stdout.write("[ %s ] %.2f%% - %.1f KB/s - ETA: %ds" % (progress, percent * 100, speed / 1024, eta) + ' ' * 10) | |
sys.stdout.flush() | |
def main(): | |
parser = argparse.ArgumentParser(description='iNav blackbox dumper') | |
parser.add_argument('port', help='the serial port iNav is connected to') | |
parser.add_argument('baudrate', type=int, help='the baudrate of the serial port') | |
parser.add_argument('output_file', help='the file to write the blackbox dump to') | |
args = parser.parse_args() | |
ser = Serial(args.port, args.baudrate, timeout=1) | |
try: | |
print('Entering CLI...') | |
ser.write('#'.encode()) | |
recv_until(ser, CLI_DELIMITER) | |
print('Switch to Mass Storage Mode...') | |
ser.write('msc\n'.encode()) | |
ser.close() | |
bbl_log_fc_path = "/run/media/christoph/EMFAT/btfl_all.bbl" | |
print('switching done...') | |
#Wait for mount to become available | |
while os.path.exists(bbl_log_fc_path)==False: | |
print('Waiting for mount...') | |
time.sleep(5) | |
#Copy File | |
print('Copy file...') | |
copy_to = './%s.bbl' % (args.output_file) | |
shutil.copyfile(bbl_log_fc_path,copy_to) | |
#Run Analyzer | |
print('Start PID-Analyzer...') | |
syscall = "./PID-Analyzer.py -n %s -l %s --show N --blackbox_decode=/usr/bin/blackbox_decode" % (args.output_file,copy_to) | |
os.system(syscall) | |
except TimeoutException as e: | |
print(e) | |
# except KeyboardInterrupt: | |
# pass | |
# ser.write('exit\n'.encode()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment