28 #include <ArmarXCore/interface/core/Log.h>
30 #include <RobotAPI/interface/core/RobotState.h>
34 #include <MemoryX/gui-plugins/WorkingMemoryGui/ui_WorkingMemoryConfigDialog.h>
49 #include <SimoxUtility/algorithm/string/string_tools.h>
50 #include <VirtualRobot/Grasping/Grasp.h>
51 #include <VirtualRobot/Grasping/GraspSet.h>
52 #include <VirtualRobot/ManipulationObject.h>
53 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
54 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
55 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationNode.h>
56 #include <VirtualRobot/XML/ObjectIO.h>
57 #include <VirtualRobot/XML/RobotIO.h>
63 #include <QDialogButtonBox>
64 #include <QFileDialog>
65 #include <QHeaderView>
66 #include <QMessageBox>
67 #include <QPushButton>
70 #include <QStringList>
73 #include <QTreeWidget>
74 #include <QTreeWidgetItem>
83 #include <Inventor/Qt/SoQt.h>
84 #include <Inventor/SoDB.h>
85 #include <Inventor/nodes/SoUnits.h>
89 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
90 #include <Inventor/actions/SoToVRML2Action.h>
91 #include <Inventor/actions/SoWriteAction.h>
92 #include <Inventor/nodes/SoRotation.h>
96 #include <eigen3/Eigen/Core>
110 Q_DECLARE_METATYPE(::memoryx::EntityBasePtr)
116 #define DEFAULT_SETTINGS_PLUGIN_NAME "WorkingMemoryGuiPlugin"
117 #define DEFAULT_SETTINGS_PRIORMEMORY_NAME "PriorKnowledge"
118 #define DEFAULT_SETTINGS_WORKINGMEMORY_NAME "WorkingMemory"
119 #define DEFAULT_SETTINGS_WORKINGMEMORY_UPDATESTOPIC "WorkingMemoryUpdates"
120 #define DEFAULT_SETTINGS_OBJECT_INSTANCES_SEGMENT_NAME "objectInstances"
121 #define DEFAULT_SETTINGS_AGENT_INSTANCES_SEGMENT_NAME "agentInstances"
122 #define DEFAULT_SETTINGS_WORLD_STATE_SEGMENT_NAME "worldState"
123 #define DEFAULT_SETTINGS_ADDITIONAL_PACKAGES "RobotAPI,Armar3,Armar4"
124 #define DEFAULT_SETTINGS_COMMONSTORAGE_NAME "CommonStorage"
125 #define MIN_OBJECT_TRANSPARENCY 0.1f
130 static const int DEFAULT_ROBOT_UPDATE_STEP = 33;
137 eItemAttr = QTreeWidgetItem::UserType + 2,
142 WorkingMemoryGuiPlugin::WorkingMemoryGuiPlugin()
145 addWidget<WorkingMemoryController>();
146 qRegisterMetaType<memoryx::EntityBasePtr>(
"memoryx::EntityBasePtr");
147 qRegisterMetaType<Qt::SortOrder>(
"Qt::SortOrder");
150 WorkingMemoryController::WorkingMemoryController()
154 objectsVisu =
nullptr;
155 agentsVisu =
nullptr;
156 debugLayerVisu =
nullptr;
157 show3DViewer =
false;
158 customToolbar =
nullptr;
160 debugDrawer =
nullptr;
161 sceneConfigDialog =
nullptr;
162 debugDrawerConfigWidget =
nullptr;
164 ui.setupUi(getWidget());
166 visuSetting_transparentExistanceCertatinty = ui.checkBox_existenceCertainty->isChecked();
167 visuSetting_showObjectModels = ui.cbShowObjectModels->isChecked();
169 showObjInstanceUncertainties = ui.cbShowPositionUncertainty->isChecked();
170 getWidget()->setEnabled(
false);
172 ui.tabWidget->removeTab(0);
176 ui.layerWidget->layout()->addWidget(debugLayerControlWidget);
179 WorkingMemoryController::~WorkingMemoryController()
182 ARMARX_INFO <<
"Destructor WorkingMemoryController";
191 std::unique_lock lock(*mutex3D);
195 debugLayerVisu->removeAllChildren();
196 debugLayerVisu->unref();
197 debugLayerVisu =
nullptr;
202 agentsVisu->removeAllChildren();
204 agentsVisu =
nullptr;
209 objectsVisu->removeAllChildren();
210 objectsVisu->unref();
211 objectsVisu =
nullptr;
216 rootVisu->removeAllChildren();
221 if (debugDrawer && debugDrawer->getObjectScheduler())
223 ARMARX_INFO <<
"Removing DebugDrawer component...";
224 debugDrawer->getObjectScheduler()->terminate();
225 ARMARX_INFO <<
"Removing DebugDrawer component...done";
230 WorkingMemoryController::onInitComponent()
237 rootVisu =
new SoSeparator();
241 std::string debugDrawerComponentName =
"WorkingMemoryDebugDrawer_" + getName();
242 ARMARX_INFO <<
"Creating component " << debugDrawerComponentName;
244 Component::create<EntityDrawerComponent>(getIceProperties(), debugDrawerComponentName);
248 debugDrawer->setMutex(mutex3D);
256 m->addObject(debugDrawer);
258 debugLayerControlWidget->setEntityDrawer(debugDrawer);
261 std::unique_lock lock(*mutex3D);
262 debugLayerVisu =
new SoSeparator();
263 debugLayerVisu->ref();
264 debugLayerVisu->addChild(debugDrawer->getVisualization());
266 objectsVisu =
new SoSeparator();
269 agentsVisu =
new SoSeparator();
273 rootVisu->addChild(objectsVisu);
274 rootVisu->addChild(agentsVisu);
275 rootVisu->addChild(debugLayerVisu);
277 up << 0.0f, 0.0f, 1.0f;
281 planeVisu = CoinVisualizationFactory::CreatePlaneVisualization(pos, up, 30000.0f, 0);
283 rootVisu->addChild(planeVisu);
286 ARMARX_INFO <<
"WorkingMemory: " << settings_workingMemoryName;
288 usingProxy(settings_workingMemoryName);
289 usingProxy(settings_priorMemoryName);
296 WorkingMemoryController::onConnectComponent()
299 ARMARX_INFO <<
" Connecting WorkingMemoryController";
303 std::unique_lock lock(mutexEntities);
304 memoryPrx = getProxy<AbstractWorkingMemoryInterfacePrx>(settings_workingMemoryName);
306 ARMARX_DEBUG <<
"Getting object instances segment " << settings_objectInstancesSegmentName;
307 objectInstancesPrx = ObjectInstanceMemorySegmentBasePrx::uncheckedCast(
308 memoryPrx->getSegment(settings_objectInstancesSegmentName));
309 ARMARX_DEBUG <<
"Getting agent instances segment " << settings_agentInstancesSegmentName;
310 agentInstancesProxy = AgentInstancesSegmentBasePrx::uncheckedCast(
311 memoryPrx->getSegment(settings_agentInstancesSegmentName));
312 ARMARX_DEBUG <<
"Getting world state segment " << settings_worldStateSegmentName;
313 worldStateProxy = WorldStateSegmentBasePrx::uncheckedCast(
314 memoryPrx->getSegment(settings_worldStateSegmentName));
317 CommonStorageInterfacePrx commonStoragePrx =
320 segmentNames = memoryPrx->getSegmentNames();
322 ARMARX_DEBUG <<
"Getting prior knowledge " << settings_priorMemoryName;
323 priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(settings_priorMemoryName);
326 classesSegmentPrx = priorKnowledgePrx->getObjectClassesSegment();
327 if (!classesSegmentPrx)
329 ARMARX_ERROR <<
"Classes Segment not found in PriorKnowledge";
335 <<
"Could not connect to other components (WM/PriorKnowledge/CommonStorage)...";
339 usingTopic(settings_workingMemoryUpdatesTopic);
340 enableMainWidgetAsync(
true);
341 QMetaObject::invokeMethod(
this,
"startTimerInGUIThread");
343 QMetaObject::invokeMethod(
this,
"refetchEntitiesFromMemory");
347 WorkingMemoryController::onDisconnectComponent()
355 emit signalClearEntities(
"");
356 emit signalRemoveTabs();
358 enableMainWidgetAsync(
false);
359 killTimer(robotUpdateTimerId);
362 std::unique_lock lock(mutexEntities);
368 WorkingMemoryController::onExitComponent()
371 ARMARX_INFO <<
" Exiting component WorkingMemoryController";
415 std::unique_lock lock(mutexEntities);
416 std::unique_lock lock2(*mutex3D);
422 WorkingMemoryController::getConfigDialog(QWidget* parent)
428 dialog->ui->editWorkingMemoryGuiName->setText(
430 dialog->ui->editWorkingMemoryName->setText(
432 dialog->ui->editPriorMemoryName->setText(
434 dialog->ui->editWorkingMemoryUpdatesTopic->setText(
436 dialog->ui->editObjectInstancesSegmentName->setText(
438 dialog->ui->editAgentInstancesSegmentName->setText(
440 dialog->ui->editWorldStateSegmentName->setText(
442 dialog->ui->editAdditionalPackages->setText(
446 return qobject_cast<WorkingMemoryConfigDialog*>(dialog);
450 WorkingMemoryController::processPackages(
const std::string& packages)
453 QString addPacksString = QString::fromStdString(packages);
454 auto addPacks = addPacksString.split(
",", QString::SkipEmptyParts);
456 for (QString& package : addPacks)
465 WorkingMemoryController::configured()
468 settings_priorMemoryName = dialog->ui->editPriorMemoryName->text().toStdString();
469 settings_workingMemoryName = dialog->ui->editWorkingMemoryName->text().toStdString();
470 settings_objectInstancesSegmentName =
471 dialog->ui->editObjectInstancesSegmentName->text().toStdString();
472 settings_agentInstancesSegmentName =
473 dialog->ui->editAgentInstancesSegmentName->text().toStdString();
474 settings_worldStateSegmentName = dialog->ui->editWorldStateSegmentName->text().toStdString();
475 settings_workingMemoryUpdatesTopic =
476 dialog->ui->editWorkingMemoryUpdatesTopic->text().toStdString();
477 settings_packages = dialog->ui->editAdditionalPackages->text().toStdString();
478 processPackages(settings_packages);
480 this->setInstanceName(dialog->ui->editWorkingMemoryGuiName->text());
484 WorkingMemoryController::loadSettings(QSettings* settings)
487 settings_priorMemoryName =
489 ->value(
"settings_priorMemoryName",
493 settings_workingMemoryName =
495 ->value(
"settings_workingMemoryName",
499 settings_objectInstancesSegmentName =
501 ->value(
"settings_objectInstancesSegmentName",
505 settings_agentInstancesSegmentName =
507 ->value(
"settings_agentInstancesSegmentName",
511 settings_worldStateSegmentName =
513 ->value(
"settings_worldStateSegmentName",
517 settings_workingMemoryUpdatesTopic =
519 ->value(
"settings_workingMemoryUpdatesTopic",
523 settings_packages = settings
524 ->value(
"settings_packages",
529 visuSetting_transparentExistanceCertatinty =
530 settings->value(
"settings_existenceCertaintyTransparency",
true).toBool();
531 ui.checkBox_existenceCertainty->setChecked(visuSetting_transparentExistanceCertatinty);
533 visuSetting_showObjectModels = settings->value(
"settings_showObjectModels",
true).toBool();
534 ui.cbShowObjectModels->setChecked(visuSetting_showObjectModels);
540 processPackages(settings_packages);
541 this->setInstanceName(instanceN);
543 settings->beginGroup(
"DebugDrawer");
544 if (!settings->allKeys().empty())
546 getSceneConfigDialog();
547 debugDrawerConfigWidget->loadSettings(settings);
549 settings->endGroup();
553 WorkingMemoryController::saveSettings(QSettings* settings)
556 settings->setValue(
"settings_priorMemoryName",
557 QString::fromStdString(settings_priorMemoryName));
558 settings->setValue(
"settings_workingMemoryName",
559 QString::fromStdString(settings_workingMemoryName));
560 settings->setValue(
"settings_objectInstancesSegmentName",
561 QString::fromStdString(settings_objectInstancesSegmentName));
562 settings->setValue(
"settings_agentInstancesSegmentName",
563 QString::fromStdString(settings_agentInstancesSegmentName));
564 settings->setValue(
"settings_worldStateSegmentName",
565 QString::fromStdString(settings_worldStateSegmentName));
566 settings->setValue(
"settings_workingMemoryUpdatesTopic",
567 QString::fromStdString(settings_workingMemoryUpdatesTopic));
568 settings->setValue(
"settings_packages", QString::fromStdString(settings_packages));
569 settings->setValue(
"instanceName", this->getInstanceName());
570 settings->setValue(
"settings_existenceCertaintyTransparency",
571 ui.checkBox_existenceCertainty->isChecked());
572 settings->setValue(
"settings_showObjectModels", ui.cbShowObjectModels->isChecked());
573 settings->beginGroup(
"DebugDrawer");
574 if (debugDrawerConfigWidget)
576 debugDrawerConfigWidget->saveSettings(settings);
578 settings->endGroup();
582 WorkingMemoryController::getScene()
594 WorkingMemoryController::getSceneConfigDialog(QWidget* parent)
597 if (!sceneConfigDialog)
599 sceneConfigDialog =
new QDialog(parent);
600 QVBoxLayout* verticalLayout =
new QVBoxLayout(sceneConfigDialog);
601 sceneConfigDialog->setLayout(verticalLayout);
603 debugDrawerConfigWidget =
605 verticalLayout->addWidget(debugDrawerConfigWidget);
607 QDialogButtonBox* buttonBox =
new QDialogButtonBox(sceneConfigDialog);
608 buttonBox->setOrientation(Qt::Horizontal);
609 buttonBox->setStandardButtons(QDialogButtonBox::Ok);
610 verticalLayout->addWidget(buttonBox);
611 QObject::connect(buttonBox, SIGNAL(accepted()), sceneConfigDialog, SLOT(accept()));
613 else if (!debugDrawerConfigWidget->getDebugDrawer())
615 debugDrawerConfigWidget->setDebugDrawer(debugDrawer);
617 return sceneConfigDialog;
621 WorkingMemoryController::connectSlots()
624 connect(ui.btnExport,
625 &QPushButton::clicked,
628 Qt::QueuedConnection);
631 connect(ui.cbShowObjectModels,
632 SIGNAL(toggled(
bool)),
634 SLOT(setupVisualization()),
635 Qt::QueuedConnection);
636 connect(ui.cbShowPositionUncertainty,
637 SIGNAL(toggled(
bool)),
639 SLOT(setupVisualization()),
640 Qt::QueuedConnection);
641 connect(ui.checkBoxVisuLayers,
642 SIGNAL(toggled(
bool)),
644 SLOT(showVisuLayers(
bool)),
645 Qt::QueuedConnection);
646 connect(ui.rbEllipse,
647 SIGNAL(toggled(
bool)),
649 SLOT(uncertaintyVisuToggled(
bool)),
650 Qt::QueuedConnection);
651 connect(ui.rbHeatMap,
652 SIGNAL(toggled(
bool)),
654 SLOT(uncertaintyVisuToggled(
bool)),
655 Qt::QueuedConnection);
656 connect(ui.rbHeatSurface,
657 SIGNAL(toggled(
bool)),
659 SLOT(uncertaintyVisuToggled(
bool)),
660 Qt::QueuedConnection);
661 connect(ui.spinEllipseRadius,
662 SIGNAL(valueChanged(
double)),
664 SLOT(setupVisualization()),
665 Qt::QueuedConnection);
667 connect(ui.spinDisplayThreshold,
668 SIGNAL(valueChanged(
double)),
670 SLOT(setupVisualization()),
671 Qt::QueuedConnection);
672 connect(ui.spinGridSize,
673 SIGNAL(valueChanged(
int)),
675 SLOT(setupVisualization()),
676 Qt::QueuedConnection);
679 SIGNAL(signalRebuildVisualization()),
681 SLOT(rebuildVisualization()),
682 Qt::QueuedConnection);
684 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
686 SLOT(updateEntityVisu(memoryx::EntityBasePtr, QString,
bool)),
687 Qt::QueuedConnection);
689 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
691 SLOT(updateEntityTree(memoryx::EntityBasePtr, QString,
bool)),
692 Qt::QueuedConnection);
694 connect(ui.checkBoxAgents,
695 SIGNAL(toggled(
bool)),
697 SLOT(showAgents(
bool)),
698 Qt::QueuedConnection);
699 connect(ui.checkBox_existenceCertainty,
700 SIGNAL(toggled(
bool)),
702 SLOT(existenceCertaintyToggled(
bool)),
703 Qt::QueuedConnection);
705 connect(ui.checkBoxShowPlane,
706 SIGNAL(toggled(
bool)),
708 SLOT(showPlane(
bool)),
709 Qt::QueuedConnection);
711 connect(
this, SIGNAL(signalAgentsRemoved()),
this, SLOT(agentsRemoved()), Qt::QueuedConnection);
713 this, SIGNAL(signalObjectRemoved()),
this, SLOT(objectsRemoved()), Qt::QueuedConnection);
715 SIGNAL(signalRefetchEntities()),
717 SLOT(refetchEntitiesFromMemory()),
718 Qt::QueuedConnection);
720 SIGNAL(signalClearEntities(
const std::string&)),
722 SLOT(clearEntitiesVisu(
const std::string&)),
723 Qt::QueuedConnection);
724 connect(
this, SIGNAL(signalRemoveTabs()),
this, SLOT(removeTabs()), Qt::QueuedConnection);
730 QString fi = QFileDialog::getSaveFileName(
731 Q_NULLPTR, tr(
"VRML 2.0 File"), QString(), tr(
"VRML Files (*.wrl)"));
732 std::string
s = std::string(fi.toLatin1());
743 std::unique_lock lock(*mutex3D);
745 SoOutput*
so =
new SoOutput();
746 if (!
so->openFile(
s.c_str()))
748 ARMARX_ERROR <<
"Could not open file " <<
s <<
" for writing." << std::endl;
752 so->setHeaderString(
"#VRML V2.0 utf8");
754 SoGroup* n =
new SoGroup;
756 n->addChild(rootVisu);
757 SoGroup* newVisu = CoinVisualizationFactory::convertSoFileChildren(n);
760 SoToVRML2Action tovrml2;
761 tovrml2.apply(newVisu);
762 SoVRMLGroup* newroot = tovrml2.getVRML2SceneGraph();
764 SoWriteAction wra(
so);
775 WorkingMemoryController::timerEvent(QTimerEvent*)
778 std::unique_lock lock(mutexEntities);
781 for (
const auto& agent : currentAgents)
783 updateAgent(agent.second);
788 WorkingMemoryController::treeItemDoubleClicked(QTreeWidgetItem* item,
int column)
795 msgBox.setText(
"Entity selected: id = " + item->text(1));
797 const std::string
id = item->text(1).toStdString();
804 WorkingMemoryController::refetchEntitiesFromMemory()
812 QObject::disconnect(
this,
813 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
815 SLOT(updateEntityTree(memoryx::EntityBasePtr, QString,
bool)));
816 QObject::disconnect(
this,
817 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
819 SLOT(updateEntityVisu(memoryx::EntityBasePtr, QString,
bool)));
821 debugDrawer->clearDebugLayer();
822 debugDrawer->clearLayer(
"ObjectUncertainties");
824 std::string selectedTabName =
"";
826 if (ui.tabWidget->count())
828 selectedTabName = ui.tabWidget->tabText(ui.tabWidget->currentIndex()).toStdString();
833 ARMARX_INFO <<
"Refetching data from WorkingmemoryPrxMemory";
843 for (
const auto& segmentName : segmentNames)
846 ARMARX_INFO <<
"Processing segment " << segmentName;
847 WorkingMemoryEntitySegmentBasePrx segmentPrx =
848 WorkingMemoryEntitySegmentBasePrx::uncheckedCast(
849 memoryPrx->getSegment(segmentName));
851 int index = addTab(segmentName);
853 if (segmentName == selectedTabName)
855 ui.tabWidget->setCurrentIndex(
index);
859 for (
const EntityBasePtr& entityBase : segmentPrx->getAllEntities())
861 ARMARX_INFO <<
"Processing entity " << entityBase->getName();
863 if (createEntity(entityBase, segmentName))
865 updateEntityTree(entityBase, QString(segmentName.c_str()),
false);
866 updateEntityVisu(entityBase, QString(segmentName.c_str()),
false);
879 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
881 SLOT(updateEntityTree(memoryx::EntityBasePtr, QString,
bool)),
882 Qt::QueuedConnection);
884 SIGNAL(entityChanged(memoryx::EntityBasePtr, QString,
bool)),
886 SLOT(updateEntityVisu(memoryx::EntityBasePtr, QString,
bool)),
887 Qt::QueuedConnection);
890 std::unique_lock lock(*mutex3D);
893 if (ui.checkBoxViewAll->isChecked() &&
main->getViewerWidget())
895 main->getViewerWidget()->viewer->viewAll();
898 emit signalRebuildVisualization();
902 WorkingMemoryController::startTimerInGUIThread()
906 startTimer((DEFAULT_ROBOT_UPDATE_STEP > 0 ? DEFAULT_ROBOT_UPDATE_STEP : 1));
926 for (
auto& [name, agentDef] : currentAgents)
928 if (agentDef.robot->getName() == pose->agent)
937 ARMARX_WARNING <<
"Agent " << pose->agent <<
" not found in current agents.";
938 obj.
setPose(agentInstancesProxy->convertToWorldPose(pose->agent, obj.
getPose()));
945 << obj.
getName() <<
" before: \n"
949 bool sync_successful =
true;
951 if (localizationTimestamp != agent->
timestamp)
956 localizationTimestamp.toMicroSeconds());
957 agent->
timestamp = localizationTimestamp;
961 <<
"Updating Pose of object with ts " << localizationTimestamp
962 <<
" with agent " << pose->agent <<
"with pose: \n"
973 <<
" could not successfully be synced. Try to calculate pose with "
974 "current agent of agentSegment.";
975 obj.
setPose(agentInstancesProxy->convertToWorldPose(pose->agent, obj.
getPose()));
979 << obj.
getName() <<
" after: \n"
997 std::unique_lock lock(mutexEntities);
999 ARMARX_DEBUG <<
"Updating object entity " << obj->getName();
1004 if (obj->getPosition()->frame != obj->getOrientation()->frame)
1006 ARMARX_ERROR <<
"Frame of pos and orientation are not the same of object "
1007 << obj->getName() <<
" pos frame: " << obj->getPosition()->frame
1008 <<
" ori frame: " << obj->getOrientation()->frame;
1011 IceUtil::Time localizationTimestamp = obj->getLocalizationTimestamp();
1012 if (lastUpdated.find(obj->getName()) == lastUpdated.end() ||
1013 localizationTimestamp.toMicroSeconds() > lastUpdated.at(obj->getName()).first)
1016 convertObjectInstanceToGlobalPose(*obj);
1017 lastUpdated[obj->getName()] =
1018 std::make_pair(localizationTimestamp.toMicroSeconds(), obj->getPose());
1022 obj->setPose(lastUpdated.at(obj->getName()).second);
1025 if (showObjInstanceUncertainties)
1028 auto uncertainty = obj->getPositionUncertainty();
1032 float variance =
std::min(5000.0f, uncertainty->getVarianceScalar());
1033 auto pos = obj->getPosition();
1037 debugDrawer->setSphereVisu(
1038 "ObjectUncertainties",
1039 obj->getName() + obj->getId() +
"_uncertainty",
1041 armarx::DrawColor{std::min(1.0f, variance / 1000),
1042 std::max(0.0f, 1 - variance / 1000),
1050 <<
"Not global position for " << obj->getName() + obj->getId();
1056 <<
"No position uncertainty for " << obj->getName() + obj->getId();
1062 <<
"Position uncertainty is not shown for "
1063 << obj->getName() + obj->getId();
1068 ARMARX_ERROR <<
"Execption during update object data...";
1073 WorkingMemoryController::createEntity(memoryx::EntityBasePtr entity,
const std::string& entityType)
1076 const std::string
id = entity->getId();
1077 if (entityType == settings_objectInstancesSegmentName ||
1078 entityType == settings_worldStateSegmentName)
1082 std::unique_lock lock(mutexEntities);
1083 EntityEntryMap::iterator itEntity = currentEntities.find(
id);
1084 found = itEntity != currentEntities.end();
1090 ARMARX_INFO <<
"Creating entity instance, id:" <<
id <<
", type " << entityType;
1093 EntityPtr e = EntityPtr::dynamicCast(entity);
1101 convertObjectInstanceToGlobalPose(*obj);
1115 std::unique_lock lock(mutexEntities);
1116 currentEntities[id] = o;
1124 else if (entityType == settings_agentInstancesSegmentName)
1129 std::unique_lock lock(mutexEntities);
1130 AgentEntryMap::iterator itAgent = currentAgents.find(
id);
1131 found = itAgent != currentAgents.end();
1135 ARMARX_INFO <<
"Creating agent entity, id:" <<
id <<
" name: " << entity->getName()
1136 <<
", type " << entity->ice_id();
1138 if (!agent || !addAgent(agent))
1154 WorkingMemoryController::updateEntityVisu(memoryx::EntityBasePtr entity,
1159 const std::string
id = entity->getId();
1160 const std::string typeS = entityType.toStdString();
1161 ARMARX_DEBUG <<
"Updating visualization, id:" <<
id <<
", type " << typeS;
1163 std::unique_lock lock1(mutexEntities);
1164 std::unique_lock lock2(*mutex3D);
1167 if (typeS == settings_objectInstancesSegmentName || typeS == settings_worldStateSegmentName)
1170 EntityEntryMap::iterator itEntity = currentEntities.find(
id);
1172 if (itEntity == currentEntities.end())
1174 ARMARX_INFO <<
"Could not find currentEntity entry for object with id = " <<
id
1175 <<
", building new entry";
1176 if (!createEntity(entity, typeS))
1181 itEntity = currentEntities.find(
id);
1187 createObjectVisualization(itEntity->second);
1192 if (itEntity->second.simoxWrapper)
1194 itEntity->second.simoxWrapper->updateFromEntity(entity);
1196 if (visuSetting_transparentExistanceCertatinty)
1199 itEntity->second.obj->getExistenceCertainty());
1201 << itEntity->second.manipObj->getName() <<
":"
1202 << itEntity->second.obj->getExistenceCertainty();
1206 if (itEntity->second.cv)
1208 itEntity->second.cv->setTransparency(t);
1215 else if (typeS == settings_agentInstancesSegmentName)
1218 AgentEntryMap::iterator itAgent = currentAgents.find(
id);
1220 if (itAgent == currentAgents.end())
1222 ARMARX_VERBOSE <<
"Could not find currentAgents entry for object with id = " <<
id
1223 <<
", loading agent data...";
1224 if (!createEntity(entity, typeS))
1232 createAgentVisualisation(
id);
1242 ARMARX_DEBUG <<
"Updating visualization END, id:" <<
id <<
", type " << typeS;
1254 std::unique_lock lock(*mutex3D);
1258 SoNode*
s =
nullptr;
1262 o.
cv = o.
manipObj->getVisualization<VirtualRobot::CoinVisualization>();
1266 if (visuSetting_transparentExistanceCertatinty)
1273 o.
cv->setTransparency(t);
1275 s = o.
cv->getCoinVisualization();
1282 CoinVisualizationFactoryPtr coinVisFactory(
new CoinVisualizationFactory());
1284 s = std::dynamic_pointer_cast<VirtualRobot::CoinVisualizationNode>(
1285 coinVisFactory->createCoordSystem(1.0, &name))
1286 ->getCoinVisualization();
1289 if (
s && objectsVisu)
1293 if (objectsVisu->findChild(
s) < 0)
1295 objectsVisu->addChild(
s);
1304 if (objWrapper && objWrapper->getManipulationObject())
1307 << objWrapper->getManipulationObject()->getName();
1319 std::unique_lock lock(*mutex3D);
1322 objWrapper->refreshVisuPose();
1326 WorkingMemoryController::updateEntityTree(memoryx::EntityBasePtr entity,
1333 ARMARX_DEBUG <<
"updateEntityTree:" << entity->getName() <<
",type:" << entityType;
1335 EntityPtr obj = EntityPtr::dynamicCast(entity);
1336 std::string entityTypeS = entityType.toStdString();
1338 if (!segmentTabs.count(entityTypeS))
1340 ARMARX_INFO <<
"Segment " << entityTypeS <<
" not yet displayed";
1344 QString objName(obj->getName().c_str());
1345 QString objId = QString::fromStdString(entity->getId());
1347 QList<QTreeWidgetItem*> oldItems;
1348 QTreeWidgetItem* objItem;
1350 QTreeWidget* tree = segmentTabs.at(entityTypeS).tree;
1351 oldItems = tree->findItems(objId, Qt::MatchFixedString, 1);
1354 objItem->setText(0, objName);
1355 objItem->setText(1, objId);
1364 QList<QTreeWidgetItem*> oldAttrs = objItem->takeChildren();
1366 while (!oldAttrs.empty())
1368 delete oldAttrs.takeFirst();
1371 QBrush evenRow(QColor(217, 255, 226));
1372 QBrush oddRow(Qt::white);
1374 memoryx::NameList attrNames = obj->getAttributeNames();
1376 for (
const auto& attrName : attrNames)
1381 attrItem->setText(0, QString::fromStdString(attrName));
1384 auto uncertainty = obj->getAttribute(attrName)->getUncertainty();
1388 QString attrValStr(attrValue->getOutputValueOnly().c_str());
1393 QString(
"\nUncertainty: ") + QString::fromStdString(uncertainty->output());
1396 attrItem->setText(1, attrValStr);
1400 attrItem->setText(1,
"Missing/unsupported value");
1404 QBrush& bgBrush = (i % 2 == 0) ? evenRow : oddRow;
1405 attrItem->setBackground(0, bgBrush);
1406 attrItem->setBackground(1, bgBrush);
1409 ARMARX_DEBUG <<
"updateEntityTree END:" << entity->getName() <<
",type:" << entityType;
1413 WorkingMemoryController::clearWorkingMemory()
1417 QMessageBox(QMessageBox::Information,
1418 "Clearing Working Memory",
1419 "Do you really want to clear the content of Working Memory?",
1423 std::unique_lock lock(mutexEntities);
1431 ARMARX_ERROR <<
"Execption during clear working memory...";
1437 WorkingMemoryController::showVisuLayers(
bool show)
1444 debugDrawer->enableAllLayers();
1448 debugDrawer->disableAllLayers();
1454 WorkingMemoryController::showAgents(
bool show)
1457 std::unique_lock lock(*mutex3D);
1459 if (!rootVisu || !agentsVisu)
1466 if (rootVisu->findChild(agentsVisu) < 0)
1468 rootVisu->addChild(agentsVisu);
1473 if (rootVisu->findChild(agentsVisu) >= 0)
1475 rootVisu->removeChild(agentsVisu);
1481 WorkingMemoryController::showPlane(
bool show)
1484 std::unique_lock lock(*mutex3D);
1486 if (!rootVisu || !planeVisu)
1493 if (rootVisu->findChild(planeVisu) < 0)
1495 rootVisu->addChild(planeVisu);
1500 if (rootVisu->findChild(planeVisu) >= 0)
1502 rootVisu->removeChild(planeVisu);
1508 WorkingMemoryController::clearEntitiesData()
1511 std::unique_lock lock(mutexEntities);
1515 for (
auto& entity : currentEntities)
1517 deletedEntities[entity.first] = entity.second;
1520 currentEntities.clear();
1522 for (
auto& entity : currentAgents)
1524 deletedAgents[entity.first] = entity.second;
1527 currentAgents.clear();
1531 WorkingMemoryController::clearEntitiesVisu(
const std::string& segmentName)
1534 std::unique_lock lock(mutexEntities);
1537 if (segmentName ==
"" || segmentName == settings_objectInstancesSegmentName)
1541 for (
auto& entity : deletedEntities)
1543 entity.second.manipObj.reset();
1545 if (entity.second.simoxWrapper)
1547 entity.second.simoxWrapper->setEntity(EntityBasePtr());
1548 entity.second.simoxWrapper =
nullptr;
1552 deletedEntities.clear();
1555 if (segmentName ==
"" || segmentName == settings_agentInstancesSegmentName)
1558 deletedAgents.clear();
1561 std::unique_lock lock2(*mutex3D);
1565 agentsVisu->removeAllChildren();
1570 objectsVisu->removeAllChildren();
1577 WorkingMemoryController::rebuildVisualization()
1580 ARMARX_INFO <<
"Rebuilding complete visualization";
1581 std::unique_lock lock1(mutexEntities);
1582 std::unique_lock lock2(*mutex3D);
1587 objectsVisu->removeAllChildren();
1590 ARMARX_INFO <<
"Rebuilding entity visualization for " << currentEntities.size()
1593 for (
auto& entity : currentEntities)
1595 ARMARX_VERBOSE <<
"Rebuilding entity visualization for: " << entity.first;
1596 createObjectVisualization(entity.second);
1602 ARMARX_INFO <<
"Rebuilding robot visualization for " << currentAgents.size() <<
" agents";
1603 agentsVisu->removeAllChildren();
1605 for (
const auto& agent : currentAgents)
1607 ARMARX_VERBOSE <<
"Rebuilding robot visualization for: " << agent.first;
1610 createAgentVisualisation(agent.first);
1616 if (
main &&
main->getViewerWidget())
1618 auto l =
main->getViewerWidget()->viewer->getScopedLock();
1620 if (ui.checkBoxViewAll->isChecked() &&
main->getViewerWidget())
1622 main->getViewerWidget()->viewer->viewAll();
1628 WorkingMemoryController::uncertaintyVisuToggled(
bool isChecked)
1634 setupVisualization();
1639 WorkingMemoryController::existenceCertaintyToggled(
bool isChecked)
1642 visuSetting_transparentExistanceCertatinty = isChecked;
1643 setupVisualization();
1647 WorkingMemoryController::setupVisualization()
1650 std::unique_lock lock(mutexEntities);
1652 for (
const auto& entity : currentEntities)
1654 setupEntityVisu(entity.second);
1657 showObjInstanceUncertainties = ui.cbShowPositionUncertainty->isChecked();
1658 visuSetting_showObjectModels = ui.cbShowObjectModels->isChecked();
1660 if (!showObjInstanceUncertainties)
1662 debugDrawer->clearLayer(
"ObjectUncertainties");
1683 std::unique_lock lock(*mutex3D);
1688 ARMARX_VERBOSE <<
"Only ObjectInstances are currently supported";
1693 ui.cbShowObjectModels->isChecked() || !obj->getPositionAttribute()->getUncertainty();
1694 bool showUncertaintyVisu = ui.cbShowPositionUncertainty->isChecked();
1696 if (showUncertaintyVisu)
1700 EntityWrappers::SimoxObjectWrapper::eEllipse;
1702 if (ui.rbEllipse->isChecked())
1704 visuType = EntityWrappers::SimoxObjectWrapper::eEllipse;
1706 else if (ui.rbHeatMap->isChecked())
1708 visuType = EntityWrappers::SimoxObjectWrapper::eHeatMap;
1710 else if (ui.rbHeatSurface->isChecked())
1712 visuType = EntityWrappers::SimoxObjectWrapper::eHeatSurface;
1714 objWrapper.
simoxWrapper->setUncertaintyVisuType(visuType);
1715 objWrapper.
simoxWrapper->setUncertaintyVisuParams(ui.spinEllipseRadius->value(),
1716 ui.spinMinVariance->value(),
1717 expf(-ui.spinDisplayThreshold->value()),
1718 ui.spinGridSize->value());
1724 objWrapper.
manipObj->setupVisualization(showMainVisu, showUncertaintyVisu);
1729 WorkingMemoryController::reportEntityCreated(
const std::string& segmentName,
1730 const EntityBasePtr& entityBase,
1731 const Ice::Current&)
1734 std::unique_lock lock(mutexEntities);
1739 <<
", entity:" << entityBase->getName();
1741 EntityPtr entity = EntityPtr::dynamicCast(entityBase);
1744 QString entityType(segmentName.c_str());
1751 updateObjectData(obj);
1763 ARMARX_VERBOSE <<
"reportEntityCreated: entity type not supported: " << segmentName;
1767 emit entityChanged(entityBase, entityType,
true);
1771 ARMARX_ERROR <<
"Execption during reportEntityCreated...";
1776 WorkingMemoryController::reportEntityUpdated(
const std::string& segmentName,
1777 const EntityBasePtr& entityBaseOld,
1778 const EntityBasePtr& entityBaseNew,
1779 const Ice::Current&)
1782 (void)entityBaseOld;
1784 ARMARX_DEBUG <<
"entity " << entityBaseNew->getName() <<
" got updated";
1785 std::unique_lock lock(mutexEntities);
1791 EntityPtr entity = EntityPtr::dynamicCast(entityBaseNew);
1793 QString entityId(entity->getId().c_str());
1794 QString entityType(segmentName.c_str());
1801 updateObjectData(obj);
1810 auto it = currentAgents.find(entityId.toStdString());
1812 if (it == currentAgents.end())
1819 emit entityChanged(entityBaseNew, entityType,
false);
1823 ARMARX_ERROR <<
"Exception during reportEntityUpdated...";
1825 ARMARX_DEBUG <<
"entity " << entityBaseNew->getName() <<
" got updated END";
1829 WorkingMemoryController::reportEntityRemoved(
const std::string& segmentName,
1830 const EntityBasePtr& entity,
1831 const Ice::Current&)
1834 std::unique_lock lock(mutexEntities);
1838 std::string entityId = entity->getId();
1847 auto it = currentEntities.find(entityId);
1849 if (it != currentEntities.end())
1851 ARMARX_INFO <<
"Object removed: id = " << entityId;
1852 deletedEntities[entityId] = currentEntities[entityId];
1853 currentEntities.erase(it);
1854 QMetaObject::invokeMethod(
this,
"objectsRemoved");
1858 ARMARX_INFO <<
"Object not present, id = " << entityId;
1869 auto it = currentAgents.find(entityId);
1871 if (it != currentAgents.end())
1873 ARMARX_INFO <<
"Object removed (agent): id = " << entityId;
1874 deletedAgents[entityId] = currentAgents[entityId];
1875 currentAgents.erase(it);
1876 QMetaObject::invokeMethod(
this,
"agentsRemoved");
1880 ARMARX_INFO <<
"agent not present, id = " << entityId;
1885 QMetaObject::invokeMethod(
this,
1886 "removeEntityInTree",
1887 Q_ARG(std::string, entityId),
1888 Q_ARG(std::string, segmentName));
1894 ARMARX_ERROR <<
"Execption during reportEntityRemoved...";
1900 WorkingMemoryController::objectsRemoved()
1903 std::unique_lock lock(mutexEntities);
1904 std::unique_lock lock2(*mutex3D);
1906 for (
auto& e : deletedEntities)
1908 if (objectsVisu && e.second.visualization &&
1909 objectsVisu->findChild(e.second.visualization) >= 0)
1911 objectsVisu->removeChild(e.second.visualization);
1914 removeEntityInTree(e.first, settings_objectInstancesSegmentName);
1917 deletedEntities.clear();
1921 WorkingMemoryController::agentsRemoved()
1924 std::unique_lock lock(mutexEntities);
1925 std::unique_lock lock2(*mutex3D);
1928 for (
auto& e : deletedAgents)
1931 if (agentsVisu && e.second.visualization &&
1932 agentsVisu->findChild(e.second.visualization) >= 0)
1934 agentsVisu->removeChild(e.second.visualization);
1937 removeEntityInTree(e.first, settings_agentInstancesSegmentName);
1940 deletedAgents.clear();
1944 WorkingMemoryController::reportSnapshotLoaded(
const std::string& segmentName,
const Ice::Current&)
1951 WorkingMemoryController::reportSnapshotCompletelyLoaded(
const Ice::Current&)
1954 std::unique_lock lock(mutexEntities);
1957 emit signalRefetchEntities();
1962 WorkingMemoryController::reportMemoryCleared(
const std::string& segmentName,
const Ice::Current&)
1965 std::unique_lock lock(mutexEntities);
1968 ARMARX_INFO <<
"segment " << segmentName <<
" cleared";
1972 if (segmentName == settings_objectInstancesSegmentName)
1975 for (
auto& entity : currentEntities)
1977 deletedEntities[entity.first] = entity.second;
1980 currentEntities.clear();
1983 if (segmentName == settings_agentInstancesSegmentName)
1985 for (
auto& entity : currentAgents)
1987 deletedAgents[entity.first] = entity.second;
1990 currentAgents.clear();
1998 ARMARX_ERROR <<
"Execption during reportMemoryCleared...";
2001 emit signalClearEntities(segmentName);
2008 this->mutex3D = mutex3D;
2012 debugDrawer->setMutex(mutex3D);
2017 WorkingMemoryController::removeEntityInTree(
const std::string entityId,
2018 const std::string entityType)
2020 ARMARX_INFO <<
"Remove qt element: " << entityId <<
", " << entityType;
2021 QString entityItemId(entityId.c_str());
2022 QTreeWidgetItem* entityItem =
nullptr;
2024 if (segmentTabs.count(entityType))
2026 QList<QTreeWidgetItem*> items =
2027 segmentTabs.at(entityType).tree->findItems(entityItemId, Qt::MatchFixedString, 1);
2028 if (items.size() > 0)
2030 entityItem = items.at(0);
2035 ARMARX_WARNING <<
"Undefined entity type: " << entityType << std::endl;
2041 ARMARX_WARNING <<
"Could not determine Qt tree item for entity " << entityId <<
", type "
2046 QList<QTreeWidgetItem*> entityAttributes = entityItem->takeChildren();
2048 while (!entityAttributes.empty())
2050 delete entityAttributes.takeFirst();
2058 WorkingMemoryController::addTab(std::string segmentName)
2063 if (!segmentTabs.count(segmentName))
2067 QWidget* tab =
new QWidget();
2068 QGridLayout* layout =
new QGridLayout();
2069 tab->setLayout(layout);
2072 QTreeWidget* tree =
new QTreeWidget();
2073 tree->setColumnCount(2);
2077 tree->setHeaderLabels(header);
2078 tree->header()->setDefaultSectionSize(200);
2079 tree->setSortingEnabled(
true);
2080 tree->sortByColumn(0, Qt::SortOrder::AscendingOrder);
2081 index = ui.tabWidget->addTab(tab, QString::fromStdString(segmentName));
2084 layout->addWidget(tree);
2087 segmentTabs.insert(std::pair<std::string, struct SegmentTab>(segmentName, et));
2090 SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
2092 SLOT(treeItemDoubleClicked(QTreeWidgetItem*,
int)));
2096 index = segmentTabs.at(segmentName).tabIndex;
2103 WorkingMemoryController::removeTab(std::string segmentName)
2106 if (segmentTabs.count(segmentName))
2109 struct SegmentTab tab = segmentTabs.at(segmentName);
2110 QObject::disconnect(tab.
tree,
2111 SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
2113 SLOT(treeItemDoubleClicked(QTreeWidgetItem*,
int)));
2115 ui.tabWidget->removeTab(tab.
tabIndex);
2116 tab.
tree->~QTreeWidget();
2117 tab.
tab->~QWidget();
2118 segmentTabs.erase(segmentName);
2123 WorkingMemoryController::removeTabs()
2127 for (
auto& entry : segmentTabs)
2129 removeTab(entry.first);
2134 WorkingMemoryController::getCustomTitlebarWidget(QWidget* parent)
2139 if (parent != customToolbar->parent())
2141 customToolbar->setParent(parent);
2144 return customToolbar;
2147 customToolbar =
new QToolBar(parent);
2148 customToolbar->setIconSize(QSize(16, 16));
2150 customToolbar->addAction(QIcon(
":/icons/view-refresh-7.png"),
2153 SLOT(refetchEntitiesFromMemory()));
2154 customToolbar->addAction(QIcon(
":/icons/dialog-close.ico"),
2155 "Clear Working Memory",
2157 SLOT(clearWorkingMemory()));
2159 return customToolbar;
2163 WorkingMemoryController::createAgentVisualisation(
const std::string entityId)
2166 std::unique_lock lock(mutexEntities);
2167 if (currentAgents.find(entityId) == currentAgents.end() || !currentAgents[entityId].robot)
2171 currentAgents[entityId].visualization =
2172 VirtualRobot::CoinVisualizationFactory::getCoinVisualization(
2173 currentAgents[entityId].robot, VirtualRobot::SceneObject::Full,
false);
2175 if (!currentAgents[entityId].visualization)
2177 ARMARX_WARNING <<
"createAgentVisualisation: could not retrieve visualisation from robot "
2178 << currentAgents[entityId].robot->getName() <<
"; using ellipse";
2179 currentAgents[entityId].visualization =
2180 VirtualRobot::CoinVisualizationFactory::CreateEllipse(0.5, 0.5, 1,
nullptr,
false);
2184 if (agentsVisu && agentsVisu->findChild(currentAgents[entityId].visualization) < 0)
2186 agentsVisu->addChild(currentAgents[entityId].visualization);
2189 updateAgentVisualisation(entityId);
2196 std::unique_lock lock(mutexEntities);
2202 ARMARX_ERROR <<
"addAgent: entity is not of type AgentInstance";
2206 const std::string agentId = agent->getId();
2209 if (!agent->hasAttribute(
"agentFilePath"))
2211 ARMARX_INFO <<
"No filepath available for agent " << agent->getName();
2214 std::string agentFilePath = agent->getAgentFilePath();
2215 ARMARX_INFO <<
"Adding agent " << agent->getName() <<
" agentId:" << agentId
2216 <<
", filepath: " << agentFilePath;
2218 if (sharedRobotInterfaceProxy)
2221 <<
" proxy: " << sharedRobotInterfaceProxy->ice_toString();
2225 ARMARX_INFO << agent->getName() <<
" proxy nullptr";
2229 if (!sharedRobotInterfaceProxy &&
2230 !agent->getStringifiedSharedRobotInterfaceProxy().empty())
2232 sharedRobotInterfaceProxy =
2233 armarx::SharedRobotInterfacePrx::checkedCast(iceCommunicator->stringToProxy(
2234 agent->getStringifiedSharedRobotInterfaceProxy()));
2237 catch (std::exception& e)
2239 ARMARX_ERROR <<
"Failed to get robot state proxy for agent " << agent->getName() <<
": "
2243 if (sharedRobotInterfaceProxy)
2246 if (sharedRobotInterfaceProxy->getRobotStateComponent())
2249 sharedRobotInterfaceProxy->getRobotStateComponent()->getArmarXPackages();
2251 for (
auto& p : packages)
2262 ARMARX_INFO <<
"RobotStateComponent proxy is empty";
2268 ARMARX_ERROR <<
"Could not find robot file " << agentFilePath;
2271 <<
" additional paths";
2286 sharedRobotInterfaceProxy, agentFilePath, sharedRobotInterfaceProxy->getScaling());
2287 robotDefinition.
robot->setName(sharedRobotInterfaceProxy->getName());
2294 robotDefinition.
robot.reset();
2295 ARMARX_ERROR <<
"addAgent: error while loading robot from file: "
2300 ARMARX_VERBOSE <<
"addAgent: loaded robot from file " << agentFilePath;
2302 robotDefinition.
robotStateComponent = sharedRobotInterfaceProxy->getRobotStateComponent();
2306 currentAgents[agentId] = robotDefinition;
2317 WorkingMemoryController::updateAgentVisualisation(
const std::string entityId)
2320 std::unique_lock lock(mutexEntities);
2321 auto entity = agentInstancesProxy->getEntityById(entityId);
2322 updateAgentVisualisation(entity);
2326 WorkingMemoryController::updateAgentVisualisation(
const EntityBasePtr entity)
2329 std::unique_lock lock(*mutex3D);
2333 if (!agent || currentAgents.find(entity->getId()) == currentAgents.end())
2338 auto entityId = entity->getId();
2345 matrix.block(0, 3, 3, 1) = agentPosition->toEigen();
2350 if (agentOrientation)
2352 matrix.block(0, 0, 3, 3) = agentOrientation->toEigen();
2355 auto& agentInstance = currentAgents[entityId];
2356 agentInstance.robot->setGlobalPose(matrix);
2363 std::unique_lock lock(mutexEntities);
2364 std::unique_lock lock2(*mutex3D);
2371 std::map<std::string, float> jointValues;
2372 std::vector<RobotNodePtr> storeNodesRemote;