-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
421 lines (374 loc) · 14.9 KB
/
mainwindow.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include "qdebug.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, thresholdFreq(100.0/1000000.0)
, remoteHost("dev.control@10.2.0.23")
{
ui->setupUi(this);
this->ui->numIterations->setEnabled(false);
this->sampling_frequency = new QEpicsPV("SOFB:SamplingFrequency");
this->energy = new QEpicsPV("SOFB:EnergyLevel");
this->num_iterations = new QEpicsPV("SOFB:NumIterations");
this->num_singular_values = new QEpicsPV("SOFB:NumSingularVals");
this->correction_factor = new QEpicsPV("SOFB:CorrectionFactor");
this->max_frequency_change = new QEpicsPV("SOFB:MaxFreqChange");
this->max_current_change = new QEpicsPV("SOFB:MaxCurrentChange");
this->max_read_fail = new QEpicsPV("SOFB:MaxReadFail");
this->correction_status = new QEpicsPV("SOFB:CorrectionStatus");
this->include_rf = new QEpicsPV("SOFB:IncludeRf");
this->apply_regularization = new QEpicsPV("SOFB:ApplyRegularization");
this->rf_only = new QEpicsPV("SOFB:OnlyRf");
this->debug_mode = new QEpicsPV("SOFB:NoSetPv");
this->normalize_inputs = new QEpicsPV("SOFB:NormalizeInputs");
this->scale_outputs = new QEpicsPV("SOFB:ScaleOutputs");
this->control_algo = new QEpicsPV("SOFB:Control:Algo");
this->estimation_algo = new QEpicsPV("SOFB:Estimation:Algo");
this->avg_algo = new QEpicsPV("SOFB:MovAvg:Algo");
this->window_size = new QEpicsPV("SOFB:MovAvg:WindowSize");
this->smoothing_factor = new QEpicsPV("SOFB:MovAvg:SmoothingFactor");
this->regularization_Param = new QEpicsPV("SOFB:RegularizationParam");
this->getFrequency = new QEpicsPV("BO-RF-SGN1:getFrequency");
this->setFrequency = new QEpicsPV("BO-RF-SGN1:setFrequency");
/* Current PVs for removing correction */
for (int cellIdx = 1; cellIdx <= 16; ++cellIdx)
{
for (int idx = 1; idx <= 2; ++idx)
{
this->getCorrectors_currents.push_back(new QEpicsPV(QString::asprintf("SRC%02d-PS-HC%d:getIload", cellIdx, idx)));
this->getCorrectors_currents.push_back(new QEpicsPV(QString::asprintf("SRC%02d-PS-VC%d:getIload", cellIdx, idx)));
this->setCorrectors_currents.push_back(QString::asprintf("SRC%02d-PS-HC%d:setReference", cellIdx, idx));
this->setCorrectors_currents.push_back(QString::asprintf("SRC%02d-PS-VC%d:setReference", cellIdx, idx));
}
}
/**/
this->correction_process = new QProcess();
this->logProcess = new QProcess();
this->base_path = "/home/dev.control/orbit-correction/";
this->data_path = this->base_path + "data";
this->pm_path = "/mnt/machine-nfs01/machine/PostMortem/orbit_correction";
this->logs_path = this->base_path + "logs";
this->logFile = new QFile(this->logs_path + "/last_run.log");
this->loggingTimer = new QTimer(this);
QObject::connect(correction_process, SIGNAL(finished(int)), this, SLOT(on_correctionEnd(int)));
QObject::connect(num_iterations, SIGNAL(valueInited(const QVariant &)), this, SLOT(onNumIterationsInit(const QVariant &)));
QObject::connect(include_rf, SIGNAL(valueInited(const QVariant &)), this, SLOT(onIncludeRfInit(const QVariant &)));
QObject::connect(apply_regularization, SIGNAL(valueInited(const QVariant &)), this, SLOT(onApplyRegularizationInit(const QVariant &)));
QObject::connect(correction_status, SIGNAL(valueInited(const QVariant &)), this, SLOT(onCorrectionStatusInit(const QVariant &)));
QObject::connect(correction_status, SIGNAL(valueChanged(const QVariant &)), this, SLOT(onCorrectionStatusChanged(const QVariant &)));
QObject::connect(rf_only, SIGNAL(valueInited(const QVariant &)), this, SLOT(checkRfOnlyRun()));
QObject::connect(rf_only, SIGNAL(valueChanged(const QVariant &)), this, SLOT(checkRfOnlyRun()));
QObject::connect(getFrequency, SIGNAL(valueInited(const QVariant &)), this, SLOT(frequencyPvInit(const QVariant &)));
QObject::connect(getFrequency, SIGNAL(valueChanged(const QVariant &)), this, SLOT(checkRfOnlyRun()));
QObject::connect(loggingTimer, SIGNAL(timeout()), this, SLOT(logDataSSH()));
QObject::connect(this, SIGNAL(stackLengthChanged(int)), this, SLOT(onStackLengthChanged(int)));
this->expert = NULL;
this->historyLogs = NULL;
this->plots = NULL;
CONNECT_CLOSE_BUTTON;
}
MainWindow::~MainWindow()
{
if (logFile->isOpen())
logFile->close();
delete ui;
}
void MainWindow::onNumIterationsInit(const QVariant& numOfIterations)
{
if (numOfIterations == -1)
{
this->inf_iterations = true;
this->ui->chkBoxInfIterations->setChecked(1);
}
else
{
this->inf_iterations = false;
this->ui->chkBoxInfIterations->setChecked(0);
}
}
void MainWindow::onIncludeRfInit(const QVariant &val)
{
this->ui->chkBoxIncludeRf->setChecked(val.toBool());
}
void MainWindow::onApplyRegularizationInit(const QVariant &val)
{
this->ui->chkBoxApplyReg->setChecked(val.toBool());
}
void MainWindow::on_chkBoxInfIterations_stateChanged(int state)
{
this->inf_iterations = state;
if (state)
{
Client::writePV("SOFB:NumIterations", -1);
this->ui->numIterations->setEnabled(false);
}
else
{
Client::writePV("SOFB:NumIterations", 1);
this->ui->numIterations->setEnabled(true);
}
}
void MainWindow::on_btnStartCorrection_clicked()
{
QMessageBox::StandardButton reply = QMessageBox::question(nullptr, "Confirmation", "Are you sure you want to start correction?", QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::No)
return;
int correction_iterations = num_iterations->get().toInt();
QString correction_iterations_lbl = QString::number(correction_iterations);
if (this->inf_iterations)
{
correction_iterations = -1;
correction_iterations_lbl = "Inf";
}
QStringList energyLevels = this->energy->getEnum();
QStringList ControlAlgos = this->control_algo->getEnum();
QStringList EstimationAlgos = this->estimation_algo->getEnum();
QStringList movAvgAlgos = this->avg_algo->getEnum();
QStringList params;
params << "-sampling_freq" << sampling_frequency->get().toString();
params << "-energy" << energyLevels[this->energy->get().toInt()];
params << "-num_iter" << QString::number(correction_iterations);
params << "-num_singular" << num_singular_values->get().toString();
params << "-correction_factor" << correction_factor->get().toString();
params << "-max_freq_change" << max_frequency_change->get().toString();
params << "-max_curr_change" << max_current_change->get().toString();
params << "-max_read_fail" << max_read_fail->get().toString();
params << "-data_path" << data_path;
params << "-logs_path" << logs_path;
params << "-pm_path" << pm_path;
params << "-control_algo" << ControlAlgos[this->control_algo->get().toInt()];
params << "-estimation_algo" << EstimationAlgos[this->estimation_algo->get().toInt()];
params << "-avg_algo" << movAvgAlgos[this->avg_algo->get().toInt()];
params << "-window_size" << window_size->get().toString();
params << "-avg_smooth" << smoothing_factor->get().toString();
params << "-reg_param" << regularization_Param->get().toString();
if (this->include_rf->get().toBool())
params << "-include_rf";
if (this->apply_regularization->get().toBool())
params << "-apply_reg";
if (this->rf_only->get().toBool())
params << "-rf_only";
if (this->debug_mode->get().toBool())
params << "-no_set";
if (this->normalize_inputs->get().toBool())
params << "-normalize_inputs";
if (this->scale_outputs->get().toBool())
params << "-scale_outputs";
disableInputs();
ui->btnStartCorrection->setEnabled(false);
ui->btnStopCorrection->setEnabled(true);
ui->lblLogs->setText("");
ui->lblCurrentIterationLimit->setText("out of " + correction_iterations_lbl);
//for (QString param : params)
//{
// std::cout << param.toStdString() << " ";
//}
if (!this->debug_mode->get().toBool())
saveCorrectorsAndRF();
QStringList sshArgs;
sshArgs << remoteHost << "sofb" << params;
if (!correction_process->startDetached("ssh", sshArgs)) {
std::cout << "Error: Unable to start Correction\n";
}
}
void MainWindow::saveCorrectorsAndRF()
{
std::array<double, 65> correction;
for (int i = 0; i < 64; ++i)
{
correction[i] = this->getCorrectors_currents[i]->get().toDouble();
}
correction[64] = this->getFrequency->get().toDouble();
correctionStack.push(correction);
emit stackLengthChanged(correctionStack.length());
}
void MainWindow::print_stdout()
{
QString p_stdout = correction_process->readAllStandardOutput();
QString p_stderr = correction_process->readAllStandardError();
if(!p_stderr.isEmpty())
ui->lblLogs->setText(ui->lblLogs->text() + p_stderr);
else
ui->lblLogs->setText(ui->lblLogs->text() + p_stdout);
}
void MainWindow::on_btnStopCorrection_clicked()
{
QMessageBox::StandardButton reply = QMessageBox::question(nullptr, "Confirmation", "Are you sure you want to stop the correction?", QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::No)
return;
QString processName = "sofb$";
QProcess pkillProcess;
//pkillProcess.start("pkill", QStringList() << processName);
QStringList sshArgs;
sshArgs << remoteHost << "pkill" << processName;
if (!pkillProcess.startDetached("ssh", sshArgs)) {
std::cout << "Error: Unable to stop Correction\n";
}
pkillProcess.waitForFinished();
if (pkillProcess.exitCode() != 0)
{
std::cout << "Error in Terminating Correction Processes: " << pkillProcess.errorString().toStdString() << std::endl;
}
}
void MainWindow::on_correctionEnd(int status)
{
QString state = "Success";
if (status)
state = "Fail";
ui->lblLogs->setText(ui->lblLogs->text() + "\n" + state);
enableInputs();
ui->btnStartCorrection->setEnabled(true);
ui->btnStopCorrection->setEnabled(false);
}
void MainWindow::disableInputs()
{
this->ui->energy->setEnabled(false);
this->ui->chkBoxInfIterations->setEnabled(false);
this->ui->numIterations->setEnabled(false);
this->ui->numSingularValues->setEnabled(false);
this->ui->maxFreqChange->setEnabled(false);
this->ui->maxCurrChange->setEnabled(false);
this->ui->maxReadFail->setEnabled(false);
this->ui->samplingFrequency->setEnabled(false);
this->ui->chkBoxIncludeRf->setEnabled(false);
this->ui->chkBoxApplyReg->setEnabled(false);
this->ui->btnRemoveCorrection->setEnabled(false);
}
void MainWindow::enableInputs()
{
this->ui->energy->setEnabled(true);
this->ui->chkBoxInfIterations->setEnabled(true);
this->ui->numSingularValues->setEnabled(true);
this->ui->maxFreqChange->setEnabled(true);
this->ui->maxCurrChange->setEnabled(true);
this->ui->maxReadFail->setEnabled(true);
this->ui->samplingFrequency->setEnabled(true);
this->ui->chkBoxIncludeRf->setEnabled(true);
this->ui->chkBoxApplyReg->setEnabled(true);
if (this->inf_iterations)
this->ui->numIterations->setEnabled(false);
else
this->ui->numIterations->setEnabled(true);
if (this->correctionStack.length())
this->ui->btnRemoveCorrection->setEnabled(true);
else
this->ui->btnRemoveCorrection->setEnabled(false);
}
void MainWindow::onCorrectionStatusInit(const QVariant &status)
{
const int running = 1;
const int debug = 3;
if (status == running || status == debug)
{
ui->btnStartCorrection->setEnabled(false);
ui->btnStopCorrection->setEnabled(false);
this->loggingTimer->start(500);
disableInputs();
}
}
void MainWindow::onCorrectionStatusChanged(const QVariant &status)
{
const int success = 0;
const int running = 1;
const int fail = 2;
const int debug = 3;
if (status == success || status == fail)
{
ui->btnStartCorrection->setEnabled(true);
ui->btnStopCorrection->setEnabled(false);
enableInputs();
this->loggingTimer->stop();
if (logFile->isOpen())
logFile->close();
} else if (status == running || status == debug)
{
ui->btnStartCorrection->setEnabled(false);
ui->btnStopCorrection->setEnabled(true);
this->loggingTimer->start(500);
disableInputs();
}
}
void MainWindow::on_btnExpert_clicked()
{
OPEN_UI(expert, Expert, this);
}
void MainWindow::startLocalLogging()
{
if(!logFile->isOpen())
{
if(!logFile->open(QIODevice::ReadOnly))
this->ui->lblLogs->setText("Warning: Could not read log file\n" + logFile->errorString());
else
this->loggingTimer->start(500);
}
}
void MainWindow::logLocalData()
{
QTextStream in(logFile);
QString content = in.readAll();
this->ui->lblLogs->setText(content);
logFile->seek(0);
}
void MainWindow::logDataSSH()
{
QStringList sshArgs;
sshArgs << remoteHost << "cat" << this->logs_path + "/last_run.log";
logProcess->start("ssh", sshArgs);
if (logProcess->waitForFinished())
{
QString output = logProcess->readAll();
this->ui->lblLogs->setText(output);
} else {
std::cout << "SSH Command: ssh " << sshArgs.join(" ").toStdString() << std::endl;
std::cout << "SSH Exit Code: " << logProcess->exitCode() << std::endl;
std::cout << "SSH Error: " << logProcess->errorString().toStdString() << std::endl;
std::cout << "Error: Unable to execute SSH command\n";
}
}
void MainWindow::frequencyPvInit(const QVariant& val)
{
currentFreq = val.toDouble();
prevFreq = val.toDouble();
}
void MainWindow::checkRfOnlyRun()
{
currentFreq = getFrequency->get().toDouble();
if (fabs(currentFreq - prevFreq) > thresholdFreq || rf_only->get().toBool())
this->ui->ledRfOnly->setValue(1);
else
this->ui->ledRfOnly->setValue(0);
prevFreq = currentFreq;
}
void MainWindow::on_btnHistoryLogs_clicked()
{
OPEN_UI(historyLogs, Logs, this);
}
void MainWindow::on_btnPlots_clicked()
{
OPEN_UI(plots, Plots, this);
}
void MainWindow::on_btnRemoveCorrection_clicked()
{
if (!correctionStack.isEmpty())
{
std::array<double, 65> correction = correctionStack.pop();
emit stackLengthChanged(correctionStack.length());
for (int i = 0; i < 64; ++i)
{
Client::writePV(this->setCorrectors_currents[i], correction[i]);
}
Client::writePV(this->setFrequency->pv(), correction[64]);
} else
{
std::cout << "Stack is empty!" <<std::endl;
}
}
void MainWindow::onStackLengthChanged(int stackLength)
{
this->ui->btnRemoveCorrection->setText(QString::asprintf("Remove Correction (%d)", stackLength));
if (!stackLength)
this->ui->btnRemoveCorrection->setEnabled(false);
}