ViewSelection.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2015-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents::ViewSelection
19  * @author David Schiebener (schiebener at kit dot edu)
20  * @author Markus Grotz ( markus dot grotz at kit dot edu )
21  * @date 2015
22  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23  * GNU General Public License
24  */
25 
26 #pragma once
27 
28 
29 #include <condition_variable>
30 #include <mutex>
31 #include <queue>
32 
33 #include <Eigen/Geometry>
34 
39 
44 #include <RobotAPI/interface/components/ViewSelectionInterface.h>
45 #include <RobotAPI/interface/core/RobotState.h>
47 
48 namespace armarx
49 {
50 
52  {
53  bool
54  operator()(ViewTargetBasePtr const& t1, ViewTargetBasePtr const& t2)
55  {
56  if (t1->priority == t2->priority)
57  {
58  return t1->timeAdded->timestamp < t2->timeAdded->timestamp;
59  }
60  return t1->priority < t2->priority;
61  }
62  };
63 
64  /**
65  * @class ViewSelectionPropertyDefinitions
66  * @brief
67  */
69  {
70  public:
72  {
73  defineOptionalProperty<std::string>(
74  "RobotStateComponentName",
75  "RobotStateComponent",
76  "Name of the robot state component that should be used");
77  defineOptionalProperty<std::string>(
78  "HeadIKUnitName",
79  "HeadIKUnit",
80  "Name of the head IK unit component that should be used");
81  defineOptionalProperty<std::string>("HeadIKKinematicChainName",
82  "IKVirtualGaze",
83  "Name of the kinematic chain for the head IK");
84  defineOptionalProperty<std::string>(
85  "HeadFrameName",
86  "Head Base",
87  "Name of the frame of the head base in the robot model");
88  defineOptionalProperty<std::string>(
89  "CameraFrameName",
90  "VirtualCentralGaze",
91  "Name of the frame of the head base in the robot model");
92  defineOptionalProperty<int>("SleepingTimeBetweenViewDirectionChanges",
93  2500,
94  "Time between two view changes, to keep the head looking "
95  "into one direction for a while (in ms)");
96  defineOptionalProperty<bool>("ActiveAtStartup",
97  true,
98  "Decide whether the automatic view selection will be "
99  "activated (can be changed via the proxy during runtime)");
100  defineOptionalProperty<bool>(
101  "VisualizeViewDirection", true, "Draw view ray on DebugLayer.");
102  defineOptionalProperty<float>(
103  "MaxOverallHeadTiltAngle",
104  55.0f,
105  "Maximal angle the head and eyes can look down (in degrees)");
106  defineOptionalProperty<float>(
107  "CentralHeadTiltAngle",
108  110.0f,
109  "Defines the height direction that will be considered 'central' in the reachable "
110  "area of the head (in degrees). Default is looking 20 degrees downwards");
111  defineOptionalProperty<float>(
112  "ProbabilityToLookForALostObject",
113  0.03f,
114  "Probability that one of the objects that have been seen but could later not been "
115  "localized again will be included in the view selection");
116  defineOptionalProperty<float>(
117  "VisuSaliencyThreshold",
118  0.0f,
119  "If greater than zero the saliency map is drawn into the debug drawer on each "
120  "iteration. The value is used as minimum saliency threshold for a point to be "
121  "shown in debug visu");
122  }
123  };
124 
125  /**
126  * @defgroup Component-ViewSelection ViewSelection
127  * @ingroup RobotAPI-Components
128  * @brief The ViewSelection component controls the head of the robot with inverse kinematics based on the uncertainty
129  * of the current requested object locations.
130  * The uncertainty of objects grow based on their motion model and the timed passed since the last localization.
131  * It can be activated or deactivated with the Ice interface and given manual target positions to look at.
132  */
133 
134  /**
135  * @ingroup Component-ViewSelection
136  * @brief The ViewSelection class
137  */
138  class ViewSelection : virtual public Component, virtual public ViewSelectionInterface
139  {
140  public:
141  /**
142  * @see armarx::ManagedIceObject::getDefaultName()
143  */
144  std::string
145  getDefaultName() const override
146  {
147  return "ViewSelection";
148  }
149 
150  protected:
151  /**
152  * @see armarx::ManagedIceObject::onInitComponent()
153  */
154  void onInitComponent() override;
155 
156  /**
157  * @see armarx::ManagedIceObject::onConnectComponent()
158  */
159  void onConnectComponent() override;
160 
161  /**
162  * @see armarx::ManagedIceObject::onDisconnectComponent()
163  */
164  void onDisconnectComponent() override;
165 
166  /**
167  * @see armarx::ManagedIceObject::onExitComponent()
168  */
169  void onExitComponent() override;
170 
171  /**
172  * @see PropertyUser::createPropertyDefinitions()
173  */
175 
176  void addManualViewTarget(const FramedPositionBasePtr& target,
177  const Ice::Current& c = Ice::emptyCurrent) override;
178 
179  void clearManualViewTargets(const Ice::Current& c = Ice::emptyCurrent) override;
180 
181  ViewTargetList getManualViewTargets(const Ice::Current& c = Ice::emptyCurrent) override;
182 
183  void
184  activateAutomaticViewSelection(const Ice::Current& c = Ice::emptyCurrent) override
185  {
186  std::unique_lock lock(manualViewTargetsMutex);
187 
188  ARMARX_INFO << "activating automatic view selection";
189 
190  doAutomaticViewSelection = true;
191  viewSelectionObserver->onActivateAutomaticViewSelection();
192  }
193 
194  void
195  deactivateAutomaticViewSelection(const Ice::Current& c = Ice::emptyCurrent) override
196  {
197  std::unique_lock lock(manualViewTargetsMutex);
198 
199  ARMARX_INFO << "deactivating automatic view selection";
200 
201  doAutomaticViewSelection = false;
202  viewSelectionObserver->onDeactivateAutomaticViewSelection();
203  }
204 
205  bool
206  isEnabledAutomaticViewSelection(const Ice::Current& c = Ice::emptyCurrent) override
207  {
208  std::unique_lock lock(manualViewTargetsMutex);
209 
210  return doAutomaticViewSelection;
211  }
212 
213  void updateSaliencyMap(const SaliencyMapBasePtr& map,
214  const Ice::Current& c = Ice::emptyCurrent) override;
215 
216  void removeSaliencyMap(const std::string& name,
217  const Ice::Current& c = Ice::emptyCurrent) override;
218 
219  ::Ice::StringSeq getSaliencyMapNames(const Ice::Current& c = Ice::emptyCurrent) override;
220 
221  void drawSaliencySphere(const ::Ice::StringSeq& names,
222  const Ice::Current& c = Ice::emptyCurrent) override;
223 
224  void clearSaliencySphere(const Ice::Current& c = Ice::emptyCurrent) override;
225 
226 
227  private:
228  void process();
229 
230  ViewTargetBasePtr nextAutomaticViewTarget();
231 
232  void getActiveSaliencyMaps(std::vector<std::string>& activeSaliencyMaps);
233 
235 
236  RobotStateComponentInterfacePrx robotStateComponent;
237  HeadIKUnitInterfacePrx headIKUnitProxy;
239 
240  ViewSelectionObserverPrx viewSelectionObserver;
241 
242  std::string headIKKinematicChainName;
243  std::string headFrameName;
244  std::string cameraFrameName;
245 
246  CIntensityGraph* visibilityMaskGraph;
247 
248  float sleepingTimeBetweenViewDirectionChanges;
249  IceUtil::Time timeOfLastViewChange;
250 
251  bool drawViewDirection;
252 
253  std::mutex manualViewTargetsMutex;
254  std::priority_queue<ViewTargetBasePtr, std::vector<ViewTargetBasePtr>, CompareViewTargets>
255  manualViewTargets;
256 
257  bool doAutomaticViewSelection;
258 
259  Eigen::Vector3f offsetToHeadCenter;
260 
261  std::condition_variable condition;
262  bool hasNewSaliencyMap;
263  std::mutex syncMutex;
264 
265  std::map<std::string, SaliencyMapBasePtr> saliencyMaps;
266 
267  float visuSaliencyThreshold;
268  };
269 } // namespace armarx
armarx::ViewSelection::deactivateAutomaticViewSelection
void deactivateAutomaticViewSelection(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.h:195
armarx::ViewSelection::drawSaliencySphere
void drawSaliencySphere(const ::Ice::StringSeq &names, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:443
armarx::ViewSelection::getSaliencyMapNames
::Ice::StringSeq getSaliencyMapNames(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:540
armarx::ViewSelectionPropertyDefinitions::ViewSelectionPropertyDefinitions
ViewSelectionPropertyDefinitions(std::string prefix)
Definition: ViewSelection.h:71
armarx::ViewSelection::updateSaliencyMap
void updateSaliencyMap(const SaliencyMapBasePtr &map, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:429
armarx::ViewSelection::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ViewSelection.cpp:555
armarx::ViewSelection::onConnectComponent
void onConnectComponent() override
Definition: ViewSelection.cpp:110
armarx::ViewSelection::onExitComponent
void onExitComponent() override
Definition: ViewSelection.cpp:147
Pose.h
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:668
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
armarx::ViewSelection::getManualViewTargets
ViewTargetList getManualViewTargets(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:409
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
RunningTask.h
armarx::CompareViewTargets
Definition: ViewSelection.h:51
GraphPyramidLookupTable.h
IntensityGraph.h
armarx::ViewSelection
The ViewSelection class.
Definition: ViewSelection.h:138
armarx::ViewSelection::addManualViewTarget
void addManualViewTarget(const FramedPositionBasePtr &target, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:375
armarx::ViewSelection::removeSaliencyMap
void removeSaliencyMap(const std::string &name, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:527
CIntensityGraph
Definition: IntensityGraph.h:51
MathTools.h
TimestampVariant.h
armarx::ViewSelection::clearManualViewTargets
void clearManualViewTargets(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:395
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::ViewSelection::activateAutomaticViewSelection
void activateAutomaticViewSelection(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.h:184
armarx::ViewSelection::clearSaliencySphere
void clearSaliencySphere(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.cpp:521
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:13
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ViewSelection::getDefaultName
std::string getDefaultName() const override
Definition: ViewSelection.h:145
IceUtil::Handle
Definition: forward_declarations.h:30
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::CompareViewTargets::operator()
bool operator()(ViewTargetBasePtr const &t1, ViewTargetBasePtr const &t2)
Definition: ViewSelection.h:54
armarx::ViewSelection::onInitComponent
void onInitComponent() override
Definition: ViewSelection.cpp:39
armarx::ViewSelection::onDisconnectComponent
void onDisconnectComponent() override
Definition: ViewSelection.cpp:126
HeadIKUnit.h
armarx::ViewSelection::isEnabledAutomaticViewSelection
bool isEnabledAutomaticViewSelection(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ViewSelection.h:206
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ViewSelectionPropertyDefinitions
Definition: ViewSelection.h:68