27 #include <SimoxUtility/algorithm/string.h>
36 #include <experimental/map>
37 #include <range/v3/range/conversion.hpp>
38 #include <range/v3/view/filter.hpp>
39 #include <range/v3/view/transform.hpp>
43 const std::string SimulationObjectPoseProvider::defaultName =
"SimulationObjectPoseProvider";
57 def->component(simulatorPrx,
"Simulator");
59 def->optional(properties.updateFrequency,
61 "Frequency at which to update the objects in the ObjectMemory.");
63 def->optional(properties.requestAllEntities,
64 "p.requestAllEntities",
65 "True if all entities should be requested all the time.");
67 def->optional(properties.initiallyRequestedEntities,
68 "p.initiallyRequestedEntities",
69 "All entities (comma separated) that should be requested from the beginning (e.g. 'Kitchen/multivitamin-juice/0'). "
70 "If you want an entity to be localized for n seconds append ':n'. If you want to see all active entities, set the component to DEBUG.");
85 for (
const auto& entity :
simox::alg::split(properties.initiallyRequestedEntities,
","))
91 <<
"Could not use multiple durations for localization request '" << entity <<
"'";
93 const auto& objectName = tokens[0];
97 const auto until = tokens.size() == 1
101 activeRequests.insert({objectName, until});
111 poseProviderTask->start();
127 return SimulationObjectPoseProvider::defaultName;
134 return SimulationObjectPoseProvider::defaultName;
137 objpose::provider::RequestObjectsOutput
139 const objpose::provider::RequestObjectsInput&
input,
140 const Ice::Current& )
142 if (properties.requestAllEntities)
145 "Requesting an object does not have an effect.";
149 const std::lock_guard l(knownObjectsMutex);
150 const std::lock_guard l2(activeRequestsMutex);
154 objpose::provider::RequestObjectsOutput output;
155 for (
const auto&
id :
input.objectIDs)
157 const std::string entityId =
id.dataset +
"/" +
id.className +
"/" +
id.instanceName;
158 if (knownObjects.find(entityId) != knownObjects.end())
160 if (activeRequests.find(entityId) != activeRequests.end())
163 <<
"' is already requested. Updating the request time.";
165 activeRequests[entityId] =
167 output.results[id].success =
true;
171 std::vector<std::string> allNames;
172 allNames.reserve(knownObjects.size());
173 for (
const auto& [name, _] : knownObjects)
175 allNames.push_back(name);
178 <<
"'. All known objects are: " << allNames;
179 output.results[id].success =
false;
185 objpose::ProviderInfo
188 objpose::ProviderInfo info;
189 info.objectType = objpose::KnownObject;
190 info.proxy = getProxy<objpose::ObjectPoseProviderPrx>();
191 info.supportedObjects = {};
196 SimulationObjectPoseProvider::poseProviderTaskRun()
200 while (poseProviderTask and not poseProviderTask->isStopped())
202 metronome.waitForNextTick();
206 armarx::SceneVisuData sceneData;
209 sceneData = simulatorPrx->getScene();
210 for (
const auto&
object : sceneData.objects)
212 ARMARX_DEBUG <<
"Found object in scene: " <<
object.name;
215 catch (
const Ice::LocalException& e)
217 ARMARX_INFO <<
"Could not get object poses from simulator: " << e.what();
222 <<
" objects and " << sceneData.robots.size() <<
" robots.";
224 std::vector<objpose::ProvidedObjectPose> providedObjects;
226 if (properties.requestAllEntities)
229 providedObjects = getAllPoses(sceneData, now);
234 updateKnownObjects(sceneData);
235 removeExpiredRequests(now);
236 providedObjects = getRequestedPoses(now);
246 catch (
const Ice::LocalException& e)
248 ARMARX_INFO <<
"Could not report object poses to object memory: " << e.what();
254 std::vector<objpose::ProvidedObjectPose>
255 SimulationObjectPoseProvider::getAllPoses(
const armarx::SceneVisuData& sceneData,
256 const DateTime& time)
const
258 auto convertPoses = [
this, time](
const auto& simObject)
259 {
return objectPoseFromVisuData(simObject, time); };
265 SimulationObjectPoseProvider::updateKnownObjects(
const armarx::SceneVisuData& sceneData)
267 const std::scoped_lock l(knownObjectsMutex);
269 knownObjects.clear();
270 for (
const auto&
object : sceneData.objects)
272 knownObjects.insert({
object.name,
object});
277 SimulationObjectPoseProvider::removeExpiredRequests(
const DateTime& time)
279 const std::scoped_lock l2(activeRequestsMutex);
281 auto isTimeout = [time](
const auto& request)
283 const auto& provideUntilTime = request.second;
284 return (not provideUntilTime.isInvalid()) and time > provideUntilTime;
287 std::experimental::erase_if(activeRequests, isTimeout);
290 std::vector<objpose::ProvidedObjectPose>
291 SimulationObjectPoseProvider::getRequestedPoses(
const DateTime& time)
const
293 const std::scoped_lock l(knownObjectsMutex);
294 const std::scoped_lock l2(activeRequestsMutex);
296 auto isRequestedObjectKnown = [
this](
const auto& request)
298 const auto& objectName = request.first;
299 return this->knownObjects.find(objectName) != this->knownObjects.end();
302 auto getPoseFromRequest = [
this, time](
const auto& request)
304 const auto&
object = this->knownObjects.at(request.first);
305 return objectPoseFromVisuData(
object, time);
308 return activeRequests | ranges::views::filter(isRequestedObjectKnown) |
312 objpose::ProvidedObjectPose
313 SimulationObjectPoseProvider::objectPoseFromVisuData(
const armarx::ObjectVisuData& visuData,
314 const DateTime& time)
const
316 objpose::ProvidedObjectPose pose;
319 pose.objectType = objpose::ObjectType::KnownObject;
320 pose.isStatic = visuData.staticObject;
324 pose.objectPose =
armarx::fromIce(visuData.objectPoses.at(visuData.name));
328 pose.timestamp = time;