eive-tmtc/pus_tc/devs/scex.py

170 lines
5.6 KiB
Python
Raw Normal View History

2022-06-21 16:49:11 +02:00
import enum
2022-06-27 20:18:26 +02:00
import json
2022-06-21 16:49:11 +02:00
from config.definitions import CustomServiceList
from tmtccmd.tc import QueueHelper
2022-06-21 16:49:11 +02:00
from tmtccmd.tc.pus_8_funccmd import generate_action_command
from tmtccmd.config import OpCodeEntry, TmTcDefWrapper
2022-06-21 16:49:11 +02:00
from config.object_ids import SCEX_HANDLER_ID
2022-06-27 20:18:26 +02:00
USE_SCEX_CONF_FILE = True
2022-06-21 16:49:11 +02:00
class OpCodes:
PING = ["0", "ping"]
2022-06-27 20:18:26 +02:00
ION_CMD = ["1", "ion"]
TEMP_CMD = ["2", "temp"]
2022-06-30 21:49:25 +02:00
EXP_STATUS_CMD = ["3", "expstatus"]
2022-06-27 20:18:26 +02:00
2022-06-30 21:49:25 +02:00
ONE_CELLS_CMD = ["4", "onecell"]
ALL_CELLS_CMD = ["5", "allcells"]
2022-06-27 20:18:26 +02:00
FRAM = ["6", "fram"]
2022-06-21 16:49:11 +02:00
class ActionIds(enum.IntEnum):
PING = 7
2022-06-21 18:15:17 +02:00
ION_CMD = 4
2022-06-27 20:18:26 +02:00
TEMP_CMD = 3
EXP_STATUS_CMD = 2
ONE_CELLS_CMD = 6
ALL_CELLS_CMD = 5
FRAM = 1
2022-06-21 16:49:11 +02:00
class Info:
PING = "Send Ping command"
2022-06-27 20:18:26 +02:00
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"
2022-06-21 16:49:11 +02:00
def add_scex_cmds(defs: TmTcDefWrapper):
oce = OpCodeEntry()
oce.add(keys=OpCodes.PING, info=Info.PING)
oce.add(keys=OpCodes.ION_CMD, info=Info.ION_CMD)
oce.add(keys=OpCodes.TEMP_CMD, info=Info.TEMP_CMD)
oce.add(keys=OpCodes.EXP_STATUS_CMD, info=Info.EXP_STATUS_CMD)
oce.add(keys=OpCodes.ONE_CELLS_CMD, info=Info.ONE_CELLS_CMD)
2022-06-27 20:18:26 +02:00
oce.add(keys=OpCodes.ALL_CELLS_CMD, info=Info.ALL_CELLS_CMD)
oce.add(keys=OpCodes.FRAM, info=Info.FRAM)
2022-06-21 18:15:17 +02:00
defs.add_service(
2022-06-21 16:49:11 +02:00
name=CustomServiceList.SCEX.value,
info="SCEX Device",
op_code_entry=oce
2022-06-21 16:49:11 +02:00
)
def pack_scex_cmds(q: QueueHelper, op_code: str):
2022-06-21 16:49:11 +02:00
if op_code in OpCodes.PING:
q.add_log_cmd(Info.PING)
2022-06-21 16:49:11 +02:00
app_data = bytes([0])
q.add_pus_tc(generate_action_command(SCEX_HANDLER_ID, ActionIds.PING, app_data))
2022-06-27 20:18:26 +02:00
if op_code in OpCodes.ION_CMD:
q.add_log_cmd(Info.ION_CMD)
2022-06-27 20:18:26 +02:00
app_data = bytes([0])
q.add_pus_tc(generate_action_command(SCEX_HANDLER_ID, ActionIds.ION_CMD, app_data))
2022-06-27 20:18:26 +02:00
if op_code in OpCodes.TEMP_CMD:
q.add_log_cmd(Info.TEMP_CMD)
2022-06-27 20:18:26 +02:00
app_data = bytes([0])
q.add_pus_tc(generate_action_command(SCEX_HANDLER_ID, ActionIds.TEMP_CMD, app_data))
2022-06-27 20:18:26 +02:00
if op_code in OpCodes.EXP_STATUS_CMD:
q.add_log_cmd(Info.EXP_STATUS_CMD)
2022-06-21 18:15:17 +02:00
app_data = bytes([0])
q.add_pus_tc(generate_action_command(SCEX_HANDLER_ID, ActionIds.EXP_STATUS_CMD, app_data))
2022-06-21 16:49:11 +02:00
2022-06-21 18:15:17 +02:00
# one cell
2022-06-27 20:18:26 +02:00
if op_code in OpCodes.ONE_CELLS_CMD:
q.add_log_cmd(Info.ONE_CELLS_CMD)
2022-06-27 20:18:26 +02:00
app_data = bytearray()
# cell number
cn = 0
2022-06-27 20:18:26 +02:00
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
2022-06-30 21:49:25 +02:00
# app_data.extend(struct.pack("!H", first_dac))
2022-06-27 20:18:26 +02:00
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(generate_action_command(SCEX_HANDLER_ID, ActionIds.ONE_CELLS_CMD, app_data))
2022-06-27 20:18:26 +02:00
2022-06-30 21:49:25 +02:00
if op_code in OpCodes.ALL_CELLS_CMD:
q.add_log_cmd(Info.ALL_CELLS_CMD)
2022-06-30 21:49:25 +02:00
app_data = bytearray()
# 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(generate_action_command(SCEX_HANDLER_ID, ActionIds.ALL_CELLS_CMD, app_data))
2022-06-30 21:49:25 +02:00
if op_code in OpCodes.FRAM:
q.add_log_cmd(Info.FRAM)
2022-06-30 21:49:25 +02:00
app_data = bytes([0])
q.add_pus_tc(generate_action_command(SCEX_HANDLER_ID, ActionIds.FRAM, app_data))
2022-06-27 20:18:26 +02:00
def append_16_bit_val(packet: bytearray, val: int):
packet.append((val >> 8) & 0xFF)
packet.append(val & 0xFF)