eive-tmtc/eive_tmtc/tmtc/power/common_power.py

347 lines
12 KiB
Python
Raw Normal View History

2023-02-17 20:00:46 +01:00
import enum
2022-11-14 10:56:14 +01:00
import struct
from typing import List
2022-11-29 16:53:29 +01:00
from eive_tmtc.gomspace.gomspace_common import (
2022-09-01 16:58:31 +02:00
pack_set_u8_param_command,
2022-08-11 18:10:15 +02:00
Channel,
GomspaceDeviceActionId,
2022-09-01 16:58:31 +02:00
prompt_and_pack_set_integer_param_command,
prompt_and_pack_get_param_command,
pack_request_config_command,
2022-08-31 21:15:31 +02:00
pack_gnd_wdt_reset_command,
2022-09-01 16:58:31 +02:00
ParamTypes,
2023-01-10 14:03:10 +01:00
pack_reboot_command,
2022-08-11 18:10:15 +02:00
)
2022-11-29 16:53:29 +01:00
from eive_tmtc.gomspace.gomspace_pdu_definitions import OUT_ENABLE_LIST
2022-09-01 16:58:31 +02:00
from spacepackets.ecss import PusTelecommand
2022-08-11 18:10:15 +02:00
from tmtccmd.config import OpCodeEntry
2022-08-11 15:40:38 +02:00
from tmtccmd.tc import DefaultPusQueueHelper
2022-08-11 18:10:15 +02:00
from tmtccmd.tc.pus_3_fsfw_hk import (
make_sid,
generate_one_diag_command,
generate_one_hk_command,
2022-10-18 11:01:01 +02:00
enable_periodic_hk_command_with_interval,
disable_periodic_hk_command,
2022-08-11 18:10:15 +02:00
)
from tmtccmd.pus.s8_fsfw_funccmd import create_action_cmd
2022-08-31 21:15:31 +02:00
from tmtccmd.util import ObjectIdU32, ObjectIdBase
2022-08-11 18:10:15 +02:00
2023-01-16 14:13:06 +01:00
class GomspaceOpCode:
2022-10-18 10:44:32 +02:00
GET_PARAM = ["get_param"]
SET_INTEGER_PARAM = ["set_int_param"]
SAVE_TABLE = ["save_table"]
RESET_GND_WATCHDOG = ["reset_gnd_wdt"]
SAVE_TABLE_DEFAULT = ["save_table_default"]
LOAD_TABLE = ["load_table"]
2023-01-10 14:03:10 +01:00
REBOOT = ["reboot"]
2022-10-18 10:44:32 +02:00
REQUEST_CONFIG_TABLE = ["cfg_table"]
class GsInfo:
GET_PARAMETER = "Get parameter"
SET_PARAMETER = "Set integer parameter"
REQUEST_CONFIG_TABLE = "Request Config Table"
RESET_GND_WATCHDOG = "Reset GND watchdog"
SAVE_TABLE = "Save table non-volatile (file)"
SAVE_TABLE_DEFAULT = "Save table non-volatile (default)"
LOAD_TABLE = "Load Table"
2023-01-10 14:03:10 +01:00
REBOOT = "Reboot PCDU module"
2022-10-18 10:44:32 +02:00
2022-08-11 18:10:15 +02:00
class PowerInfo:
INFO_CORE = "Core Information"
INFO_AUX = "Auxiliary Information"
2022-12-22 16:12:31 +01:00
SWITCHER_HK = "Switcher State Information"
2022-08-11 18:10:15 +02:00
INFO_ALL = "All Information"
2023-01-24 13:06:08 +01:00
REQUEST_SWITCHER_SET = "Request Switcher Information"
2022-10-18 10:44:32 +02:00
ENABLE_INFO_HK = "Enable Core Info HK"
DISABLE_INFO_HK = "Disable Core Info HK"
2022-08-31 21:15:31 +02:00
RESET_ALL_GND_WDTS = "Reset all Ground Watchdogs"
2022-10-18 10:44:32 +02:00
REQUEST_CORE_HK_ONCE = "Requesting Core HK once"
REQUEST_AUX_HK_ONCE = "Requesting Aux HK once"
PRINT_SWITCH_V_I = "Print Switch V I Info"
PRINT_LATCHUPS = "Print latchups"
2022-08-11 18:10:15 +02:00
2022-08-11 15:40:38 +02:00
class PowerOpCodes:
# PDU 1
2022-09-01 16:58:31 +02:00
TCS_ON = ["tcs_on"]
TCS_OFF = ["tcs_off"]
SYRLINKS_ON = ["syrlinks_on"]
SYRLINKS_OFF = ["syrlinks_off"]
STAR_TRACKER_ON = ["str_on"]
STAR_TRACKER_OFF = ["str_off"]
MGT_ON = ["mgt_on"]
MGT_OFF = ["mgt_off"]
SUS_N_ON = ["sus_nom_on"]
SUS_N_OFF = ["sus_nom_off"]
SCEX_ON = ["scex_on"]
SCEX_OFF = ["scex_off"]
PLOC_ON = ["ploc_on"]
PLOC_OFF = ["ploc_off"]
ACS_A_ON = ["acs_a_on"]
ACS_A_OFF = ["acs_a_off"]
2022-08-11 15:40:38 +02:00
# PDU 2
2022-09-01 16:58:31 +02:00
PL_PCDU_VBAT_NOM_ON = ["plpcdu_vbat_nom_on"]
PL_PCDU_VBAT_NOM_OFF = ["plpcdu_vbat_nom_off"]
RW_ON = ["rw_on"]
RW_OFF = ["rw_off"]
HEATER_ON = ["heater_on"]
HEATER_OFF = ["heater_off"]
SUS_R_ON = ["sus_red_on"]
SUS_R_OFF = ["sus_red_off"]
SOLAR_ARRAY_DEPL_ON = ["sa_depl_on"]
SOLAR_ARRAY_DEPL_OFF = ["sa_depl_off"]
PL_PCDU_VBAT_RED_ON = ["plpcdu_vbat_red_on"]
PL_PCDU_VBAT_RED_OFF = ["plpcdu_vbat_red_off"]
ACS_B_ON = ["acs_b_on"]
ACS_B_OFF = ["acs_b_off"]
PL_CAM_ON = ["cam_on"]
PL_CAM_OFF = ["cam_off"]
2022-08-11 15:40:38 +02:00
2023-01-10 14:03:10 +01:00
REBOOT = ["reboot"]
2022-08-11 15:40:38 +02:00
INFO_CORE = ["info"]
2022-12-22 16:12:31 +01:00
SWITCHER_HK = ["switcher_states"]
2022-10-18 10:44:32 +02:00
ENABLE_INFO_HK = ["info_hk_on"]
DISABLE_INFO_HK = ["info_hk_off"]
2022-08-29 13:20:44 +02:00
INFO_AUX = ["info_aux"]
INFO_ALL = ["info_all"]
2022-08-31 21:15:31 +02:00
RESET_ALL_GND_WDTS = ["reset_gnd_wdts"]
2022-10-18 10:44:32 +02:00
# Request HK
REQUEST_CORE_HK_ONCE = ["hk_core"]
REQUEST_AUX_HK_ONCE = ["hk_aux"]
PRINT_SWITCH_V_I = ["print_switch_vi"]
PRINT_LATCHUPS = ["print_latchups"]
2023-02-17 20:00:46 +01:00
class SetId(enum.IntEnum):
2022-10-18 11:01:01 +02:00
CORE = 1
AUX = 2
CONFIG = 3
2022-10-18 10:44:32 +02:00
def pack_common_power_cmds(
prefix: str, object_id: ObjectIdU32, q: DefaultPusQueueHelper, op_code: str
):
objb = object_id.as_bytes
if op_code in PowerOpCodes.ENABLE_INFO_HK:
2022-10-18 11:01:01 +02:00
interval = float(input("Specify HK interval in floating point seconds: "))
q.add_log_cmd(f"{prefix}: {PowerInfo.ENABLE_INFO_HK} with interval {interval}")
cmds = enable_periodic_hk_command_with_interval(
2023-01-12 15:48:49 +01:00
True, make_sid(objb, SetId.CORE), interval
2022-10-18 11:01:01 +02:00
)
for cmd in cmds:
q.add_pus_tc(cmd)
if op_code in PowerOpCodes.DISABLE_INFO_HK:
q.add_log_cmd(f"{prefix}: {PowerInfo.DISABLE_INFO_HK}")
2023-01-12 15:48:49 +01:00
q.add_pus_tc(disable_periodic_hk_command(True, make_sid(objb, SetId.CORE)))
2022-08-11 15:40:38 +02:00
2023-06-19 17:16:00 +02:00
def pack_common_gomspace_cmds( # noqa C901: Complexity is okay here.
prefix: str, object_id: ObjectIdU32, q: DefaultPusQueueHelper, op_code: str
):
objb = object_id.as_bytes
2022-10-18 10:44:32 +02:00
if op_code in PowerOpCodes.PRINT_SWITCH_V_I:
q.add_log_cmd(f"{prefix}: {PowerInfo.PRINT_SWITCH_V_I}")
q.add_pus_tc(
create_action_cmd(
object_id=objb, action_id=GomspaceDeviceActionId.PRINT_SWITCH_V_I
)
)
2023-01-10 14:03:10 +01:00
if op_code in PowerOpCodes.REBOOT:
q.add_log_cmd(f"{prefix}: {GsInfo.REBOOT}")
q.add_pus_tc(pack_reboot_command(object_id))
2022-10-18 10:44:32 +02:00
if op_code in PowerOpCodes.PRINT_LATCHUPS:
q.add_log_cmd(f"{prefix}: {PowerInfo.PRINT_LATCHUPS}")
q.add_pus_tc(
create_action_cmd(
object_id=objb, action_id=GomspaceDeviceActionId.PRINT_LATCHUPS
)
)
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.SET_INTEGER_PARAM:
q.add_log_cmd(f"{prefix}: {GsInfo.SET_PARAMETER}")
2022-09-01 16:58:31 +02:00
print("Please specify the parameter type from index")
for idx, v in enumerate(ParamTypes):
print(f"{idx}: {v.name}")
ptype = int(input("Index: "))
prompt_and_pack_set_integer_param_command(q, object_id, ParamTypes(ptype))
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.GET_PARAM:
q.add_log_cmd(f"{prefix}: {GsInfo.GET_PARAMETER}")
prompt_and_pack_get_param_command(q, object_id)
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.REQUEST_CONFIG_TABLE:
q.add_log_cmd(f"{prefix}: {GsInfo.REQUEST_CONFIG_TABLE}")
q.add_pus_tc(pack_request_config_command(object_id.as_bytes))
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.SAVE_TABLE:
2022-08-31 21:15:31 +02:00
q.add_log_cmd(f"{prefix}: {GsInfo.SAVE_TABLE}")
source_table = int(
input(
"Source table [0: Board Config, 1: Module Config, "
"2: Calibration Parameter, 4: TM Data]: "
)
)
2022-09-01 16:58:31 +02:00
if source_table not in [0, 1, 2, 4]:
raise ValueError("Invalid source table index")
2022-08-31 21:15:31 +02:00
# Not used for now
"""
target_table = int(input(
"Target table. [Default: Source table]: "
))
"""
q.add_pus_tc(
create_action_cmd(
2022-08-31 21:15:31 +02:00
object_id=object_id.as_bytes,
action_id=GomspaceDeviceActionId.SAVE_TABLE,
2022-08-31 21:15:31 +02:00
user_data=bytes([source_table]),
)
)
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.SAVE_TABLE_DEFAULT:
2022-08-31 21:15:31 +02:00
source_table = int(
input(
2023-09-12 13:48:38 +02:00
"Source table [0: Board Config, 1: Module Config, 2: Calibration"
" Parameter]: "
2022-08-31 21:15:31 +02:00
)
)
2022-09-01 16:58:31 +02:00
if source_table not in [0, 1, 2]:
raise ValueError("Invalid source table index")
2022-08-31 21:15:31 +02:00
q.add_pus_tc(
create_action_cmd(
2022-08-31 21:15:31 +02:00
object_id=object_id.as_bytes,
action_id=GomspaceDeviceActionId.SAVE_TABLE_DEFAULT,
2022-08-31 21:15:31 +02:00
user_data=bytes([source_table]),
)
)
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.LOAD_TABLE:
2022-09-01 17:31:24 +02:00
target_table = int(
input(
2023-09-12 13:48:38 +02:00
"Target table ID [0: Board Config, 1: Module Config, 2: Calibration"
" Parameter, 4: HK TM]: "
2022-09-01 17:31:24 +02:00
)
)
if target_table not in [0, 1, 2, 4]:
raise ValueError("Invalid source table index")
if target_table != 4:
source_table = int(
input(
2023-09-12 13:48:38 +02:00
"Source table (file or default) [0: Board Config, 1: Module Config,"
" 2: Calibration Parameter, value + 4 for default table]: "
2022-09-01 17:31:24 +02:00
)
)
if source_table not in [0, 1, 2, 4, 5, 6]:
raise ValueError("Invalid source table index")
else:
# Will be ignored
source_table = 4
q.add_pus_tc(
create_action_cmd(
2022-09-01 17:31:24 +02:00
object_id=object_id.as_bytes,
action_id=GomspaceDeviceActionId.LOAD_TABLE,
2022-09-01 17:31:24 +02:00
user_data=bytes([source_table, target_table]),
)
)
2023-01-16 14:13:06 +01:00
if op_code in GomspaceOpCode.RESET_GND_WATCHDOG:
2022-08-31 21:15:31 +02:00
q.add_log_cmd(f"{prefix}: {GsInfo.RESET_GND_WATCHDOG}")
q.add_pus_tc(pack_gnd_wdt_reset_command(object_id))
def pack_reset_gnd_wdt_cmd(
q: DefaultPusQueueHelper, prefix: str, object_id: ObjectIdBase
):
q.add_log_cmd(f"{prefix}: {GsInfo.RESET_GND_WATCHDOG}")
q.add_pus_tc(pack_gnd_wdt_reset_command(object_id))
2022-08-27 16:28:33 +02:00
def req_hk_cmds(
prefix: str,
q: DefaultPusQueueHelper,
op_code: str,
obj_id: bytes,
set_id_pair: [int, int],
):
2022-10-18 10:44:32 +02:00
if op_code in PowerOpCodes.REQUEST_CORE_HK_ONCE:
q.add_log_cmd(f"{prefix}: {PowerInfo.REQUEST_CORE_HK_ONCE}")
2022-08-27 16:28:33 +02:00
hk_sid = make_sid(object_id=obj_id, set_id=set_id_pair[0])
2022-08-11 18:10:15 +02:00
q.add_pus_tc(generate_one_diag_command(sid=hk_sid))
2022-10-18 10:44:32 +02:00
if op_code in PowerOpCodes.REQUEST_AUX_HK_ONCE:
q.add_log_cmd(f"{prefix}: {PowerInfo.REQUEST_AUX_HK_ONCE}")
2022-08-27 16:28:33 +02:00
hk_sid = make_sid(object_id=obj_id, set_id=set_id_pair[1])
2022-08-11 18:10:15 +02:00
q.add_pus_tc(generate_one_hk_command(sid=hk_sid))
2022-09-01 16:58:31 +02:00
def pack_pdu_disable_safe_off_cmd() -> PusTelecommand:
pass
2022-08-11 15:40:38 +02:00
def generic_on_cmd(
2022-08-11 18:10:15 +02:00
object_id: bytes, q: DefaultPusQueueHelper, info_str: str, out_idx: int
2022-08-11 15:40:38 +02:00
):
2022-08-11 18:10:15 +02:00
q.add_log_cmd(info_str + " on")
2023-01-12 14:11:30 +01:00
q.add_pus_tc(create_generic_on_cmd(object_id, out_idx))
def create_generic_on_cmd(object_id: bytes, out_idx: int):
return pack_set_u8_param_command(
object_id,
OUT_ENABLE_LIST[out_idx].parameter_address,
Channel.on,
2022-08-11 15:40:38 +02:00
)
def generic_off_cmd(
2022-08-11 18:10:15 +02:00
object_id: bytes, q: DefaultPusQueueHelper, info_str: str, out_idx: int
2022-08-11 15:40:38 +02:00
):
2022-08-11 18:10:15 +02:00
q.add_log_cmd(info_str + " off")
2023-01-12 14:11:30 +01:00
q.add_pus_tc(create_generic_off_cmd(object_id, out_idx))
def create_generic_off_cmd(object_id: bytes, out_idx: int):
return pack_set_u8_param_command(
object_id,
OUT_ENABLE_LIST[out_idx].parameter_address,
Channel.off,
2022-08-11 15:40:38 +02:00
)
2022-08-24 11:19:49 +02:00
2022-10-18 11:08:28 +02:00
def add_common_power_defs(oce: OpCodeEntry):
oce.add(keys=PowerOpCodes.REQUEST_CORE_HK_ONCE, info=PowerInfo.REQUEST_CORE_HK_ONCE)
oce.add(keys=PowerOpCodes.REQUEST_AUX_HK_ONCE, info=PowerInfo.REQUEST_AUX_HK_ONCE)
oce.add(keys=PowerOpCodes.ENABLE_INFO_HK, info=PowerInfo.ENABLE_INFO_HK)
oce.add(keys=PowerOpCodes.DISABLE_INFO_HK, info=PowerInfo.DISABLE_INFO_HK)
def add_gomspace_cmd_defs(oce: OpCodeEntry):
oce.add(
2022-10-18 10:44:32 +02:00
keys=PowerOpCodes.REQUEST_CORE_HK_ONCE,
info=PowerInfo.REQUEST_CORE_HK_ONCE,
)
oce.add(
2022-10-18 10:44:32 +02:00
keys=PowerOpCodes.REQUEST_AUX_HK_ONCE,
info=PowerInfo.REQUEST_AUX_HK_ONCE,
)
2022-10-18 10:44:32 +02:00
oce.add(keys=PowerOpCodes.PRINT_LATCHUPS, info=PowerInfo.PRINT_LATCHUPS)
2023-01-16 14:13:06 +01:00
oce.add(keys=GomspaceOpCode.GET_PARAM, info=GsInfo.GET_PARAMETER)
oce.add(keys=GomspaceOpCode.REBOOT, info=GsInfo.REBOOT)
oce.add(keys=GomspaceOpCode.SET_INTEGER_PARAM, info=GsInfo.SET_PARAMETER)
oce.add(keys=GomspaceOpCode.REQUEST_CONFIG_TABLE, info=GsInfo.REQUEST_CONFIG_TABLE)
oce.add(keys=GomspaceOpCode.SAVE_TABLE, info=GsInfo.SAVE_TABLE)
oce.add(keys=GomspaceOpCode.SAVE_TABLE_DEFAULT, info=GsInfo.SAVE_TABLE_DEFAULT)
oce.add(keys=GomspaceOpCode.LOAD_TABLE, info=GsInfo.LOAD_TABLE)
oce.add(keys=GomspaceOpCode.RESET_GND_WATCHDOG, info=GsInfo.RESET_GND_WATCHDOG)
2022-11-14 10:56:14 +01:00
OBC_ENDIANNESS = "<"
def unpack_array_in_data(
data: bytes, start_addr: int, width: int, entries: int, struct_spec: str
) -> List:
return [
struct.unpack(
f"{OBC_ENDIANNESS}{struct_spec}",
data[start_addr + (i * width) : start_addr + ((i + 1) * width)],
)[0]
for i in range(entries)
]