2#include "ui_mainwindow.h"
6#include <yarp/os/Log.h>
9 QMainWindow(parent),loadingWidget(this),
16 currentSampleFreq = 5;
17 smoothSliderPressed =
false;
19 connect(&portThread,SIGNAL(portCreated()),
20 this,SLOT(onPortCreated()));
21 connect(&portThread,SIGNAL(openErrorDialog(QString)),
22 this,SLOT(onOpenErrorDialog(QString)));
26 calibratingTimer.setInterval(100);
27 calibratingTimer.setSingleShot(
false);
28 connect(&calibratingTimer,SIGNAL(timeout()),
this,SLOT(onCalibratingTimer()),Qt::QueuedConnection);
30 connect(ui->btnCalibrate,SIGNAL(clicked()),
this,SLOT(onCalibrate()));
31 connect(ui->btnShowTouchThres,SIGNAL(clicked()),
this,SLOT(onThreashold()));
32 connect(ui->btnBinarization,SIGNAL(toggled(
bool)),
this,SLOT(onBinarization(
bool)));
33 connect(ui->btnSmooth,SIGNAL(toggled(
bool)),
this,SLOT(onSmooth(
bool)));
34 connect(ui->sliderScaleSmooth,SIGNAL(valueChanged(
int)),
this,SLOT(onSmoothValueChanged(
int)));
35 connect(ui->sliderScaleSmooth,SIGNAL(sliderPressed()),
this,SLOT(onSmoothValuePressed()));
36 connect(ui->sliderScaleSmooth,SIGNAL(sliderReleased()),
this,SLOT(onSmoothValueReleased()));
38 connect(ui->spinNeighbor,SIGNAL(valueChanged(
double)),
this,SLOT(onSpinNeighborChanged(
double)));
39 connect(ui->spinCompContactGain,SIGNAL(valueChanged(
double)),
this,SLOT(onSpinCompContactGainChanged(
double)));
40 connect(ui->spinCompGain,SIGNAL(valueChanged(
double)),
this,SLOT(onSpinCompGainChanged(
double)));
41 connect(ui->spinThreashold,SIGNAL(valueChanged(
int)),
this,SLOT(onSpinThresholdChanged(
int)));
43 connect(ui->spinSampleFreq,SIGNAL(valueChanged(
int)),
this,SLOT(onSampleFreqChanged(
int)));
45 updateTimer.setSingleShot(
false);
46 updateTimer.setInterval(1000/currentSampleFreq);
47 connect(&updateTimer,SIGNAL(timeout()),
this,SLOT(onUpdateTimer()));
60void MainWindow::onOpenErrorDialog(QString msg)
62 QMessageBox::critical(
this,
"Error",msg);
65void MainWindow::onPortCreated()
67 QString guiName = rf->check(
"name", Value(
"skinManGui")).asString().c_str();
68 QString guiRpcPortName =
"/" + guiName +
"/rpc:o";
69 QString guiMonitorPortName =
"/" + guiName +
"/monitor:i";
70 QString guiInfoPortName =
"/" + guiName +
"/info:i";
72 if (!portThread.
guiRpcPort->open(guiRpcPortName.toLatin1().data())) {
73 QString msg = QString(
"Unable to open port %1").arg(guiRpcPortName);
74 QMessageBox::critical(
this,
"Error",msg);
78 QString msg = QString(
"Unable to open port %1").arg(guiMonitorPortName);
79 QMessageBox::critical(
this,
"Error",msg);
83 QString msg = QString(
"Unable to open port %1").arg(guiInfoPortName);
84 QMessageBox::critical(
this,
"Error",msg);
96 currentSampleFreq = 5;
97 currentSampleNum = 100;
101 int outPorts = portThread.
guiRpcPort->getOutputCount();
109 printLog(
"GUI not connected. Connect it to the module to make it work.");
120 if(
string(reply.toString().c_str()).compare(
"on") == 0){
121 ui->btnBinarization->setEnabled(
true);
122 ui->btnBinarization->setText(
"ON");
123 ui->btnBinarization->setChecked(
true);
125 ui->btnBinarization->setEnabled(
true);
126 ui->btnBinarization->setText(
"OFF");
127 ui->btnBinarization->setChecked(
false);
131 if(
string(reply.toString().c_str()).compare(
"on") == 0){
132 ui->btnSmooth->setText(
"ON");
133 ui->sliderContainer->setEnabled(
true);
134 ui->btnSmooth->setChecked(
true);
136 ui->btnSmooth->setText(
"OFF");
137 ui->sliderContainer->setEnabled(
false);
138 ui->btnSmooth->setChecked(
false);
142 currentSmoothFactor = reply.get(0).asFloat64();
143 ui->sliderScaleSmooth->setValue(currentSmoothFactor * 10);
145 onSmoothValueChanged(currentSmoothFactor * 10);
148 if(reply.isNull() || reply.size()==0 || !reply.get(0).isInt32()){
149 printLog(
"Error while getting the safety threshold");
152 currentThreshold = reply.get(0).asInt32();
153 ui->spinThreashold->setValue(currentThreshold);
157 if(reply.isNull() || reply.size()==0 || (!reply.get(0).isFloat64() && !reply.get(0).isInt32())){
158 printLog(
"Error while getting the compensation gain");
161 currentCompGain = reply.get(0).asFloat64();
162 ui->spinCompGain->setValue(currentCompGain);
166 if(reply.isNull() || reply.size()==0 || (!reply.get(0).isFloat64() && !reply.get(0).isInt32())){
167 printLog(
"Error while getting the contact compensation gain");
170 currentContCompGain = reply.get(0).asFloat64();
171 ui->spinCompContactGain->setValue(currentContCompGain);
175 if(reply.isNull() || reply.size()==0 || (!reply.get(0).isFloat64() && !reply.get(0).isInt32())){
176 printLog(
"Error while getting the max neighbor distance");
179 currentMaxNeighDist = reply.get(0).asFloat64();
180 ui->spinNeighbor->setValue(currentMaxNeighDist * 100);
185 if(reply.isNull() || reply.size()!=3){
186 printLog(
"Error while reading the module information");
190 ss<< reply.get(0).toString().c_str()<< endl;
191 ss<< reply.get(1).toString().c_str()<<
"\nInput ports:";
192 Bottle* portList = reply.get(2).asList();
193 portNames.resize(portList->size()/2);
194 portDim.resize(portList->size()/2);
196 for(
unsigned int i=0;i<portDim.size();i++){
197 portNames[i] = portList->get(i*2).toString().c_str();
198 portDim[i] = portList->get(i*2+1).asInt32();
200 ss<<
"\n - "<< portNames[i]<<
" ("<< portDim[i]<<
" taxels)";
202 ui->infoPanel->setPlainText(QString(
"%1").arg(ss.str().c_str()));
207 if(
string(reply.toString().c_str()).compare(
"yes")==0){
208 loadingWidget.
start();
209 calibratingTimer.start();
212 ui->controlsWidget->setEnabled(
true);
213 ui->sampleFreqContainer->setEnabled(
true);
214 ui->treeCompensation->setEnabled(
true);
219void MainWindow::onCalibratingTimer()
222 if(
string(reply.toString().c_str()).compare(
"yes")==0){
226 ui->controlsWidget->setEnabled(
true);
227 calibratingTimer.stop();
228 loadingWidget.
stop();
230 QString msg =
"Calibrating Done!";
231 ui->statusBar->showMessage(msg);
232 ui->infoPanel->appendPlainText(msg);
236void MainWindow::onCalibrate()
239 loadingWidget.
start(
"Calibrating...don't touch the skin!!");
240 calibratingTimer.start();
241 ui->controlsWidget->setEnabled(
false);
244void MainWindow::onSpinThresholdChanged(
int value)
246 int safetyThr = value;
247 if(safetyThr == currentThreshold)
253 b.addInt32(safetyThr);
258 currentThreshold = getReply.get(0).asInt32();
260 if(safetyThr==currentThreshold){
261 QString msg = QString(
"Safety threshold changed: %1").arg(safetyThr);
262 ui->statusBar->showMessage(msg);
263 ui->infoPanel->appendPlainText(msg);
267 QString msg = QString(
"Unable to set the threshold to: %1 m.\nSet command reply: %2").arg(safetyThr).arg(setReply.toString().c_str());
268 QMessageBox::critical(
this,
"Error",msg);
271 ui->spinThreashold->setValue(currentThreshold);
274void MainWindow::onSpinCompGainChanged(
double value)
276 double compGain = value;
277 if(compGain == currentCompGain)
283 b.addFloat64(compGain);
288 currentCompGain = getReply.get(0).asFloat64();
290 if(compGain==currentCompGain){
291 QString msg = QString(
"Compensation gain changed: %1").arg(compGain);
292 ui->statusBar->showMessage(msg);
293 ui->infoPanel->appendPlainText(msg);
296 QString msg = QString(
"Unable to set the compensation gain to: %1 m.\nSet command reply: %2").arg(compGain).arg(setReply.toString().c_str());
297 QMessageBox::critical(
this,
"Error",msg);
300 ui->spinCompGain->setValue(currentCompGain);
303void MainWindow::onSpinCompContactGainChanged(
double value)
305 double contCompGain = value;
306 if(contCompGain == currentContCompGain){
313 b.addFloat64(contCompGain);
318 currentContCompGain = getReply.get(0).asFloat64();
320 if(contCompGain==currentContCompGain){
321 QString msg = QString(
"Contact compensation gain changed: %1").arg(contCompGain);
322 ui->statusBar->showMessage(msg);
323 ui->infoPanel->appendPlainText(msg);
327 QString msg = QString(
"Unable to set the contact compensation gain to: %1 m.\nSet command reply: %2").arg(contCompGain).arg(setReply.toString().c_str());
328 QMessageBox::critical(
this,
"Error",msg);
331 ui->spinCompContactGain->setValue(currentContCompGain);
334void MainWindow::onSpinNeighborChanged(
double value)
336 double maxNeighDist = 1
e-2*value;
338 if(maxNeighDist == currentMaxNeighDist){
345 b.addFloat64(maxNeighDist);
350 currentMaxNeighDist = getReply.get(0).asFloat64();
352 if(maxNeighDist==currentMaxNeighDist){
353 QString msg = QString(
"Max neighbor distance changed: %1 m").arg(maxNeighDist);
354 ui->statusBar->showMessage(msg);
355 ui->infoPanel->appendPlainText(msg);
359 QString msg = QString(
"Unable to set the max neighbor distance to: %1 m.\nSet command reply: %2").arg(maxNeighDist).arg(setReply.toString().c_str());
360 QMessageBox::critical(
this,
"Error",msg);
362 ui->spinNeighbor->setValue(currentMaxNeighDist);
366void MainWindow::onSmoothValuePressed()
368 smoothSliderPressed =
true;
371void MainWindow::onSmoothValueReleased()
373 if(!smoothSliderPressed){
377 smoothSliderPressed =
false;
379 changeSmooth(ui->sliderScaleSmooth->value());
384void MainWindow::changeSmooth(
int val)
387 double value = (double)val/10;
388 double smoothFactor =
round(value, 1);
389 if(smoothFactor==currentSmoothFactor){
397 b.addFloat64(smoothFactor);
402 currentSmoothFactor = getReply.get(0).asFloat64();
403 currentSmoothFactor =
round(currentSmoothFactor, 1);
405 if(smoothFactor==currentSmoothFactor){
406 QString msg = QString(
"Smooth factor changed: %1").arg(smoothFactor);
407 ui->statusBar->showMessage(msg);
408 ui->infoPanel->appendPlainText(msg);
411 QString msg = QString(
"Unable to set the smooth factor to: %1\nSet command reply: ").arg(smoothFactor).arg(setReply.toString().c_str());
412 QMessageBox::critical(
this,
"Error",msg);
415 ui->sliderScaleSmooth->setValue(currentSmoothFactor*10);
418void MainWindow::onThreashold()
422 for(
unsigned int j=0; j<portDim.size(); j++){
424 msg<<
"Thresholds for port "<< portNames[j]<<
":\n";
425 for(
unsigned int i=0; i<portDim[j]; i++){
430 msg<<
"TR"<< i/12<<
":\t";
432 msg << int(touchThr.get(index).asFloat64())<<
";\t";
435 QMessageBox::information(
this,
"Information",QString(
"%1").arg(msg.str().c_str()));
439void MainWindow::onSmoothValueChanged(
int val)
441 QString d = QString(
"%L1").arg((
double)val/10,0,
'f',1);
442 ui->lblSmoothValue->setText(d);
444 int w = ui->sliderScaleSmooth->width() - 30;
445 double newX = ((double)w/(
double)10) * (
double)val;
446 ui->lblSmoothValue->setGeometry(newX,0,30,20);
449void MainWindow::onSmooth(
bool btnState)
458 if(
string(reply.toString().c_str()).compare(
"on") == 0){
459 ui->btnSmooth->setText(
"ON");
460 ui->smoothControlContainer->setEnabled(
true);
461 QString msg =
"Smooth filter turned on";
462 ui->statusBar->showMessage(msg);
463 ui->infoPanel->appendPlainText(msg);
465 QString msg = QString(
"Error! Unable to turn the smooth filter on: %1").arg(reply.toString().c_str());
466 ui->statusBar->showMessage(msg);
467 ui->infoPanel->appendPlainText(msg);
474 if(
string(reply.toString().c_str()).compare(
"off") == 0){
475 ui->btnSmooth->setText(
"OFF");
476 ui->smoothControlContainer->setEnabled(
false);
477 QString msg =
"Smooth filter turned off";
478 ui->statusBar->showMessage(msg);
479 ui->infoPanel->appendPlainText(msg);
481 QString msg = QString(
"Error! Unable to turn the smooth filter off: %1").arg(reply.toString().c_str());
482 ui->statusBar->showMessage(msg);
483 ui->infoPanel->appendPlainText(msg);
488void MainWindow::onBinarization(
bool btnState)
496 if(
string(reply.toString().c_str()).compare(
"on")==0){
497 ui->btnBinarization->setText(
"ON");
498 QString msg =
"Binarization filter turned on";
499 ui->statusBar->showMessage(msg);
500 ui->infoPanel->appendPlainText(msg);
502 QString msg = QString(
"Error! Unable to turn the binarization filter on: %1").arg( reply.toString().c_str());
503 ui->statusBar->showMessage(msg);
504 ui->infoPanel->appendPlainText(msg);
512 if(
string(reply.toString().c_str()).compare(
"off")==0){
513 ui->btnBinarization->setText(
"OFF");
514 QString msg =
"Binarization filter turned off";
515 ui->statusBar->showMessage(msg);
516 ui->infoPanel->appendPlainText(msg);
518 QString msg = QString(
"Error! Unable to turn the binarization filter off: %1").arg(reply.toString().c_str());
519 ui->statusBar->showMessage(msg);
520 ui->infoPanel->appendPlainText(msg);
528 QString msg = text +
"\n";
531 yDebug(
"%s", msg.toLatin1().data());
535 double q = pow(10.0, decimalDigit);
536 return double(
int((value*q)+0.5))/q;
540void MainWindow::onUpdateTimer()
545 if(portThread.
guiRpcPort->getOutputCount()>0){
557 setMsgFreq(
false, 0);
560 if(!b || b->size()==0){
561 setMsgFreq(
false, 0);
564 double freq = (*b)[0];
565 setMsgFreq(
true, freq);
569 for(
unsigned int i=0; i<portDim.size(); i++){
570 numTax += portDim[i];
572 const int numTr = numTax/12;
576 vector<float> driftPerTr(numTr);
578 for(
int i=0; i<numTr; i++){
580 for(
int j=0; j<12; j++){
581 sumTr += (float) (*b)(i*12+j+1);
583 driftPerTr[i] = (float)
round(sumTr/12.0, 2);
586 if(ui->treeCompensation->topLevelItemCount() <= 0){
587 for(
size_t i=0; i<portNames.size();i++){
588 QTreeWidgetItem *portItem =
new QTreeWidgetItem();
589 ui->treeCompensation->addTopLevelItem(portItem);
591 for(
unsigned int j=0; j<portDim[i]/12; j++){
592 QTreeWidgetItem *trItem =
new QTreeWidgetItem();
593 portItem->addChild(trItem);
595 for(
int k=0; k<12; k++){
596 QTreeWidgetItem *taxelItem =
new QTreeWidgetItem();
597 trItem->addChild(taxelItem);
604 double sumPort, meanPort;
605 stringstream trS, taxS;
610 for(
size_t i=0; i<portNames.size();i++){
613 QTreeWidgetItem *portItem = ui->treeCompensation->topLevelItem(i);
615 for(
unsigned int j=0; j<portDim[i]/12; j++){
616 meanTr = driftPerTr[portIndex];
622 QTreeWidgetItem *trItem = portItem->child(j);
623 trItem->setText(1,trS.str().c_str());
624 trItem->setText(3,QString(
"%L1").arg(meanTr,0,
'f',3));
626 for(
int k=0; k<12; k++){
627 double drift =
round((*b)(index), 2);
631 QTreeWidgetItem *taxelItem = trItem->child(k);
633 taxelItem->setText(2,taxS.str().c_str());
634 taxelItem->setText(3,QString(
"%L1").arg(drift,0,
'f',3));
639 meanPort = sumPort/(portDim[i]/12);
640 portItem->setText(0, portNames[i].c_str());
641 portItem->setText(3, QString(
"%L1").arg(meanPort,0,
'f',3));
652 printLog(infoMsg->toString().c_str());
657void MainWindow::onSampleFreqChanged(
int value)
659 if (value == currentSampleFreq || !updateTimer.isActive()){
662 currentSampleFreq = value;
664 updateTimer.setInterval(1000/currentSampleFreq);
669void MainWindow::setMsgFreq(
bool freqUpdated,
double freq){
671 double auxFreq =
round(freq, 2);
673 text = QString(
"SkinDriftCompensation frequency: %L1").arg(freq,0,
'f',3);
676 text =
"Cannot read the frequency. Probably the skinDriftCompensation module has stopped working.";
678 if(auxFreq != prevFreq){
680 ui->infoPanel->appendPlainText(text);
double round(double value, int decimalDigit)
MainWindow(FirmwareUpdaterCore *core, bool adminMode, QWidget *parent=0)
BufferedPort< Vector > * driftCompMonitorPort
Bottle sendRpcCommand(bool responseExpected, SkinManagerCommand cmd)
BufferedPort< Bottle > * driftCompInfoPort