ArMemToPointCloudProvider.cpp
Go to the documentation of this file.
2
7
11
13
14namespace visionx
15{
17 {
18 addPlugin(virtualRobotReaderPlugin);
19 }
20
21 std::string
26
27 std::string
29 {
30 return "ArMemToPointCloudProvider";
31 }
32
35 {
38
39 defs->optional(p.memoryName, "mem.01_MemoryName", "Name of the memory.");
40 defs->optional(
41 p.coreSegmentName,
42 "mem.02_CoreSegmentName",
43 "Name of the core segment. If empty or (auto), this is derived from the point type.");
44 defs->optional(p.providerSegmentName,
45 "mem.03_ProviderSegmentName",
46 "Name of the provider segment(s).");
47 defs->optional(p.entityName, "mem.04_EntityName", "Name of the entity.");
48
49 defs->optional(p.pointtype, "pc.01_PointType", "The type of the point cloud.");
50 defs->optional(p.width, "pc.02_Width", "Width of the buffer.");
51 defs->optional(p.height, "pc.03_Height", "Height of the buffer.");
52 defs->optional(p.elementPath,
53 "pc.04_ElementPath",
54 "Path inside the ARON structure to the point cloud element (delimited by '" +
55 p.elementPathDelimiter + "').");
56 defs->optional(p.robotName, "p.robotName", "Name of the robot.");
57
58
59 defs->optional(p.convertToGlobalFrame, "p.convertToGlobalFrame", "");
60
61 return defs;
62 }
63
64 void
66 {
68
69 switch (visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
70 {
72 registerToUpdates<pcl::PointXYZ>();
73 break;
75 registerToUpdates<pcl::PointXYZL>();
76 break;
78 registerToUpdates<pcl::PointXYZRGBA>();
79 break;
81 registerToUpdates<pcl::PointXYZRGBL>();
82 break;
83 }
84 }
85
86 void
88 {
89 try
90 {
91 memoryReader = memoryNameSystem().useReader(armarx::armem::MemoryID(p.memoryName, ""));
92 }
94 {
95 ARMARX_ERROR << e.what();
96 }
97
98 pollingTask = new armarx::SimpleRunningTask<>([this]() { this->runPolling(); });
99 pollingTask->start();
100 }
101
102 void
103 ArMemToPointCloudProvider::runPolling()
104 {
105 while (true)
106 {
107 ARMARX_INFO << "Processing initial point clouds";
108 processUpdates(armarx::armem::MemoryID(), {});
109 ARMARX_INFO << "Done processing initial point clouds";
111 }
112 }
113
114 void
118
119 void
120 ArMemToPointCloudProvider::processUpdates(
121 const armarx::armem::MemoryID& entityID,
122 const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
123 {
124 // Discard everything in between
125 if (not processing.exchange(true))
126 {
127 ARMARX_INFO << deactivateSpam() << "Processing update and provide pointcloud...";
128
129 switch (
130 visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
131 {
133 queryAndProvide<pcl::PointXYZ, arondto::PointCloudXYZ>();
134 break;
136 queryAndProvide<pcl::PointXYZL, arondto::PointCloudXYZL>();
137 break;
139 queryAndProvide<pcl::PointXYZRGBA, arondto::PointCloudXYZRGBA>();
140 break;
142 queryAndProvide<pcl::PointXYZRGBL, arondto::PointCloudXYZRGBL>();
143 break;
144 }
145
146 processing = false;
147 }
148 }
149
150 MetaPointCloudFormatPtr
152 {
153 // Copied from AK PointCloudProvider
154 MetaPointCloudFormatPtr info = new MetaPointCloudFormat();
155
156 switch (visionx::armem::pointcloud::String2PointType.at(simox::alg::to_lower(p.pointtype)))
157 {
159 info->type = PointContentType::ePoints;
160 info->capacity = p.width * p.height * sizeof(Point3D);
161 break;
163 info->type = PointContentType::eLabeledPoints;
164 info->capacity = p.width * p.height * sizeof(LabeledPoint3D);
165 break;
167 info->type = PointContentType::eColoredPoints;
168 info->capacity = p.width * p.height * sizeof(ColoredPoint3D);
169 break;
171 info->type = PointContentType::eColoredLabeledPoints;
172 info->capacity = p.width * p.height * sizeof(ColoredLabeledPoint3D);
173 break;
174 }
175
176 //info->frameId = getProperty<std::string>("frameId").getValue();
177 info->size = info->capacity;
178 return info;
179 }
180
181 template <class PointT>
182 void
183 ArMemToPointCloudProvider::registerToUpdates()
184 {
185 // Connect to memory updates.
186 auto callback = [this](const armarx::armem::MemoryID& entityID,
187 const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
188 { this->processUpdates(entityID, updatedSnapshotIDs); };
190 p.memoryName, p.getCoreSegmentName<PointT>(), p.providerSegmentName, p.entityName);
191 ARMARX_INFO << "Listening to: " << listenTo.str();
192 memoryNameSystem().subscribe(listenTo, callback);
193 }
194
195 template <class PointT, class AronT>
196 void
197 ArMemToPointCloudProvider::queryAndProvide()
198 {
199 // query latest pc from memory
200 armarx::armem::MemoryID toQuery(
201 p.memoryName, p.getCoreSegmentName<PointT>(), p.providerSegmentName, p.entityName);
202 std::optional<armarx::armem::wm::EntitySnapshot> data =
203 memoryReader.getLatestSnapshotIn(toQuery);
204
205 if (data.has_value())
206 {
207 ARMARX_CHECK(data->size() == 1);
208
209 armarx::aron::data::DictPtr aron = data->getInstance(0).data();
210 const armarx::aron::Path elementPath = armarx::aron::Path::FromString(
211 p.elementPath, armarx::aron::Path::DEFAULT_ROOT_IDENTIFIER, p.elementPathDelimiter);
212
213 armarx::aron::data::VariantPtr aronPointCloud = aron->navigateRelative(elementPath);
214 ARMARX_CHECK_NOT_NULL(aronPointCloud);
215
217 aronAdapted->setElement("pointcloud", aronPointCloud);
218
219 AronT arondto = AronT::FromAron(aronAdapted);
220
221 typename pcl::PointCloud<PointT>::Ptr toProvide(new pcl::PointCloud<PointT>());
222 *toProvide = arondto.pointcloud;
223
224 if (p.convertToGlobalFrame)
225 {
226
227 ARMARX_INFO << "Source frame " << p.frame;
228
229 ARMARX_INFO << deactivateSpam(10) << "Converting to global frame";
230 const auto robot =
231 virtualRobotReaderPlugin->get().getSynchronizedRobot(p.robotName);
233 *toProvide, *toProvide, p.frame, armarx::GlobalFrame, robot);
234 }
235
236 ARMARX_VERBOSE << "Providing point cloud with " << toProvide->points.size()
237 << " points.";
238
239
240 providePointCloud(toProvide);
241 }
242 }
243
244 template <class PointT>
245 std::string
246 ArMemToPointCloudProvider::Properties::getCoreSegmentName() const
247 {
248 if (coreSegmentName.empty() or coreSegmentName == "(auto)")
249 {
251 }
252 else
253 {
254 return coreSegmentName;
255 }
256 }
257
260
261} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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.