35 #include <QPushButton>
36 #include <QStringList>
40 #include <QMessageBox>
43 #include <Inventor/SoDB.h>
44 #include <Inventor/Qt/SoQt.h>
45 #include <Inventor/nodes/SoUnits.h>
56 #include <SimoxUtility/math/convert/pos_rpy_to_mat4f.h>
57 #include <VirtualRobot/XML/RobotIO.h>
58 #include <VirtualRobot/XML/ObjectIO.h>
59 #include <VirtualRobot/ManipulationObject.h>
60 #include <VirtualRobot/Grasping/GraspSet.h>
61 #include <VirtualRobot/Grasping/Grasp.h>
62 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
67 #define DEFAULT_SETTINGS_SIMULATOR_PROXY_NAME "Simulator"
68 #define DEFAULT_SETTINGS_SIMULATORVIEWER_PROXY_NAME "SimulatorViewer_SimulationWindow"
69 #define DEFAULT_SETTINGS_TIMER_MS_UPDATE_SIM_INFO 500.0f
71 static const int POLL_DELAY_MS = 1000;
75 addWidget<SimulatorControlController>();
81 ui.setupUi(getWidget());
83 ui.layerTable->setColumnWidth(0, 200);
84 ui.layerTable->setColumnWidth(1, 40);
85 ui.layerTable->setColumnWidth(2, 40);
86 ui.layerTable->setColumnWidth(3, 80);
87 ui.layerTable->setColumnWidth(4, 80);
88 ui.layerTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
89 ui.layerTable->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
90 ui.layerTable->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
91 ui.layerTable->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);
92 ui.layerTable->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);
93 for (
int i = ui.layerTable->rowCount(); i; --i)
95 ui.layerTable->removeRow(0);
110 usingProxy(settings_simulatorPrxName);
117 ARMARX_INFO <<
"Using simulator proxy: " << settings_simulatorPrxName <<
flush;
118 simulatorPrx = getProxy<SimulatorInterfacePrx>(settings_simulatorPrxName);
122 layerPollTimer.start(POLL_DELAY_MS);
123 enableMainWidgetAsync(
true);
130 layerPollTimer.stop();
131 enableMainWidgetAsync(
false);
157 customToolbar->setParent(parent);
161 customToolbar =
new QToolBar(parent);
162 customToolbar->addAction(
"Play / Stop",
this, SLOT(playStopSim()));
163 customToolbar->addSeparator();
164 customToolbar->addAction(
"Reinit",
this, SLOT(reInit()));
166 return customToolbar.data();
171 connect(ui.cbWindow, SIGNAL(toggled(
bool)),
this, SLOT(showSimWindow(
bool)), Qt::QueuedConnection);
172 connect(ui.cbShowCoordSystem, SIGNAL(toggled(
bool)),
this, SLOT(showCoordSystem(
bool)), Qt::QueuedConnection);
173 connect(ui.cbContacts, SIGNAL(toggled(
bool)),
this, SLOT(showContacts(
bool)), Qt::QueuedConnection);
174 connect(ui.rbFull, SIGNAL(toggled(
bool)),
this, SLOT(selectVisuType()), Qt::QueuedConnection);
175 connect(ui.rbCol, SIGNAL(toggled(
bool)),
this, SLOT(selectVisuType()), Qt::QueuedConnection);
176 connect(ui.pushButtonPlayStop, SIGNAL(clicked()),
this, SLOT(playStopSim()), Qt::QueuedConnection);
177 connect(ui.pushButtonStep, SIGNAL(clicked()),
this, SLOT(stepSim()), Qt::QueuedConnection);
178 connect(ui.pushButtonReInit, SIGNAL(clicked()),
this, SLOT(reInit()), Qt::QueuedConnection);
180 connect(ui.spinBoxAntiAliasing, SIGNAL(valueChanged(
int)),
this, SLOT(antiAliasing(
int)), Qt::QueuedConnection);
183 QObject::connect(&layerSignalMapperVisible, SIGNAL(mapped(QString)),
this, SLOT(layerToggleVisibility(QString)));
184 QObject::connect(&layerSignalMapperClear, SIGNAL(mapped(QString)),
this, SLOT(layerClear(QString)));
185 QObject::connect(&layerSignalMapperRemove, SIGNAL(mapped(QString)),
this, SLOT(layerRemove(QString)));
187 QObject::connect(&layerPollTimer, SIGNAL(timeout()),
this, SLOT(layerPoll()));
189 connect(ui.pushButtonAddRobot, SIGNAL(clicked()),
this, SLOT(on_pushButtonAddRobot_clicked()));
190 connect(ui.pushButtonManipRobotUpdate, SIGNAL(clicked()),
this, SLOT(on_pushButtonManipRobotUpdate_clicked()));
191 connect(ui.pushButtonManipRobSetPose, SIGNAL(clicked()),
this, SLOT(on_pushButtonManipRobSetPose_clicked()));
193 ui.tableObjects->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
194 ui.layerTable->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
200 QString textSimTime(
"SimulationTime:");
201 QString textComTime(
"CommunicationTime:");
202 QString textRobots(
"Robots:");
203 QString textJoints(
"Actuated Joints:");
204 QString textObjects(
"Objects:");
205 QString textContacts(
"Contacts:");
206 QString textDraw(
"DrawTime:");
207 QString textSync(
"SyncTime:");
209 QString textCam(
"Camera ");
215 SimulatorInformation i = simulatorPrx->getSimulatorInformation();
217 textSimTime += QString::number((
double)i.simTimeStepMeasuredMS,
'f', 2);
218 textSimTime += QString(
" ms (x ");
219 textSimTime += QString::number((
double)i.simTimeFactor,
'f', 2);
220 textSimTime += QString(
")");
222 textComTime += QString::number((
double)i.comTimeMS,
'f', 2);
223 textComTime += QString(
" ms");
225 textSync += QString::number((
double)i.syncEngineTimeMS,
'f', 2);
226 textSync += QString(
" ms");
228 textRobots += QString::number(i.nrRobots);
229 textJoints += QString::number(i.nrActuatedJoints);
230 textObjects += QString::number(i.nrObjects);
231 textContacts += QString::number(i.nrContacts);
232 curTime = QString::number(i.currentSimulatorTimeSec,
'f', 3);
236 ui.tableObjects->setRowCount(i.objects.size());
238 for (std::size_t ob = 0; ob < i.objects.size(); ++ob)
240 const auto& o = i.objects.at(ob);
241 QString name = QString::fromStdString(o.name);
243 PoseBasePtr pose = o.objectPoses.begin()->second;
248 QString pos = QString::number(x,
'f', 2) +
"," + QString::number(y,
'f', 2) +
"," + QString::number(z,
'f', 2);
250 VirtualRobot::MathTools::eigen4f2rpy(gp, rpy);
251 QString ori = QString::number(rpy(0),
'f', 2) +
"," + QString::number(rpy(1),
'f', 2) +
"," + QString::number(rpy(2),
'f', 2);
265 ui.tableObjects->setItem(ob, 0,
new QTableWidgetItem {name});
266 ui.tableObjects->setItem(ob, 1,
new QTableWidgetItem {pos});
267 ui.tableObjects->setItem(ob, 2,
new QTableWidgetItem {ori});
268 ui.tableObjects->setItem(ob, 3,
new QTableWidgetItem {
status});
272 if (!simulatorViewerPrx)
274 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
277 if (simulatorViewerPrx)
279 SimulatorViewerInformation i = simulatorViewerPrx->getSimulatorInformation();
280 textDraw += QString::number((
double)i.drawTimeMS + (
double)i.comTimeMS,
'f', 2);
281 textDraw += QString(
" ms");
286 PoseBasePtr p = simulatorViewerPrx->getCameraPose();
287 PosePtr p2 = PosePtr::dynamicCast(p);
290 MathTools::eigen4f2rpy(p3, rpy);
292 textCam += QString::number(p2->position->x * 1000.0f,
'f', 2);
294 textCam += QString::number(p2->position->y * 1000.0f,
'f', 2);
296 textCam += QString::number(p2->position->z * 1000.0f,
'f', 2);
298 textCam += QString::number(rpy(0),
'f', 2);
300 textCam += QString::number(rpy(1),
'f', 2);
302 textCam += QString::number(rpy(2),
'f', 2);
310 ui.lSimTime->setText(textSimTime);
311 ui.lComTime->setText(textComTime);
312 ui.lRobots->setText(textRobots);
313 ui.lJoints->setText(textJoints);
314 ui.lObjects->setText(textObjects);
315 ui.lContacts->setText(textContacts);
316 ui.lDrawTime->setText(textDraw);
317 ui.lSyncTime->setText(textSync);
318 ui.lcdNumberSimTime->setText(curTime);
320 ui.labelCamPose->setText(textCam);
327 if (!simulatorViewerPrx)
329 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
332 if (simulatorViewerPrx)
334 simulatorViewerPrx->enableSimulatorWindow(enable);
347 if (!simulatorViewerPrx)
349 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
352 if (simulatorViewerPrx)
354 simulatorViewerPrx->showBaseCoordSystem(enable, 5.0f);
367 simulatorPrx->showContacts(enable,
"contacts");
383 bool fullModel = ui.rbFull->isChecked();
385 if (!simulatorViewerPrx)
387 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
390 if (simulatorViewerPrx)
392 simulatorViewerPrx->selectVisuType(fullModel);
405 bool running = simulatorPrx->isRunning();
409 simulatorPrx->pause();
410 ui.pushButtonStep->setEnabled(
true);
414 simulatorPrx->start();
415 ui.pushButtonStep->setEnabled(
false);
428 bool running = simulatorPrx->isRunning();
432 simulatorPrx->step();
450 simulatorPrx->reInitialize();
462 if (!simulatorViewerPrx)
464 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
467 if (simulatorViewerPrx)
469 simulatorViewerPrx->setAntiAliasing(steps);
482 if (!simulatorViewerPrx)
484 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
487 if (simulatorViewerPrx)
489 std::string name = layerName.toStdString();
491 simulatorViewerPrx->removeLayer(name);
505 if (!simulatorViewerPrx)
507 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
510 if (simulatorViewerPrx)
512 std::string name = layerName.toStdString();
514 simulatorViewerPrx->clearLayer(name);
528 if (!simulatorViewerPrx)
530 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
533 if (simulatorViewerPrx)
535 auto name = layerName.toStdString();
537 if (layerVisibilityCheckBoxes.count(name))
539 const bool vis = layerVisibilityCheckBoxes.at(name).second->isChecked();
540 ARMARX_VERBOSE <<
"Toggling visibility from " << !vis <<
" to " << vis <<
" for layer " << name <<
"...";
541 simulatorViewerPrx->enableLayerVisu(name, vis);
556 if (!simulatorViewerPrx)
558 simulatorViewerPrx = getProxy<SimulatorViewerControlInterfacePrx>(settings_simulatorViewerPrxName);
561 if (simulatorViewerPrx)
563 auto layerInfo = simulatorViewerPrx->layerInformation();
564 ui.layerTable->setRowCount(layerInfo.size());
565 LayerVisibilityCheckBoxesType newLayerVisibilityCheckBoxes;
567 const bool hideEmptyLayers = ui.checkBoxHideEmptyLayers->isChecked();
569 for (
const auto& layer : layerInfo)
571 auto& layerEntry = newLayerVisibilityCheckBoxes[layer.layerName];
575 if (layerVisibilityCheckBoxes.count(layer.layerName))
578 layerEntry = layerVisibilityCheckBoxes.at(layer.layerName);
579 layerVisibilityCheckBoxes.erase(layer.layerName);
582 for (; tableIdx < ui.layerTable->rowCount(); ++tableIdx)
584 const QTableWidgetItem* item = ui.layerTable->item(tableIdx, 0);
587 found = (item->text() == QString::fromStdString(layer.layerName));
591 ARMARX_WARNING <<
"layerPoll(search old): item " << tableIdx <<
" is null";
601 ui.layerTable->insertRow(0);
603 QString name = QString::fromStdString(layer.layerName);
606 ui.layerTable->setItem(0, 0,
new QTableWidgetItem {name});
609 QCheckBox* box {
new QCheckBox};
610 ui.layerTable->setCellWidget(0, 2, box);
611 layerEntry.second = box;
613 layerSignalMapperVisible.setMapping(box, name);
614 QObject::connect(box, SIGNAL(stateChanged(
int)), &layerSignalMapperVisible, SLOT(map()));
617 QPushButton* clear {
new QPushButton{
"Clear"}};
618 ui.layerTable->setCellWidget(0, 3, clear);
619 layerSignalMapperClear.setMapping(clear, name);
620 QObject::connect(clear, SIGNAL(clicked()), &layerSignalMapperClear, SLOT(map()));
623 QPushButton* remove {
new QPushButton{
"Remove"}};
624 ui.layerTable->setCellWidget(0, 4, remove);
625 layerSignalMapperRemove.setMapping(remove, name);
626 QObject::connect(remove, SIGNAL(clicked()), &layerSignalMapperRemove, SLOT(map()));
632 QCheckBox* box = layerEntry.second;
634 ui.layerTable->setItem(tableIdx, 1,
new QTableWidgetItem {QString::number(layer.elementCount)});
635 box->blockSignals(
true);
636 box->setChecked(layer.visible);
637 box->blockSignals(
false);
639 if (!layer.elementCount && hideEmptyLayers)
642 ui.layerTable->hideRow(tableIdx);
645 ui.layerTable->showRow(tableIdx);
648 for (
const auto& pair : layerVisibilityCheckBoxes)
650 ui.layerTable->removeRow(ui.layerTable->row(pair.second.first));
652 for (
int i = ui.layerTable->rowCount() - 1; i >= 0; --i)
654 const QTableWidgetItem* item = ui.layerTable->item(i, 0);
657 if (layerVisibilityCheckBoxes.count(item->text().toStdString()))
659 ui.layerTable->removeRow(i);
664 ARMARX_WARNING <<
"layerPoll(delete old): item " << i <<
" is null";
667 layerVisibilityCheckBoxes = std::move(newLayerVisibilityCheckBoxes);
668 ui.layerTable->setRowCount(layerVisibilityCheckBoxes.size());
713 void armarx::SimulatorControlController::on_pushButtonAddRobot_clicked()
719 const auto result = simulatorPrx->addScaledRobot(
720 ui.lineEditAddRobotXML->text().toStdString(),
721 ui.doubleSpinBoxAddRobotScale->value());
722 ui.labelAddRobotResult->setText(QString::fromStdString(result));
725 void armarx::SimulatorControlController::on_pushButtonManipRobotUpdate_clicked()
731 ui.comboBoxManipRobotName->clear();
732 for (
const auto& n : simulatorPrx->getRobotNames())
734 ui.comboBoxManipRobotName->addItem(QString::fromStdString(n));
738 void armarx::SimulatorControlController::on_pushButtonManipRobSetPose_clicked()
745 ui.doubleSpinBoxManipRobTX->value(),
746 ui.doubleSpinBoxManipRobTY->value(),
747 ui.doubleSpinBoxManipRobTZ->value(),
749 ui.doubleSpinBoxManipRobRX->value(),
750 ui.doubleSpinBoxManipRobRY->value(),
751 ui.doubleSpinBoxManipRobRZ->value()
753 simulatorPrx->setRobotPose(ui.comboBoxManipRobotName->currentText().toStdString(),
new Pose{p});