Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XTCE decom with CoDICE example #72

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions imap_processing/codice/CcsdsHeaderXtceGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CCSDSParameters:
def __init__(self):
self.parameters = [
{
"name": "VERSION",
"parameterTypeRef": "uint3",
"description": "CCSDS Packet Version Number (always 0)",
},
{
"name": "TYPE",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Type Indicator (0=telemetry)",
},
{
"name": "SEC_HDR_FLG",
"parameterTypeRef": "uint1",
"description": "CCSDS Packet Secondary Header Flag (always 1)",
},
{
"name": "PKT_APID",
"parameterTypeRef": "uint11",
"description": "CCSDS Packet Application Process ID",
},
{
"name": "SEG_FLGS",
"parameterTypeRef": "uint2",
"description": "CCSDS Packet Grouping Flags (3=not part of group)",
},
{
"name": "SRC_SEQ_CTR",
"parameterTypeRef": "uint14",
"description": "CCSDS Packet Sequence Count (increments with each new packet)",
},
{
"name": "PKT_LEN",
"parameterTypeRef": "uint16",
"description": "CCSDS Packet Length (number of bytes after Packet length minus 1)",
},
{
"name": "SHCOARSE",
"parameterTypeRef": "uint32",
"description": "CCSDS Packet Time Stamp (coarse time)",
},
GFMoraga marked this conversation as resolved.
Show resolved Hide resolved
]


# Other utility functions related to CCSDS parameters can also be added here
72 changes: 72 additions & 0 deletions imap_processing/codice/L0/ccsds-header.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version='1.0' encoding='utf-8'?>
<xtce:SpaceSystem xmlns:xtce="http://www.omg.org/space" name="CCSDS_Headers">
<xtce:Header date="2023" version="1.0" author="IMAP SDC" />
<xtce:TelemetryMetaData>
<xtce:ParameterTypeSet>
<xtce:IntegerParameterType name="uint1" signed="false">
<xtce:IntegerDataEncoding sizeInBits="1" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint2" signed="false">
<xtce:IntegerDataEncoding sizeInBits="2" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint3" signed="false">
<xtce:IntegerDataEncoding sizeInBits="3" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint11" signed="false">
<xtce:IntegerDataEncoding sizeInBits="11" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint14" signed="false">
<xtce:IntegerDataEncoding sizeInBits="14" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint16" signed="false">
<xtce:IntegerDataEncoding sizeInBits="16" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
<xtce:IntegerParameterType name="uint32" signed="false">
<xtce:IntegerDataEncoding sizeInBits="32" encoding="unsigned" />
<xtce:UnitSet />
</xtce:IntegerParameterType>
</xtce:ParameterTypeSet>
<xtce:ParameterSet>
<xtce:Parameter name="VERSION" parameterTypeRef="uint3">
<xtce:LongDescription>CCSDS Packet Version Number (always 0)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="TYPE" parameterTypeRef="uint1">
<xtce:LongDescription>CCSDS Packet Type Indicator (0=telemetry)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SEC_HDR_FLG" parameterTypeRef="uint1">
<xtce:LongDescription>CCSDS Packet Secondary Header Flag (always 1)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="PKT_APID" parameterTypeRef="uint11">
<xtce:LongDescription>CCSDS Packet Application Process ID</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SEG_FLGS" parameterTypeRef="uint2">
<xtce:LongDescription>CCSDS Packet Grouping Flags (3=not part of group)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="SRC_SEQ_CTR" parameterTypeRef="uint14">
<xtce:LongDescription>CCSDS Packet Sequence Count (increments with each new packet)</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="PKT_LEN" parameterTypeRef="uint16">
<xtce:LongDescription>CCSDS Packet Length (number of bytes after Packet length minus 1)</xtce:LongDescription>
</xtce:Parameter>
</xtce:ParameterSet>
<xtce:ContainerSet>
<xtce:SequenceContainer name="CCSDSPacket">
<xtce:EntryList>
<xtce:ParameterRefEntry parameterRef="VERSION" />
<xtce:ParameterRefEntry parameterRef="TYPE" />
<xtce:ParameterRefEntry parameterRef="SEC_HDR_FLG" />
<xtce:ParameterRefEntry parameterRef="PKT_APID" />
<xtce:ParameterRefEntry parameterRef="SEG_FLGS" />
<xtce:ParameterRefEntry parameterRef="SRC_SEQ_CTR" />
<xtce:ParameterRefEntry parameterRef="PKT_LEN" />
</xtce:EntryList>
</xtce:SequenceContainer>
</xtce:ContainerSet>
</xtce:TelemetryMetaData>
</xtce:SpaceSystem>
235 changes: 235 additions & 0 deletions imap_processing/codice/L0/decom_python_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
"""
Main script to extract and decommute parameters from a binary file containing packets.

This script reads a binary file containing packet data, searches for a packet with a specific Application Process Identifier (APID),
and then decommutes the packet's parameters using a provided decommutation table. The extracted parameter values are printed.

Usage:
1. Set the 'bin_file_path' variable to the path of the binary file containing packet data.
2. Replace 'target_apid' with the desired APID to search for.
3. Define the 'decomm_table' with the decommutation information for different parameters.
4. Run the script to extract and print decommuted parameter values.

Example:
Assuming 'read_binary_file' and other functions are defined:
- Given a binary file at 'bin_file_path' and a desired 'target_apid':
- If a packet with the 'target_apid' is found, its parameters are extracted and printed.
- If no matching packet is found, a message indicating such is printed.
"""

