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

176 lines
5.5 KiB
Python
Raw Normal View History

2023-02-17 20:00:46 +01:00
import enum
2023-02-01 16:25:17 +01:00
import struct
from eive_tmtc.pus_tm.defs import PrintWrapper
2022-12-06 10:34:12 +01:00
from spacepackets.ecss import PusTelecommand
2022-11-29 16:53:29 +01:00
from eive_tmtc.config.definitions import CustomServiceList
from eive_tmtc.config.object_ids import BPX_HANDLER_ID
2022-12-06 10:34:12 +01:00
from tmtccmd.config.tmtc import (
tmtc_definitions_provider,
TmtcDefinitionWrapper,
OpCodeEntry,
)
2022-11-29 16:53:29 +01:00
from tmtccmd.tc import service_provider
2022-08-18 14:08:05 +02:00
from tmtccmd.tc.decorator import ServiceProviderParams
from tmtccmd.pus.s8_fsfw_funccmd import create_action_cmd
from tmtccmd.tc.pus_3_fsfw_hk import generate_one_hk_command, make_sid
2023-01-31 12:56:13 +01:00
from tmtccmd.tc.pus_200_fsfw_mode import pack_mode_data, Mode
from tmtccmd.tc.pus_200_fsfw_mode import Subservice as ModeSubservices
2023-02-01 16:25:17 +01:00
from tmtccmd.util.tmtc_printer import FsfwTmTcPrinter
2023-02-17 20:00:46 +01:00
class BpxSetId(enum.IntEnum):
GET_HK_SET = 0
GET_CFG_SET = 5
2023-01-16 14:13:06 +01:00
class BpxActionId:
REBOOT = 2
RESET_COUNTERS = 3
SET_CFG = 4
GET_CFG = 5
2023-01-16 14:13:06 +01:00
class BpxOpCode:
2022-02-03 16:02:55 +01:00
HK = ["0", "hk"]
2022-12-06 10:34:12 +01:00
OFF = ["off"]
ON = ["on"]
2022-02-03 16:02:55 +01:00
RST_BOOT_CNT = ["1", "rst_boot_cnt"]
REQUEST_CFG = ["2", "cfg"]
REQUEST_CFG_HK = ["3", "cfg_hk"]
REBOOT = ["4", "reboot"]
2022-12-06 10:34:12 +01:00
@tmtc_definitions_provider
def add_bpx_cmd_definitions(defs: TmtcDefinitionWrapper):
oce = OpCodeEntry()
2023-01-16 14:13:06 +01:00
oce.add(keys=BpxOpCode.ON, info="On command")
oce.add(keys=BpxOpCode.OFF, info="Off command")
oce.add(keys=BpxOpCode.HK, info="Request BPX HK")
oce.add(keys=BpxOpCode.RST_BOOT_CNT, info="Reset Boot Count")
oce.add(keys=BpxOpCode.REQUEST_CFG, info="Request Configuration Struct (Step 1)")
2022-12-06 10:34:12 +01:00
oce.add(
2023-01-16 14:13:06 +01:00
keys=BpxOpCode.REQUEST_CFG_HK, info="Request Configuration Struct HK (Step 2)"
2022-12-06 10:34:12 +01:00
)
2023-01-16 14:13:06 +01:00
oce.add(keys=BpxOpCode.REBOOT, info="Reboot Command")
2022-12-06 10:34:12 +01:00
defs.add_service(
name=CustomServiceList.BPX_BATTERY.value,
info="BPX Battery Handler",
op_code_entry=oce,
)
2022-08-12 22:33:16 +02:00
@service_provider(CustomServiceList.BPX_BATTERY.value)
2022-08-18 14:08:05 +02:00
def pack_bpx_commands(p: ServiceProviderParams):
op_code = p.op_code
q = p.queue_helper
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.HK:
2022-07-04 15:22:53 +02:00
q.add_log_cmd("Requesting BPX battery HK set")
2023-01-16 14:13:06 +01:00
sid = make_sid(object_id=BPX_HANDLER_ID, set_id=BpxSetId.GET_HK_SET)
2022-07-04 15:22:53 +02:00
q.add_pus_tc(generate_one_hk_command(sid=sid))
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.OFF:
2022-12-06 10:34:12 +01:00
q.add_log_cmd("Off mode")
2023-01-16 15:05:33 +01:00
mode_cmd = pack_mode_data(BPX_HANDLER_ID, Mode.OFF, 0)
2022-12-06 10:34:12 +01:00
q.add_pus_tc(
PusTelecommand(
service=200,
subservice=ModeSubservices.TC_MODE_COMMAND,
app_data=mode_cmd,
)
)
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.ON:
2022-12-06 10:34:12 +01:00
q.add_log_cmd("On mode")
2023-01-16 15:05:33 +01:00
mode_cmd = pack_mode_data(BPX_HANDLER_ID, Mode.ON, 0)
2022-12-06 10:34:12 +01:00
q.add_pus_tc(
PusTelecommand(
service=200,
subservice=ModeSubservices.TC_MODE_COMMAND,
app_data=mode_cmd,
)
)
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.RST_BOOT_CNT:
2022-07-04 15:22:53 +02:00
q.add_log_cmd("Resetting reboot counters")
q.add_pus_tc(
create_action_cmd(
2023-01-16 14:13:06 +01:00
object_id=BPX_HANDLER_ID, action_id=BpxActionId.RESET_COUNTERS
2022-07-04 15:22:53 +02:00
)
)
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.REQUEST_CFG:
2022-07-04 15:22:53 +02:00
q.add_log_cmd("Requesting configuration struct")
q.add_pus_tc(
create_action_cmd(object_id=BPX_HANDLER_ID, action_id=BpxActionId.GET_CFG)
2022-02-03 15:30:58 +01:00
)
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.REQUEST_CFG_HK:
2022-07-04 15:22:53 +02:00
q.add_log_cmd("Requesting configuration struct HK")
2023-01-16 14:13:06 +01:00
sid = make_sid(object_id=BPX_HANDLER_ID, set_id=BpxSetId.GET_CFG_SET)
2022-07-04 15:22:53 +02:00
q.add_pus_tc(generate_one_hk_command(sid=sid))
2023-01-16 14:13:06 +01:00
if op_code in BpxOpCode.REBOOT:
2022-07-04 15:22:53 +02:00
q.add_log_cmd("Rebooting BPX battery")
q.add_pus_tc(
create_action_cmd(object_id=BPX_HANDLER_ID, action_id=BpxActionId.REBOOT)
2022-02-03 16:02:55 +01:00
)
2023-02-01 16:25:17 +01:00
HEADER_LIST = [
"Charge Current",
"Discharge Current",
"Heater Current",
"Battery Voltage",
"Batt Temp 1",
"Batt Temp 2",
"Batt Temp 3",
"Batt Temp 4",
"Reboot Counter",
"Boot Cause",
]
def handle_bpx_hk_data(printer: FsfwTmTcPrinter, set_id: int, hk_data: bytes):
pw = PrintWrapper(printer)
if set_id == BpxSetId.GET_HK_SET:
fmt_str = "!HHHHhhhhIB"
inc_len = struct.calcsize(fmt_str)
(
charge_current,
discharge_current,
heater_current,
batt_voltage,
batt_temp_1,
batt_temp_2,
batt_temp_3,
batt_temp_4,
reboot_cntr,
boot_cause,
) = struct.unpack(fmt_str, hk_data[0:inc_len])
content_list = [
charge_current,
discharge_current,
heater_current,
batt_voltage,
batt_temp_1,
batt_temp_2,
batt_temp_3,
batt_temp_4,
reboot_cntr,
boot_cause,
]
validity_buffer = hk_data[inc_len:]
pw.dlog(str(HEADER_LIST))
pw.dlog(str(content_list))
2023-05-23 09:54:51 +02:00
printer.get_validity_buffer(validity_buffer=validity_buffer, num_vars=10)
2023-02-01 16:25:17 +01:00
elif set_id == BpxSetId.GET_CFG_SET:
battheat_mode = hk_data[0]
battheat_low = struct.unpack("!b", hk_data[1:2])[0]
battheat_high = struct.unpack("!b", hk_data[2:3])[0]
header_list = [
"Battery Heater Mode",
"Battery Heater Low Limit",
"Battery Heater High Limit",
]
content_list = [battheat_mode, battheat_low, battheat_high]
validity_buffer = hk_data[3:]
pw.dlog(str(header_list))
pw.dlog(str(content_list))
2023-05-23 09:54:51 +02:00
printer.get_validity_buffer(validity_buffer=validity_buffer, num_vars=10)