From fa10129bfb27a426c51ef8a1214b65ddc076dded Mon Sep 17 00:00:00 2001 From: Andy Zeng Date: Wed, 4 Mar 2026 12:07:50 +1100 Subject: [PATCH 1/2] add skeleton CAN parser, update cmake build, and gitignore --- .gitignore | 2 ++ CMakeLists.txt | 2 ++ Q1/Question-1.cc | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..608b1d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a74efb..a62b906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/Q1/Question-1.cc b/Q1/Question-1.cc index 9ad4a7f..bccca9d 100644 --- a/Q1/Question-1.cc +++ b/Q1/Question-1.cc @@ -20,3 +20,31 @@ // 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 int TARGET_ID = 0x705; + const double FACTOR = 0.1; + const double OFFSET = 0.0; + + // Parse lines from candump.log + std::string line; + while (std::getline(logFile, line)) { + // Extract timestamp + const std::string ts = line.substr(0, 19); + outFile << ts << "\n"; + } + + // Close files + logFile.close(); + outFile.close(); + std::cout << "Values written to output.txt\n"; + return 0; +} From d5c03e131aab8bb64bf057c904ee04f26d6b2a9a Mon Sep 17 00:00:00 2001 From: Andy Zeng Date: Wed, 4 Mar 2026 13:16:30 +1100 Subject: [PATCH 2/2] final Q1 --- Q1/Question-1.cc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Q1/Question-1.cc b/Q1/Question-1.cc index bccca9d..4d895ac 100644 --- a/Q1/Question-1.cc +++ b/Q1/Question-1.cc @@ -30,16 +30,28 @@ int main() { if (!outFile.is_open()) { std::cerr << "Cannot open wheelspeed_rr.txt\n"; return 1; } // Values from SensorBus.dbc - const int TARGET_ID = 0x705; - const double FACTOR = 0.1; - const double OFFSET = 0.0; + 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 - const std::string ts = line.substr(0, 19); - outFile << ts << "\n"; + 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(rr_msb) << 8) | rr_lsb; + double wheelSpeedRR = signed_value * FACTOR; + + // Store values + outFile << ts << ": " << wheelSpeedRR << "\n"; } // Close files