ArMemToPointCloudProvider.cpp
Go to the documentation of this file.
2
6
10
12
13namespace 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
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();
203 const armarx::aron::Path elementPath = armarx::aron::Path::FromString(
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 =
224 virtualRobotReaderPlugin->get().getSynchronizedRobot(p.robotName);
226 *toProvide, *toProvide, p.frame, armarx::GlobalFrame, robot);
227 }
228
229 ARMARX_VERBOSE << "Providing point cloud with " << toProvide->points.size()
230 << " points.";
231
232
233 providePointCloud(toProvide);
234 }
235 }
236
237 template <class PointT>
238 std::string
239 ArMemToPointCloudProvider::Properties::getCoreSegmentName() const
240 {
241 if (coreSegmentName.empty() or coreSegmentName == "(auto)")
242 {
244 }
245 else
246 {
247 return coreSegmentName;
248 }
249 }
250
251} // namespace visionx
uint8_t data[1]
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition Clock.cpp:99
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
void setComponent(ManagedIceObject *component)
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
static Path FromString(const std::string &, const std::string &rootIdentifier=DEFAULT_ROOT_IDENTIFIER, const std::string &delimeter=DEFAULT_DELIMETER)
Definition Path.cpp:139
static const constexpr char * DEFAULT_ROOT_IDENTIFIER
Definition Path.h:38
void onConnectPointCloudProvider() override
This is called when the Component::onConnectComponent() setup is called.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onInitPointCloudProvider() override
This is called when the Component::onInitComponent() is called.
MetaPointCloudFormatPtr getDefaultPointCloudFormat() override
default point cloud format used to initialize shared memory
void onExitPointCloudProvider() override
This is called when the Component::onExitComponent() setup is called.
std::string getDefaultName() const override
Retrieve default name of component.
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
std::shared_ptr< Variant > VariantPtr
aron::data::DictPtr make_dict(_Args &&... args)
Definition Dict.h:107
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >
std::string GetCoreSegmentNameFor()
Definition constants.h:23
std::map< std::string, PointType > String2PointType
Definition constants.h:97
ArmarX headers.
void transformPointCloudFromTo(const pcl::PointCloud< PointT > &sourceCloud, pcl::PointCloud< PointT > &targetCloud, const std::string &sourceFrame, const std::string &targetFrame, const VirtualRobot::RobotConstPtr &robot)
Transform a point cloud from a robot frame to another.