eive-tmtc/eive_tmtc/tmtc/acs/gps.py

128 lines
4.0 KiB
Python
Raw Normal View History

2023-02-07 16:48:23 +01:00
import datetime
2023-03-23 13:49:11 +01:00
import enum
2023-02-01 11:17:04 +01:00
import logging
2023-02-01 16:25:17 +01:00
import struct
2021-09-08 13:20:22 +02:00
2022-11-29 16:53:29 +01:00
from eive_tmtc.config.definitions import CustomServiceList
2023-02-01 16:25:17 +01:00
from eive_tmtc.pus_tm.defs import PrintWrapper
from tmtccmd.config import TmtcDefinitionWrapper, OpCodeEntry
from tmtccmd.config.tmtc import tmtc_definitions_provider
2023-03-30 15:12:33 +02:00
from tmtccmd.tc.pus_200_fsfw_mode import create_mode_command, Mode
2022-08-08 16:32:18 +02:00
from tmtccmd.tc import DefaultPusQueueHelper
2023-02-07 16:48:23 +01:00
from tmtccmd.tc.pus_3_fsfw_hk import (
make_sid,
create_request_one_hk_command,
create_enable_periodic_hk_command_with_interval,
create_disable_periodic_hk_command,
)
2023-02-01 16:25:17 +01:00
from tmtccmd.util.tmtc_printer import FsfwTmTcPrinter
2021-09-08 13:20:22 +02:00
2023-02-01 11:17:04 +01:00
_LOGGER = logging.getLogger(__name__)
2021-09-08 13:20:22 +02:00
2022-05-24 01:34:33 +02:00
2023-01-16 14:13:06 +01:00
class OpCode:
2023-03-30 15:12:33 +02:00
OFF = "off"
ON = "on"
2023-02-05 12:52:43 +01:00
REQ_OS_HK = ["hk"]
2023-02-07 16:48:23 +01:00
ENABLE_HK = ["enable_hk"]
DISABLE_HK = ["disable_hk"]
2023-02-05 12:52:43 +01:00
RESET_GNSS = ["reset"]
2022-05-24 01:34:33 +02:00
class Info:
2023-03-30 15:12:33 +02:00
OFF = "Off"
ON = "On"
2022-05-24 01:34:33 +02:00
REQ_OS_HK = "Request One-Shot HK"
2023-02-07 16:48:23 +01:00
ENABLE_HK = "Enable HK"
DISABLE_HK = "Disable HK"
2022-05-24 01:34:33 +02:00
RESET_GNSS = "Reset GNSS using reset pin"
2023-03-23 13:49:11 +01:00
class SetId(enum.IntEnum):
2022-05-24 01:34:33 +02:00
HK = 0
@tmtc_definitions_provider
def add_gps_cmds(defs: TmtcDefinitionWrapper):
2022-07-05 02:12:54 +02:00
oce = OpCodeEntry()
2023-01-16 14:13:06 +01:00
oce.add(keys=OpCode.RESET_GNSS, info=Info.RESET_GNSS)
oce.add(keys=OpCode.REQ_OS_HK, info=Info.REQ_OS_HK)
2023-02-07 16:48:23 +01:00
oce.add(keys=OpCode.ENABLE_HK, info=Info.ENABLE_HK)
oce.add(keys=OpCode.DISABLE_HK, info=Info.DISABLE_HK)
2023-03-30 15:12:33 +02:00
oce.add(keys=OpCode.OFF, info=Info.OFF)
oce.add(keys=OpCode.ON, info=Info.ON)
2022-07-05 02:12:54 +02:00
defs.add_service(
2022-05-24 01:34:33 +02:00
name=CustomServiceList.GPS_CTRL.value,
2022-05-25 10:37:38 +02:00
info="GPS/GNSS Controller",
2022-07-05 02:12:54 +02:00
op_code_entry=oce,
2022-05-24 01:34:33 +02:00
)
2021-09-08 13:20:22 +02:00
2022-08-08 16:32:18 +02:00
def pack_gps_command(object_id: bytes, q: DefaultPusQueueHelper, op_code: str):
2023-02-07 16:48:23 +01:00
sid = make_sid(object_id=object_id, set_id=SetId.HK)
2023-01-16 14:13:06 +01:00
if op_code in OpCode.RESET_GNSS:
2022-05-24 01:49:57 +02:00
# TODO: This needs to be re-implemented
2023-02-01 11:17:04 +01:00
_LOGGER.warning("Reset pin handling needs to be re-implemented")
2023-02-07 16:48:23 +01:00
if op_code in OpCode.ENABLE_HK:
interval = float(input("Please specify interval in floating point seconds: "))
if interval <= 0:
raise ValueError("invalid interval")
q.add_log_cmd(f"GPS: {Info.ENABLE_HK}")
cmds = create_enable_periodic_hk_command_with_interval(
diag=False, sid=sid, interval_seconds=interval
2022-05-25 10:37:38 +02:00
)
2023-02-07 16:48:23 +01:00
for cmd in cmds:
q.add_pus_tc(cmd)
if op_code in OpCode.DISABLE_HK:
q.add_log_cmd(f"gps: {Info.DISABLE_HK}")
q.add_pus_tc(create_disable_periodic_hk_command(diag=False, sid=sid))
if op_code in OpCode.REQ_OS_HK:
q.add_log_cmd(f"GPS: {Info.REQ_OS_HK}")
q.add_pus_tc(create_request_one_hk_command(sid=sid))
2023-03-30 15:12:33 +02:00
if op_code in OpCode.ON:
q.add_log_cmd(f"GPS: {Info.ON}")
q.add_pus_tc(create_mode_command(object_id, Mode.ON, 0))
if op_code in OpCode.OFF:
q.add_log_cmd(f"GPS: {Info.OFF}")
q.add_pus_tc(create_mode_command(object_id, Mode.OFF, 0))
2023-02-01 16:25:17 +01:00
def handle_gps_data(printer: FsfwTmTcPrinter, hk_data: bytes):
pw = PrintWrapper(printer)
pw.dlog(f"Received GPS data, HK data length {len(hk_data)}")
current_idx = 0
fmt_str = "!ddddBBBHBBBBBI"
inc_len = struct.calcsize(fmt_str)
(
lat,
long,
alt,
speed,
fix,
sats_in_use,
sats_in_view,
year,
month,
day,
hours,
minutes,
seconds,
unix_seconds,
) = struct.unpack(fmt_str, hk_data[current_idx : current_idx + inc_len])
current_idx += inc_len
2023-02-07 16:48:23 +01:00
if year == 0:
date_string = "No date string, year is 0"
else:
date_string = datetime.datetime(
year=year, month=month, day=day, hour=hours, minute=minutes, second=seconds
)
2023-02-01 16:25:17 +01:00
pw.dlog(f"Lat: {lat} deg")
pw.dlog(f"Long: {long} deg")
pw.dlog(f"Altitude: {alt} m | Speed: {speed} m/s")
pw.dlog(
f"Fix Type: {fix} | Sats in View {sats_in_view} | Sats in Use {sats_in_use}"
)
pw.dlog(f"GNSS Date: {date_string}")
pw.dlog(f"Unix seconds {unix_seconds}")
printer.print_validity_buffer(validity_buffer=hk_data[current_idx:], num_vars=14)