NaturalGraspFilter.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 version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotComponents
17 * @author Christoph Pohl ( christoph.pohl at kit dot edu)
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include <functional>
24 
25 #include "NaturalGraspFilter.h"
26 
32 
33 using namespace armarx;
34 
36 {
37 }
38 
40 {
41  usingProxy("RobotStateComponent");
42  // usingProxy("LongtermMemory");
43 }
44 
46 {
47  getProxy(rsc, "RobotStateComponent");
48  localRobot = armarx::RemoteRobot::createLocalCloneFromFile(rsc, VirtualRobot::RobotIO::eCollisionModel);
49  // Enabling this would automatically register the SelectionCriterion with the GraspSelectionManager
50  // graspSelectionManager = getProxy<GraspSelectionManagerInterfacePrx>(getProperty<std::string>("GraspSelectionManagerName").getValue());
51  // graspSelectionManager->registerAsGraspSelectionCriterion(GraspSelectionCriterionInterfacePrx::uncheckedCast(getProxy()));
52 }
53 
55 {
56  return "NaturalGraspFilter";
57 }
58 
59 GeneratedGraspList NaturalGraspFilter::filterGrasps(const GeneratedGraspList& grasps, const Ice::Current&)
60 {
61  GeneratedGraspList result;
62  for (const GeneratedGrasp& g : grasps)
63  {
64  Eigen::Matrix3f orientation = FramedPosePtr::dynamicCast(g.framedPose)->toGlobalEigen(localRobot).block<3, 3>(0, 0);
65  Eigen::Vector3f rotatedYAxis = orientation * Eigen::Vector3f::UnitY();
66  rotatedYAxis.normalize();
67  if (rotatedYAxis(2) > 0.1)
68  {
69  ARMARX_WARNING << g.graspName << " seems to be an unnatural Grasp; it will not be used further!";
70  continue;
71  }
72  result.push_back(g);
73  }
74  return result;
75 }
76 
77 GraspingPlacementList
78 NaturalGraspFilter::filterPlacements(const GraspingPlacementList& placements, const Ice::Current&)
79 {
80  GraspingPlacementList result;
81  ARMARX_INFO << "Number of Placements: " << placements.size();
82  // TODO: Create a Ranking function; maybe rank for the fewest angles in the kinematic chain.
83 
84  for (const GraspingPlacement& placement : placements)
85  {
86  // Note: something goes wrong if auto is used
87  Eigen::Matrix3f robotOrientation = FramedPosePtr::dynamicCast(placement.robotPose)->toGlobalEigen(localRobot).block<3, 3>(
88  0, 0);
89  Eigen::Matrix3f graspOrientation = FramedPosePtr::dynamicCast(placement.grasp.framedPose)->toGlobalEigen(
90  localRobot).block<3, 3>(0, 0);
91  Eigen::Matrix3f graspToRobotTransform = robotOrientation.inverse() * graspOrientation;
92  Eigen::Vector3f graspXAxisInRobotCoordinates = graspToRobotTransform * Eigen::Vector3f::UnitX();
93  graspXAxisInRobotCoordinates.normalize();
94  float x = graspXAxisInRobotCoordinates(0);
95  float y = graspXAxisInRobotCoordinates(1);
96  float z = graspXAxisInRobotCoordinates(2);
97  // Eigen::Vector2f Projection2D {graspXAxisInRobotCoordinates(0), graspXAxisInRobotCoordinates(1)};
98  // TODO: Don't hardcode this. Use the left eefName smhw;
99  // TODO: check if the values are sensible;
100  bool isInSegment = false;
101  if (placement.grasp.eefName == "Hand_L_EEF")
102  {
103  // isInSegment = vectorIsWithinCircleSegment(Projection2D, 0.333 * M_PI , 1.1666 * M_PI);
104  isInSegment = (x > -0.1 && (y < 0 || x * x + z * z > 0.5 * 0.5));
105  }
106  else
107  {
108  // isInSegment = vectorIsWithinCircleSegment(Projection2D, -M_PI / 6., 0.666 * M_PI);
109  isInSegment = (x > -0.1 && (y > 0 || x * x + z * z > 0.5 * 0.5));
110  }
111  if (isInSegment)
112  {
113  result.push_back(placement);
114  }
115  }
116  return result;
117 }
118 
119 Ice::Int NaturalGraspFilter::getHash(const Ice::Current&) const
120 {
121  return std::hash<std::string> {}(graspSelectionManager->ice_getIdentity().category + "." + graspSelectionManager->ice_getIdentity().name);
122 }
123 
124 bool NaturalGraspFilter::vectorIsWithinCircleSegment(const Eigen::Vector2f vector, double angle1, double angle2)
125 {
126  ARMARX_CHECK_EXPRESSION(std::abs(angle2 - angle1) < M_PI) <<
127  "Works only for circle segments smaller than Pi and is " << std::abs(angle2 - angle1);
128  Eigen::Rotation2Df rot1(angle1);
129  Eigen::Rotation2Df rot2(angle2);
130  Eigen::Vector2f boundary1 = rot1 * Eigen::Vector2f::UnitX();
131  Eigen::Vector2f boundary2 = rot2 * Eigen::Vector2f::UnitX();
132  bool vectorIsCounterClockwiseToBoundary1 = boundary1(0) * vector(1) - boundary1(1) * vector(0) > 0;
133  bool vectorIsClockwiseToBoundary2 = - boundary2(0) * vector(1) + boundary2(1) * vector(0) > 0;
134  return vectorIsCounterClockwiseToBoundary1 && vectorIsClockwiseToBoundary2;
135 }
RemoteRobot.h
armarx::NaturalGraspFilter::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: NaturalGraspFilter.cpp:54
armarx::NaturalGraspFilter::filterPlacements
GraspingPlacementList filterPlacements(const GraspingPlacementList &placements, const Ice::Current &) override
Definition: NaturalGraspFilter.cpp:78
armarx::RemoteRobot::createLocalCloneFromFile
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
Definition: RemoteRobot.cpp:441
armarx::NaturalGraspFilter::NaturalGraspFilter
NaturalGraspFilter()
Definition: NaturalGraspFilter.cpp:35
NaturalGraspFilter.h
armarx::GraspSelectionCriterionBase::graspSelectionManager
GraspSelectionManagerInterfacePrx graspSelectionManager
Definition: GraspSelectionCriterionBase.h:62
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
M_PI
#define M_PI
Definition: MathTools.h:17
FramedPose.h
MemoryXCoreObjectFactories.h
armarx::NaturalGraspFilter::getHash
Ice::Int getHash(const Ice::Current &) const override
Definition: NaturalGraspFilter.cpp:119
armarx::NaturalGraspFilter::filterGrasps
GeneratedGraspList filterGrasps(const GeneratedGraspList &grasps, const Ice::Current &) override
Definition: NaturalGraspFilter.cpp:59
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
ObjectInstance.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
MemoryXTypesObjectFactories.h
armarx::NaturalGraspFilter::onConnectGraspSelectionCriterion
void onConnectGraspSelectionCriterion() override
Definition: NaturalGraspFilter.cpp:45
armarx::NaturalGraspFilter::onInitGraspSelectionCriterion
void onInitGraspSelectionCriterion() override
Definition: NaturalGraspFilter.cpp:39
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28