Skip to content

Commit efd2f85

Browse files
committed
working code but still WIP refactors in the background
1 parent e966867 commit efd2f85

File tree

7 files changed

+89
-106
lines changed

7 files changed

+89
-106
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ AllowShortIfStatementsOnASingleLine: Always
55
AllowShortBlocksOnASingleLine: Always
66
IndentAccessModifiers: false
77
AccessModifierOffset: -4
8+
AlignAfterOpenBracket: BlockIndent # DontAlign

Telemetry-Main/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ add_library(libradio STATIC ../XBeeRadio/libradio/src/radio.cpp)
1515
target_include_directories(libradio PUBLIC ../XBeeRadio/libradio/src/)
1616
target_link_libraries(libradio mbed-os)
1717

18-
add_library(BT817Q STATIC ./dash/BT817Q.cpp ./dash/layouts.cpp)
19-
target_include_directories(BT817Q PUBLIC ./dash)
20-
target_link_libraries(BT817Q mbed-core-flags)
18+
add_library(dash STATIC ./dash/BT817Q.cpp ./dash/layouts.cpp)
19+
target_include_directories(dash PUBLIC ./dash)
20+
target_link_libraries(dash mbed-core-flags)
21+
22+
add_library(fsdaq STATIC ./fsdaq/file_batch_writer.cpp) # ./fsdaq/can_processor_generated.cpp
23+
target_include_directories(fsdaq PUBLIC ./fsdaq)
24+
target_link_libraries(fsdaq mbed-core-flags)
2125

2226
set(CMAKE_CXX_STANDARD 20)
2327
add_executable(${PROJECT_NAME} main.cpp VehicleStateManager.cpp)
24-
target_link_libraries(${PROJECT_NAME} mbed-os libradio mbed-storage-sd mbed-storage-fat BT817Q)
28+
target_link_libraries(${PROJECT_NAME} mbed-os dash fsdaq libradio mbed-storage-sd mbed-storage-fat)
2529
mbed_set_post_build(${PROJECT_NAME})

