Last active
June 3, 2016 19:26
-
-
Save comzeradd/034e1c04e517e9dc892755906d79568a to your computer and use it in GitHub Desktop.
Decoding qb50 hex payload
This file contains 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 python3 | |
import json | |
import sys | |
import socket | |
from datetime import datetime, timedelta | |
EPOCH_DATE = datetime.strptime('20000101T000000Z', '%Y%m%dT%H%M%SZ') | |
UDP_IP = '127.0.0.1' | |
UDP_PORT = 16886 | |
def decode_payload(payload): | |
dt = payload[:32] | |
datasets = payload[32:] | |
# calculate initial datetime | |
seconds = int(dt[24:] + dt[16:24] + dt[8:16] + dt[:8], 2) | |
telemetry = [] | |
while datasets: | |
dataset = datasets[:64] | |
if len(dataset) < 64: | |
return | |
print('d: {0}'.format(dataset)) | |
datasets = datasets[64:] | |
dataset_datetime = EPOCH_DATE + timedelta(seconds=seconds) | |
seconds += 60 | |
satellite_datetime = datetime.strftime(dataset_datetime, '%Y%m%dT%H%M%SZ') | |
# mode | |
status = dataset[:8] | |
# battery voltage | |
u = float(int(dataset[8:16], 2)) | |
bat_v = round((u + 60) / 20, 2) | |
# battery current | |
u = float(int(dataset[16:24], 2)) | |
bat_c = round((u - 127) / 127, 2) | |
# 3v3 current | |
u = float(int(dataset[24:32], 2)) | |
v3_c = round(u / 40, 2) | |
# 5v current | |
u = float(int(dataset[32:40], 2)) | |
v5_c = round(u / 40, 2) | |
# temperature comms | |
u = float(int(dataset[40:48], 2)) | |
comms_t = round((u - 60) / 4, 2) | |
# temperature eps | |
u = float(int(dataset[48:56], 2)) | |
eps_t = round((u - 60) / 4, 2) | |
# temperature battery | |
u = float(int(dataset[56:], 2)) | |
batt_t = round((u - 60) / 4, 2) | |
data = { | |
'satellite_datetime': satellite_datetime, | |
'damod_data': { | |
'status': status, | |
'bat_v': bat_v, | |
'bat_c': bat_c, | |
'v3_c': v3_c, | |
'v5_c': v5_c, | |
'comms_t': comms_t, | |
'eps_t': eps_t, | |
'batt_t': batt_t | |
} | |
} | |
telemetry.append(data) | |
print(json.dumps(telemetry, indent=4, sort_keys=True)) | |
def hex_to_binary(hex_payload): | |
payload = '' | |
for part, index in enumerate(hex_payload): | |
payload += bin(part)[2:].zfill(8) | |
return payload | |
if __name__ == "__main__": | |
print('Listening on {0}:{1}'.format(UDP_IP, UDP_PORT)) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind((UDP_IP, UDP_PORT)) | |
while True: | |
hex_payload, addr = sock.recvfrom(1024) | |
print('hex payload: {0}'.format(hex_payload)) | |
payload = hex_to_binary(hex_payload) | |
print('binary payload: {0}'.format(payload)) | |
decode_payload(payload[48:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment