36 usingProxy(getProperty<std::string>(
"ViewSelectionName").getValue());
37 usingTopic(getProperty<std::string>(
"ViewSelectionName").getValue() +
"Observer");
38 usingProxy(getProperty<std::string>(
"RobotStateComponentName").getValue());
39 usingProxy(getProperty<std::string>(
"HeadIKUnitName").getValue());
40 usingProxy(getProperty<std::string>(
"WorkingMemoryName").getValue());
41 usingProxy(getProperty<std::string>(
"PriorKnowledgeName").getValue());
43 randomNoiseLevel = getProperty<float>(
"RandomNoiseLevel").getValue();
45 cameraFrameName = getProperty<std::string>(
"CameraFrameName").getValue();
46 headFrameName = getProperty<std::string>(
"HeadFrameName").getValue();
47 centralHeadTiltAngle = getProperty<float>(
"CentralHeadTiltAngle").getValue();
49 halfCameraOpeningAngle = getProperty<float>(
"HalfCameraOpeningAngle").getValue();
50 deviationFromCameraCenterFactor = 0.5f;
52 std::string graphFileName =
"RobotAPI/ViewSelection/graph40k.gra";
61 << saliencyEgosphereGraph->
getNodes()->size() <<
"nodes";
64 nodeVisitedForObject.resize(saliencyEgosphereGraph->
getNodes()->size());
67 randomNoiseGraph->
set(0);
77 offsetToHeadCenter << 0, 0, 150;
80 &ObjectLocalizationSaliency::process,
81 1000.f / getProperty<float>(
"UpdateFrequency").getValue(),
83 "ViewSelectionCalculation",
85 processorTask->setDelayWarningTolerance(2600);
91 viewSelection = getProxy<ViewSelectionInterfacePrx>(
92 getProperty<std::string>(
"ViewSelectionName").getValue());
93 viewSelection = viewSelection->ice_compress(
true);
94 robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(
95 getProperty<std::string>(
"RobotStateComponentName").getValue());
97 memoryProxy = getProxy<memoryx::WorkingMemoryInterfacePrx>(
98 getProperty<std::string>(
"WorkingMemoryName").getValue());
99 priorKnowledge = getProxy<memoryx::PriorKnowledgeInterfacePrx>(
100 getProperty<std::string>(
"PriorKnowledgeName").getValue());
101 objectInstancesProxy = memoryx::ObjectInstanceMemorySegmentBasePrx::uncheckedCast(
102 memoryProxy->getSegment(
"objectInstances"));
103 objectClassesProxy = memoryx::ObjectClassMemorySegmentBasePrx::uncheckedCast(
104 memoryProxy->getSegment(
"objectClasses"));
108 auto objClassSegment = priorKnowledge->getObjectClassesSegment();
109 std::vector<std::string> ids = objClassSegment->getAllEntityIds();
112 for (std::string&
id : ids)
115 memoryx::ObjectClassPtr::dynamicCast(objClassSegment->getEntityById(
id));
122 if (!wrapper->getRecognitionMethod().empty() &&
123 wrapper->getRecognitionMethod() !=
"<none>")
125 recognizableObjClasses[objClass->getName()] = objClass;
126 ARMARX_INFO << objClass->getName() <<
" has " << wrapper->getRecognitionMethod();
131 if (viewSelection->isEnabledAutomaticViewSelection())
147 delete graphLookupTable;
148 delete saliencyEgosphereGraph;
149 delete randomNoiseGraph;
153 ObjectLocalizationSaliency::process()
155 std::unique_lock<std::mutex> lock(mutex);
159 delta -= IceUtil::Time::milliSeconds(50);
163 if (delta > IceUtil::Time::seconds(0))
171 generateObjectLocalizationSaliency();
173 SaliencyMapBasePtr objectLocalizationSaliencyMap =
new SaliencyMapBase();
174 objectLocalizationSaliencyMap->name =
"objectLocalizationSaliency";
175 saliencyEgosphereGraph->
graphToVec(objectLocalizationSaliencyMap->map);
176 viewSelection->updateSaliencyMap(objectLocalizationSaliencyMap);
178 SaliencyMapBasePtr randomNoise =
new SaliencyMapBase();
179 randomNoise->name =
"randomNoise";
180 randomNoiseGraph->
graphToVec(randomNoise->map);
181 viewSelection->updateSaliencyMap(randomNoise);
185 std::lock_guard<std::mutex> lock2(mutex);
187 lastDiff = (stopTime - startTime);
191 ObjectLocalizationSaliency::generateObjectLocalizationSaliency()
195 saliencyEgosphereGraph->
set(0);
197 for (
size_t i = 0; i < saliencyEgosphereGraph->
getNodes()->size(); i++)
199 nodeVisitedForObject.at(i) = -1;
204 std::vector<memoryx::ObjectInstancePtr> localizableObjects;
205 std::vector<FramedPositionPtr> localizableObjectPositions;
206 float maxObjectDistance = getProperty<float>(
"MaxObjectDistance").getValue();
210 int numberOfLostObjects = 0;
212 memoryx::EntityBaseList objectInstances = objectInstancesProxy->getAllEntities();
214 for (
size_t i = 0; i < objectInstances.size(); i++)
217 memoryx::ObjectInstancePtr::dynamicCast(objectInstances.at(i));
221 if (recognizableObjClasses.count(object->getMostProbableClass()) > 0 &&
222 !object->getPosition()->getFrame().empty())
224 if (objectClassesProxy->hasEntityByName(
226 ->getMostProbableClass()))
229 position->changeFrame(robot, headFrameName);
230 float objDist = position->toEigen().norm();
232 if (objDist <= maxObjectDistance)
234 localizableObjects.push_back(
object);
235 localizableObjectPositions.push_back(position);
237 if (object->getExistenceCertainty() < 0.5)
239 numberOfLostObjects++;
244 ARMARX_DEBUG <<
"Discarding " <<
object->getName() <<
" which is at "
245 << position->output() <<
" ObjectDistance " << objDist;
254 if (randomNoiseLevel > 0)
256 generateRandomNoise(localizableObjects, objectInstances);
260 const float probabilityToAddALostObject =
261 (numberOfLostObjects > 0)
262 ? getProperty<float>(
"ProbabilityToLookForALostObject").getValue() / numberOfLostObjects
266 for (
size_t i = 0; i < localizableObjects.size(); i++)
270 if ((object->getExistenceCertainty() >= 0.5) ||
271 (rand() % 100000 < 100000 * probabilityToAddALostObject))
276 memoryx::MultivariateNormalDistributionPtr::dynamicCast(
277 object->getPositionUncertainty());
279 if (!uncertaintyGaussian)
281 uncertaintyGaussian =
285 float priority =
object->getLocalizationPriority() / 100.0;
286 float saliency = priority * log(1.0 + uncertaintyGaussian->getVarianceScalar());
291 float modifiedHalfCameraOpeningAngle = halfCameraOpeningAngle;
292 float objDist = position->toEigen().norm();
293 float shortDistance = 0.5f * maxObjectDistance;
294 if (objDist < shortDistance)
296 modifiedHalfCameraOpeningAngle = (objDist - 0.1f * shortDistance) /
297 (0.9f * shortDistance) *
298 halfCameraOpeningAngle;
299 modifiedHalfCameraOpeningAngle =
std::max(0.0f, modifiedHalfCameraOpeningAngle);
303 Eigen::Vector3f vec = position->toEigen() - offsetToHeadCenter;
305 int closestNodeIndex =
307 addSaliencyRecursive(closestNodeIndex,
309 positionInSphereCoordinates,
311 modifiedHalfCameraOpeningAngle);
318 ObjectLocalizationSaliency::addSaliencyRecursive(
const int currentNodeIndex,
319 const float saliency,
321 const int objectIndex,
322 const float maxDistanceOnArc)
327 float normalizedDistance =
330 saliencyEgosphereGraph->
getNodes()->at(currentNodeIndex)->getPosition()) /
337 (1.0f - deviationFromCameraCenterFactor * normalizedDistance * normalizedDistance) *
340 ->setIntensity(newValue);
343 nodeVisitedForObject.at(currentNodeIndex) = objectIndex;
348 for (
size_t i = 0; i < saliencyEgosphereGraph->
getNodeAdjacency(currentNodeIndex)->size(); i++)
350 neighbourIndex = saliencyEgosphereGraph->
getNodeAdjacency(currentNodeIndex)->at(i);
352 if (nodeVisitedForObject.at(neighbourIndex) != objectIndex)
356 saliencyEgosphereGraph->
getNodes()->at(neighbourIndex)->getPosition()) <=
359 addSaliencyRecursive(
360 neighbourIndex, saliency, objectSphereCoord, objectIndex, maxDistanceOnArc);
364 nodeVisitedForObject.at(neighbourIndex) = objectIndex;
371 ObjectLocalizationSaliency::generateRandomNoise(
372 std::vector<memoryx::ObjectInstancePtr>& localizableObjects,
373 memoryx::EntityBaseList& objectInstances)
377 memoryx::EntityBaseList requestedObjectClasses = objectClassesProxy->getAllEntities();
379 bool unlocalizedObjectExists =
false;
380 for (
size_t i = 0; i < requestedObjectClasses.size(); i++)
382 bool isInInstancesList =
false;
383 for (
size_t j = 0; j < objectInstances.size(); j++)
385 if (requestedObjectClasses.at(i)->getName() ==
386 memoryx::ObjectInstanceBasePtr::dynamicCast(objectInstances.at(j))
387 ->getMostProbableClass())
389 isInInstancesList =
true;
393 if (!isInInstancesList)
395 unlocalizedObjectExists =
true;
400 if (unlocalizedObjectExists)
402 if (localizableObjects.size() == 0)
404 ARMARX_DEBUG <<
"There are objects requested, and none of them has been localized yet";
405 setRandomNoise(centralHeadTiltAngle + 30, 3.0f);
409 ARMARX_DEBUG <<
"There are objects requested, and some but not all of them have been "
411 setRandomNoise(centralHeadTiltAngle + 20, 2.0f);
416 ARMARX_DEBUG <<
"There are no requested objects that were not localized yet. "
417 "requestedObjectClasses.size() = "
418 << requestedObjectClasses.size() <<
", objectInstances.size() = "
419 << objectInstances.size();
420 setRandomNoise(centralHeadTiltAngle, 1.0f);
427 std::lock_guard<std::mutex> lock(mutex);
429 next = IceUtil::Time::milliSeconds(timestamp);
433 ObjectLocalizationSaliency::setRandomNoise(
const float centralAngleForVerticalDirection,
434 const float directionVariabilityFactor)
437 Eigen::Vector3f currentViewTargetEigen(
439 FramedPosition currentViewTarget(currentViewTargetEigen, cameraFrameName, robot->getName());
440 currentViewTarget.changeFrame(robot, headFrameName);
441 currentViewTargetEigen = currentViewTarget.toEigen() - offsetToHeadCenter;
446 const int randomFactor = 100000;
447 const float noiseFactor = randomNoiseLevel / randomFactor;
448 const float distanceWeight =
std::min(0.6f / directionVariabilityFactor, 1.0f);
449 const float centralityWeight =
std::min(0.1f / directionVariabilityFactor, 0.2f);
451 for (
size_t i = 0; i < nodes->size(); i++)
456 float distanceFactor = 1.0f - distanceWeight * distanceOnSphere /
458 float verticalCenterFactor =
459 1.0f - centralityWeight * fabs(nodeCoord.
fPhi - centralAngleForVerticalDirection) /
461 float horizontalCenterFactor = 1.0f - centralityWeight * fabs(nodeCoord.
fTheta) / 120.0f;
462 node->
setIntensity(verticalCenterFactor * horizontalCenterFactor * distanceFactor *
463 noiseFactor * (rand() % randomFactor));