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_state/aron/RobotState.aron.generated.h>
45 
46 namespace armarx
47 {
48 
51  {
54 
55  defs->optional(properties.object.maxFrequencyHz, "p.object.maxFrequency");
56 
57  return defs;
58  }
59 
61  {
62  addPlugin(objectClientPlugin);
63  }
64 
65  std::string
67  {
68  return "ObjectInstanceToIndex";
69  }
70 
71  void
73  {
74  using This = ObjectInstanceToIndex;
75  // This should not be necessary but seems to be. ToDo: Look into this.
77 
78  memoryNameSystem().subscribe(armem::robot_state::memoryID, this, &This::processRobotState);
80  armem::objects::instaceSegmentID, this, &This::processObjectInstance);
81  }
82 
83  void
85  {
86  try
87  {
88  indexSpatialMemoryWriter = memoryNameSystem().useWriter(armem::index::spatialSegmentID);
89  }
91  {
92  ARMARX_ERROR << e.what();
93  }
94 
95  {
98  }
99  }
100 
101  void
103  {
104  }
105 
106  void
108  {
109  }
110 
111  void
113  {
114  using namespace armarx::RemoteGui::Client;
115 
116  VBoxLayout root = {VSpacer()};
117  RemoteGui_createTab(getName(), root, &tab);
118  }
119 
120  void
122  {
123  }
124 
125  void
126  ObjectInstanceToIndex::processRobotState(const armem::MemoryID& id,
127  const std::vector<armem::MemoryID>& snapshotIDs)
128  {
129 #if 0
130  if (not robotMemoryReader)
131  {
132  try
133  {
134  robotMemoryReader = memoryNameSystem().getReader(objectInstanceSegmentID);
135  }
136  catch (const armem::error::CouldNotResolveMemoryServer& e)
137  {
138  ARMARX_INFO << e.what();
139  return;
140  }
141  }
142  ARMARX_CHECK(robotMemoryReader);
143 
144  // Get only the latest snapshot per entity.
145  std::map<armem::MemoryID, const armem::MemoryID*> entityToSnapshot;
146  for (const armem::MemoryID& snapshotID : snapshotIDs)
147  {
148  const armem::MemoryID entityID = snapshotID.getEntityID();
149  if (auto it = entityToSnapshot.find(entityID); it != entityToSnapshot.end())
150  {
151  if (it->second->timestamp < snapshotID.timestamp)
152  {
153  entityToSnapshot[entityID] = &snapshotID;
154  }
155  }
156  else
157  {
158  entityToSnapshot[entityID] = &snapshotID;
159  }
160  }
161 
162  // Query them.
163  std::vector<armem::MemoryID> queryIDs;
164  for (const auto& [_, snapshotID] : entityToSnapshot)
165  {
166  queryIDs.push_back(*snapshotID);
167  }
168 
169  armem::client::QueryResult result = robotMemoryReader.queryMemoryIDs(queryIDs);
170  if (result.success)
171  {
172  // Prepare the commit.
173 
174  armem::MemoryID provSegID = indexSpatialSegmentID.withProviderSegmentName(getName());
175 
176  armem::Commit commit;
177 
178  result.memory.forEachInstance(
179  [&commit, &provSegID](const armem::wm::EntityInstance& instance)
180  {
181  const armem::arondto::ObjectInstance data = instance.dataAs<armem::arondto::ObjectInstance>();
182 
183  armem::index::arondto::Spatial spatial;
184  armem::toAron(spatial.id, instance.id());
185  spatial.oobbGlobal;
186  spatial.aabbGlobal;
187 
188  armem::EntityUpdate& update = commit.add();
189  update.entityID = provSegID.withEntityName(instance.id().str());
190  update.referencedTime = instance.id().timestamp;
191  update.instancesData = { spatial.toAron() };
192 
193  return true;
194  });
195  }
196  else
197  {
198  ARMARX_INFO << result.errorMessage;
199  }
200 #endif
201  }
202 
203  void
204  ObjectInstanceToIndex::processObjectInstance(const armem::MemoryID& id,
205  const std::vector<armem::MemoryID>& snapshotIDs)
206  {
207  std::scoped_lock lock(objectMutex);
208  if (not object.has_value())
209  {
210  object = armem::objects::ObjectInstanceToIndex{
211  .objectPoseClient = objectClientPlugin->createClient(),
212  .indexSpatialMemoryWriter = indexSpatialMemoryWriter,
213  .indexSpatialProviderSegmentID =
215  .indexNamedProviderSegmentID =
217  .params =
219  .maxFrequency = armarx::Frequency::Hertz(properties.object.maxFrequencyHz)},
220  .state = {}};
221  }
222  ARMARX_CHECK(object.has_value());
223 
224  object->fetchAndCommitObjectInstances(snapshotIDs);
225  }
226 
227 } // namespace armarx
memory_ids.h
ObjectInstanceToIndex.h
armarx::ObjectInstanceToIndex::RemoteGui_update
void RemoteGui_update() override
Definition: ObjectInstanceToIndex.cpp:121
armarx::armem::robot_state::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:28
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:65
armarx::ObjectInstanceToIndex::createRemoteGuiTab
void createRemoteGuiTab()
Definition: ObjectInstanceToIndex.cpp:112
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:84
armarx::ObjectInstanceToIndex::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ObjectInstanceToIndex.cpp:84
armarx::ObjectInstanceToIndex::ObjectInstanceToIndex
ObjectInstanceToIndex()
Definition: ObjectInstanceToIndex.cpp:60
armarx::RemoteGui::Client::VBoxLayout
Definition: Widgets.h:167
armarx::armem::client::plugins::PluginUser::memoryNameSystem
MemoryNameSystem & memoryNameSystem()
Definition: PluginUser.cpp:20
stl.h
armarx::armem::objects::instaceSegmentID
const MemoryID instaceSegmentID
Definition: memory_ids.cpp:32
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:289
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Definition: ManagedIceObject.h:186
armarx::skills::gui::Parameters
aron::data::DictPtr Parameters
Definition: SkillManagerWrapper.h:21
armarx::RemoteGui::Client::VSpacer
Definition: Widgets.h:204
armarx::ObjectInstanceToIndex::getDefaultName
std::string getDefaultName() const override
Definition: ObjectInstanceToIndex.cpp:66
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:72
armarx::ObjectInstanceToIndex::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ObjectInstanceToIndex.cpp:102
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:417
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::index::spatialSegmentID
const MemoryID spatialSegmentID
Definition: memory_ids.cpp:31
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:25
armarx::plugins::ObjectPoseClientPlugin::createClient
objpose::ObjectPoseClient createClient()
Definition: ObjectPoseClientPlugin.cpp:37
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:196
operations.h
armarx::ObjectInstanceToIndex::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObjectInstanceToIndex.cpp:50
armarx::armem::index::namedSegmentID
const MemoryID namedSegmentID
Definition: memory_ids.cpp:30
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
aron_conversions.h
memory_definitions.h
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_startRunningTask
void RemoteGui_startRunningTask()
Definition: LightweightRemoteGuiComponentPlugin.cpp:119
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:310
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:425
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:80
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:462
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_createTab
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
Definition: LightweightRemoteGuiComponentPlugin.cpp:103
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:24
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::ObjectInstanceToIndex::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ObjectInstanceToIndex.cpp:107
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:20
MemoryNameSystem.h
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:191
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:27
memory_ids.h