all import changes

This commit is contained in:
2023-02-01 16:25:17 +01:00
parent 6b657b5623
commit c8ea75b2ad
22 changed files with 203 additions and 212 deletions

View File

@ -0,0 +1,118 @@
# -*- coding: utf-8 -*-
"""
@file rad_sensor.py
@brief Tests for the radiation sensor handler
@author J. Meier
@date 01.07.2021
"""
import struct
from eive_tmtc.config.definitions import CustomServiceList
from eive_tmtc.pus_tm.defs import PrintWrapper
from spacepackets.ecss.tc import PusTelecommand
from eive_tmtc.pus_tc.service_200_mode import pack_mode_data, Mode
from tmtccmd.config import TmtcDefinitionWrapper, OpCodeEntry
from tmtccmd.config.tmtc import tmtc_definitions_provider
from tmtccmd.tc import DefaultPusQueueHelper
from tmtccmd.tc.pus_3_fsfw_hk import generate_one_hk_command, make_sid
from tmtccmd.util import ObjectIdU32
from tmtccmd.util.tmtc_printer import FsfwTmTcPrinter
class SetId:
HK = 3
class OpCode:
ON = ["0", "on"]
NORMAL = ["1", "normal"]
OFF = ["2", "off"]
REQ_HK_ONCE = ["3", "hk-os"]
DEBUG_ON = ["10", "dbg-on"]
DEBUG_OFF = ["11", "dbg-off"]
class Info:
ON = "Switch Rad Sensor on"
NORMAL = "Switch Rad Sensor normal"
OFF = "Switch Rad sensor off"
REQ_OS_HK = "Request one-shot HK"
DEBUG_ON = "Switch debug output on"
DEBUG_OFF = "Switch debug output off"
class CommandId:
START_CONVERSIONS = 2
READ_CONVERSIONS = 3
ENABLE_DEBUG_OUTPUT = 4
DISABLE_DEBUG_OUTPUT = 5
@tmtc_definitions_provider
def add_rad_sens_cmds(defs: TmtcDefinitionWrapper):
oce = OpCodeEntry()
oce.add(info=Info.ON, keys=OpCode.ON)
oce.add(info=Info.OFF, keys=OpCode.OFF)
oce.add(info=Info.NORMAL, keys=OpCode.NORMAL)
oce.add(info=Info.REQ_OS_HK, keys=OpCode.REQ_HK_ONCE)
oce.add(info=Info.DEBUG_ON, keys=OpCode.DEBUG_ON)
oce.add(info=Info.DEBUG_OFF, keys=OpCode.DEBUG_OFF)
defs.add_service(
name=CustomServiceList.RAD_SENSOR.value,
info="Radiation Sensor",
op_code_entry=oce,
)
def pack_rad_sensor_test_into(
object_id: ObjectIdU32, q: DefaultPusQueueHelper, op_code: str
):
q.add_log_cmd(f"Commanding Radiation sensor handler {object_id}")
if op_code in OpCode.ON:
rad_sensor_mode_cmd(object_id, Mode.ON, Info.ON, q)
if op_code in OpCode.NORMAL:
rad_sensor_mode_cmd(object_id, Mode.NORMAL, Info.NORMAL, q)
if op_code in OpCode.OFF:
rad_sensor_mode_cmd(object_id, Mode.OFF, Info.OFF, q)
if op_code in OpCode.REQ_HK_ONCE:
q.add_log_cmd(f"Rad sensor: {Info.REQ_OS_HK}")
q.add_pus_tc(
generate_one_hk_command(sid=make_sid(object_id.as_bytes, set_id=SetId.HK))
)
if op_code in OpCode.DEBUG_ON:
q.add_log_cmd(f"Rad sensor: {Info.DEBUG_ON}")
command = object_id.as_bytes + struct.pack("!I", CommandId.ENABLE_DEBUG_OUTPUT)
q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
if op_code in OpCode.DEBUG_OFF:
q.add_log_cmd(f"Rad sensor: {Info.DEBUG_OFF}")
command = object_id.as_bytes + struct.pack("!I", CommandId.DISABLE_DEBUG_OUTPUT)
q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
def rad_sensor_mode_cmd(
object_id: ObjectIdU32, mode: Mode, info: str, q: DefaultPusQueueHelper
):
q.add_log_cmd(f"Rad sensor: {info}")
mode_data = pack_mode_data(object_id.as_bytes, mode, 0)
q.add_pus_tc(PusTelecommand(service=200, subservice=1, app_data=mode_data))
def handle_rad_sensor_data(printer: FsfwTmTcPrinter, set_id: int, hk_data: bytes):
if set_id == SetId.HK:
pw = PrintWrapper(printer)
current_idx = 0
pw.dlog("Received Radiation Sensor HK data")
fmt_str = "!fHHHHHH"
inc_len = struct.calcsize(fmt_str)
(temp, ain0, ain1, ain4, ain5, ain6, ain7) = struct.unpack(
fmt_str, hk_data[current_idx : current_idx + inc_len]
)
ain_dict = {0: ain0, 1: ain1, 4: ain4, 5: ain5, 6: ain6, 7: ain7}
pw.dlog(f"Temperature: {temp} C")
pw.dlog(f"AIN Channel | Raw Value (hex) | Raw Value (dec)")
for idx, val in ain_dict.items():
pw.dlog(f"{idx} | {val:#06x} | {str(val).ljust(5)}")
current_idx += inc_len
printer.print_validity_buffer(validity_buffer=hk_data[current_idx:], num_vars=7)

