Skip to content

Commit 8450592

Browse files
authored
Merge pull request #75 from Hyperloop-UPV/fix/deleted_packet_generation
Added the deleted content from code generation as well as this new functionalitys: -No longer accepts period_ms -Now sockets can have ip on adj as "backend", this will use the general_info.json ip for the backend
1 parent 9c87b50 commit 8450592

8 files changed

Lines changed: 136 additions & 179 deletions

File tree

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
url = https://github.com/HyperloopUPV-H8/ST-LIB
44
[submodule "Core/Inc/Communications/JSON_ADE"]
55
path = Core/Inc/Code_generation/JSON_ADE
6-
url = https://github.com/HyperloopUPV-H8/JSON_ADE
6+
url = https://github.com/HyperloopUPV-H8/adj

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include(CTest)
55
enable_testing()
66

77
set(EXECUTABLE ${PROJECT_NAME}.elf)
8-
set(BOARD_NAME "TEST" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json")
8+
set(BOARD_NAME "VCU" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json" FORCE)
99
if(BOARD_NAME STREQUAL "")
1010
message(FATAL_ERROR "BOARD_NAME cannot be empty")
1111
endif()

Core/Inc/Code_generation/JSON_ADE

Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ def __init__(self,name:str,board:dict,JSONpath:str):
66
self.name = name
77
self.id = board["board_id"]
88
self.ip = board["board_ip"]
9+
10+
# Load backend IP from general_info.json
11+
backend_ip = "0.0.0.0"
12+
try:
13+
with open(JSONpath + "/general_info.json") as f:
14+
general_info = json.load(f)
15+
if "addresses" in general_info and "backend" in general_info["addresses"]:
16+
backend_ip = general_info["addresses"]["backend"]
17+
except Exception as e:
18+
print(f"Warning: Could not load backend IP from general_info.json: {e}")
19+
920
#Sockets:
1021
try:
1122
with open(JSONpath+"/boards/"+name+"/sockets.json") as s:
1223
socks = json.load(s)
13-
self.sockets=self.SocketsDescription(socks,self.ip)
24+
self.sockets = self.SocketsDescription(socks, self.ip, backend_ip)
1425
except Exception as e:
1526
raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}")
1627
#Packets:
1728
self.sending_packets = []
18-
self.data_size =0
19-
self.order_size =0
29+
self.data_size = 0
30+
self.order_size = 0
2031
self.measurement_lists = []
2132
self.packets = {}
2233
for measurement in board["measurements"]:
@@ -60,11 +71,11 @@ def fix_sendind_packets(sending_packets:list):
6071
period_type = item.get("period_type")
6172
socket = item.get("socket")
6273
name = item.get("name")
63-
key = (period,period_type, socket)
74+
key = (period, period_type, socket)
6475
lookup.setdefault(key, []).append(name)
6576

66-
for (period,period_type, socket), names in lookup.items():
67-
entry = {"period": period,"period_type":period_type, "socket": socket}
77+
for (period, period_type, socket), names in lookup.items():
78+
entry = {"period": period, "period_type": period_type, "socket": socket}
6879
if len(names) == 1:
6980
entry["name"] = names[0]
7081
else:
@@ -76,23 +87,30 @@ def fix_sendind_packets(sending_packets:list):
7687

7788

7889
class SocketsDescription:
79-
def __init__(self,sockets:list,board_ip:str):
80-
self.allSockets=[]
90+
def __init__(self, sockets: list, board_ip: str, backend_ip: str):
91+
self.allSockets = []
8192
self.ServerSockets = []
8293
self.Sockets = []
8394
self.DatagramSockets = []
8495
self.board_ip = board_ip
96+
self.backend_ip = backend_ip
8597
for sock in sockets:
8698
name = sock["name"].replace(" ", "_").replace("-", "_")
8799
sock_type = sock["type"]
88-
self.allSockets.append({"name": name,"type":sock_type})
100+
self.allSockets.append({"name": name, "type": sock_type})
89101

90102
if sock_type == "ServerSocket":
91-
self.ServerSockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "port": sock["port"]})
103+
self.ServerSockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "port": sock["port"]})
92104
elif sock_type == "Socket":
93-
self.Sockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "local_port": sock["local_port"], "remote_ip": sock["remote_ip"], "remote_port": sock["remote_port"]})
105+
remote_ip = sock["remote_ip"]
106+
if remote_ip == "backend":
107+
remote_ip = self.backend_ip
108+
self.Sockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "local_port": sock["local_port"], "remote_ip": remote_ip, "remote_port": sock["remote_port"]})
94109
elif sock_type == "DatagramSocket":
95-
self.DatagramSockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "port": sock["port"],"remote_ip":sock["remote_ip"]})
110+
remote_ip = sock["remote_ip"]
111+
if remote_ip == "backend":
112+
remote_ip = self.backend_ip
113+
self.DatagramSockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "port": sock["port"], "remote_ip": remote_ip})
96114

