-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathlogger.cpp
314 lines (255 loc) · 8.75 KB
/
logger.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
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
/*
Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
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/>.
*/
#include "logger.h"
#include <QCoreApplication>
#include <QDebug>
Logger::Logger(QObject *parent) :
QObject(parent)
{
mPort = new SerialPort(this);
mPacketInterface = new PacketInterface(this);
mValueFile = new QFile("Data/BLDC_Values");
mPrintFile = new QFile("Data/BLDC_Print");
mValueFile->open(QIODevice::WriteOnly | QIODevice::Text);
mPrintFile->open(QIODevice::WriteOnly | QIODevice::Text);
mValueStream = new QTextStream(mValueFile);
mPrintStream = new QTextStream(mPrintFile);
mPort->openPort("/dev/rfcomm0");
// Video
mVidW = 1280;
mVidH = 720;
mVidFps = 25.0;
mFAudioSamp = 44100;
mFrameGrabber = new FrameGrabber(mVidW, mVidH, mVidFps, 0, this);
mFrameGrabber->start(QThread::InheritPriority);
mPlotter = new FramePlotter(this);
mPlotter->start(QThread::InheritPriority);
mCoder = new VideoCoder(mVidW, mVidH, mVidFps, "Data/v_video.avi", this);
mCoder->start(QThread::InheritPriority);
// Audio recording
mTimer = 0;
mAudio = 0;
if (QAudioDeviceInfo::availableDevices(QAudio::AudioInput).size() > 0) {
mAudioFile.setFileName("Data/v_audio.raw");
mAudioFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
QAudioFormat format;
// Set up the desired format, for example:
format.setSampleRate(mFAudioSamp);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format)) {
qWarning() << "Default format not supported, trying to use the nearest.";
format = info.nearestFormat(format);
}
mAudio = new QAudioInput(format, this);
mAudio->setNotifyInterval(1000 / mVidFps);
mAudio->start(&mAudioFile);
} else {
mTimer = new QTimer(this);
mTimer->setInterval(1000 / mVidFps);
mTimer->start();
}
mConsoleReader = new ConsoleReader(this);
connect(mConsoleReader, SIGNAL(textReceived(QString)),
this, SLOT(consoleLineReceived(QString)));
connect(mPort, SIGNAL(serial_data_available()),
this, SLOT(serialDataAvailable()));
if (mTimer != 0) {
connect(mTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}
if (mAudio != 0) {
connect(mAudio, SIGNAL(notify()),
this, SLOT(audioNotify()));
// Lower the volume to avoid clipping. This seems to be passed to
// pulseaudio.
mAudio->setVolume(0.1);
}
connect(mPacketInterface, SIGNAL(dataToSend(QByteArray&)),
this, SLOT(packetDataToSend(QByteArray&)));
connect(mPacketInterface, SIGNAL(valuesReceived(MC_VALUES)),
this, SLOT(mcValuesReceived(MC_VALUES)));
connect(mPacketInterface, SIGNAL(printReceived(QString)),
this, SLOT(printReceived(QString)));
connect(mPacketInterface, SIGNAL(samplesReceived(QByteArray)),
this, SLOT(samplesReceived(QByteArray)));
connect(mPacketInterface, SIGNAL(rotorPosReceived(double)),
this, SLOT(rotorPosReceived(double)));
connect(mPacketInterface, SIGNAL(experimentSamplesReceived(QVector<double>)),
this, SLOT(experimentSamplesReceived(QVector<double>)));
connect(mPlotter, SIGNAL(frameReady(QImage)),
mCoder, SLOT(setNextFrame(QImage)));
}
Logger::~Logger()
{
mValueFile->close();
mPrintFile->close();
mFrameGrabber->stopAndWait();
mPlotter->stopAndWait();
mCoder->stopAndWait();
if (mAudio != 0) {
mAudioFile.close();
delete mAudio;
rawToWav("Data/v_audio.wav", "Data/v_audio.raw", mFAudioSamp);
}
delete mValueStream;
delete mPrintStream;
delete mValueFile;
delete mPrintFile;
}
void Logger::consoleLineReceived(QString line)
{
if (line == "q" || line == "Q") {
qDebug() << "Saving files...";
if (mAudio != 0) {
mAudio->stop();
}
QCoreApplication::exit(0);
} else if (line == "1") {
mPacketInterface->setCurrent(20);
} else if (line == "2") {
mPacketInterface->setCurrent(-20);
} else if (line == "3") {
mPacketInterface->setDutyCycle(0.1);
} else if (line == "4") {
mPacketInterface->setDutyCycle(-0.1);
} else if (line == "5") {
mPacketInterface->setRpm(2000);
} else if (line == "6") {
mPacketInterface->setRpm(-2000);
} else {
qDebug() << "Unknown command:" << line;
qDebug() << "Stopping motor";
mPacketInterface->setCurrent(0);
}
}
void Logger::serialDataAvailable()
{
while (mPort->bytesAvailable() > 0) {
QByteArray data = mPort->readAll();
mPacketInterface->processData(data);
}
}
void Logger::timerSlot()
{
mPacketInterface->getValues();
}
void Logger::packetDataToSend(QByteArray &data)
{
if (mPort->isOpen()) {
mPort->writeData(data.data(), data.size());
}
}
void Logger::mcValuesReceived(MC_VALUES values)
{
QString str;
str.sprintf("%.1f,%.2f,%.1f,%.2f,%.2f,%d\n",
values.v_in,
values.duty_now,
values.rpm,
values.current_in,
values.current_motor,
values.fault_code);
*mValueStream << str;
mLastMcVal = values;
}
void Logger::printReceived(QString str)
{
*mPrintStream << str;
}
void Logger::samplesReceived(QByteArray data)
{
(void)data;
}
void Logger::rotorPosReceived(double pos)
{
(void)pos;
}
void Logger::experimentSamplesReceived(QVector<double> samples)
{
(void)samples;
}
void Logger::audioNotify()
{
mPacketInterface->sendTerminalCmd("fault");
mPacketInterface->getValues();
if (mFrameGrabber->isOpened()) {
FramePlotter::FrameData data;
data.img = mFrameGrabber->getLatestFrame();
data.values = mLastMcVal;
mPlotter->setNextValues(data);
} else {
qDebug() << "mFrameGrabber not open";
}
}
void Logger::write_little_endian(unsigned int word, int num_bytes, QFile &wav_file)
{
char buf;
while(num_bytes > 0) {
buf = word & 0xff;
wav_file.write(&buf, 1);
num_bytes--;
word >>= 8;
}
}
// Found at: http://karplus4arduino.wordpress.com/2011/10/08/making-wav-files-from-c-programs/
void Logger::rawToWav(const char *out_file, const char *in_file, int s_rate)
{
QFile wav_file;
wav_file.setFileName(out_file);
QFile raw_file;
raw_file.setFileName(in_file);
unsigned int sample_rate;
unsigned int num_channels;
unsigned int bytes_per_sample;
unsigned int byte_rate;
num_channels = 1; /* monoaural */
bytes_per_sample = 1;
if (s_rate <= 0) {
sample_rate = 44100;
} else {
sample_rate = (unsigned int)s_rate;
}
byte_rate = sample_rate * num_channels * bytes_per_sample;
wav_file.open(QIODevice::WriteOnly | QIODevice::Truncate);
raw_file.open(QIODevice::ReadOnly);
int num_samples = raw_file.size() / 2;
/* write RIFF header */
wav_file.write("RIFF", 4);
write_little_endian(36 + bytes_per_sample * num_samples * num_channels, 4, wav_file);
wav_file.write("WAVE", 4);
/* write fmt subchunk */
wav_file.write("fmt ", 4);
write_little_endian(16, 4, wav_file); /* SubChunk1Size is 16 */
write_little_endian(1, 2, wav_file); /* PCM is format 1 */
write_little_endian(num_channels, 2, wav_file);
write_little_endian(sample_rate, 4, wav_file);
write_little_endian(byte_rate, 4, wav_file);
write_little_endian(num_channels * bytes_per_sample, 2, wav_file); /* block align */
write_little_endian(8 * bytes_per_sample, 2, wav_file); /* bits/sample */
/* write data subchunk */
wav_file.write("data", 4);
for(;;) {
QByteArray d = raw_file.read(500);
if (d.isEmpty()) {
break;
}
wav_file.write(d);
}
wav_file.close();
raw_file.close();
}