-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathQuestion-1.cc
More file actions
62 lines (53 loc) · 2.41 KB
/
Question-1.cc
File metadata and controls
62 lines (53 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>
// Question 1: This is an extension task that requires you to decode sensor data from a CAN log file.
// CAN (Controller Area Network) is a communication standard used in automotive applications (including Redback cars)
// to allow communication between sensors and controllers.
//
// Your Task: Using the definition in the Sensors.dbc file, extract the "WheelSpeedRR" values
// from the candump.log file. Parse these values correctly and store them in an output.txt file with the following format:
// (<UNIX_TIME>): <DECODED_VALUE>
// eg:
// (1705638753.913408): 1234.5
// (1705638754.915609): 6789.0
// ...
// The above values are not real numbers; they are only there to show the expected data output format.
// You do not need to use any external libraries. Use the resources below to understand how to extract sensor data.
// Hint: Think about manual bit masking and shifting, data types required,
// what formats are used to represent values, etc.
// 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;
}