Skip to content

Fw-136-Code-generation-for-data-packets#479

Closed
Cantonplas wants to merge 8 commits intodevelopmentfrom
Fw-136-Code-generation-for-packets
Closed

Fw-136-Code-generation-for-data-packets#479
Cantonplas wants to merge 8 commits intodevelopmentfrom
Fw-136-Code-generation-for-packets

Conversation

@Cantonplas
Copy link
Contributor

Python script that creates the .hpp for the data packets based on the .json files from the JSON.ADE directory. It also has description classes to make it more easy for more code generation based on this script. Works quite good, it has an example attached that has been autogenerated.

@jmaralo
Copy link
Member

jmaralo commented Dec 28, 2024

This should be in the template project, not the ST-LIB

StackPacket* lcu_data;


DataPacket(uint16_t idpacket316,uint32_t current_control_frequency,uint32_t levitation_control_frequency,float32 lcu_coil_current_ref_1,float32 lcu_coil_current_ref_2,float32 lcu_coil_current_ref_3,float32 lcu_coil_current_ref_4,float32 lcu_coil_current_ref_5,float32 lcu_coil_current_ref_6,float32 lcu_coil_current_ref_7,float32 lcu_coil_current_ref_8,float32 lcu_coil_current_ref_9,float32 lcu_coil_current_ref_10,float32 pos_y,float32 pos_y_d,float32 pos_y_i,float32 pos_z,float32 pos_z_d,float32 pos_z_i,float32 pos_rot_x,float32 pos_rot_x_d,float32 pos_rot_x_i,float32 pos_rot_y,float32 pos_rot_y_d,float32 pos_rot_y_i,float32 pos_rot_z,float32 pos_rot_z_d,float32 pos_rot_z_i,uint16_t idpacket315,general_state general_state,slave_state slave_state,float32 lcu_coil_temp_1,float32 lcu_coil_temp_2,float32 lcu_coil_temp_3,float32 lcu_coil_temp_4,float32 lcu_coil_temp_5,float32 lcu_coil_temp_6,float32 lcu_coil_temp_7,float32 lcu_coil_temp_8,float32 lcu_coil_temp_9,float32 lcu_coil_temp_10,float32 lcu_driver_temp_1,float32 lcu_driver_temp_2,float32 lcu_driver_temp_3,float32 lcu_driver_temp_4,float32 lcu_driver_temp_5,float32 lcu_driver_temp_6,float32 lcu_driver_temp_7,float32 lcu_driver_temp_8,float32 lcu_driver_temp_9,float32 lcu_driver_temp_10,float32 lcu_coil_current_1,float32 lcu_coil_current_2,float32 lcu_coil_current_3,float32 lcu_coil_current_4,float32 lcu_coil_current_5,float32 lcu_coil_current_6,float32 lcu_coil_current_7,float32 lcu_coil_current_8,float32 lcu_coil_current_9,float32 lcu_coil_current_10,float32 lcu_vbat_1,float32 lcu_vbat_2,float32 lcu_vbat_3,float32 lcu_vbat_4,float32 lcu_vbat_5,float32 lcu_vbat_6,float32 lcu_vbat_7,float32 lcu_vbat_8,float32 lcu_vbat_9,float32 lcu_vbat_10,float32 lcu_airgap_1,float32 lcu_airgap_2,float32 lcu_airgap_3,float32 lcu_airgap_4,float32 lcu_airgap_5,float32 lcu_airgap_6,float32 lcu_airgap_7,float32 lcu_airgap_8)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the constructor should take references, like uint16_t &idpacket316, uint32_t &current_control_frequency, and so on, otherwise it wont work as expected

Comment on lines 8 to 91
class BoardDescription:
def __init__(self,name:str,board:dict):
self.name = name
self.id = board["board_id"]
self.ip = board["board_ip"]
self.size =0
i = 0
self.packets = {}
for packets in board["packets"]:
packets_name = re.split(r'_|\.', packets)[0]
self.packets[packets_name] = []
measurement = self._MeasurementFileSearch(packets,board["measurements"])
with open("Inc/Packet_generation/JSON_ADE/boards/" + name+"/" + board["packets"][i]) as f:
p= json.load(f)
with open("Inc/Packet_generation/JSON_ADE/boards/" + name + "/" + measurement) as f:
m = json.load(f)
i += 1
for packet in p["packets"]:
self.packets[packets_name].append(PacketDescription(packet,m))
self.size = self.size +1
@staticmethod
def _MeasurementFileSearch(packet:str,measurements:dict):
packet_name = packet.split('_')[0]
for measurement in measurements:
measurement_name = measurement.split('_')[0]
if packet_name[0] == measurement_name[0]:
return measurement
else:
return measurements[0]
class PacketDescription:
def __init__(self, packet:dict,measurements:dict):
self.id =packet["id"]
self.name = (packet["name"].replace(" ", "_"))
self.type = packet["type"]
self.variables = []
self.measurements = []
i=0
for variable in packet["variables"]:
self.variables.append(variable["name"])
self.measurements.append(MeasurmentsDescription(measurements,variable["name"]))


class MeasurmentsDescription:
def __init__(self,measurements:dict, variable:str):
self.id = variable
measurement = self._MeasurementSearch(measurements,variable)
if measurement is None:
raise Exception("Measurement not found")
else:
self.name = measurement["name"]
self.type = self._unsigned_int_correction(measurement["type"])
if self.type == "enum":
self.enum = self._create_enum(measurement)
self.type = measurement["id"]

@staticmethod
def _MeasurementSearch(measurements:dict, variable:str):
for measurment in measurements["measurements"]:
if measurment["id"] == variable:
return measurment
return None

@staticmethod
def _create_enum(measurement: dict):
if "enumValues" not in measurement:
raise ValueError("Measurement does not contain 'enumValues'")

enum = enum_template.replace("%name%", measurement["id"])
values = ""
for value in measurement["enumValues"]:
values += value + ",\n"
if values.endswith(",\n"):
values = values[:-2]
values += "\n"
enum = enum.replace("%values%", values.strip())
return enum

@staticmethod
def _unsigned_int_correction(type:str):
aux_type = type[:4]
if aux_type == "uint":
type += "_t"
return type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are going to reuse this logic in multiple places, it would be great if you could split this file into two, one which parses the documents and another one that fills the template

@Cantonplas Cantonplas closed this Dec 29, 2024
@Cantonplas Cantonplas deleted the Fw-136-Code-generation-for-packets branch January 13, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants