/
pushn8simulationdevice.cpp
executable file
·252 lines (206 loc) · 8.19 KB
/
pushn8simulationdevice.cpp
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
/*
* Copyright (c) 2011 Nokia Corporation
*
* This file is part of the Push Snowboarding Project, More info at:
* www.pushsnowboading.com
*
* Author: Clovis Scotti <scotti@ieee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "pushn8simulationdevice.h"
PushN8SimulationDevice::PushN8SimulationDevice()
{
QString simulationFile = QFileDialog::getOpenFileName(0, "Select where to read the log from", FSC_RUNS_FOLDERS_ROOT, "Log Files (RawLog.xml)");
if(simulationFile == "") {
simulationFile = "E:/sample_input.xml";
}
qDebug() << "Simulation input stream is: " << simulationFile;
data_input = new QFile(simulationFile, this);
data_input->open(QFile::ReadOnly | QFile::Text);
if(data_input)
xml.setDevice(data_input);
if(xml.readNextStartElement()) {
if(xml.name() == "N8SensorsLog") {
qDebug() << "Format is ok";
} else {
qDebug() << "Format is not ok";
}
} else {
qDebug() << "Could not read first start Element";
}
digitalAcc = false;
timerId = 0;
timerPeriod = 10;
}
PushN8SimulationDevice::~PushN8SimulationDevice()
{
data_input->close();
delete data_input;
}
QString PushN8SimulationDevice::get_description()
{
return QString("Devices Simulation Stream");
}
bool PushN8SimulationDevice::is_online()
{
return (data_input != NULL);
}
void PushN8SimulationDevice::timerEvent(QTimerEvent *)
{
// while(!(xml.isEndElement() && xml.name() == "N8SensorsLog")) {
bool reachedEnd = false;
while(xml.readNextStartElement())
{
NPushLogTick * newTick = 0;
if(xml.name() == "acc_data")
{
newTick = readAccTick();
} else if(xml.name() == "mag_data")
{
newTick = readMagTick();
} else if(xml.name() == "gps_data")
{
newTick = readGpsTick();
} else if(xml.name() == "imu_data")
{
newTick = readImuTick();
//Imu tick has inner elements, when it returns we can quit
reachedEnd = true;
} else if(xml.name() == "gsr_data")
{
newTick = readGsrTick();
} else if(xml.name() == "heart_data")
{
newTick = readHeartTick();
} else if(xml.name() == "foot_data")
{
newTick = readFootTick();
} else {
// qDebug() << "Unrecognized tag " << xml.name();
xml.skipCurrentElement();
}
if(newTick != 0) {
if(receivers(SIGNAL(reading_ready(NPushLogTick*))) > 0)
{
emit reading_ready(newTick);
} else {
delete newTick;
}
}
if(reachedEnd)
break;
}
if(xml.isEndElement() && xml.name() == "N8SensorsLog") {
qDebug() << "Simulation Ended!!";
this->stop_readings();
}
// }
}
NPushAccTick * PushN8SimulationDevice::readAccTick()
{
QAccelerometerReading fake;
fake.setX(xml.attributes().value("x").toString().toFloat());
fake.setY(xml.attributes().value("y").toString().toFloat());
fake.setZ(xml.attributes().value("z").toString().toFloat());
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
NPushAccTick * tick = new NPushAccTick(&fake, tstamp);
return tick;
}
NPushFootTick * PushN8SimulationDevice::readFootTick()
{
QChar side = xml.attributes().value("side").toString().at(0);
// qDebug() << "CAUTION!! Flipping to correct runs from yesterday (march 8th)";
//CAUTION!! Flipping to correct runs from yesterday (march 8th).
int toes = xml.attributes().value("toes").toString().toInt();
int heel = xml.attributes().value("heel").toString().toInt();
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
NPushFootTick * tick = new NPushFootTick(side.toAscii(), toes, heel, tstamp);
return tick;
}
NPushGpsTick * PushN8SimulationDevice::readGpsTick()
{
// <gps_data tstamp="1291460032" longitude="11.2806" ground_speed="5.85" latitude="47.1801" altitude="2360" />
QGeoPositionInfo fake;
QDateTime tstamp;
QGeoCoordinate fakeCoord;
fakeCoord.setAltitude(xml.attributes().value("altitude").toString().toDouble());
fakeCoord.setLatitude(xml.attributes().value("latitude").toString().toDouble());
fakeCoord.setLongitude(xml.attributes().value("longitude").toString().toDouble());
tstamp = QDateTime::fromTime_t(floor(xml.attributes().value("tstamp").toString().toDouble()));//-3600.0));
fake.setTimestamp(tstamp);
fake.setCoordinate(fakeCoord);
fake.setAttribute(QGeoPositionInfo::GroundSpeed, xml.attributes().value("ground_speed").toString().toFloat());
NPushGpsTick * tick = new NPushGpsTick(fake);
return tick;
}
NPushGSRTick * PushN8SimulationDevice::readGsrTick()
{
int reading = xml.attributes().value("reading").toString().toInt();
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
NPushGSRTick * tick = new NPushGSRTick(reading, tstamp);
return tick;
}
NPushHeartTick * PushN8SimulationDevice::readHeartTick()
{
int last32 = xml.attributes().value("last32").toString().toInt();
int last16 = xml.attributes().value("last16").toString().toInt();
int lastBeat = xml.attributes().value("lastBeat").toString().toInt();
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
NPushHeartTick * tick = new NPushHeartTick(last32, last16, lastBeat, tstamp);
return tick;
}
NPushIMUTick * PushN8SimulationDevice::readImuTick()
{
QStringList values;
values.push_back("IMU");
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
while(xml.readNextStartElement()) {
if(xml.name() == "acc") {
values.append(xml.readElementText().split(","));
} else if(xml.name() == "gyro") {
values.append(xml.readElementText().split(","));
} else if(xml.name() == "mag") {
values.append(xml.readElementText().split(","));
} else {
xml.skipCurrentElement();
}
}
values.push_back("END");
if(!digitalAcc) {
if(values[4].toInt() < 0 ||
values[5].toInt() < 0 ||
values[6].toInt() < 0) {
digitalAcc = true;
}
}
NPushIMUTick * tick = new NPushIMUTick(values, tstamp);
tick->digitalAcc = digitalAcc;
return tick;
}
NPushMagTick * PushN8SimulationDevice::readMagTick()
{
QMagnetometerReading fake;
fake.setX(xml.attributes().value("x").toString().toFloat()*0.001);
fake.setY(xml.attributes().value("y").toString().toFloat()*0.001);
fake.setZ(xml.attributes().value("z").toString().toFloat()*0.001);
quint64 tstamp = (quint64)((double)xml.attributes().value("tstamp").toString().toDouble()*1000.0);
NPushMagTick * tick = new NPushMagTick(&fake, tstamp);
return tick;
}