-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrajectory.cpp
More file actions
165 lines (141 loc) · 3.86 KB
/
trajectory.cpp
File metadata and controls
165 lines (141 loc) · 3.86 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
#include "trajectory.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
Trajectory::Trajectory(QObject *parent) :
QObject(parent)
{
length = 0;
state = UNKNOWN;
indicators = QStringList() << "times" << "usignal" << "coords";
indicators << "set_id=" << "motor_id=" << "dest=" << "prec=";
times = NULL;
uSignal = NULL;
coordinates = NULL;
}
Trajectory::~Trajectory()
{
CleanArrays();
}
uint16_t Trajectory::Convert2_8to16(char msb, char lsb)
{
uint16_t mspart = ((uint8_t)msb)<<8;
uint16_t lspart = (uint8_t)lsb;
return mspart + lspart;
}
void Trajectory::AddData(const QString indicator, QByteArray newData)
{
if (indicator == "set_id=") {
uint8_t setIDn = (uint8_t)newData.at(0);
switch (setIDn) {
case 0:
setID = "Entrance";
break;
case 1:
setID = "Exit1";
break;
case 2:
setID = "Exit2";
break;
default:
setID = "Unknown";
break;
}
return;
}
if (indicator == "motor_id=") {
motorID = (uint8_t)newData.at(0);
return;
}
if (indicator == "dest=") {
destination = Convert2_8to16(newData.at(0), newData.at(1));
return;
}
// check for trajectory arrays
int len2 = newData.size()/2;
// we expect the new Data of even size because every trajectory array element
// has a size of 2 bytes
if (newData.size() != len2*2) {
qDebug() <<"Wrong data: odd length of data array.";
return;
}
// if first data array received
if (length == 0) {
length = len2;
}
// if length of received data is not equal to length of other data arrays
if (length != len2) {
qDebug() <<"Wrong data: different lengths of data arrays.";
return;
}
uint16_t *array;
if (indicator == "times") {
state |= TIMES_RECEIVED;
times = new uint16_t[length];
array = times;
} else if (indicator == "usignal") {
state |= USIGNAL_RECEIVED;
uSignal = new uint16_t[length];
array = uSignal;
} else if (indicator == "coords") {
state |= COORDINATES_RECEIVED;
coordinates = new uint16_t[length];
array = coordinates;
} else {
qDebug() <<"Unknown data recieved.";
return;
}
SetArray(array, &newData);
}
void Trajectory::SetArray(uint16_t* array, QByteArray* newData)
{
for(int i=0; i<length; i++) {
array[i] = Convert2_8to16(newData->at(2*i), newData->at(2*i+1));
}
}
void Trajectory::CleanArrays()
{
state = UNKNOWN;
length = 0;
if (times) delete[] times;
if (uSignal) delete[] uSignal;
if (coordinates) delete[] coordinates;
}
int Trajectory::AllDataReceived()
{
return ((state & TIMES_RECEIVED) &&
(state & USIGNAL_RECEIVED) &&
(state & COORDINATES_RECEIVED));
}
void Trajectory::WriteToFile()
{
if (AllDataReceived()) {
QString filename = setID
+ QString::number(motorID)
+ "_U="
+ QString::number(uSignal[0])
+ "_dest="
+ QString::number(destination)
+ ".yaml";
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream ya(&file);
ya << "t: [" << times[0];
for(int i=1; i<length; i++) {
ya << ", " << times[i];
}
ya << "]" << endl;
ya << "u: [" << uSignal[0];
for(int i=1; i<length; i++) {
ya << ", " << uSignal[i];
}
ya << "]" << endl;
ya << "x: [" << coordinates[0];
for(int i=1; i<length; i++) {
ya << ", " << coordinates[i];
}
ya << "]" << endl;
file.close();
CleanArrays();
}
}