97115

98116

@@ -110,14 +128,11 @@ def __init__(self, packet:dict,measurements:list, filename:str="Unknown"):
110128
self.measurements.append(MeasurmentsDescription(measurements,variable, filename))
111129

112130
@staticmethod
113-
def check_for_sending(packet:dict):
131+
def check_for_sending(packet: dict):
114132
if "period" in packet and "period_type" in packet and "socket" in packet:
115133
name = packet["name"].replace(" ", "_").replace("-", "_")
116-
return {"name": name,"period": packet["period"],"period_type":packet["period_type"],"socket": packet["socket"]}
134+
return {"name": name, "period": packet["period"], "period_type": packet["period_type"], "socket": packet["socket"]}
117135

118-
elif "period_ms" in packet and "socket" in packet:
119-
name = packet["name"].replace(" ", "_").replace("-", "_")
120-
return {"name": name,"period": packet["period_ms"],"period_type":"ms","socket": packet["socket"]}
121136
else:
122137
return None
123138
class MeasurmentsDescription:
@@ -166,4 +181,6 @@ def _unsigned_int_correction(type:str):
166181
type += "_t"
167182
elif type == "float32":
168183
type = "float"
184+
elif type == "float64":
185+
type = "double"
169186
return type

Core/Inc/Code_generation/Packet_generation/Packet_generation.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ def GenerateDataEnum(board:BoardDescription):
3838
return Enums
3939

4040

41-
def GenerateDataPackets(board:BoardDescription):
42-
Packets =[]
41+
def GenerateDataPackets(board: BoardDescription):
42+
Packets = []
4343
totaldata = []
4444
for packet in board.packets:
4545
for packet_instance in board.packets[packet]:
4646
if packet_instance.type != "order":
4747
tempdata = ""
4848
tempdata_but_pointer = ""
4949
for variable in packet_instance.variables:
50-
tempdata +=(str(variable) +",")
51-
tempdata_but_pointer +=("&"+str(variable) +",")
50+
tempdata += (str(variable) + ",")
51+
tempdata_but_pointer += ("&" + str(variable) + ",")
5252
if tempdata.endswith(","):
5353
tempdata = tempdata[:-1]
5454
if tempdata_but_pointer.endswith(","):
@@ -61,7 +61,7 @@ def GenerateDataPackets(board:BoardDescription):
6161
"type": measurement.type
6262
})
6363

64-
aux_packet = {"name": packet_instance.name, "data":tempdata_but_pointer.replace(" ", "_").replace("-", "_") , "id": packet_instance.id, "variables": packet_variables}
64+
aux_packet = {"name": packet_instance.name, "data": tempdata_but_pointer.replace(" ", "_").replace("-", "_"), "id": packet_instance.id, "variables": packet_variables}
6565
Packets.append(aux_packet)
6666
for measurement in packet_instance.measurements:
6767
aux_data = {"type": measurement.type, "name": measurement.id.replace(" ", "_").replace("-", "_")}
@@ -135,8 +135,8 @@ def Generate_DataPackets_hpp(board_input:str):
135135

