Repository navigation
Expand file tree
/
Copy pathcan_parser.cpp
More file actions
167 lines (146 loc) · 6.51 KB
/
Copy pathcan_parser.cpp
File metadata and controls
167 lines (146 loc) · 6.51 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// File: can_parser.cpp
// Author: Blake Hill
// Usage: Parses CAN messages through the CAN dbc file, create a CanParser object and
// then pass in CAN_message_t objects for their signals.
#include "can_parser.hpp"
#include <fstream>
#include <iostream>
#include <stdexcept>
using msgMap = std::unordered_map<uint32_t, const dbcppp::IMessage *>;
using MUX = dbcppp::ISignal::EMultiplexer;
// MARK: Constructor
CanParser::CanParser() {
dbc = load_dbc();
msgMap = loadMessages();
}
CanParser::CanParser(const std::filesystem::path dbc_file_path) : dbc_file_path(dbc_file_path) {
dbc = load_dbc();
msgMap = loadMessages();
}
std::unique_ptr<dbcppp::INetwork> CanParser::load_dbc() {
std::ifstream dbc_file(dbc_file_path);
if (!dbc_file.is_open()) {
throw std::runtime_error("Failed to open DBC file:" + dbc_file_path.string());
}
std::unique_ptr<dbcppp::INetwork> dbc = dbcppp::INetwork::LoadDBCFromIs(dbc_file);
if (!dbc) {
throw std::runtime_error("Failed to load DBC file:" + dbc_file_path.string());
}
return dbc;
}
messageMap CanParser::loadMessages() {
std::unordered_map<uint32_t, const dbcppp::IMessage *> messages;
for (const dbcppp::IMessage &msg : dbc->Messages()) {
messages.insert(std::make_pair(static_cast<uint32_t>(msg.Id()), &msg));
}
return messages;
}
messageMap CanParser::getMessages() { return msgMap; }
// MARK: Parse_frame
std::optional<SignalMap> CanParser::parse_frame(const CAN_message_t &frame) {
auto iter = msgMap.find(frame.id);
if (iter != msgMap.end()) {
const dbcppp::IMessage *message = iter->second;
if (frame.len != message->MessageSize()) {
std::cerr << "Err: Message " << message->Name() << " needs length "
<< message->MessageSize() << ", frame has length " << frame.len << "\n";
}
// Check if there is a MUX'ed signal selector here, take the first one
const dbcppp::ISignal *mux_selector = nullptr;
for (const dbcppp::ISignal &sig : message->Signals()) {
if (sig.MultiplexerIndicator() == MUX::MuxSwitch) {
mux_selector = &sig;
break;
}
}
const auto mux_val = mux_selector ? mux_selector->Decode(frame.buf) : 0;
// Loop through signals in message and decode them into a SignalMap obj to return
std::unordered_map<std::string, double> sigMap;
for (const dbcppp::ISignal &sig : message->Signals()) {
// Check if sig is a MUX'ed value, if selector is null or val doesn't match, skip sig
if (sig.MultiplexerIndicator() == MUX::MuxValue) {
if (!mux_selector || sig.MultiplexerSwitchValue() != mux_val) {
continue;
}
}
const auto raw_sig = sig.Decode(frame.buf);
sigMap[sig.Name()] = sig.RawToPhys(raw_sig);
}
return SignalMap(sigMap);
} else {
std::cerr << "ID " << frame.id << " not found\n";
return std::nullopt;
}
}
// MARK: Create_frame
std::optional<CAN_message_t> CanParser::create_frame(
const uint32_t id, const std::unordered_map<std::string, double> &input_signals
) {
auto iter = msgMap.find(id);
if (iter != msgMap.end()) {
const dbcppp::IMessage *msg = iter->second;
CAN_message_t can_msg = {};
if (static_cast<size_t>(msg->MessageSize()) > sizeof(can_msg.buf)) {
std::cerr << "Message size " << msg->MessageSize() << " too large\n";
return std::nullopt;
}
// For each signal, put that data into can_msg.
// If signal not found in input, frame isn't valid.
auto msgSignals = msg->Signals();
for (const auto &msgSignal : msgSignals) {
auto in_sig_iter = input_signals.find(msgSignal.Name());
if (in_sig_iter != input_signals.end()) { // If signal is found in input_signals
double in_val = in_sig_iter->second;
// If in_val is within signal min/max, make msgBuf with it
if ((in_val >= msgSignal.Minimum()) && (in_val <= msgSignal.Maximum())) {
uint64_t raw = msgSignal.PhysToRaw(in_val);
msgSignal.Encode(raw, can_msg.buf);
} else {
printf(
"Signal: %s, val: %f not in bounds; Min: %f, Max: %f\n",
msgSignal.Name().c_str(), in_val, msgSignal.Minimum(), msgSignal.Maximum()
);
return std::nullopt;
}
} else {
std::cerr << "Signal " << msgSignal.Name() << " not in input_signals\n";
return std::nullopt;
}
}
// If signals are fully complete, fill out the rest of the msg struct and return it.
can_msg.id = id;
can_msg.len = static_cast<uint8_t>(msg->MessageSize());
can_msg.flags.extended = (msg->Id() > 0x7FF);
can_msg.flags.remote = false; // false = 0
return can_msg;
} else {
std::cerr << "ID " << id << " not found\n";
return std::nullopt;
}
}
void CanParser::print_network_info() {
std::cout << "DBC Version: " << dbc->Version() << "\n";
std::cout << "Number of Nodes: " << dbc->Nodes_Size() << "\n";
std::cout << "Number of Messages: " << dbc->Messages_Size() << "\n";
std::cout << "Number of Value Tables: " << dbc->ValueTables_Size() << "\n";
std::cout << "Number of Environment Variables: " << dbc->EnvironmentVariables_Size() << "\n";
std::cout << "Number of Attribute Definitions: " << dbc->AttributeDefinitions_Size() << "\n";
std::cout << "Number of Attribute Defaults: " << dbc->AttributeDefaults_Size() << "\n";
std::cout << "Number of Attribute Values: " << dbc->AttributeValues_Size() << "\n";
std::cout << "Comment: " << dbc->Comment() << "\n";
}
void CanParser::print_messages() {
try {
for (const auto &msg : dbc->Messages()) {
std::cout << msg.Name() << " | ID: " << msg.Id() << " | Bytes: " << msg.MessageSize()
<< '\n';
for (const auto &sig : msg.Signals()) {
std::cout << " " << sig.Name() << " | Start Bit: " << sig.StartBit()
<< " | Length: " << sig.BitSize() << " | Factor: " << sig.Factor()
<< " | Offset: " << sig.Offset() << " | Unit: " << sig.Unit() << '\n';
}
}
} catch (const std::exception &err) {
std::cerr << "Error: " << err.what() << '\n';
}
}