GraspingManager.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::GraspingManager
19  * @author Valerij Wittenbeck (valerij dot wittenbeck at student dot kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
29 #include <RobotComponents/interface/components/GraspingManager/GraspingManagerInterface.h>
30 #include <RobotComponents/interface/components/GraspingManager/GraspGeneratorInterface.h>
31 #include <RobotComponents/interface/components/GraspingManager/GraspSelectionManagerInterface.h>
32 #include <RobotComponents/interface/components/GraspingManager/RobotPlacementInterface.h>
33 #include <RobotComponents/interface/components/MotionPlanning/MotionPlanningServer.h>
34 //#include <RobotComponents/interface/components/RobotIK.h>
35 #include <RobotComponents/interface/components/PlannedMotionProviderInterface.h>
37 
38 #include <RobotAPI/interface/core/RobotState.h>
39 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
40 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
41 #include <MemoryX/interface/gui/EntityDrawerInterface.h>
44 #include <Eigen/Geometry>
48 
49 namespace armarx
50 {
51  /**
52  * @class GraspingManagerPropertyDefinitions
53  * @brief
54  */
57  {
58  public:
61  {
62  defineOptionalProperty<std::string>("GraspGeneratorName", "SimpleGraspGenerator", "Name of the GraspGenerator proxy");
63  defineOptionalProperty<std::string>("RobotPlacementName", "SimpleRobotPlacement", "Name of the RobotPlacement proxy");
64  defineOptionalProperty<std::string>("RobotNodeSetNames", "HipYawRightArm;HipYawLeftArm", "Names of the robot node sets to use for IK calculations (';' delimited)");
65  defineOptionalProperty<std::string>("RobotCollisionNodeSet", "Robot", "Name of the collision nodeset used for motion planning", PropertyDefinitionBase::eModifiable);
66  defineRequiredProperty<std::string>("JointToLinkSetMapping", "Mapping from joint set to link set, i.e. which collision set to use for which kinematic chain. Format: JointSet1:CollisionSet1;JointSet2:CollisionSet2", PropertyDefinitionBase::eModifiable);
67  defineOptionalProperty<std::string>("ReachabilitySpaceFilePaths",
68  "Armar3/reachability/reachability_right_hand_smoothened.bin;Armar3/reachability/reachability_left_hand_smoothened.bin",
69  "Paths to the reachability space files (needed only if no reachability space is loaded for the chosen RobotNodeSets) (';' delimited)");
70  defineOptionalProperty<std::string>("PlanningBoundingBox", "-15000, -4000, -3.1416, 15000, 15000, 3.1416", "x_min, y_min, alpha_min, x_max, y_max, alpha_max");
71  defineOptionalProperty<float>("VisualizationSlowdownFactor", 1.0f, "1.0 is a good value for clear visualization, 0 the visualization should not slow down the process", PropertyDefinitionBase::eModifiable);
72  defineOptionalProperty<bool>("EnableVisualization", true, "If false no visualization is done.", PropertyDefinitionBase::eModifiable);
73  defineOptionalProperty<float>("MaxDistanceForDirectGrasp", 1200.0f, "x-y-Distance from robot base to object for which the grasping manager tries to find a solution without platform movement", PropertyDefinitionBase::eModifiable);
74  defineOptionalProperty<bool>("UseVoxelGridCSpace", false, "If true, the VoxelGridCSpace is used instead of the SimoxCSpace.", PropertyDefinitionBase::eModifiable);
75  defineOptionalProperty<std::string>("VoxelGridProviderName", "VoxelGridProvider", "Name of the Voxel Grid Provider", PropertyDefinitionBase::eModifiable);
76  defineOptionalProperty<bool>("FilterUnnaturalGrasps", false, "If set to true, uses the NaturalGraspFilter to exclude unnatural looking grasps and grasp poses.", PropertyDefinitionBase::eModifiable);
77 
78 
79 
80  }
81  };
82 
83  /**
84  * @defgroup Component-GraspingManager GraspingManager
85  * @ingroup RobotComponents-Components
86  * @brief Provides methods for grasp planning purposes.
87  *
88  * The GraspingManager implements a grasping pipeline containing the following stages (each stage is implemented by the respective Component in the parathesis):
89  * \li Grasp generation (SimpleGraspGenerator)
90  * \li Grasp filtering (GraspSelectionManager)
91  * \li Robot placement (SimpleRobotPlacement)
92  * \li Finding a target configuration (RobotIK)
93  * \li Trajectory planning (MotionPlanningServer)
94  *
95  * The stages are executed sequentially.
96  * The Input for the grasping pipeline is a graspable object.
97  * The output contains trajectories in configuration and pose space to execute a grasp for the given object.
98  *
99  * The GraspingManager requires the following properties:
100  * \li a list of robotnodeset names (; delimited) for IK calculations
101  * \li a list of workspace files (; delimited) for reachability considerations
102  *
103  * \note The Armar3Simulation scenario must be active before running the GraspingManager scenario.
104  */
105 
106  /**
107  * @ingroup Component-GraspingManager
108  * @brief The GraspingManager class
109  */
111  virtual public Component,
112  virtual public GraspingManagerInterface,
113  virtual public NaturalIKComponentPluginUser
114  {
115  public:
116  /**
117  * @see armarx::ManagedIceObject::getDefaultName()
118  */
119  std::string getDefaultName() const override
120  {
121  return "GraspingManager";
122  }
123 
124  /**
125  * @brief Runs the grasping pipeline.
126  *
127  * Checks if the robot is close enough to the object to directly grasp it, attempting to calculate the
128  * inverse kinematics directly. In case nothing is found, tries to calculate the movement a robotPlacement
129  * is generated and the calculation of the IKs is repeated for the new position.
130  *
131  * @param objectInstanceEntityId the id for retrieval of the target object from working memory.
132  * @param c
133  * @return A list of grasping trajectories in configuration and pose space.
134  */
135  GraspingTrajectory generateGraspingTrajectory(const std::string& objectInstanceEntityId, const Ice::Current& c = Ice::emptyCurrent) override;
136  GraspingTrajectoryList generateGraspingTrajectoryList(const std::string& objectInstanceEntityId, const Ice::Current& c = Ice::emptyCurrent) override;
137  GraspingTrajectoryList generateGraspingTrajectoryListForGraspList(const GeneratedGraspList& grasps, const Ice::Current& c = Ice::emptyCurrent) override;
138  void visualizeGraspingTrajectory(const GraspingTrajectory& trajectory, float visuSlowdownFactor, const Ice::Current& c = Ice::emptyCurrent) override;
139 
140  MotionPlanningDataList generateIKs(const std::string& objectInstanceEntityId, const Ice::Current& c = Ice::emptyCurrent) override;
141 
142 
143  ///// single step methods /////
144  GeneratedGraspList generateGraspsByObjectName(const std::string& objectName, const Ice::Current& = Ice::emptyCurrent) override;
145 
146 
147  protected:
148  /**
149  * @see armarx::ManagedIceObject::onInitComponent()
150  */
151  void onInitComponent() override;
152 
153  /**
154  * @see armarx::ManagedIceObject::onConnectComponent()
155  */
156  void onConnectComponent() override;
157 
158  /**
159  * @see armarx::ManagedIceObject::onDisconnectComponent()
160  */
161  void onDisconnectComponent() override;
162 
163  /**
164  * @see armarx::ManagedIceObject::onExitComponent()
165  */
166  void onExitComponent() override;
167 
168  /**
169  * @see PropertyUser::createPropertyDefinitions()
170  */
172  {
174  }
175 
176  /**
177  * @brief Visualizes the grasp using the DebugDrawer topic
178  */
179  std::pair<std::string, Ice::StringSeq> visualizeGrasp(const GeneratedGrasp& grasp, int id, const DrawColor& color = DrawColor {1, 1, 1, 1});
180  private:
181  /**
182  * @brief Creates a pointer to the SimoxCSpace::SimoxCSpace() and initializes it.
183  *
184  * Creates the pointer, adds objects from the working memory and sets up the AgentPlanningInformation.
185  * The Agent is then linked to the pointer and the CSpace is initialized.
186  * @return Pointer to a SimoxCSpace
187  */
188  SimoxCSpacePtr createCSpace();
189 
190  /**
191  * @brief Wrapper for the GraspGeneratorInterface::generateGrasps() method of the GraspGenerator.
192  * @param objectInstanceEntityId
193  *
194  * Gets a list of grasps from the GraspGenerator and sorts them depending on their score. If no grasps are
195  * generated, a warning is thrown. Initiates the visualization of the grasps.
196  * @return
197  */
198  GeneratedGraspList generateGrasps(const std::string& objectInstanceEntityId);
199 
200  /**
201  * @brief Wrapper for the GraspSelectionManagerInterface::filterGrasps() method of the GraspSelectionManager.
202  */
203  GeneratedGraspList filterGrasps(const GeneratedGraspList& grasps);
204  /**
205  * @brief Wrapper for the GraspSelectionManagerInterface::filterPlacements() method o
206  * the GraspSelectionManager.
207  */
208  GraspingPlacementList filterPlacements(const GraspingPlacementList& placements);
209 
210  /**
211  * Wrapper for the RobotPlacementInterface::generateRobotPlacements().
212  * @param grasps
213  * @param objectInstanceEntityId
214  * @return List of grasping placements.
215  */
216  GraspingPlacementList generateRobotPlacements(const GeneratedGraspList& grasps);
217 
218  /**
219  * Wrapper for the PlannedMotionProviderInterface::planMotion() method.
220  *
221  * Calculates the center and size of the bounding box and initializes one SimoxCSpace for the arms and one for
222  * the platform.
223  *
224  * @param mpd
225  * @return
226  */
227  GraspingTrajectory planMotion(const MotionPlanningData& mpd);
228 
229  /**
230  * @brief Draws the trajectory in the DebugDrawer topic.
231  * @param t
232  */
233  void drawTrajectory(const GraspingTrajectory& t, float visuSlowdownFactor);
234 
235  /**
236  * @brief Iterates through the list of found graspPlacements to calculate SingleIKs
237  *
238  * Iterates through all found graspPlacements and calls the GraspingManager::calculateSingleIK() method with
239  * the correct parameters. Then calculates the required parameters, like the currentRobotPose, GlobalFrame,
240  * desiredRobotPose, currentConfig, ikSolution, rnsToUse, gp.grasp.eefName and gp.grasp, for the
241  * MotionPlanningData for each non-empty IKSolution.
242  *
243  * @return Vector of of MotionPlanningData
244  */
245  std::vector<MotionPlanningData> calculateIKs(const GraspingPlacementList& graspPlacements);
246 
247  /**
248  * @brief Calculates the inverse kinematics for a single grasp.
249  *
250  * Calculates the inverse kinematics for a target grasp configuration. If checkCollisionFree is true, the arms
251  * and the platform are added as constraints in the calculation. Otherwise, only the position and orientation
252  * of the rns, tcp & target_pose are considered.
253  *
254  * @param robotNodeSetName
255  * @param eef
256  * @param globalRobotPose
257  * @param tcpPose
258  * @param checkCollisionFree
259  * @return NameValueMap of the solved IK
260  */
261  NameValueMap calculateSingleIK(const ::std::string& robotNodeSetName, const std::string& eef, const PoseBasePtr& globalRobotPose, const ::armarx::FramedPoseBasePtr& tcpPose, bool checkCollisionFree = true);
262 
263  /**
264  * Wrapper for the property JointToLinkSetMapping
265  *
266  * @return Map of each pair in the JointSetCollisionSet
267  */
268  armarx::StringStringDictionary getJointSetCollisionSetMapping();
269 
270  /**
271  * Generate a list of valid GraspingTrajectories given a list of grasps.
272  *
273  * @param grasps The list of grasps to generate trajetories for
274  * @param objId The object ID. Only used for logging.
275  * @return The list of valid generated grasps
276  */
277  GraspingTrajectoryList generateGraspingTrajectoryListForGraspListInternal(const GeneratedGraspList& grasps);
278  MotionPlanningDataList generateIKsInternal(const GeneratedGraspList& grasps);
279 
280  std::vector<std::string> getRobotNodeSetNodesWithoutAllwaysColliding(const std::vector<std::string>& robotColNodeNames, const std::string& armCollisionSet);
281 
282  void setDescriptionPositionForObject(const std::string& objId);
283  void setNextStepDescription(const std::string& description);
284  void resetStepDescription();
285  std::string graspGeneratorName;
286  std::string robotPlacementName;
287  std::vector<std::string> robotNodeSetNames;
288  std::vector<std::string> reachabilitySpaceFilePaths;
289 
290  Vector3fRange planningBoundingBox;
291  GraspGeneratorInterfacePrx gg;
292  GraspSelectionManagerInterfacePrx gsm;
293  GraspSelectionCriterionInterfacePrx gsc;
294  RobotPlacementInterfacePrx rp;
295 
296  PlannedMotionProviderInterfacePrx pmp;
297  SimoxCSpacePtr cacheCSpace;
298 
299  //RobotIKInterfacePrx rik;
301  VirtualRobot::RobotPtr localRobot, ikRobot;
302  SimoxCSpacePtr cspace;
303 
304  memoryx::CommonStorageInterfacePrx cs;
305  memoryx::WorkingMemoryInterfacePrx wm;
306  memoryx::PriorKnowledgeInterfacePrx prior;
307 
308  memoryx::EntityDrawerInterfacePrx entityDrawer;
309  std::string layerName;
310  std::string robotVisuId;
311 
312  int step = 0;
313  Vector3Ptr globalDescriptionPosition;
314 
315  std::vector<CSpaceVisualizerTaskHandle> planningTasks;
316  Mutex graspManagerMutex;
317  armarx::TimeKeeper stepTimer;
318 
319  RobotNameHelperPtr rnh;
320  };
321 }
322 
armarx::TimeKeeper
The TimeKeeper class tracks the passing of time and allows to stop it, restart it,...
Definition: TimeKeeper.h:41
armarx::GraspingManager::onExitComponent
void onExitComponent() override
Definition: GraspingManager.cpp:201
TimeKeeper.h
armarx::GraspingManager::generateGraspsByObjectName
GeneratedGraspList generateGraspsByObjectName(const std::string &objectName, const Ice::Current &=Ice::emptyCurrent) override
Definition: GraspingManager.cpp:1039
armarx::GraspingManager::onConnectComponent
void onConnectComponent() override
Definition: GraspingManager.cpp:130
armarx::GraspingManager::generateGraspingTrajectoryListForGraspList
GraspingTrajectoryList generateGraspingTrajectoryListForGraspList(const GeneratedGraspList &grasps, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspingManager.cpp:988
armarx::GraspingManager::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: GraspingManager.h:171
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
armarx::GraspingManager::onInitComponent
void onInitComponent() override
Definition: GraspingManager.cpp:96
armarx::GraspingManager::visualizeGraspingTrajectory
void visualizeGraspingTrajectory(const GraspingTrajectory &trajectory, float visuSlowdownFactor, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspingManager.cpp:1018
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::navigation::platform_controller::platform_global_trajectory::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformGlobalTrajectoryController.h:93
armarx::GraspingManager::visualizeGrasp
std::pair< std::string, Ice::StringSeq > visualizeGrasp(const GeneratedGrasp &grasp, int id, const DrawColor &color=DrawColor {1, 1, 1, 1})
Visualizes the grasp using the DebugDrawer topic.
Definition: GraspingManager.cpp:224
armarx::GraspingManager::getDefaultName
std::string getDefaultName() const override
Definition: GraspingManager.h:119
armarx::GraspingManager::generateGraspingTrajectory
GraspingTrajectory generateGraspingTrajectory(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Runs the grasping pipeline.
Definition: GraspingManager.cpp:943
armarx::RobotStateComponentInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
Definition: RobotVisuWidget.h:57
armarx::RobotNameHelperPtr
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
Definition: RobotNameHelper.h:34
armarx::GraspingManager
The GraspingManager class.
Definition: GraspingManager.h:110
armarx::NaturalIKComponentPluginUser
Provides a ready-to-use NaturalIK.
Definition: NaturalIKComponentPlugin.h:41
MemoryXCoreObjectFactories.h
armarx::GraspingManager::generateIKs
MotionPlanningDataList generateIKs(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspingManager.cpp:1025
armarx::GraspingManagerPropertyDefinitions
Definition: GraspingManager.h:55
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
SimoxCSpace.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::SimoxCSpacePtr
IceInternal::Handle< SimoxCSpace > SimoxCSpacePtr
An ice handle for a SimoxCSpace.
Definition: SimoxCSpace.h:55
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::GraspingManager::generateGraspingTrajectoryList
GraspingTrajectoryList generateGraspingTrajectoryList(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraspingManager.cpp:973
CSpaceVisualizerTask.h
MemoryXTypesObjectFactories.h
armarx::Mutex
boost::mutex Mutex
Definition: Synchronization.h:131
armarx::Vector3Ptr
IceInternal::Handle< Vector3 > Vector3Ptr
Definition: Pose.h:165
NaturalIKComponentPlugin.h
armarx::GraspingManager::onDisconnectComponent
void onDisconnectComponent() override
Definition: GraspingManager.cpp:196
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::PropertyDefinitionBase::eModifiable
@ eModifiable
Definition: PropertyDefinitionInterface.h:57
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
RobotNameHelper.h
armarx::GraspingManagerPropertyDefinitions::GraspingManagerPropertyDefinitions
GraspingManagerPropertyDefinitions(std::string prefix)
Definition: GraspingManager.h:59
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18