136136
#--------------OrderPackets.hpp generation---------------#
137137

138-
def Get_order_context(board:BoardDescription):
139-
def GenerateOrderEnum(board:BoardDescription):
138+
def Get_order_context(board: BoardDescription):
139+
def GenerateOrderEnum(board: BoardDescription):
140140
Enums = []
141141
for packet in board.packets:
142142
for packet_instance in board.packets[packet]:
@@ -146,18 +146,17 @@ def GenerateOrderEnum(board:BoardDescription):
146146
Enums.append(measurement.enum)
147147
return Enums
148148

149-
150-
def GenerateOrderPackets(board:BoardDescription):
151-
Packets =[]
149+
def GenerateOrderPackets(board: BoardDescription):
150+
Packets = []
152151
totaldata = []
153152
for packet in board.packets:
154153
for packet_instance in board.packets[packet]:
155154
if packet_instance.type == "order":
156155
tempdata = ""
157156
tempdata_but_pointer = ""
158157
for variable in packet_instance.variables:
159-
tempdata +=(str(variable) +",")
160-
tempdata_but_pointer +=("&"+str(variable) +",")
158+
tempdata += (str(variable) + ",")
159+
tempdata_but_pointer += ("&" + str(variable) + ",")
161160
if tempdata.endswith(","):
162161
tempdata = tempdata[:-1]
163162
tempdata_but_pointer = tempdata_but_pointer[:-1]
@@ -169,17 +168,16 @@ def GenerateOrderPackets(board:BoardDescription):
169168
"type": measurement.type
170169
})
171170

172-
aux_packet = {"name": packet_instance.name, "data":tempdata_but_pointer , "id": packet_instance.id, "variables": packet_variables}
171+
aux_packet = {"name": packet_instance.name, "data": tempdata_but_pointer, "id": packet_instance.id, "variables": packet_variables}
173172
Packets.append(aux_packet)
174173
for measurement in packet_instance.measurements:
175-
aux_data = {"type": measurement.type, "name": measurement.id}
174+
aux_data = {"type": measurement.type, "name": measurement.name}
176175
if not any(x["name"] == aux_data["name"] for x in totaldata):
177176
totaldata.append(aux_data)
178177

179-
return Packets,totaldata
180-
178+
return Packets, totaldata
181179

