26#include <experimental/map>
28#include <SimoxUtility/algorithm/string.h>
37#include <range/v3/range/conversion.hpp>
38#include <range/v3/view/filter.hpp>
39#include <range/v3/view/transform.hpp>
45 const std::string SimulationObjectPoseProvider::defaultName =
"SimulationObjectPoseProvider";
59 def->component(simulatorPrx,
"Simulator");
61 def->optional(properties.updateFrequency,
63 "Frequency at which to update the objects in the ObjectMemory.");
65 def->optional(properties.requestAllEntities,
66 "p.requestAllEntities",
67 "True if all entities should be requested all the time.");
69 def->optional(properties.initiallyRequestedEntities,
70 "p.initiallyRequestedEntities",
71 "All entities (comma separated) that should be requested from the beginning "
72 "(e.g. 'Kitchen/multivitamin-juice/0'). "
73 "If you want an entity to be localized for n seconds append ':n'. If you "
74 "want to see all active entities, set the component to DEBUG.");
89 for (
const auto& entity : simox::alg::split(properties.initiallyRequestedEntities,
","))
91 const auto tokens = simox::alg::split(entity,
":");
95 <<
"Could not use multiple durations for localization request '" << entity <<
"'";
97 const auto& objectName = tokens[0];
101 const auto until = tokens.size() == 1
105 activeRequests.insert({objectName, until});
115 poseProviderTask->start();
131 return SimulationObjectPoseProvider::defaultName;
137 return SimulationObjectPoseProvider::defaultName;
140 objpose::provider::RequestObjectsOutput
142 const objpose::provider::RequestObjectsInput& input,
143 const Ice::Current& )
145 if (properties.requestAllEntities)
148 "Requesting an object does not have an effect.";
152 const std::lock_guard l(knownObjectsMutex);
153 const std::lock_guard l2(activeRequestsMutex);
157 objpose::provider::RequestObjectsOutput output;
158 for (
const auto&
id : input.objectIDs)
160 const std::string entityId =
id.dataset +
"/" +
id.className +
"/" +
id.instanceName;
161 if (knownObjects.find(entityId) != knownObjects.end())
163 if (activeRequests.find(entityId) != activeRequests.end())
166 <<
"' is already requested. Updating the request time.";
168 activeRequests[entityId] =
170 output.results[id].success =
true;
174 std::vector<std::string> allNames;
175 allNames.reserve(knownObjects.size());
176 for (
const auto& [name, _] : knownObjects)
178 allNames.push_back(name);
181 <<
"'. All known objects are: " << allNames;
182 output.results[id].success =
false;
188 objpose::ProviderInfo
191 objpose::ProviderInfo info;
192 info.objectType = objpose::KnownObject;
194 info.supportedObjects = {};
199 SimulationObjectPoseProvider::poseProviderTaskRun()
203 while (poseProviderTask and not poseProviderTask->
isStopped())
205 metronome.waitForNextTick();
209 armarx::SceneVisuData sceneData;
212 sceneData = simulatorPrx->getScene();
213 for (
const auto&
object : sceneData.objects)
215 ARMARX_DEBUG <<
"Found object in scene: " <<
object.name;
218 catch (
const Ice::LocalException& e)
220 ARMARX_INFO <<
"Could not get object poses from simulator: " << e.what();
225 <<
" objects and " << sceneData.robots.size() <<
" robots.";
227 std::vector<objpose::ProvidedObjectPose> providedObjects;
229 if (properties.requestAllEntities)
232 providedObjects = getAllPoses(sceneData, now);
237 updateKnownObjects(sceneData);
238 removeExpiredRequests(now);
239 providedObjects = getRequestedPoses(now);
249 catch (
const Ice::LocalException& e)
251 ARMARX_INFO <<
"Could not report object poses to object memory: " << e.what();
257 std::vector<objpose::ProvidedObjectPose>
258 SimulationObjectPoseProvider::getAllPoses(
const armarx::SceneVisuData& sceneData,
261 auto convertPoses = [
this, time](
const auto& simObject)
262 {
return objectPoseFromVisuData(simObject, time); };
264 return sceneData.objects | ranges::views::transform(convertPoses) | ranges::to_vector;
268 SimulationObjectPoseProvider::updateKnownObjects(
const armarx::SceneVisuData& sceneData)
270 const std::scoped_lock l(knownObjectsMutex);
272 knownObjects.clear();
273 for (
const auto&
object : sceneData.objects)
275 knownObjects.insert({
object.name,
object});
280 SimulationObjectPoseProvider::removeExpiredRequests(
const DateTime& time)
282 const std::scoped_lock l2(activeRequestsMutex);
284 auto isTimeout = [time](
const auto& request)
286 const auto& provideUntilTime = request.second;
287 return (not provideUntilTime.isInvalid()) and time > provideUntilTime;
290 std::experimental::erase_if(activeRequests, isTimeout);
293 std::vector<objpose::ProvidedObjectPose>
294 SimulationObjectPoseProvider::getRequestedPoses(
const DateTime& time)
const
296 const std::scoped_lock l(knownObjectsMutex);
297 const std::scoped_lock l2(activeRequestsMutex);
299 auto isRequestedObjectKnown = [
this](
const auto& request)
301 const auto& objectName = request.first;
302 return this->knownObjects.find(objectName) != this->knownObjects.end();
305 auto getPoseFromRequest = [
this, time](
const auto& request)
307 const auto&
object = this->knownObjects.at(request.first);
308 return objectPoseFromVisuData(
object, time);
311 return activeRequests | ranges::views::filter(isRequestedObjectKnown) |
312 ranges::views::transform(getPoseFromRequest) | ranges::to_vector;
316 SimulationObjectPoseProvider::objectPoseFromVisuData(
const armarx::ObjectVisuData& visuData,
319 objpose::ProvidedObjectPose pose;
322 pose.objectType = objpose::ObjectType::KnownObject;
323 pose.isStatic = visuData.staticObject;
325 pose.objectID = armarx::ObjectID(visuData.name);
327 pose.objectPose =
armarx::fromIce(visuData.objectPoses.at(visuData.name));
331 pose.timestamp = time;
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Default component property definition container.
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
static DateTime Invalid()
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
static Frequency Hertz(std::int64_t hertz)
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
objpose::ObjectPoseStorageInterfacePrx objectPoseTopic
bool isStopped()
Retrieve whether stop() has been called.
void onInitComponent() override
objpose::ProviderInfo getProviderInfo(const Ice::Current &) override
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
static std::string GetDefaultName()
Get the component's default name.
void onExitComponent() override
objpose::provider::RequestObjectsOutput requestObjects(const objpose::provider::RequestObjectsInput &input, const Ice::Current &) override
std::string getDefaultName() const override
Represents a point in time.
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
An object pose provided by an ObjectPoseProvider.
std::string providerName
Name of the providing component.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
std::string const GlobalFrame
Variable of the global coordinate system.
objpose::AABB toIce(const simox::AxisAlignedBoundingBox &aabb)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >