GraspCandidateObserver.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 as
6* published by the Free Software Foundation; either version 2 of
7* the License, or (at your option) any later version.
8*
9* ArmarX is distributed in the hope that it will be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU Lesser General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with this program. If not, see <http://www.gnu.org/licenses/>.
16*
17* @package RobotAPI
18* @author Simon Ottenhaus
19* @copyright 2019 Humanoids Group, H2T, KIT
20* @license http://www.gnu.org/licenses/gpl-2.0.txt
21* GNU General Public License
22*/
23
25
26//#include <ArmarXCore/core/checks/ConditionCheckEqualsPoseWithTolerance.h>
33
36
37#define TCP_POSE_CHANNEL "TCPPose"
38#define TCP_TRANS_VELOCITIES_CHANNEL "TCPVelocities"
39using namespace armarx;
40using namespace armarx::grasping;
41
45
46void
48{
49 usingTopic(getProperty<std::string>("GraspCandidatesTopicName").getValue());
50 offeringTopic(getProperty<std::string>("ConfigTopicName").getValue());
51}
52
53void
55{
57 getProperty<std::string>("ConfigTopicName").getValue());
58 graspCandidateWriter.connect(memoryNameSystem());
59}
60
67
68bool
69GraspCandidateObserver::FilterMatches(const CandidateFilterConditionPtr& filter,
70 const std::string& providerName,
71 const GraspCandidatePtr& candidate)
72{
73 if (filter->providerName != "*" && filter->providerName != providerName)
74 {
75 return false;
76 }
77 if (filter->minimumSuccessProbability > candidate->graspSuccessProbability)
78 {
79 return false;
80 }
81 if (filter->approach != AnyApproach &&
82 (candidate->executionHints == 0 || filter->approach != candidate->executionHints->approach))
83 {
84 return false;
85 }
86 if (filter->preshape != AnyAperture &&
87 (candidate->executionHints == 0 || filter->preshape != candidate->executionHints->preshape))
88 {
89 return false;
90 }
91 if (filter->objectType != objpose::AnyObject && filter->objectType != candidate->objectType)
92 {
93 return false;
94 }
95 return true;
96}
97
98std::string
100{
101 switch (type)
102 {
103 case objpose::AnyObject:
104 return "AnyObject";
105 case objpose::KnownObject:
106 return "KnownObject";
107 case objpose::UnknownObject:
108 return "UnknownObject";
109 default:
110 return "ERROR";
111 }
112}
113
114void
115GraspCandidateObserver::handleProviderUpdate(const std::string& providerName, int candidateCount)
116{
117 if (updateCounters.count(providerName) == 0)
118 {
119 updateCounters[providerName] = 0;
120 }
121 updateCounters[providerName]++;
122 if (providers.count(providerName) == 0)
123 {
124 providers[providerName] = new ProviderInfo();
125 }
126
127 if (!existsChannel(providerName))
128 {
129 offerChannel(providerName, "Channel of " + providerName);
130 }
131 offerOrUpdateDataField(providerName,
132 "updateCounter",
133 Variant(updateCounters[providerName]),
134 "Counter that increases for each update");
136 providerName, "candidateCount", Variant(candidateCount), "Number of provided candiates");
137}
138
139void
140GraspCandidateObserver::reportGraspCandidates(const std::string& providerName,
141 const GraspCandidateSeq& candidates,
142 const Ice::Current&)
143{
144 std::unique_lock lock(dataMutex);
145 this->candidates[providerName] = candidates;
146 graspCandidateWriter.commitGraspCandidateSeq(
147 candidates, armarx::armem::Time::Now(), providerName);
148 handleProviderUpdate(providerName, candidates.size());
149}
150
151void
153 const BimanualGraspCandidateSeq& candidates,
154 const Ice::Current&)
155{
156 std::unique_lock lock(dataMutex);
157 this->bimanualCandidates[providerName] = candidates;
158 handleProviderUpdate(providerName, candidates.size());
159}
160
161void
162GraspCandidateObserver::reportProviderInfo(const std::string& providerName,
163 const ProviderInfoPtr& info,
164 const Ice::Current&)
165{
166 std::unique_lock lock(dataMutex);
167 providers[providerName] = info;
168 if (updateCounters.count(providerName) == 0)
169 {
170 updateCounters[providerName] = 0;
171 }
172
173
174 if (!existsChannel(providerName))
175 {
176 offerChannel(providerName, "Channel of " + providerName);
177 }
178 offerOrUpdateDataField(providerName, "objectType", ObjectTypeToString(info->objectType), "");
179}
180
181InfoMap
183{
184 std::unique_lock lock(dataMutex);
185 return providers;
186}
187
188StringSeq
190{
191 std::unique_lock lock(dataMutex);
192 return getAvailableProviderNames();
193}
194
195ProviderInfoPtr
196GraspCandidateObserver::getProviderInfo(const std::string& providerName, const Ice::Current&)
197{
198 std::unique_lock lock(dataMutex);
199 checkHasProvider(providerName);
200 return providers[providerName];
201}
202
203bool
204GraspCandidateObserver::hasProvider(const std::string& providerName, const Ice::Current& c)
205{
206 std::unique_lock lock(dataMutex);
207 return hasProvider(providerName);
208}
209
210GraspCandidateSeq
212{
213 std::unique_lock lock(dataMutex);
214 GraspCandidateSeq all;
215 for (const auto& pair : candidates)
216 {
217 all.insert(all.end(), pair.second.begin(), pair.second.end());
218 }
219 return all;
220}
221
222GraspCandidateSeq
224 const Ice::Current& c)
225{
226 return getCandidatesByProviders(Ice::StringSeq{providerName});
227}
228
229GraspCandidateSeq
230GraspCandidateObserver::getCandidatesByProviders(const Ice::StringSeq& providerNames,
231 const Ice::Current& c)
232{
233 std::unique_lock lock(dataMutex);
234 GraspCandidateSeq all;
235 for (const auto& pr : providerNames)
236 {
237 const auto it = candidates.find(pr);
238 if (it != candidates.end())
239 {
240 all.insert(all.end(), it->second.begin(), it->second.end());
241 }
242 }
243 return all;
244}
245
246GraspCandidateSeq
247GraspCandidateObserver::getCandidatesByFilter(const CandidateFilterConditionPtr& filter,
248 const Ice::Current&)
249{
250 std::unique_lock lock(dataMutex);
251 GraspCandidateSeq matching;
252 for (const auto& pair : candidates)
253 {
254 for (const grasping::GraspCandidatePtr& candidate : pair.second)
255 {
256 if (FilterMatches(filter, pair.first, candidate))
257 {
258 matching.push_back(candidate);
259 }
260 }
261 }
262 return matching;
263}
264
265Ice::Int
267 const Ice::Current&)
268{
269 std::unique_lock lock(dataMutex);
270 checkHasProvider(providerName);
271 return updateCounters[providerName];
272}
273
274IntMap
275GraspCandidateObserver::getAllUpdateCounters(const Ice::Current& providerName)
276{
277 std::unique_lock lock(dataMutex);
278 return updateCounters;
279}
280
281bool
282GraspCandidateObserver::setProviderConfig(const std::string& providerName,
283 const StringVariantBaseMap& config,
284 const Ice::Current&)
285{
286 std::unique_lock lock(dataMutex);
287 if (providers.count(providerName) == 0)
288 {
289 return false;
290 }
291 configTopic->setServiceConfig(providerName, config);
292 return true;
293}
294
295void
296GraspCandidateObserver::setSelectedCandidates(const GraspCandidateSeq& candidates,
297 const Ice::Current&)
298{
299 std::unique_lock lock(selectedCandidatesMutex);
300 selectedCandidates = candidates;
301}
302
303GraspCandidateSeq
305{
306 std::unique_lock lock(selectedCandidatesMutex);
307 return selectedCandidates;
308}
309
310BimanualGraspCandidateSeq
312{
313 std::unique_lock lock(dataMutex);
314 BimanualGraspCandidateSeq all;
315 for (const auto& pair : bimanualCandidates)
316 {
317 all.insert(all.end(), pair.second.begin(), pair.second.end());
318 }
319 return all;
320}
321
322void
324 const grasping::BimanualGraspCandidateSeq& candidates,
325 const Ice::Current&)
326{
327 std::unique_lock lock(selectedCandidatesMutex);
328 selectedBimanualCandidates = candidates;
329}
330
331BimanualGraspCandidateSeq
333{
334 std::unique_lock lock(selectedCandidatesMutex);
335 return selectedBimanualCandidates;
336}
337
338void
340 const Ice::Current&)
341{
342 std::unique_lock lock(dataMutex);
343
344 if (candidates.count(providerName))
345 {
346 candidates[providerName].clear();
347 }
348}
349
350bool
351GraspCandidateObserver::hasProvider(const std::string& providerName)
352{
353 return providers.count(providerName) > 0;
354}
355
356void
357GraspCandidateObserver::checkHasProvider(const std::string& providerName)
358{
359 if (!hasProvider(providerName))
360 {
361 throw LocalException("Unknown provider name '")
362 << providerName << "'. Available providers: " << getAvailableProviderNames();
363 }
364}
365
366StringSeq
368{
369 StringSeq names;
370 for (const auto& pair : providers)
371 {
372 names.push_back(pair.first);
373 }
374 return names;
375}
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void reportBimanualGraspCandidates(const std::string &providerName, const grasping::BimanualGraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
void onConnectObserver() override
Framework hook.
grasping::GraspCandidateSeq getCandidatesByProvider(const std::string &providerName, const Ice::Current &c=Ice::emptyCurrent) override
grasping::ProviderInfoPtr getProviderInfo(const std::string &providerName, const ::Ice::Current &=Ice::emptyCurrent) override
grasping::BimanualGraspCandidateSeq getSelectedBimanualCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
bool setProviderConfig(const std::string &providerName, const StringVariantBaseMap &config, const ::Ice::Current &=Ice::emptyCurrent) override
grasping::GraspCandidateSeq getAllCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
void setSelectedCandidates(const grasping::GraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
grasping::BimanualGraspCandidateSeq getAllBimanualCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
void reportGraspCandidates(const std::string &providerName, const grasping::GraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
grasping::IntMap getAllUpdateCounters(const Ice::Current &providerName) override
static std::string ObjectTypeToString(objpose::ObjectType type)
void setSelectedBimanualCandidates(const grasping::BimanualGraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
PropertyDefinitionsPtr createPropertyDefinitions() override
void clearCandidatesByProvider(const std::string &providerName, const Ice::Current &c) override
grasping::GraspCandidateSeq getSelectedCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
void onInitObserver() override
Framework hook.
void reportProviderInfo(const std::string &providerName, const grasping::ProviderInfoPtr &info, const ::Ice::Current &=Ice::emptyCurrent) override
static bool FilterMatches(const grasping::CandidateFilterConditionPtr &filter, const std::string &providerName, const grasping::GraspCandidatePtr &candidate)
grasping::GraspCandidateSeq getCandidatesByFilter(const grasping::CandidateFilterConditionPtr &filter, const ::Ice::Current &=Ice::emptyCurrent) override
grasping::StringSeq getAvailableProviderNames(const ::Ice::Current &) override
bool hasProvider(const std::string &providerName, const Ice::Current &c) override
grasping::GraspCandidateSeq getCandidatesByProviders(const Ice::StringSeq &providerNames, const Ice::Current &c=Ice::emptyCurrent) override
grasping::InfoMap getAvailableProvidersWithInfo(const ::Ice::Current &=Ice::emptyCurrent) override
Ice::Int getUpdateCounterByProvider(const std::string &providerName, const ::Ice::Current &=Ice::emptyCurrent) override
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
bool existsChannel(const std::string &channelName) const
void offerChannel(std::string channelName, std::string description)
Offer a channel.
Definition Observer.cpp:131
bool offerOrUpdateDataField(std::string channelName, std::string datafieldName, const Variant &value, const std::string &description)
Definition Observer.cpp:242
static DateTime Now()
Definition DateTime.cpp:51
const simox::meta::IntEnumNames names
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.