ArMemToPointCloudProvider.cpp
Go to the documentation of this file.
2 
6 
10 
12 
13 namespace visionx
14 {
16  {
17  addPlugin(virtualRobotReaderPlugin);
18  }
19 
20  std::string
22  {
23  return "ArMemToPointCloudProvider";
24  }
25 
28  {
31 
32  defs->optional(p.memoryName, "mem.01_MemoryName", "Name of the memory.");
33  defs->optional(
34  p.coreSegmentName,
35  "mem.02_CoreSegmentName",
36  "Name of the core segment. If empty or (auto), this is derived from the point type.");
37  defs->optional(p.providerSegmentName,
38  "mem.03_ProviderSegmentName",
39  "Name of the provider segment(s).");
40  defs->optional(p.entityName, "mem.04_EntityName", "Name of the entity.");
41 
42  defs->optional(p.pointtype, "pc.01_PointType", "The type of the point cloud.");
43  defs->optional(p.width, "pc.02_Width", "Width of the buffer.");
44  defs->optional(p.height, "pc.03_Height", "Height of the buffer.");
45  defs->optional(p.elementPath,
46  "pc.04_ElementPath",
47  "Path inside the ARON structure to the point cloud element (delimited by '" +
48  p.elementPathDelimiter + "').");
49  defs->optional(p.robotName, "p.robotName", "Name of the robot.");
50 
51 
52  defs->optional(p.convertToGlobalFrame, "p.convertToGlobalFrame", "");
53 
54  return defs;
55  }
56 
57  void
59  {
61 
62  switch (visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
63  {
65  registerToUpdates<pcl::PointXYZ>();
66  break;
68  registerToUpdates<pcl::PointXYZL>();
69  break;
71  registerToUpdates<pcl::PointXYZRGBA>();
72  break;
74  registerToUpdates<pcl::PointXYZRGBL>();
75  break;
76  }
77  }
78 
79  void
81  {
82  try
83  {
84  memoryReader = memoryNameSystem().useReader(armarx::armem::MemoryID(p.memoryName, ""));
85  }
87  {
88  ARMARX_ERROR << e.what();
89  }
90 
91  pollingTask = new armarx::SimpleRunningTask<>([this]() { this->runPolling(); });
92  pollingTask->start();
93  }
94 
95  void
96  ArMemToPointCloudProvider::runPolling()
97  {
98  while (true)
99  {
100  ARMARX_INFO << "Processing initial point clouds";
101  processUpdates(armarx::armem::MemoryID(), {});
102  ARMARX_INFO << "Done processing initial point clouds";
104  }
105  }
106 
107  void
109  {
110  }
111 
112  void
113  ArMemToPointCloudProvider::processUpdates(
114  const armarx::armem::MemoryID& entityID,
115  const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
116  {
117  // Discard everything in between
118  if (not processing.exchange(true))
119  {
120  ARMARX_INFO << deactivateSpam() << "Processing update and provide pointcloud...";
121 
122  switch (
123  visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
124  {
126  queryAndProvide<pcl::PointXYZ, arondto::PointCloudXYZ>();
127  break;
129  queryAndProvide<pcl::PointXYZL, arondto::PointCloudXYZL>();
130  break;
132  queryAndProvide<pcl::PointXYZRGBA, arondto::PointCloudXYZRGBA>();
133  break;
135  queryAndProvide<pcl::PointXYZRGBL, arondto::PointCloudXYZRGBL>();
136  break;
137  }
138 
139  processing = false;
140  }
141  }
142 
143  MetaPointCloudFormatPtr
145  {
146  // Copied from AK PointCloudProvider
147  MetaPointCloudFormatPtr info = new MetaPointCloudFormat();
148 
149  switch (visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
150  {
152  info->type = PointContentType::ePoints;
153  info->capacity = p.width * p.height * sizeof(Point3D);
154  break;
156  info->type = PointContentType::eLabeledPoints;
157  info->capacity = p.width * p.height * sizeof(LabeledPoint3D);
158  break;
160  info->type = PointContentType::eColoredPoints;
161  info->capacity = p.width * p.height * sizeof(ColoredPoint3D);
162  break;
164  info->type = PointContentType::eColoredLabeledPoints;
165  info->capacity = p.width * p.height * sizeof(ColoredLabeledPoint3D);
166  break;
167  }
168 
169  //info->frameId = getProperty<std::string>("frameId").getValue();
170  info->size = info->capacity;
171  return info;
172  }
173 
174  template <class PointT>
175  void
176  ArMemToPointCloudProvider::registerToUpdates()
177  {
178  // Connect to memory updates.
179  auto callback = [this](const armarx::armem::MemoryID& entityID,
180  const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
181  { this->processUpdates(entityID, updatedSnapshotIDs); };
183  p.memoryName, p.getCoreSegmentName<PointT>(), p.providerSegmentName, p.entityName);
184  ARMARX_INFO << "Listening to: " << listenTo.str();
185  memoryNameSystem().subscribe(listenTo, callback);
186  }
187 
188  template <class PointT, class AronT>
189  void
190  ArMemToPointCloudProvider::queryAndProvide()
191  {
192  // query latest pc from memory
193  armarx::armem::MemoryID toQuery(
194  p.memoryName, p.getCoreSegmentName<PointT>(), p.providerSegmentName, p.entityName);
195  std::optional<armarx::armem::wm::EntitySnapshot> data =
196  memoryReader.getLatestSnapshotIn(toQuery);
197 
198  if (data.has_value())
199  {
200  ARMARX_CHECK(data->size() == 1);
201 
202  armarx::aron::data::DictPtr aron = data->getInstance(0).data();
204  p.elementPath, armarx::aron::Path::DEFAULT_ROOT_IDENTIFIER, p.elementPathDelimiter);
205 
206  armarx::aron::data::VariantPtr aronPointCloud = aron->navigateRelative(elementPath);
207  ARMARX_CHECK_NOT_NULL(aronPointCloud);
208 
210  aronAdapted->setElement("pointcloud", aronPointCloud);
211 
212  AronT arondto = AronT::FromAron(aronAdapted);
213 
214  typename pcl::PointCloud<PointT>::Ptr toProvide(new pcl::PointCloud<PointT>());
215  *toProvide = arondto.pointcloud;
216 
217  if (p.convertToGlobalFrame)
218  {
219 
220  ARMARX_INFO << "Source frame " << p.frame;
221 
222  ARMARX_INFO << deactivateSpam(10) << "Converting to global frame";
223  const auto robot = virtualRobotReaderPlugin->get().getSynchronizedRobot(p.robotName);
224  visionx::transformPointCloudFromTo<PointT>(
225  *toProvide, *toProvide, p.frame, armarx::GlobalFrame, robot);
226  }
227 
228  ARMARX_VERBOSE << "Providing point cloud with " << toProvide->points.size()
229  << " points.";
230 
231 
232  providePointCloud(toProvide);
233  }
234  }
235 
236  template <class PointT>
237  std::string
238  ArMemToPointCloudProvider::Properties::getCoreSegmentName() const
239  {
240  if (coreSegmentName.empty() or coreSegmentName == "(auto)")
241  {
242  return visionx::armem::pointcloud::GetCoreSegmentNameFor<PointT>();
243  }
244  else
245  {
246  return coreSegmentName;
247  }
248  }
249 
250 } // namespace visionx
visionx::ArMemToPointCloudProvider::onExitPointCloudProvider
void onExitPointCloudProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: ArMemToPointCloudProvider.cpp:108
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:70
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
visionx::armem::pointcloud::String2PointType
std::map< std::string, PointType > String2PointType
Definition: constants.h:97
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
client.h
armarx::aron::make_dict
aron::data::DictPtr make_dict(_Args &&... args)
Definition: Dict.h:107
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:104
MemoryID.h
armarx::armem::client::plugins::PluginUser::memoryNameSystem
MemoryNameSystem & memoryNameSystem()
Definition: PluginUser.cpp:22
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::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
FramedPointCloud.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::aron::Path
The Path class.
Definition: Path.h:36
ArMemToPointCloudProvider.h
armarx::aron::Path::DEFAULT_ROOT_IDENTIFIER
static const constexpr char * DEFAULT_ROOT_IDENTIFIER
Definition: Path.h:39
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:83
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::aron::Path::FromString
static Path FromString(const std::string &, const std::string &rootIdentifier=DEFAULT_ROOT_IDENTIFIER, const std::string &delimeter=DEFAULT_DELIMETER)
Definition: Path.cpp:137
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
visionx::PointCloudProviderPropertyDefinitions
Definition: PointCloudProvider.h:42
error.h
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:184
visionx::armem::pointcloud::PointType::PointXYZL
@ PointXYZL
armarx::armem::client::util::MemoryListener::subscribe
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
Definition: MemoryListener.cpp:116
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::robot_state::VirtualRobotReader::getSynchronizedRobot
VirtualRobot::RobotPtr getSynchronizedRobot(const std::string &name, const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure, bool blocking=true)
visionx::ArMemToPointCloudProvider::onInitPointCloudProvider
void onInitPointCloudProvider() override
This is called when the Component::onInitComponent() is called.
Definition: ArMemToPointCloudProvider.cpp:58
armarx::armem::client::plugins::ReaderWriterPlugin::get
T & get()
Definition: ReaderWriterPlugin.h:87
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
visionx::armem::pointcloud::PointType::PointXYZ
@ PointXYZ
visionx::ArMemToPointCloudProvider::getDefaultPointCloudFormat
MetaPointCloudFormatPtr getDefaultPointCloudFormat() override
default point cloud format used to initialize shared memory
Definition: ArMemToPointCloudProvider.cpp:144
visionx::PointCloudProvider::providePointCloud
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
Definition: PointCloudProvider.h:122
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:440
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ArMemToPointCloudProvider::onConnectPointCloudProvider
void onConnectPointCloudProvider() override
This is called when the Component::onConnectComponent() setup is called.
Definition: ArMemToPointCloudProvider.cpp:80
visionx::ArMemToPointCloudProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArMemToPointCloudProvider.cpp:27
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
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
Logging.h
visionx::ArMemToPointCloudProvider::ArMemToPointCloudProvider
ArMemToPointCloudProvider()
Definition: ArMemToPointCloudProvider.cpp:15
visionx::armem::pointcloud::PointType::PointXYZRGBL
@ PointXYZRGBL
visionx::armem::pointcloud::PointType::PointXYZRGBA
@ PointXYZRGBA
armarx::armem::client::Reader::getLatestSnapshotIn
std::optional< wm::EntitySnapshot > getLatestSnapshotIn(const MemoryID &id, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the latest snapshot under the given memory ID.
Definition: Reader.cpp:370
visionx::ArMemToPointCloudProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ArMemToPointCloudProvider.cpp:21
visionx::PointT
pcl::PointXYZRGBA PointT
Definition: MaskRCNNPointCloudObjectLocalizer.h:79
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29