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..4d895ac 100644 --- a/Q1/Question-1.cc +++ b/Q1/Question-1.cc @@ -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(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; +}