-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.cpp
More file actions
84 lines (65 loc) · 1.95 KB
/
protocol.cpp
File metadata and controls
84 lines (65 loc) · 1.95 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
#include "protocol.h"
Protocol::Protocol(QObject* parent)
: QObject(parent), timeStep(1) {
generate({});
}
void Protocol::setDt(double dt) {
timeStep = dt;
generate(segments);
}
double Protocol::dt() const {
// interval in seconds, default in constructor is 0.5 seconds
return timeStep;
}
const QVector<double>& Protocol::xvals() const {
return xValues;
}
void Protocol::setXvals(const QVector<double>& x) {
xValues = x;
}
const QVector<double>& Protocol::yvals() const {
return yValues;
}
void Protocol::setYvals(const QVector<double>& y) {
yValues = y;
}
const QVector<QVector<double>> Protocol::shareSegments()
{
return segments;
}
void Protocol::generate(const QVector<QVector<double>>& segs) {
if (!segs.isEmpty()) {
segments = segs;
QVector<double> xtot;
QVector<double> ytot;
double totalTime = 0.0;
for (const auto& seg : segs) {
if (seg.size() != 3) continue; // Expecting [duration, start, end]
double duration = seg[0];
double start = seg[1];
double end = seg[2];
QVector<double> x = { totalTime, totalTime + duration };
QVector<double> y = { start, end };
int steps = static_cast<int>((duration * 60.0) / timeStep);
QVector<double> xExpanded(steps + 1);
for (int i = 0; i <= steps; ++i) {
xExpanded[i] = x[0] + i * (x[1] - x[0]) / steps;
}
QVector<double> yExpanded(steps + 1);
for (int i = 0; i <= steps; ++i) {
double t = xExpanded[i];
yExpanded[i] = start + ((t - x[0]) / (x[1] - x[0])) * (end - start);
}
xtot.append(xExpanded);
ytot.append(yExpanded);
totalTime += duration;
}
xValues = xtot;
yValues = ytot;
}
}
void Protocol::clear() {
xValues.clear();
yValues.clear();
segments.clear();
}