-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdatastream_can.cpp
More file actions
147 lines (127 loc) · 3.72 KB
/
datastream_can.cpp
File metadata and controls
147 lines (127 loc) · 3.72 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
#include <QTextStream>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QCanBus>
#include <QCanBusFrame>
#include <thread>
#include <mutex>
#include <chrono>
#include <thread>
#include <fstream>
#include "datastream_can.h"
using namespace PJ;
DataStreamCAN::DataStreamCAN() : connect_dialog_{ new ConnectDialog() }
{
connect(connect_dialog_, &QDialog::accepted, this, &DataStreamCAN::connectCanInterface);
}
void DataStreamCAN::connectCanInterface()
{
const ConnectDialog::Settings p = connect_dialog_->settings();
QString errorString;
can_interface_ = QCanBus::instance()->createDevice(p.backendName, p.deviceInterfaceName, &errorString);
if (!can_interface_)
{
qDebug() << tr("Error creating device '%1', reason: '%2'").arg(p.backendName).arg(errorString);
return;
}
if (p.useConfigurationEnabled)
{
for (const ConnectDialog::ConfigurationItem& item : p.configurations)
can_interface_->setConfigurationParameter(item.first, item.second);
}
can_interface_->setConfigurationParameter(QCanBusDevice::CanFdKey, true);
if (!can_interface_->connectDevice())
{
qDebug() << tr("Connection error: %1").arg(can_interface_->errorString());
delete can_interface_;
can_interface_ = nullptr;
}
else
{
std::ifstream dbc_file{ p.canDatabaseLocation.toStdString() };
frame_processor_ = std::make_unique<CanFrameProcessor>(dbc_file, dataMap(), p.protocol);
QVariant bitRate = can_interface_->configurationParameter(QCanBusDevice::BitRateKey);
if (bitRate.isValid())
{
qDebug() << tr("Backend: %1, connected to %2 at %3 kBit/s")
.arg(p.backendName)
.arg(p.deviceInterfaceName)
.arg(bitRate.toInt() / 1000);
}
else
{
qDebug() << tr("Backend: %1, connected to %2").arg(p.backendName).arg(p.deviceInterfaceName);
}
}
}
bool DataStreamCAN::start(QStringList*)
{
if (running_) {
return running_;
}
connect_dialog_->show();
int res = connect_dialog_->exec();
if (res != QDialog::Accepted)
{
return false;
}
thread_ = std::thread([this]() { this->loop(); });
return true;
}
void DataStreamCAN::shutdown()
{
running_ = false;
if (thread_.joinable())
thread_.join();
}
bool DataStreamCAN::isRunning() const
{
return running_;
}
DataStreamCAN::~DataStreamCAN()
{
shutdown();
}
bool DataStreamCAN::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
{
return true;
}
bool DataStreamCAN::xmlLoadState(const QDomElement& parent_element)
{
return true;
}
void DataStreamCAN::pushSingleCycle()
{
std::lock_guard<std::mutex> lock(mutex());
// Since readAllFrames is introduced in Qt5.12, reading using for
auto n_frames = can_interface_->framesAvailable();
for (int i = 0; i < n_frames; i++)
{
auto frame = can_interface_->readFrame();
auto id = frame.frameId();
double timestamp = frame.timeStamp().seconds() + frame.timeStamp().microSeconds() * 1e-6;
//set bit 31 if extended, so that it is found in the dbc
if (id > 0x7FF)
id = id | (1<<31);
frame_processor_->ProcessCanFrame(id, (const uint8_t*)frame.payload().data(), frame.payload().length(), timestamp);
//qDebug() << tr("Processed Single Frame with id:%2 (length: %1)").arg(frame.payload().length()).arg(id);
}
}
void DataStreamCAN::loop()
{
// Block until both are initalized
while (can_interface_ == nullptr || frame_processor_ == nullptr)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
running_ = true;
while (running_)
{
auto prev = std::chrono::high_resolution_clock::now();
pushSingleCycle();
emit dataReceived();
std::this_thread::sleep_until(prev + std::chrono::milliseconds(20));
}
}