182-
packets,data = GenerateOrderPackets(board)
180+
packets, data = GenerateOrderPackets(board)
183181
context = {
184182
"board": board.name,
185183
"enums": GenerateOrderEnum(board),
@@ -203,6 +201,5 @@ def Generate_OrderPackets_hpp(board_input:str):
203201
template = env.get_template("OrderTemplate.hpp")
204202
context = Get_order_context(board_instance)
205203

206-
207-
with open(order_packets_path,"w") as Output:
204+
with open(order_packets_path, "w") as Output:
208205
Output.write(template.render(context))

Core/Src/Examples/LinearSensorCharacterization.cpp

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#ifdef EXAMPLE_LINEAR_SENSOR_CHARACTERIZATION
22

3+
#include "Communications/Packets/DataPackets.hpp"
4+
#include "Communications/Packets/OrderPackets.hpp"
35
#include "ST-LIB.hpp"
46
#include "main.h"
57

68
using namespace ST_LIB;
79

8-
constexpr auto led = ST_LIB::DigitalOutputDomain::DigitalOutput(ST_LIB::PB0);
10+
double slope{1.0};
11+
double offset{0.0};
12+
13+
double value{0.0};
14+
double sensor_value{0.0};
15+
16+
double real_value{0.0};
17+
18+
constinit float raw_value{0.0f};
19+
constexpr auto sensor = ADCDomain::ADC(
20+
ST_LIB::PA0,
21+
raw_value,
22+
ADCDomain::Resolution::BITS_16,
23+
ADCDomain::SampleTime::CYCLES_8_5
24+
);
925

10-
#ifdef STLIB_ETH
1126
#if defined(USE_PHY_LAN8742)
1227
constexpr auto eth = EthernetDomain::Ethernet(
1328
EthernetDomain::PINSET_H10,
@@ -32,26 +47,76 @@ constexpr auto eth = EthernetDomain::Ethernet(
3247
#else
3348
#error "No PHY selected for Ethernet pinset selection"
3449
#endif
35-
using ExampleEthernetBoard = ST_LIB::Board<eth, led>;
36-
#else
37-
using ExampleEthernetBoard = ST_LIB::Board<led>;
38-
#endif
50+
using ExampleEthernetBoard = ST_LIB::Board<eth, sensor>;
51+
52+
extern "C" void Error_Handler(void) {
53+
ErrorHandler("HAL error handler triggered");
54+
while (1) {
55+
}
56+
}
57+
58+
void characterize(float raw, double read) {
59+
// Incremental OLS accumulators for y = slope * x + offset.
60+
static uint64_t sample_count{0};
61+
static double sum_x{0.0};
62+
static double sum_y{0.0};
63+
static double sum_xx{0.0};
64+
static double sum_xy{0.0};
65+
66+
const double x = static_cast<double>(raw);
67+
const double y = read;
68+
69+
++sample_count;
70+
sum_x += x;
71+
sum_y += y;
72+
sum_xx += x * x;
73+
sum_xy += x * y;
74+
75+
if (sample_count < 2) {
76+
offset = y - (slope * x);
77+
return;
78+
}
79+
80+
const double n = static_cast<double>(sample_count);
81+
const double denominator = (n * sum_xx) - (sum_x * sum_x);
82+
if (denominator == 0.0) {
83+
return;
84+
}
85+
86+
slope = ((n * sum_xy) - (sum_x * sum_y)) / denominator;
87+
offset = (sum_y - (slope * sum_x)) / n;
88+
}
3989

4090
int main(void) {
4191
Hard_fault_check();
42-
4392
ExampleEthernetBoard::init();
44-
#ifdef STLIB_ETH
93+
94+
Scheduler::start();
95+
// Comms
96+
OrderPackets::characterize_init(real_value);
97+
DataPackets::characterization_init(slope, offset);
98+
DataPackets::Value_init(sensor_value, value);
99+
DataPackets::start();
100+
101+
OrderPackets::start();
102+
103+
// Instances
45104
auto& eth_instance = ExampleEthernetBoard::instance_of<eth>();
46-
#endif
47-
auto& led_instance = ExampleEthernetBoard::instance_of<led>();
105+
auto& sensor_instance = ExampleEthernetBoard::instance_of<sensor>();
48106

49-
led_instance.turn_on();
50107
while (1) {
51-
#ifdef STLIB_ETH
52108
eth_instance.update();
53-
#endif
109+
Scheduler::update();
110+
111+
sensor_instance.read();
112+
sensor_value = static_cast<double>(raw_value);
113+
value = slope * sensor_value + offset;
114+
115+
if (OrderPackets::characterize_flag) {
116+
OrderPackets::characterize_flag = false;
117+
characterize(raw_value, real_value);
118+
DataPackets::packets_socket->send_packet(*DataPackets::characterization_packet);
119+
}
54120
}
55121
}
56-
57-
#endif // EXAMPLE_LINEAR_SENSOR_CHARACTERIZATION
122+
#endif

Core/Src/Runes/generated_metadata.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
extern "C" {
66
const char DESCRIPTION[255] __attribute__((section(".metadata_pool"))) =
77
"****************" // placeholder for beggining
8-
"20260206T223828" // DateTime using ISO-8601 format
8+
"20260218T203535" // DateTime using ISO-8601 format
99
" " // alignment
10-
"dcf817b3" // STLIB commit
10+
"019403b6" // STLIB commit
1111
"--------" // ADJ commit
12-
"4b3d0ec4" // Board commit
12+
"9c87b508" // Board commit
1313
// the '=' is used for unparsing
1414
;
1515
}

0 commit comments

Comments
 (0)