Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ file(GLOB_RECURSE GENERATOR_JSONS

set(GENERATED_DATA_PACKETS ${CMAKE_SOURCE_DIR}/Core/Inc/Communications/Packets/DataPackets.hpp)
set(GENERATED_ORDER_PACKETS ${CMAKE_SOURCE_DIR}/Core/Inc/Communications/Packets/OrderPackets.hpp)
set(GENERATED_STATE_MACHINE ${CMAKE_SOURCE_DIR}/Core/Inc/state_machine.hpp)
add_custom_command(
OUTPUT
${GENERATED_DATA_PACKETS}
Expand All @@ -79,18 +78,14 @@ add_custom_command(
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/Packet_generation.py
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/State_machine_generation/State_machine_generation.py
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/State_machine_generation/State_machine_description.py
${CMAKE_SOURCE_DIR}/state_machine.json
${GENERATOR_JSONS}
COMMENT "Generating packets and state machine"
COMMENT "Generating packets for ${Board}"
)

add_custom_target(run_generator ALL
DEPENDS
${GENERATED_DATA_PACKETS}
${GENERATED_ORDER_PACKETS}
${GENERATED_STATE_MACHINE}
)

file(GLOB_RECURSE METADATA_SOURCES
Expand Down
7 changes: 0 additions & 7 deletions Core/Inc/Code_generation/Generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
from Packet_generation.Packet_generation import *
from State_machine_generation.State_machine_generation import *

if len(sys.argv)<2:
print("Please enter a board name,exiting...")
Expand All @@ -22,12 +21,6 @@
if __name__ == "__main__":
Generate_DataPackets_hpp(board)
Generate_OrderPackets_hpp(board)
# Generate_Protections_hpp(board), no protections for now
with open("state_machine.json", "r") as file:
data = json.load(file)
sm = parse_state_machine(data)
generate_code(sm)




Expand Down
13 changes: 7 additions & 6 deletions Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "ST-LIB.hpp"

//Data packets for {{board}} -AUTOGENERATED CODE, DO NOT MODIFY-
/*Data packets for {{board}}
-AUTOGENERATED CODE, DO NOT MODIFY-*/
class DataPackets{
public:
{% for enum in enums -%}
Expand All @@ -16,14 +17,14 @@ class DataPackets{
{% for packet in packets -%}
static void {{packet.name}}_init({% for variable in packet.variables %}{{variable.type}} &{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %})
{
{{packet.name}} = new HeapPacket(static_cast<uint16_t>({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
{{packet.name}}_packet = new HeapPacket(static_cast<uint16_t>({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
}

{% endfor -%}

public:
{%for packet in packets -%}
inline static HeapPacket *{{packet.name}}{nullptr};
inline static HeapPacket *{{packet.name}}_packet{nullptr};
{% endfor %}
{% for socket in sockets -%}
inline static {{socket.type}} *{{socket.name}}{nullptr};
Expand All @@ -32,7 +33,7 @@ class DataPackets{
static void start()
{
{% for packet in packets -%}
if ({{packet.name}} == nullptr) {
if ({{packet.name}}_packet == nullptr) {
ErrorHandler("Packet {{packet.name}} not initialized");
}
{% endfor %}
Expand All @@ -50,10 +51,10 @@ class DataPackets{
{%- for packet in sending_packets %}
Scheduler::register_task({% if packet.period_type == "ms" %}{{ (packet.period*1000)|round|int }}{% else %}{{ packet.period|round|int }}{% endif %}, +[](){
{% if packet.name is string -%}
DataPackets::{{packet.socket}}->send_packet(*DataPackets::{{packet.name}});
DataPackets::{{packet.socket}}->send_packet(*DataPackets::{{packet.name}}_packet);
{% else %}
{% for name in packet.name -%}
DataPackets::{{packet.socket}}->send_packet(*DataPackets::{{name}});
DataPackets::{{packet.socket}}->send_packet(*DataPackets::{{name}}_packet);
{% endfor -%}
{%- endif %}
}); {%- endfor %}
Expand Down
4 changes: 2 additions & 2 deletions Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class OrderPackets{
OrderPackets() = default;

{% for packet in packets -%}
inline static HeapOrder *{{packet.name}}{nullptr};
inline static HeapOrder *{{packet.name}}_order{nullptr};
{% endfor %}

{% for packet in packets -%}
static void {{packet.name}}_init({% for variable in packet.variables %}{{variable.type}} &{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %})
{
{{packet.name}} = new HeapOrder({{packet.id}}, &{{packet.name}}_cb{% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
{{packet.name}}_order = new HeapOrder({{packet.id}}, &{{packet.name}}_cb{% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
}
{% endfor %}

Expand Down
39 changes: 23 additions & 16 deletions Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@ def __init__(self,name:str,board:dict,JSONpath:str):
self.id = board["board_id"]
self.ip = board["board_ip"]
#Sockets:
with open(JSONpath+"/boards/"+name+"/sockets.json") as s:
socks = json.load(s)
self.sockets=self.SocketsDescription(socks,self.ip)
try:
with open(JSONpath+"/boards/"+name+"/sockets.json") as s:
socks = json.load(s)
self.sockets=self.SocketsDescription(socks,self.ip)
except Exception as e:
raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}")
#Packets:
self.sending_packets = []
self.data_size =0
self.order_size =0
self.measurement_lists = []
self.packets = {}
for measurement in board["measurements"]:
with open(JSONpath+"/boards/" + name + "/" + measurement) as f:
m = json.load(f)
self.measurement_lists.append(m)
try:
with open(JSONpath+"/boards/" + name + "/" + measurement) as f:
m = json.load(f)
self.measurement_lists.append(m)
except Exception as e:
raise Exception(f"Error in file {JSONpath}/boards/{name}/{measurement}: {e}")
for packets in board["packets"]:
packets_name = re.split(r'_|\.', packets)[0]
self.packets[packets_name] = []
with open(JSONpath+"/boards/" + name+"/" + packets) as f:
p= json.load(f)
try:
with open(JSONpath+"/boards/" + name+"/" + packets) as f:
p= json.load(f)
except Exception as e:
raise Exception(f"Error in file {JSONpath}/boards/{name}/{packets}: {e}")
i=0
for packet in p:
self.packets[packets_name].append(PacketDescription(packet,self.measurement_lists))
self.packets[packets_name].append(PacketDescription(packet,self.measurement_lists, packets))
aux_sending= PacketDescription.check_for_sending(packet)
if aux_sending is not None:
self.sending_packets.append(aux_sending)
Expand Down Expand Up @@ -88,7 +97,7 @@ def __init__(self,sockets:list,board_ip:str):


class PacketDescription:
def __init__(self, packet:dict,measurements:list):
def __init__(self, packet:dict,measurements:list, filename:str="Unknown"):
self.id =packet["id"]
self.name = packet["name"].replace(" ", "_").replace("-", "_")
self.type = packet["type"]
Expand All @@ -98,7 +107,7 @@ def __init__(self, packet:dict,measurements:list):
return
for variable in packet["variables"]:
self.variables.append(variable)
self.measurements.append(MeasurmentsDescription(measurements,variable))
self.measurements.append(MeasurmentsDescription(measurements,variable, filename))

@staticmethod
def check_for_sending(packet:dict):
Expand All @@ -112,15 +121,15 @@ def check_for_sending(packet:dict):
else:
return None
class MeasurmentsDescription:
def __init__(self,measurements:list, variable:str):
def __init__(self,measurements:list, variable:str, filename:str="Unknown"):
self.id = variable
if not hasattr(self.__class__, 'viewed_measurements'):
self.__class__.viewed_measurements = {}
measurement = self._MeasurementSearch(measurements,variable)

if measurement is None:
print(variable)
raise Exception("Measurement not found")
print(f"Measurement not found for variable: {variable} in file: {filename}\n")
raise Exception(f"Measurement not found for variable: {variable} in file: {filename}")

self.name = measurement["name"]
self.type = (self._unsigned_int_correction(measurement["type"]).replace(" ", "_").replace("-", "_"))
Expand Down Expand Up @@ -157,6 +166,4 @@ def _unsigned_int_correction(type:str):
type += "_t"
elif type == "float32":
type = "float"
elif type == "float64":
type = "double"
return type
12 changes: 0 additions & 12 deletions Core/Inc/Code_generation/Packet_generation/ProtectionsTemplate.hpp

This file was deleted.

Loading