View File

@ -0,0 +1,208 @@
import enum
import json
from spacepackets.ecss import PusTelecommand
from eive_tmtc.config.definitions import CustomServiceList
from tmtccmd.config.tmtc import tmtc_definitions_provider
from tmtccmd.tc.pus_200_fsfw_mode import Mode, pack_mode_data, Subservice
from tmtccmd.tc import service_provider
from tmtccmd.tc.decorator import ServiceProviderParams
from tmtccmd.pus.s8_fsfw_funccmd import create_action_cmd
from tmtccmd.config import OpCodeEntry, TmtcDefinitionWrapper
from eive_tmtc.config.object_ids import SCEX_HANDLER_ID
USE_SCEX_CONF_FILE = True
class OpCode:
PING = ["0", "ping"]
ION_CMD = ["1", "ion"]
TEMP_CMD = ["2", "temp"]
EXP_STATUS_CMD = ["3", "expstatus"]
ONE_CELLS_CMD = ["4", "onecell"]
ALL_CELLS_CMD = ["5", "allcells"]
FRAM = ["6", "fram"]
SWITCH_ON = ["7", "on"]
SWITCH_OFF = ["8", "off"]
class ActionId(enum.IntEnum):
PING = 7
ION_CMD = 4
TEMP_CMD = 3
EXP_STATUS_CMD = 2
ONE_CELLS_CMD = 6
ALL_CELLS_CMD = 5
FRAM = 1
class Info:
PING = "Send Ping command"
ION_CMD = "Read Ion"
TEMP_CMD = "Read Temperature"
EXP_STATUS_CMD = "Read Experiment Status"
ONE_CELLS_CMD = "One Cell"
ALL_CELLS_CMD = "All Cells"
FRAM = "Read FRAM"
SWITCH_ON = "Switch Scex on"
SWITCH_OFF = "Switch Scex off"
@tmtc_definitions_provider
def add_scex_cmds(defs: TmtcDefinitionWrapper):
oce = OpCodeEntry()
oce.add(keys=OpCode.PING, info=Info.PING)
oce.add(keys=OpCode.ION_CMD, info=Info.ION_CMD)
oce.add(keys=OpCode.TEMP_CMD, info=Info.TEMP_CMD)
oce.add(keys=OpCode.EXP_STATUS_CMD, info=Info.EXP_STATUS_CMD)
oce.add(keys=OpCode.ONE_CELLS_CMD, info=Info.ONE_CELLS_CMD)
oce.add(keys=OpCode.ALL_CELLS_CMD, info=Info.ALL_CELLS_CMD)
oce.add(keys=OpCode.FRAM, info=Info.FRAM)
oce.add(keys=OpCode.SWITCH_ON, info=Info.SWITCH_ON)
oce.add(keys=OpCode.SWITCH_OFF, info=Info.SWITCH_OFF)
defs.add_service(
name=CustomServiceList.SCEX.value, info="SCEX Device", op_code_entry=oce
)
@service_provider(CustomServiceList.SCEX.value)
def pack_scex_cmds(p: ServiceProviderParams):
op_code = p.op_code
q = p.queue_helper
if op_code in OpCode.SWITCH_ON:
q.add_log_cmd(Info.SWITCH_ON)
q.add_pus_tc(
PusTelecommand(
service=200,
subservice=Subservice.TC_MODE_COMMAND,
app_data=pack_mode_data(SCEX_HANDLER_ID, Mode.ON, 0),
)
)
if op_code in OpCode.SWITCH_OFF:
q.add_log_cmd(Info.SWITCH_OFF)
q.add_pus_tc(
PusTelecommand(
service=200,
subservice=Subservice.TC_MODE_COMMAND,
app_data=pack_mode_data(SCEX_HANDLER_ID, Mode.OFF, 0),
)
)
if op_code in OpCode.PING:
q.add_log_cmd(Info.PING)
app_data = bytes([0])
q.add_pus_tc(create_action_cmd(SCEX_HANDLER_ID, ActionId.PING, app_data))
if op_code in OpCode.ION_CMD:
q.add_log_cmd(Info.ION_CMD)
app_data = bytes([0])
q.add_pus_tc(create_action_cmd(SCEX_HANDLER_ID, ActionId.ION_CMD, app_data))
if op_code in OpCode.TEMP_CMD:
q.add_log_cmd(Info.TEMP_CMD)
app_data = bytes([0])
q.add_pus_tc(create_action_cmd(SCEX_HANDLER_ID, ActionId.TEMP_CMD, app_data))
if op_code in OpCode.EXP_STATUS_CMD:
q.add_log_cmd(Info.EXP_STATUS_CMD)
app_data = bytes([0])
q.add_pus_tc(
create_action_cmd(SCEX_HANDLER_ID, ActionId.EXP_STATUS_CMD, app_data)
)
# one cell
if op_code in OpCode.ONE_CELLS_CMD:
q.add_log_cmd(Info.ONE_CELLS_CMD)
app_data = bytearray([0])
# cell number
cn = 0
while True:
cell_select = input("Which solar cell should be measured? (1-10): ")
if not cell_select.isdigit():
print("Invalid cell number. Try again.")
continue
cell_select = int(cell_select)
if cell_select < 1 or cell_select > 10:
print(
f"Invalid cell number {cell_select}, "
f"Please enter a valid number: "
)
continue
cn = cell_select - 1
break
if USE_SCEX_CONF_FILE:
with open("template/scex_conf.json") as json_file:
json_data = json.load(json_file)
first_dac = json_data["first_dac"]
last_dac = json_data["last_dac"]
res_switch1 = json_data["res_switch1"]
res_switch2 = json_data["res_switch2"]
dac_weight1 = json_data["dac_weight1"]
dac_weight2 = json_data["dac_weight2"]
dac_weight3 = json_data["dac_weight3"]
# in app_data
# app_data.extend(struct.pack("!H", first_dac))
app_data.append(cell_select)
append_16_bit_val(packet=app_data, val=first_dac[cn])
append_16_bit_val(packet=app_data, val=last_dac[cn])
append_16_bit_val(packet=app_data, val=res_switch1[cn])
append_16_bit_val(packet=app_data, val=res_switch2[cn])
app_data.append(dac_weight1[cn])
app_data.append(dac_weight2[cn])
app_data.append(dac_weight3[cn])
q.add_pus_tc(
create_action_cmd(SCEX_HANDLER_ID, ActionId.ONE_CELLS_CMD, app_data)
)
if op_code in OpCode.ALL_CELLS_CMD:
q.add_log_cmd(Info.ALL_CELLS_CMD)
app_data = bytearray([0])
# cell number
cn = 0
if USE_SCEX_CONF_FILE:
with open("template/scex_conf.json") as json_file:
json_data = json.load(json_file)
first_dac = json_data["first_dac"]
last_dac = json_data["last_dac"]
res_switch1 = json_data["res_switch1"]
res_switch2 = json_data["res_switch2"]
dac_weight1 = json_data["dac_weight1"]
dac_weight2 = json_data["dac_weight2"]
dac_weight3 = json_data["dac_weight3"]
# in app_data
# app_data.extend(struct.pack("!H", first_dac))
append_16_bit_val(packet=app_data, val=first_dac[cn])
append_16_bit_val(packet=app_data, val=last_dac[cn])
append_16_bit_val(packet=app_data, val=res_switch1[cn])
append_16_bit_val(packet=app_data, val=res_switch2[cn])
app_data.append(dac_weight1[cn])
app_data.append(dac_weight2[cn])
app_data.append(dac_weight3[cn])
q.add_pus_tc(
create_action_cmd(SCEX_HANDLER_ID, ActionId.ALL_CELLS_CMD, app_data)
)
if op_code in OpCode.FRAM:
q.add_log_cmd(Info.FRAM)
app_data = bytes([0])
q.add_pus_tc(create_action_cmd(SCEX_HANDLER_ID, ActionId.FRAM, app_data))
def append_16_bit_val(packet: bytearray, val: int):
packet.append((val >> 8) & 0xFF)
packet.append(val & 0xFF)