Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
build/
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ foreach(IDX RANGE 1 3 1)
target_link_libraries(Question-${IDX}-out pthread)
add_test(question-${IDX} Question-${IDX}-out)
endforeach()

configure_file(Q1/candump.log ${CMAKE_BINARY_DIR}/candump.log COPYONLY)
40 changes: 40 additions & 0 deletions Q1/Question-1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,43 @@
// Resources:
// https://www.csselectronics.com/pages/can-bus-simple-intro-tutorial
// https://www.csselectronics.com/pages/can-dbc-file-database-intro

int main() {
// Setup files
std::ifstream logFile("candump.log");
std::ofstream outFile("output.txt");

if (!logFile.is_open()) { std::cerr << "Cannot open candump.log\n"; return 1; }
if (!outFile.is_open()) { std::cerr << "Cannot open wheelspeed_rr.txt\n"; return 1; }

// Values from SensorBus.dbc
const std::string TARGET_ID = "705";
const double FACTOR = 0.1;

// Parse lines from candump.log
std::string line;
while (std::getline(logFile, line)) {
// Select ECU_WheelSpeed data only
const size_t hashPos = line.find('#');
if (line.substr(hashPos - 3, 3) != TARGET_ID) continue;

// Extract timestamp
std::string ts = line.substr(0, line.find(')') + 1);

// Extract wheelspeed value
uint64_t raw_data = std::stoull(line.substr(hashPos + 1), nullptr, 16);
uint8_t rr_lsb = (raw_data >> 24) & 0xFF;
uint8_t rr_msb = (raw_data >> 16) & 0xFF;
int16_t signed_value = (static_cast<int16_t>(rr_msb) << 8) | rr_lsb;
double wheelSpeedRR = signed_value * FACTOR;

// Store values
outFile << ts << ": " << wheelSpeedRR << "\n";
}

// Close files
logFile.close();
outFile.close();
std::cout << "Values written to output.txt\n";
return 0;
}