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 
24 #include "GraspCandidateObserver.h"
25 
26 //#include <ArmarXCore/core/checks/ConditionCheckEqualsPoseWithTolerance.h>
33 
36 
37 #define TCP_POSE_CHANNEL "TCPPose"
38 #define TCP_TRANS_VELOCITIES_CHANNEL "TCPVelocities"
39 using namespace armarx;
40 using namespace armarx::grasping;
41 
43 {
44 }
45 
46 void
48 {
49  usingTopic(getProperty<std::string>("GraspCandidatesTopicName").getValue());
50  offeringTopic(getProperty<std::string>("ConfigTopicName").getValue());
51 }
52 
53 void
55 {
56  configTopic = getTopic<GraspCandidateProviderInterfacePrx>(
57  getProperty<std::string>("ConfigTopicName").getValue());
58  graspCandidateWriter.connect(memoryNameSystem());
59 }
60 
63 {
65  new GraspCandidateObserverPropertyDefinitions(getConfigIdentifier()));
66 }
67 
68 bool
69 GraspCandidateObserver::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 
98 std::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 
114 void
115 GraspCandidateObserver::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");
135  offerOrUpdateDataField(
136  providerName, "candidateCount", Variant(candidateCount), "Number of provided candiates");
137 }
138 
139 void
140 GraspCandidateObserver::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 
151 void
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 
161 void
162 GraspCandidateObserver::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 
181 InfoMap
183 {
184  std::unique_lock lock(dataMutex);
185  return providers;
186 }
187 
188 StringSeq
189 GraspCandidateObserver::getAvailableProviderNames(const Ice::Current&)
190 {
191  std::unique_lock lock(dataMutex);
192  return getAvailableProviderNames();
193 }
194 
195 ProviderInfoPtr
196 GraspCandidateObserver::getProviderInfo(const std::string& providerName, const Ice::Current&)
197 {
198  std::unique_lock lock(dataMutex);
199  checkHasProvider(providerName);
200  return providers[providerName];
201 }
202 
203 bool
204 GraspCandidateObserver::hasProvider(const std::string& providerName, const Ice::Current& c)
205 {
206  std::unique_lock lock(dataMutex);
207  return hasProvider(providerName);
208 }
209 
210 GraspCandidateSeq
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 
222 GraspCandidateSeq
223 GraspCandidateObserver::getCandidatesByProvider(const std::string& providerName,
224  const Ice::Current& c)
225 {
226  return getCandidatesByProviders(Ice::StringSeq{providerName});
227 }
228 
229 GraspCandidateSeq
230 GraspCandidateObserver::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 
246 GraspCandidateSeq
247 GraspCandidateObserver::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 
265 Ice::Int
267  const Ice::Current&)
268 {
269  std::unique_lock lock(dataMutex);
270  checkHasProvider(providerName);
271  return updateCounters[providerName];
272 }
273 
274 IntMap
275 GraspCandidateObserver::getAllUpdateCounters(const Ice::Current& providerName)
276 {
277  std::unique_lock lock(dataMutex);
278  return updateCounters;
279 }
280 
281 bool
282 GraspCandidateObserver::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 
295 void
296 GraspCandidateObserver::setSelectedCandidates(const GraspCandidateSeq& candidates,
297  const Ice::Current&)
298 {
299  std::unique_lock lock(selectedCandidatesMutex);
300  selectedCandidates = candidates;
301 }
302 
303 GraspCandidateSeq
305 {
306  std::unique_lock lock(selectedCandidatesMutex);
307  return selectedCandidates;
308 }
309 
310 BimanualGraspCandidateSeq
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 
322 void
324  const grasping::BimanualGraspCandidateSeq& candidates,
325  const Ice::Current&)
326 {
327  std::unique_lock lock(selectedCandidatesMutex);
328  selectedBimanualCandidates = candidates;
329 }
330 
331 BimanualGraspCandidateSeq
333 {
334  std::unique_lock lock(selectedCandidatesMutex);
335  return selectedBimanualCandidates;
336 }
337 
338 void
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 
350 bool
351 GraspCandidateObserver::hasProvider(const std::string& providerName)
352 {
353  return providers.count(providerName) > 0;
354 }
355 
356 void
357 GraspCandidateObserver::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 
366 StringSeq
367 GraspCandidateObserver::getAvailableProviderNames()
368 {
369  StringSeq names;
370  for (const auto& pair : providers)
371  {
372  names.push_back(pair.first);
373  }
374  return names;
375 }
armarx::grasping
Definition: aron_conversions.h:9
armarx::GraspCandidateObserver::FilterMatches
static bool FilterMatches(const grasping::CandidateFilterConditionPtr &filter, const std::string &providerName, const grasping::GraspCandidatePtr &candidate)
Definition: GraspCandidateObserver.cpp:69
armarx::GraspCandidateObserverPropertyDefinitions
Definition: GraspCandidateObserver.h:39
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::GraspCandidateObserver::getCandidatesByFilter
grasping::GraspCandidateSeq getCandidatesByFilter(const grasping::CandidateFilterConditionPtr &filter, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:247
armarx::GraspCandidateObserver::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: GraspCandidateObserver.cpp:62
armarx::GraspCandidateObserver::getProviderInfo
grasping::ProviderInfoPtr getProviderInfo(const std::string &providerName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:196
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::GraspCandidateObserver::getAllBimanualCandidates
grasping::BimanualGraspCandidateSeq getAllBimanualCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:311
ConditionCheckEquals.h
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::GraspCandidateObserver::reportProviderInfo
void reportProviderInfo(const std::string &providerName, const grasping::ProviderInfoPtr &info, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:162
ConditionCheckInRange.h
armarx::GraspCandidateObserver::getCandidatesByProvider
grasping::GraspCandidateSeq getCandidatesByProvider(const std::string &providerName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:223
armarx::GraspCandidateObserver::getAllCandidates
grasping::GraspCandidateSeq getAllCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:211
armarx::GraspCandidateObserver::getCandidatesByProviders
grasping::GraspCandidateSeq getCandidatesByProviders(const Ice::StringSeq &providerNames, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:230
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
GraspCandidateObserver.h
armarx::GraspCandidateObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: GraspCandidateObserver.cpp:47
armarx::GraspCandidateObserver::clearCandidatesByProvider
void clearCandidatesByProvider(const std::string &providerName, const Ice::Current &c) override
Definition: GraspCandidateObserver.cpp:339
ConditionCheckEqualsPoseWithTolerance.h
armarx::GraspCandidateObserver::setProviderConfig
bool setProviderConfig(const std::string &providerName, const StringVariantBaseMap &config, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:282
armarx::GraspCandidateObserver::setSelectedBimanualCandidates
void setSelectedBimanualCandidates(const grasping::BimanualGraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:323
armarx::armem::client::query_fns::all
auto all()
Definition: query_fns.h:10
armarx::GraspCandidateObserver::getSelectedBimanualCandidates
grasping::BimanualGraspCandidateSeq getSelectedBimanualCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:332
armarx::GraspCandidateObserver::setSelectedCandidates
void setSelectedCandidates(const grasping::GraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:296
armarx::GraspCandidateObserver::ObjectTypeToString
static std::string ObjectTypeToString(objpose::ObjectType type)
Definition: GraspCandidateObserver.cpp:99
armarx::GraspCandidateObserver::reportBimanualGraspCandidates
void reportBimanualGraspCandidates(const std::string &providerName, const grasping::BimanualGraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:152
armarx::GraspCandidateObserver::GraspCandidateObserver
GraspCandidateObserver()
Definition: GraspCandidateObserver.cpp:42
armarx::GraspCandidateObserver::getAllUpdateCounters
grasping::IntMap getAllUpdateCounters(const Ice::Current &providerName) override
Definition: GraspCandidateObserver.cpp:275
ExpressionException.h
armarx::GraspCandidateObserver::getAvailableProvidersWithInfo
grasping::InfoMap getAvailableProvidersWithInfo(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:182
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
ConditionCheckSmaller.h
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
ConditionCheckMagnitudeChecks.h
ConditionCheckLarger.h
armarx::GraspCandidateObserver::hasProvider
bool hasProvider(const std::string &providerName, const Ice::Current &c) override
Definition: GraspCandidateObserver.cpp:204
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::GraspCandidateObserver::getUpdateCounterByProvider
Ice::Int getUpdateCounterByProvider(const std::string &providerName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:266
armarx::GraspCandidateObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: GraspCandidateObserver.cpp:54
armarx::GraspCandidateObserver::getSelectedCandidates
grasping::GraspCandidateSeq getSelectedCandidates(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:304
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GraspCandidateObserver::reportGraspCandidates
void reportGraspCandidates(const std::string &providerName, const grasping::GraspCandidateSeq &candidates, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraspCandidateObserver.cpp:140
ConditionCheckUpdated.h