'''
Parameters
----------
bin_file_path : str
Path to the binary file containing packet data.
target_apid : int
APID of the packet to search for.
decomm_table : list
List of dictionaries, each containing decommutation information for a parameter.
Each dictionary should contain:
- "mnemonic": A unique identifier for the parameter.
- "sequence": An optional parameter sequence number.
- "startByte": Starting byte index in the packet.
- "startBitInByte": Starting bit index within the starting byte.
- "startBit": Overall starting bit index in the packet.
- "lengthInBits": Number of bits to extract for this parameter.
- "dataType": Data type of the parameter, e.g., "unsigned_int", "float", etc.
'''
def read_binary_file(file_path):
with open(file_path, "rb") as file:
data = file.read()
return data


"""
Extracts a value from binary data by interpreting a specified range of bits.

This function is used to extract a value from a sequence of binary data by specifying the starting bit position and the number of bits to consider. The bits are interpreted as an unsigned integer value.

Parameters
----------
data (bytes): The binary data from which the value will be extracted.
start_bit (int): The index of the starting bit for extraction.
length (int): The number of bits to extract.

Returns
-------
int: The extracted value represented as an integer.

"""


def extract_bits(data, start_bit, length):
byte_offset = start_bit // 8
bit_shift = start_bit % 8
mask = (1 << length) - 1

value = 0
for i in range(length):
byte = data[byte_offset + i]
value |= (byte >> bit_shift) << (i * 8)

return value & mask


"""
Finds the index of the first occurrence of a packet with a specific APID in binary data.

This function searches through a sequence of binary data to find the index of the first packet that matches the specified Application Process Identifier (APID). The APID is a unique identifier used in packet-based data communication protocols.

Parameters
----------
bin_data (bytes): The binary data to search within.
target_apid (int): The target APID to search for.

Returns
-------
int: The index of the first occurrence of the packet with the specified APID, or -1 if not found.

Example:
binary_data = bytes([0x12, 0x34, 0x56, 0x12, 0x78, 0x90]) # Example binary data
target_apid = 0x1234 # Example target APID
packet_index = find_packet_with_apid(binary_data, target_apid)
"""


def find_packet_with_apid(bin_data, target_apid):
# Search for the target APID in the binary data
target_apid_bytes = target_apid.to_bytes(2, byteorder="big")
idx = bin_data.find(target_apid_bytes)

return idx


"""
Decommutes packet data using a provided decommutation table.

This function takes a packet's binary data and a decommutation table as input, and returns a dictionary of parameter values extracted from the packet according to the table.

Parameters
----------
packet_data (bytes): Binary data of the packet to decommute.
decomm_table (list): List of dictionaries, each containing decommutation information for a parameter.
Each dictionary should contain:
- "mnemonic": A unique identifier for the parameter.
- "sequence": An optional parameter sequence number.
- "startByte": Starting byte index in the packet.
- "startBitInByte": Starting bit index within the starting byte.
- "startBit": Overall starting bit index in the packet.
- "lengthInBits": Number of bits to extract for this parameter.
- "dataType": Data type of the parameter, e.g., "unsigned_int", "float", etc.

"""


# Decommutation table variables may not all be the same
def decommute_packet(packet_data, decomm_table):
parameters = {}

for entry in decomm_table:
mnemonic = entry["mnemonic"]
sequence = entry["sequence"]
start_byte = entry["startByte"]
start_bit_in_byte = entry["startBitInByte"]
start_bit = entry["startBit"]
length_in_bits = entry["lengthInBits"]
data_type = entry["dataType"]

value = extract_bits(packet_data, start_bit, length_in_bits)
parameters[mnemonic] = value

return parameters


if __name__ == "__main__":
bin_file_path = "../RAW.bin"
target_apid = 0x460 # Replace with the APID of the desired packet
decomm_table = [
{
"mnemonic": "SHCOARSE",
"sequence": 7,
"startByte": 6,
"startBitInByte": 0,
"startBit": 48,
"lengthInBits": 32,
"dataType": "UINT",
},
{
"mnemonic": "Spare",
"sequence": 8,
"startByte": 10,
"startBitInByte": 0,
"startBit": 80,
"lengthInBits": 6,
"dataType": "UINT",
},
{
"mnemonic": "Power_Cycle_Rq",
"sequence": 9,
"startByte": 10,
"startBitInByte": 6,
"startBit": 86,
"lengthInBits": 1,
"dataType": "UINT",
},
{
"mnemonic": "Power_Off_Rq",
"sequence": 10,
"startByte": 10,
"startBitInByte": 7,
"startBit": 87,
"lengthInBits": 1,
"dataType": "UINT",
},
{
"mnemonic": "Heater_Control_Enabled",
"sequence": 11,
"startByte": 11,
"startBitInByte": 0,
"startBit": 88,
"lengthInBits": 1,
"dataType": "UINT",
},
{
"mnemonic": "Heater_1_State",
"sequence": 12,
"startByte": 11,
"startBitInByte": 1,
"startBit": 89,
"lengthInBits": 1,
"dataType": "UINT",
},
{
"mnemonic": "Heater_2_State",
"sequence": 13,
"startByte": 11,
"startBitInByte": 2,
"startBit": 90,
"lengthInBits": 1,
"dataType": "UINT",
},
{
"mnemonic": "Spare2",
"sequence": 14,
"startByte": 11,
"startBitInByte": 3,
"startBit": 91,
"lengthInBits": 5,
"dataType": "UINT",
},
]

data = read_binary_file(bin_file_path)
idx = find_packet_with_apid(data, target_apid)

if idx >= 0:
packet_data = data[idx:]
parameters = decommute_packet(packet_data, decomm_table)
print("Decommuted Parameters of Apid:")
for mnemonic, value in parameters.items():
print(f"{mnemonic}: {value}")
else:
print("Packet with APID not found in the binary data.")

# FileNotFoundError: If the specified file does not exist.
# IOError: If an error occurs while reading the file.
Loading
Loading