ObjectInstanceToIndex.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 RobotAPI::ArmarXObjects::ObjectInstanceToIndex
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ObjectInstanceToIndex.h"
24 
25 #include <Eigen/Geometry>
26 
27 #include <SimoxUtility/math/pose/pose.h>
28 #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
29 
31 
37 #include <RobotAPI/libraries/armem_index/aron/Spatial.aron.generated.h>
39 #include <RobotAPI/libraries/armem_objects/aron/ObjectInstance.aron.generated.h>
41 #include <RobotAPI/libraries/armem_robot/aron/RobotState.aron.generated.h>
45 
46 
47 namespace armarx
48 {
49 
52  {
55 
56  defs->optional(properties.object.maxFrequencyHz, "p.object.maxFrequency");
57 
58  return defs;
59  }
60 
61 
63  {
64  addPlugin(objectClientPlugin);
65  }
66 
67  std::string
69  {
70  return "ObjectInstanceToIndex";
71  }
72 
73 
74  void
76  {
77  using This = ObjectInstanceToIndex;
78  // This should not be necessary but seems to be. ToDo: Look into this.
80 
81  memoryNameSystem().subscribe(armem::robot_state::memoryID, this, &This::processRobotState);
83  armem::objects::instaceSegmentID, this, &This::processObjectInstance);
84  }
85 
86 
87  void
89  {
90  try
91  {
92  indexSpatialMemoryWriter = memoryNameSystem().useWriter(armem::index::spatialSegmentID);
93  }
95  {
96  ARMARX_ERROR << e.what();
97  }
98 
99  {
102  }
103  }
104 
105 
106  void
108  {
109  }
110 
111 
112  void
114  {
115  }
116 
117 
118  void
120  {
121  using namespace armarx::RemoteGui::Client;
122 
123  VBoxLayout root = {VSpacer()};
124  RemoteGui_createTab(getName(), root, &tab);
125  }
126 
127 
128  void
130  {
131  }
132 
133 
134  void
135  ObjectInstanceToIndex::processRobotState(const armem::MemoryID& id,
136  const std::vector<armem::MemoryID>& snapshotIDs)
137  {
138 #if 0
139  if (not robotMemoryReader)
140  {
141  try
142  {
143  robotMemoryReader = memoryNameSystem().getReader(objectInstanceSegmentID);
144  }
145  catch (const armem::error::CouldNotResolveMemoryServer& e)
146  {
147  ARMARX_INFO << e.what();
148  return;
149  }
150  }
151  ARMARX_CHECK(robotMemoryReader);
152 
153  // Get only the latest snapshot per entity.
154  std::map<armem::MemoryID, const armem::MemoryID*> entityToSnapshot;
155  for (const armem::MemoryID& snapshotID : snapshotIDs)
156  {
157  const armem::MemoryID entityID = snapshotID.getEntityID();
158  if (auto it = entityToSnapshot.find(entityID); it != entityToSnapshot.end())
159  {
160  if (it->second->timestamp < snapshotID.timestamp)
161  {
162  entityToSnapshot[entityID] = &snapshotID;
163  }
164  }
165  else
166  {
167  entityToSnapshot[entityID] = &snapshotID;
168  }
169  }
170 
171  // Query them.
172  std::vector<armem::MemoryID> queryIDs;
173  for (const auto& [_, snapshotID] : entityToSnapshot)
174  {
175  queryIDs.push_back(*snapshotID);
176  }
177 
178  armem::client::QueryResult result = robotMemoryReader.queryMemoryIDs(queryIDs);
179  if (result.success)
180  {
181  // Prepare the commit.
182 
183  armem::MemoryID provSegID = indexSpatialSegmentID.withProviderSegmentName(getName());
184 
185  armem::Commit commit;
186 
187  result.memory.forEachInstance(
188  [&commit, &provSegID](const armem::wm::EntityInstance& instance)
189  {
190  const armem::arondto::ObjectInstance data = instance.dataAs<armem::arondto::ObjectInstance>();
191 
192  armem::index::arondto::Spatial spatial;
193  armem::toAron(spatial.id, instance.id());
194  spatial.oobbGlobal;
195  spatial.aabbGlobal;
196 
197  armem::EntityUpdate& update = commit.add();
198  update.entityID = provSegID.withEntityName(instance.id().str());
199  update.referencedTime = instance.id().timestamp;
200  update.instancesData = { spatial.toAron() };
201 
202  return true;
203  });
204  }
205  else
206  {
207  ARMARX_INFO << result.errorMessage;
208  }
209 #endif
210  }
211 
212 
213  void
214  ObjectInstanceToIndex::processObjectInstance(const armem::MemoryID& id,
215  const std::vector<armem::MemoryID>& snapshotIDs)
216  {
217  std::scoped_lock lock(objectMutex);
218  if (not object.has_value())
219  {
220  object = armem::objects::ObjectInstanceToIndex{
221  .objectPoseClient = objectClientPlugin->createClient(),
222  .indexSpatialMemoryWriter = indexSpatialMemoryWriter,
223  .indexSpatialProviderSegmentID =
225  .indexNamedProviderSegmentID =
227  .params = armem::objects::ObjectInstanceToIndex::Parameters{
228  .maxFrequency = armarx::Frequency::Hertz(properties.object.maxFrequencyHz)
229  },
230  .state = {}
231  };
232  }
233  ARMARX_CHECK(object.has_value());
234 
235  object->fetchAndCommitObjectInstances(snapshotIDs);
236  }
237 
238 } // namespace armarx
memory_ids.h
ObjectInstanceToIndex.h
armarx::ObjectInstanceToIndex::RemoteGui_update
void RemoteGui_update() override
Definition: ObjectInstanceToIndex.cpp:129
armarx::armem::robot_state::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:29
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:64
armarx::ObjectInstanceToIndex::createRemoteGuiTab
void createRemoteGuiTab()
Definition: ObjectInstanceToIndex.cpp:119
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::ObjectInstanceToIndex::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ObjectInstanceToIndex.cpp:88
armarx::ObjectInstanceToIndex::ObjectInstanceToIndex
ObjectInstanceToIndex()
Definition: ObjectInstanceToIndex.cpp:62
armarx::RemoteGui::Client::VBoxLayout
Definition: Widgets.h:167
armarx::armem::client::plugins::PluginUser::memoryNameSystem
MemoryNameSystem & memoryNameSystem()
Definition: PluginUser.cpp:22
stl.h
armarx::armem::objects::instaceSegmentID
const MemoryID instaceSegmentID
Definition: memory_ids.cpp:33
armarx::armem::toAron
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
Definition: aron_conversions.cpp:19
armarx::armem::client::MemoryNameSystem::useWriter
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Definition: MemoryNameSystem.cpp:276
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::RemoteGui::Client::VSpacer
Definition: Widgets.h:204
armarx::ObjectInstanceToIndex::getDefaultName
std::string getDefaultName() const override
Definition: ObjectInstanceToIndex.cpp:68
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::ObjectInstanceToIndex::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ObjectInstanceToIndex.cpp:75
armarx::ObjectInstanceToIndex::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ObjectInstanceToIndex.cpp:107
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::index::spatialSegmentID
const MemoryID spatialSegmentID
Definition: memory_ids.cpp:32
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
error.h
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::plugins::ObjectPoseClientPlugin::createClient
objpose::ObjectPoseClient createClient()
Definition: ObjectPoseClientPlugin.cpp:34
armarx::armem::client::util::MemoryListener::subscribe
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
Definition: MemoryListener.cpp:116
simox.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
operations.h
armarx::ObjectInstanceToIndex::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObjectInstanceToIndex.cpp:51
armarx::armem::index::namedSegmentID
const MemoryID namedSegmentID
Definition: memory_ids.cpp:31
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
aron_conversions.h
memory_definitions.h
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_startRunningTask
void RemoteGui_startRunningTask()
Definition: LightweightRemoteGuiComponentPlugin.cpp:110
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:305
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:81
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:440
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_createTab
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
Definition: LightweightRemoteGuiComponentPlugin.cpp:95
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::ObjectInstanceToIndex::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ObjectInstanceToIndex.cpp:113
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:23
MemoryNameSystem.h
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:177
armarx::RemoteGui::Client
Definition: EigenWidgets.cpp:8
memory_ids.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
memory_ids.h