-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs8recordermodel.cpp
More file actions
350 lines (309 loc) · 8.93 KB
/
cs8recordermodel.cpp
File metadata and controls
350 lines (309 loc) · 8.93 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "cs8recordermodel.h"
#include <QFile>
#include <QDebug>
#include <QDataStream>
const double PI =3.141592653589793238463;
cs8RecorderModel::cs8RecorderModel(QObject *parent) :
QAbstractTableModel(parent),mColumnCount(0)
{
mColumnHeaders=new QStringListModel(this);
}
cs8RecorderModel::~cs8RecorderModel()
{
}
int cs8RecorderModel::rowCount(const QModelIndex &parent) const
{
return mRowCount;
}
int cs8RecorderModel::columnCount(const QModelIndex &parent) const
{
return mColumnCount;
}
QVariant cs8RecorderModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
double value=mData.at(index.column()+(index.row()*mColumnCount));
if (mColumnHeaders->data(mColumnHeaders->index(index.column(), 0),Qt::DisplayRole).toString()
.contains("Pos["))
value=value*180/PI;
return value;
break;
}
return QVariant();
}
QVariant cs8RecorderModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if ((section<mColumnCount) && (role==Qt::DisplayRole))
{
if (orientation==Qt::Horizontal)
return mColumnHeaders->data(mColumnHeaders->index(section,0), Qt::DisplayRole).toString();
//QString("%1[%2]")
//.arg(mColumnHeaders->data(mColumnHeaders->index(section,0), Qt::DisplayRole).toString())
//.arg((int)mJointNumbers.at(section));
}
return QVariant();
}
bool cs8RecorderModel::loadRecording(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return false;
QByteArray data=file.readAll();
if (!parseHeader(data))
return false;
if (!parseColumnHeaders(data))
return false;
if (!parseData(data))
return false;
return true;
}
QString cs8RecorderModel::configurationVersion() const
{
return mConfigurationVersion;
}
void cs8RecorderModel::setConfigurationVersion(const QString &configurationVersion)
{
mConfigurationVersion = configurationVersion;
}
QString cs8RecorderModel::val3Version() const
{
return mVal3Version;
}
void cs8RecorderModel::setVal3Version(const QString &val3Version)
{
mVal3Version = val3Version;
}
QString cs8RecorderModel::robotType() const
{
return mRobotType;
}
void cs8RecorderModel::setRobotType(const QString &robotType)
{
mRobotType = robotType;
}
QString cs8RecorderModel::serialNumber() const
{
return mSerialNumber;
}
void cs8RecorderModel::setSerialNumber(const QString &serialNumber)
{
mSerialNumber = serialNumber;
}
QStringListModel* cs8RecorderModel::columnHeaders() const
{
return mColumnHeaders;
}
QVector<double> cs8RecorderModel::values(const int column)
{
QVector<double> vec;
for(int row=0; row<mRowCount; row++)
vec.append(data(index(row,column)).toDouble());
return vec;
}
QVector<QPointF> cs8RecorderModel::valuePairs(const int col1, const int col2, int start, int stop) const
{
QVector<QPointF> vec;
for(int row=start; row<stop; row++)
vec.append(QPointF(data(index(row,col1)).toDouble(),data(index(row,col2)).toDouble()));
return vec;
}
QVector<QPointF> cs8RecorderModel::sampleSeries(const int col1) const
{
QVector<QPointF> vec;
for(int row=0; row<mRowCount; row++)
vec.append(QPointF(row,data(index(row,col1)).toDouble()));
return vec;
}
bool cs8RecorderModel::exportToCSV(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly))
return false;
QString line;
QTextStream out(&file);
// write meta data
out << mSerialNumber << "," << mConfigurationVersion << "," << mVal3Version << "," << mRobotType << "\n";
// write headers
out << mColumnHeaders->stringList().join(",") << "\n";
for (int r=0; r<rowCount(); r++)
{
for (int c=0; c<columnCount(); c++)
{
out << data(index(r,c)).toDouble() << (c<columnCount()?",":"");
}
out << "\n";
}
file.close();
return true;
}
double cs8RecorderModel::calcMinValue(const int column)
{
double min=9999999;
for(int row=0; row<mRowCount; row++)
min=qMin(min,data(index(row,column)).toDouble());
return min;
}
double cs8RecorderModel::calcMaxValue(const int column)
{
double max=-9999999;
for(int row=0; row<mRowCount; row++)
max=qMax(max,data(index(row,column)).toDouble());
return max;
}
void cs8RecorderModel::removeLeadingZero(QByteArray & ba)
{
while (ba.length()>0 && ba.at(0)==0x00)
ba.remove(0,1);
}
int cs8RecorderModel::nextEndTag(const QByteArray &ba)
{
for (int i=0; i<ba.length()-1; i++)
{
qDebug() << static_cast<unsigned char>(ba[i]) << " " << ba[i];
if ((ba[i]==0x0 && ba[i+1]!=0x0) || (static_cast<unsigned char>(ba[i])==0xff && static_cast<unsigned char>(ba[i+1])!=0xff))
return i;
}
return -1;
}
bool cs8RecorderModel::parseHeader(QByteArray &data)
{
int pos;
// check file is a recording file
QByteArray ba;
ba.resize(4);
ba.clear();
ba[0]= 0x03;
ba[1]= 0x00;
ba[2]= 0x00;
ba[3]= 0x00;
qDebug() << "valid recorder file " << (data.indexOf(ba)>=0?"Yes":"No");
if (data.indexOf(ba)!=0)
return false;
// remove prefix
data.remove(0,8);
removeLeadingZero(data);
//
// extract robot type
// end of robot type field
pos=nextEndTag(data);
if (pos==-1)
return false;
ba=data.mid(0,pos);
removeLeadingZero(ba);
mRobotType=ba;
data.remove(0,pos+2);
removeLeadingZero(data);
// val3 version
// end of val3 version field
pos=nextEndTag(data);
if (pos==-1)
return false;
ba=data.mid(0,pos);
removeLeadingZero(ba);
mVal3Version=ba;
data.remove(0,pos+2);
removeLeadingZero(data);
// configuration version
pos=nextEndTag(data);
if (pos==-1)
return false;
ba=data.mid(0,pos);
removeLeadingZero(ba);
mConfigurationVersion=ba;
data.remove(0,pos+2);
removeLeadingZero(data);
// serial number
pos=nextEndTag(data);
if (pos==-1)
return false;
ba=data.mid(0,pos);
removeLeadingZero(ba);
mSerialNumber=ba;
data.remove(0,pos+2);
// extract control data
pos=data.indexOf(0x05);
pos=((data.indexOf(0x09)<pos) && (data.indexOf(0x09)!=-1))?data.indexOf(0x09):pos;
if (pos==-1)
return false;
ba=data.mid(0,pos);
mControlData=ba;
data.remove(0,pos+1);
mColumnCount=mControlData.at(2);
return true;
}
bool cs8RecorderModel::parseColumnHeaders(QByteArray &data)
{
QByteArray ba;
int pos;
QStringList columnHeaders;
mJointNumbers.resize(mColumnCount);
for (int i=0; i<mColumnCount; i++)
{
removeLeadingZero(data);
pos=nextEndTag(data);
if (pos==-1)
return false;
ba=data.mid(0,pos);
while (ba.at(0)==0x00)
ba.remove(0,1);
while ((ba.at(ba.length()-1)==0x00) || (static_cast<unsigned char>(ba.at(ba.length()-1))==0xff))
ba.chop(1);
bool ok;
int jointNumber=QString(ba).right(1).toInt(&ok);
if (!ok)
// joint number is text data
jointNumber=ba.at(ba.length()-1)<6?ba.at(ba.length()-1):0;
else
// joint number is encoded as binary data after variable name
ba.chop(1);
//
if (ba.at(ba.length()-1)<6 && ba.at(ba.length()-1)>0)
ba.chop(1);
mJointNumbers[i]=jointNumber;
QString header;
header=ba;
header=QString("%1[%2]").arg(header).arg((int)mJointNumbers.at(i));
columnHeaders << header;
data.remove(0,i<mColumnCount-1?pos+2:pos+1);
}
mColumnHeaders->setStringList(columnHeaders);
return true;
}
bool cs8RecorderModel::parseData(QByteArray &data_)
{
//mColumnCount=12;
mData.clear();
QDataStream st(data_);
qDebug() << data_.size();
double *xy = reinterpret_cast<double*>(data_.data());
int i = 0, n = (data_.size()) / 2 / sizeof(double);
int row=0;
while (i < n)
{
for (int col=0; col<mColumnCount; col++)
{
double val=xy[i++];
//qDebug() << val;
mData.append(val);
}
row++;
}
mRowCount=row;
beginResetModel();
endResetModel();
for (int r=0; r<rowCount(); r++)
{
QString rowText;
for (int c=0; c<columnCount(); c++)
{
//qDebug() << data(index(r,c)).toLongLong();
rowText+=QString("%1,").arg(data(index(r,c)).toDouble(),6);
}
qDebug() << rowText;
}
return true;
}