Telemetry-Main/fsdaq/can_processor_generated.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ void CANProcessor::process_message() {
1515
break;
1616

1717
case SME_RPDO_Throttle_Demand:
18-
uint16_t raw_SME_THROTL_TorqueDemand;
18+
uint16_t raw_SME_THROTL_TorqueDemand = 0;
19+
raw_SME_THROTL_TorqueDemand |= msg.data[0] & 0x07f;
1920

2021
uint16_t raw_SME_THROTL_MaxSpeed;
2122

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
#include "encoder_generated.hpp"
2+
#include "file_batch_writer.hpp"
23

34
namespace fsdaq {
45

5-
class FileBatchWriter {
6-
public:
7-
FileBatchWriter(FILE *file)
6+
FileBatchWriter::FileBatchWriter(FILE *file)
87
: curr_batch(std::make_unique<DataBatch>()), fp(file) {}
98

10-
void append_row(DataRow next_row) {
9+
void FileBatchWriter::append_row(DataRow& next_row) {
10+
// printf("setRow()\n");
1111
curr_batch->setRow(next_row, row_idx);
1212

1313
row_idx++;
1414
if (row_idx == ROWS_PER_BATCH) {
15-
write_fsdaq_batch(curr_batch.get(), fp);
15+
write_fsdaq_batch(curr_batch.get(), this->fp);
1616
row_idx = 0;
1717
}
1818
}
1919

20-
private:
21-
std::unique_ptr<DataBatch> curr_batch;
22-
23-
int row_idx = 0;
24-
25-
FILE *fp;
26-
};
27-
2820
} // namespace fsdaq

Telemetry-Main/fsdaq/file_batch_writer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FileBatchWriter {
88
public:
99
FileBatchWriter(FILE *file);
1010

11-
void append_row(DataRow next_row);
11+
void append_row(DataRow& next_row);
1212

1313
private:
1414
std::unique_ptr<DataBatch> curr_batch;

Telemetry-Main/main.cpp

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#include "BT817Q.hpp"
21
#include "FATFileSystem.h"
32
#include "SDBlockDevice.h"
43
#include "Ticker.h"
54
#include "VehicleStateManager.hpp"
5+
#include "dash/layouts.h"
66
#include "fsdaq/encoder_generated.hpp"
7-
#include "fsdaq/writer.hpp"
8-
#include "layouts.h"
7+
#include "fsdaq/file_batch_writer.hpp"
98
#include "radio.hpp"
109
#include <stdbool.h>
1110
#include <string>
@@ -43,7 +42,8 @@ SDBlockDevice sd{
4342
FATFileSystem fs{"sd"};
4443

4544
FILE *sd_fp;
46-
fsdaq::FileBatchWriter data_writer{sd_fp};
45+
// fsdaq::FileBatchWriter data_writer{sd_fp};
46+
fsdaq::DataRow current_row{};
4747

4848
Layouts eve(PC_12, PC_11, PC_10, PD_2, PB_7, PC_13, EvePresets::CFA800480E3);
4949

@@ -125,8 +125,7 @@ void update_dash() {
125125
const VehicleState vsm_state = vsm.getState();
126126
uint8_t max_temp = 0;
127127

128-
for (auto [TEMPS_CELL0, TEMPS_CELL1, TEMPS_CELL2, TEMPS_CELL3, TEMPS_CELL4,
129-
TEMPS_CELL5] : vsm_state.accSegTemps) {
128+
for (auto [TEMPS_CELL0, TEMPS_CELL1, TEMPS_CELL2, TEMPS_CELL3, TEMPS_CELL4, TEMPS_CELL5] : vsm_state.accSegTemps) {
130129
max_temp =
131130
max(TEMPS_CELL0,
132131
max(TEMPS_CELL1,
@@ -171,55 +170,49 @@ void update_dash() {
171170
// eve.drawStandardLayout2(params);
172171

173172
eve.drawLayout3(
174-
Faults{false,
175-
static_cast<bool>(!vsm_state.accStatus.PRECHARGE_DONE),
176-
static_cast<bool>(!vsm_state.accStatus.SHUTDOWN_STATE)},
177-
static_cast<float>(vsm_state.accPower.PACK_VOLTAGE / 100.0), max_temp,
178-
vsm_state.smeTemp.CONTROLLER_TEMP, vsm_state.smeTemp.MOTOR_TEMP,
173+
Faults{false, static_cast<bool>(!vsm_state.accStatus.PRECHARGE_DONE), static_cast<bool>(!vsm_state.accStatus.SHUTDOWN_STATE)},
174+
static_cast<float>(vsm_state.accPower.PACK_VOLTAGE / 100.0),
175+
max_temp,
176+
vsm_state.smeTemp.CONTROLLER_TEMP,
177+
vsm_state.smeTemp.MOTOR_TEMP,
179178
vsm_state.accPower.SOC,
180179
static_cast<float>(vsm_state.pdbPowerA.GLV_VOLTAGE),
181-
static_cast<bool>(vsm_state.etcStatus.RTD), n);
180+
static_cast<bool>(vsm_state.etcStatus.RTD),
181+
n
182+
);
182183
}
183184

184-
// void update_data() {
185-
//
186-
// };
187-
188185
int main() {
189-
mbed_can.attach([]() { queue.call(&process_can) }, interface::can::RxIrq);
190-
191186
printf("Hello world\n");
192187

188+
ThisThread::sleep_for(500ms);
189+
193190
// Initializing Dash
194-
if (ENABLE_DASH) init_dash();
191+
if (ENABLE_DASH) { init_dash(); update_dash(); }
195192

196193
// Initializing SD card
197194
if (ENABLE_SD) init_sd();
198195

199-
// int radio_temp = radio.get_temp();
200-
// if (radio_temp >= 70 || radio_temp <= 10) {
201-
// printf("Radio temperature returned an unrealistic value.
202-
// Disabling.\n"); state.radio_on = false;
203-
// }
204-
205-
// int x = 0;
206-
// Timer t;
207-
// t.start();
208-
// auto p = Layouts::StandardLayoutParams{.faults= Faults{}, .soc = 3};
209-
// //for testing
196+
fsdaq::FileBatchWriter data_writer{sd_fp};
210197

211198
// event_queue.call_every(RADIO_UPDATE_HZ, &update_radio())
199+
212200
if (ENABLE_SD) {
213201
// TODO::::
214-
queue.call_every(SD_UPDATE_HZ, []() { data_writer.append_row(current_row) });
202+
queue.call_every(SD_UPDATE_HZ, [&data_writer]() { data_writer.append_row(current_row); });
203+
204+
// mbed_can.attach(
205+
// []() { queue.call([]() { data_writer.append_row(current_row); }); },
206+
// interface::can::RxIrq
207+
// );
215208
}
216209
if (ENABLE_DASH) {
217210
queue.call_every(DASH_UPDATE_HZ, &update_dash);
218211
}
219212
Timer t;
220213
t.start();
221214
queue.call_every(1ms, [&t]() {
222-
printf("%fms\n", t.elapsed_time().count() / 1000.0f);
215+
// printf("%fms\n", t.elapsed_time().count() / 1000.0f);
223216
t.reset();
224217
vsm.update();
225218
const VehicleState state = vsm.getState();
@@ -520,6 +513,5 @@ int main() {
520513
// current_row.SMPC_SER_FIRMWARE_VER
521514
});
522515
queue.call_every(100ms, []() { n++; });
523-
queue.call()
524-
queue.dispatch_forever();
516+
queue.dispatch_forever();
525517
}

Telemetry-Main/scripts/dbc_to_cpp.py

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -136,49 +136,50 @@ def generate_encoder_hpp(db: cantools.db.Database, rows_per_batch: int = 80):
136136
out_file.close()
137137
template_file.close()
138138

139-
def generate_can_processor_hpp(db: cantools.db.Database):
140-
out_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.hpp", "w")
141-
template_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.hpp.in", "r")
142-
template = template_file.read()
143-
144-
can_message_ids = []
145-
for msg in db.messages:
146-
can_message_ids.append(" "*4 + f"{msg.name} = {hex(msg.frame_id)},")
147-
148-
template = template.replace("@MESSAGE_IDS@", "\n".join(can_message_ids))
149-
150-
out_file.write(template)
151-
out_file.close()
152-
template_file.close()
153-
154-
def generate_can_processor_cpp(db: cantools.db.Database):
155-
out_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.cpp", "w")
156-
template_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.cpp.in", "r")
157-
template = template_file.read()
158-
159-
can_message_processing = []
160-
for msg in db.messages:
161-
msg_processing_lines = [" "*4 + f"case {msg.name}:"]
162-
for signal in msg.signals:
163-
if signal.is_float:
164-
raise ValueError("can't handle float signals yet!")
165-
# msg_processing_lines.append(" "*8 + f"current_row->{signal.name} = 100;")
166-
raw_buf_type = f"{signal.is_signed and "" or "u"}int{8 * math.ceil(signal.length / 8)}_t"
167-
byte_offset = signal.start / 8
168-
bit_offset_in_byte = signal.start % 8
169-
# underlying_bytes =
170-
last_byte_idx = math.ceil(signal.length / 8)
171-
msg_processing_lines.append(" "*8 + f"{raw_buf_type} raw_{signal.name};");
172-
173-
msg_processing_lines.append("");
174-
msg_processing_lines.append(" "*8 + "break;")
175-
can_message_processing.append("\n".join(msg_processing_lines))
176-
177-
template = template.replace("@CAN_MESSAGE_PROCESSING@", "\n\n".join(can_message_processing))
178-
179-
out_file.write(template)
180-
out_file.close()
181-
template_file.close()
139+
# def generate_can_processor_hpp(db: cantools.db.Database):
140+
# out_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.hpp", "w")
141+
# template_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.hpp.in", "r")
142+
# template = template_file.read()
143+
#
144+
# can_message_ids = []
145+
# for msg in db.messages:
146+
# can_message_ids.append(" "*4 + f"{msg.name} = {hex(msg.frame_id)},")
147+
#
148+
# template = template.replace("@MESSAGE_IDS@", "\n".join(can_message_ids))
149+
#
150+
# out_file.write(template)
151+
# out_file.close()
152+
# template_file.close()
153+
154+
# def generate_can_processor_cpp(db: cantools.db.Database):
155+
# out_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.cpp", "w")
156+
# template_file = open(Path(__file__).parent.parent / "fsdaq" / "can_processor_generated.cpp.in", "r")
157+
# template = template_file.read()
158+
#
159+
# can_message_processing = []
160+
# for msg in db.messages:
161+
# msg_processing_lines = [" "*4 + f"case {msg.name}:"]
162+
# for signal in msg.signals:
163+
# if signal.is_float:
164+
# raise ValueError("can't handle float signals yet!")
165+
# # msg_processing_lines.append(" "*8 + f"current_row->{signal.name} = 100;")
166+
# raw_buf_type = f"{signal.is_signed and "" or "u"}int{8 * math.ceil(signal.length / 8)}_t"
167+
# byte_offset = math.floor(signal.start / 8)
168+
# end_byte_offset = math.floor(signal.start + signal.length / 8)
169+
# bit_offset_in_byte = signal.start % 8
170+
# # underlying_bytes =
171+
# last_byte_idx = math.ceil(signal.length / 8)
172+
# msg_processing_lines.append(" "*8 + f"{raw_buf_type} raw_{signal.name};");
173+
#
174+
# msg_processing_lines.append("");
175+
# msg_processing_lines.append(" "*8 + "break;")
176+
# can_message_processing.append("\n".join(msg_processing_lines))
177+
#
178+
# template = template.replace("@CAN_MESSAGE_PROCESSING@", "\n\n".join(can_message_processing))
179+
#
180+
# out_file.write(template)
181+
# out_file.close()
182+
# template_file.close()
182183

183184

184185

@@ -189,17 +190,9 @@ def generate_can_processor_cpp(db: cantools.db.Database):
189190
db = cantools.db.Database()
190191
db.add_dbc_file("../CANbus.dbc")
191192

192-
193-
# for k, v in signal_to_datatype.items():
194-
# v = str(v).removeprefix("(DataType(")
195-
# v = v.removesuffix("),)")
196-
# print("{:33s} {}".format(k, v))
197-
198-
# n = 50
199-
# generate_nanoarrow_code({k: signal_to_datatype[k] for k in list(signal_to_datatype)[:n]}, 8)
200193
generate_encoder_hpp(db, rows_per_batch=80)
201-
generate_can_processor_hpp(db)
202-
generate_can_processor_cpp(db)
194+
# generate_can_processor_hpp(db)
195+
# generate_can_processor_cpp(db)
203196

204197

205198

0 commit comments

Comments
 (0)