WorkingMemoryObjectPoseProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package MemoryX::ArmarXObjects::WorkingMemoryObjectPoseProvider
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <SimoxUtility/json.h>
26 #include <SimoxUtility/algorithm/string.h>
27 
28 #include <VirtualRobot/ManipulationObject.h>
29 
34 
39 
42 
43 
44 void memoryx::to_json(nlohmann::json &j, const Config &config)
45 {
46  j["objectNames"] = config.objectNames;
47 }
48 
49 void memoryx::from_json(const nlohmann::json &j, Config &config)
50 {
51  j.at("objectNames").get_to(config.objectNames);
52 }
53 
54 
55 namespace memoryx
56 {
57 
59  {
61 
62  defs->component(workingMemory);
63  defs->component(robotStateComponent);
64  defs->component(priorKnowledge);
65 
66  defs->optional(updateFrequency, "UpdateFrequency", "Target number of updates per second.");
67  defs->optional(configFile, "ConfigFile", "Path to the config file.");
68  defs->optional(loadObjectDatasetsStr, "LoadDatasets", "Only load the files for the following datasets, separated by ;. Load all if input is empty.");
69 
70  return defs;
71  }
72 
74  {
75  return "WorkingMemoryObjectPoseProvider";
76  }
77 
78 
80  {
81  configFile = armarx::ArmarXDataPath::resolvePath(configFile);
82  ARMARX_INFO << "Loading config file '" << configFile << "' ...";
83  if (std::filesystem::is_regular_file(configFile))
84  {
85  const nlohmann::json j = nlohmann::read_json(configFile);
86  j.get_to(config);
87  }
88  else
89  {
90  ARMARX_INFO << "No config file at '" << configFile << "'";
91  }
92  }
93 
94 
96  {
97  ARMARX_CHECK_NOT_NULL(workingMemory);
98  ARMARX_CHECK_NOT_NULL(robotStateComponent);
99  ARMARX_CHECK_NOT_NULL(priorKnowledge);
100 
101  ARMARX_IMPORTANT << "Loading workingmemory and priorknowledge entites and files. This may take a while....";
102  attachments.initFromProxies(workingMemory, robotStateComponent);
103 
104  std::vector<std::string> loadDatasets = simox::alg::split(loadObjectDatasetsStr, ";");
105 
106  objectClassSegment.initFromProxy(priorKnowledge, loadDatasets);
107  ARMARX_IMPORTANT << "... done loading!";
108 
109  // A periodic task logs an important info when the cycle time is not met.
110  // To avoid this, we use a running task.
111  task = new armarx::SimpleRunningTask<>([this]()
112  {
113  armarx::CycleUtil cycle(int(1000 / updateFrequency));
114 
115  while (task && !task->isStopped())
116  {
117  provideObjectInstancesPoses();
118  cycle.waitForCycleDuration();
119  }
120  });
121 
122  task->start();
123  }
124 
125 
127  {
128  if (task)
129  {
130  task->stop();
131  }
132  }
133 
134 
136  {
137  }
138 
139 
140  void WorkingMemoryObjectPoseProvider::provideObjectInstancesPoses()
141  {
142  std::scoped_lock lock(mutex);
143  std::vector<ObjectInstancePtr> instances = attachments.queryObjects();
144  provideObjectInstancesPoses(instances);
145  }
146 
147  void WorkingMemoryObjectPoseProvider::provideObjectInstancesPoses(const std::vector<ObjectInstancePtr>& objectInstances)
148  {
150 
151  for (const auto& instance : objectInstances)
152  {
153  objectPoses.push_back(toProvidedObjectPose(instance).toIce());
154  }
155 
156  objectPoseTopic->reportObjectPoses(getName(), objectPoses);
157  }
158 
159 
161  WorkingMemoryObjectPoseProvider::toProvidedObjectPose(
162  const ObjectInstancePtr& instance)
163  {
165 
166  pose.objectType = armarx::objpose::KnownObject;
167  std::string className = instance->getMostProbableClass();
168  if (auto it = config.objectNames.find(className); it != config.objectNames.end())
169  {
170  ARMARX_VERBOSE << deactivateSpam(60) << "Replacing class '" << className << "' by '" << it->second << "'";
171  className = it->second;
172  }
173  pose.objectID = armarx::ObjectID(className);
174  pose.objectID.setInstanceName(instance->getId());
175 
176  pose.objectPose = instance->getPose()->toEigen();
177  pose.objectPoseFrame = instance->getPose()->getFrame();
178  if (pose.objectPoseFrame.empty())
179  {
181  }
182 
183  pose.confidence = instance->getExistenceCertainty();
184  if (instance->hasLocalizationTimestamp())
185  {
186  fromIce(instance->getLocalizationTimestamp(), pose.timestamp);
187  }
188  else
189  {
191  }
192 
193  std::optional<ObjectClassWrapper> objectClass = objectClassSegment.getClass(className);
194  //ARMARX_IMPORTANT << "Looking for class: " << className;
195  if (objectClass)
196  {
197  VirtualRobot::CollisionModelPtr collisionModel = objectClass->manipulationObject->getCollisionModel();
198  VirtualRobot::BoundingBox bb = collisionModel->getBoundingBox(false);
199  Eigen::Vector3f bbMin = bb.getMin();
200  Eigen::Vector3f bbMax = bb.getMax();
201  Eigen::Vector3f extents = bbMax - bbMin;
202  //ARMARX_IMPORTANT << "Bounding box: " << extents.transpose();
203 
204  pose.localOOBB = simox::OrientedBoxf(
205  (0.5 * (bbMin + bbMax)).eval(),
207  extents);
208  }
209 
210  pose.providerName = getName();
211 
212  return pose;
213  }
214 
215 
216 
217  armarx::objpose::ProviderInfo WorkingMemoryObjectPoseProvider::getProviderInfo(const Ice::Current&)
218  {
219  armarx::objpose::ProviderInfo info;
220 
221  return info;
222  }
223 
224 
225  void WorkingMemoryObjectPoseProvider::attachObjectToRobotNode(const AttachObjectToRobotNodeInput& attachment, const Ice::Current&)
226  {
227  std::scoped_lock lock(mutex);
228  attachments.attachObjectToRobotNode(attachment);
229  }
230 
231 
232  void WorkingMemoryObjectPoseProvider::detachObjectFromRobotNode(const DetachObjectFromRobotNodeInput& detachment, const Ice::Current&)
233  {
234  std::scoped_lock lock(mutex);
235  attachments.detachObjectFromRobotNode(detachment);
236  }
237 
238 }
armarx::ArmarXDataPath::resolvePath
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Definition: ArmarXDataPath.cpp:542
memoryx::from_json
void from_json(const nlohmann::json &j, Config &config)
Definition: WorkingMemoryObjectPoseProvider.cpp:49
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:70
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
memoryx::Config::objectNames
std::map< std::string, std::string > objectNames
Definition: WorkingMemoryObjectPoseProvider.h:47
ice_conversions.h
armarx::objpose::ProvidedObjectPose::objectID
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition: ProvidedObjectPose.h:47
memoryx::WorkingMemoryObjectPoseProvider::getProviderInfo
armarx::objpose::ProviderInfo getProviderInfo(const Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemoryObjectPoseProvider.cpp:217
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
memoryx::ObjectInstanceToRobotNodeAttachments::attachObjectToRobotNode
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:34
armarx::armem::attachment::ObjectID
armem::MemoryID ObjectID
Definition: types.h:79
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
Pose.h
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ice_conversions.h
memoryx::ObjectClassSegmentWrapper::getClass
std::optional< ObjectClassWrapper > getClass(std::string const &className) const
Definition: ObjectClassSegmentWrapper.cpp:62
armarx::objpose::ProvidedObjectPose::objectPoseFrame
std::string objectPoseFrame
Definition: ProvidedObjectPose.h:50
armarx::ObjectPoseProviderPluginUser::objectPoseTopic
objpose::ObjectPoseTopicPrx objectPoseTopic
Definition: ObjectPoseProviderPlugin.h:56
memoryx::Config
Definition: WorkingMemoryObjectPoseProvider.h:45
memoryx::ObjectInstanceToRobotNodeAttachments::detachObjectFromRobotNode
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:62
memoryx::WorkingMemoryObjectPoseProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: WorkingMemoryObjectPoseProvider.cpp:58
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
memoryx::WorkingMemoryObjectPoseProvider::onInitComponent
void onInitComponent() override
Definition: WorkingMemoryObjectPoseProvider.cpp:79
armarx::core::eigen::fromIce
void fromIce(Eigen::Vector2f &e, const Ice::FloatSeq &ice)
Definition: ice_conversions.cpp:10
memoryx::ObjectClassSegmentWrapper::initFromProxy
void initFromProxy(memoryx::PriorKnowledgeInterfacePrx const &priorKnowledge, const std::vector< std::string > &datasets)
Definition: ObjectClassSegmentWrapper.cpp:16
ObjectID.h
memoryx::to_json
void to_json(nlohmann::json &j, const Config &config)
Definition: WorkingMemoryObjectPoseProvider.cpp:44
memoryx::ObjectInstancePtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
Definition: ObjectInstance.h:42
memoryx::ObjectInstanceToRobotNodeAttachments::initFromProxies
void initFromProxies(const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent)
Set the proxies.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:20
MemoryXCoreObjectFactories.h
armarx::CycleUtil::waitForCycleDuration
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition: CycleUtil.cpp:53
armarx::objpose::ProvidedObjectPose::providerName
std::string providerName
Name of the providing component.
Definition: ProvidedObjectPose.h:40
WorkingMemoryObjectPoseProvider.h
armarx::core::eigen::toIce
void toIce(const Eigen::Vector2f &e, Ice::FloatSeq &ice)
Definition: ice_conversions.cpp:17
memoryx::WorkingMemoryObjectPoseProvider::onConnectComponent
void onConnectComponent() override
Definition: WorkingMemoryObjectPoseProvider.cpp:95
armarx::objpose::ProvidedObjectPoseSeq
std::vector< ProvidedObjectPose > ProvidedObjectPoseSeq
Definition: forward_declarations.h:25
memoryx::WorkingMemoryObjectPoseProvider::onDisconnectComponent
void onDisconnectComponent() override
Definition: WorkingMemoryObjectPoseProvider.cpp:126
CycleUtil.h
armarx::objpose::ProvidedObjectPose
An object pose provided by an ObjectPoseProvider.
Definition: ProvidedObjectPose.h:25
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::objpose::ProvidedObjectPose::objectType
ObjectType objectType
Known or unknown object.
Definition: ProvidedObjectPose.h:42
ObjectInstance.h
ProvidedObjectPose.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::Logging::deactivateSpam
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.
Definition: Logging.cpp:92
armarx::objpose::ProvidedObjectPose::localOOBB
std::optional< simox::OrientedBoxf > localOOBB
Object bounding box in object's local coordinate frame.
Definition: ProvidedObjectPose.h:64
memoryx::WorkingMemoryObjectPoseProvider::detachObjectFromRobotNode
void detachObjectFromRobotNode(const DetachObjectFromRobotNodeInput &detachment, const Ice::Current &) override
Definition: WorkingMemoryObjectPoseProvider.cpp:232
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
memoryx::ObjectInstanceToRobotNodeAttachments::queryObjects
std::vector< ObjectInstancePtr > queryObjects()
Get all entities from objectInstanceSegment and cast them to ObjectInstance.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:85
armarx::objpose::ProvidedObjectPose::confidence
float confidence
Confidence in [0, 1] (1 = full, 0 = none).
Definition: ProvidedObjectPose.h:58
memoryx::WorkingMemoryObjectPoseProvider::attachObjectToRobotNode
void attachObjectToRobotNode(const AttachObjectToRobotNodeInput &attachment, const Ice::Current &) override
Definition: WorkingMemoryObjectPoseProvider.cpp:225
armarx::objpose::ProvidedObjectPose::timestamp
DateTime timestamp
Source timestamp.
Definition: ProvidedObjectPose.h:60
memoryx::WorkingMemoryObjectPoseProvider::getDefaultName
std::string getDefaultName() const override
Definition: WorkingMemoryObjectPoseProvider.cpp:73
memoryx::WorkingMemoryObjectPoseProvider::onExitComponent
void onExitComponent() override
Definition: WorkingMemoryObjectPoseProvider.cpp:135
ArmarXDataPath.h
armarx::objpose::ProvidedObjectPose::objectPose
Eigen::Matrix4f objectPose
Definition: ProvidedObjectPose.h:49
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
armarx::ObjectID::setInstanceName
void setInstanceName(const std::string &instanceName)
Definition: ObjectID.h:36