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 "NaturalGraspFilter.h"
24 
25 #include <functional>
26 
29 
33 
34 using namespace armarx;
35 
37 {
38 }
39 
40 void
42 {
43  usingProxy("RobotStateComponent");
44  // usingProxy("LongtermMemory");
45 }
46 
47 void
49 {
50  getProxy(rsc, "RobotStateComponent");
51  localRobot =
52  armarx::RemoteRobot::createLocalCloneFromFile(rsc, VirtualRobot::RobotIO::eCollisionModel);
53  // Enabling this would automatically register the SelectionCriterion with the GraspSelectionManager
54  // graspSelectionManager = getProxy<GraspSelectionManagerInterfacePrx>(getProperty<std::string>("GraspSelectionManagerName").getValue());
55  // graspSelectionManager->registerAsGraspSelectionCriterion(GraspSelectionCriterionInterfacePrx::uncheckedCast(getProxy()));
56 }
57 
58 std::string
60 {
61  return "NaturalGraspFilter";
62 }
63 
64 GeneratedGraspList
65 NaturalGraspFilter::filterGrasps(const GeneratedGraspList& grasps, const Ice::Current&)
66 {
67  GeneratedGraspList result;
68  for (const GeneratedGrasp& g : grasps)
69  {
70  Eigen::Matrix3f orientation =
71  FramedPosePtr::dynamicCast(g.framedPose)->toGlobalEigen(localRobot).block<3, 3>(0, 0);
72  Eigen::Vector3f rotatedYAxis = orientation * Eigen::Vector3f::UnitY();
73  rotatedYAxis.normalize();
74  if (rotatedYAxis(2) > 0.1)
75  {
76  ARMARX_WARNING << g.graspName
77  << " seems to be an unnatural Grasp; it will not be used further!";
78  continue;
79  }
80  result.push_back(g);
81  }
82  return result;
83 }
84 
85 GraspingPlacementList
86 NaturalGraspFilter::filterPlacements(const GraspingPlacementList& placements, const Ice::Current&)
87 {
88  GraspingPlacementList result;
89  ARMARX_INFO << "Number of Placements: " << placements.size();
90  // TODO: Create a Ranking function; maybe rank for the fewest angles in the kinematic chain.
91 
92  for (const GraspingPlacement& placement : placements)
93  {
94  // Note: something goes wrong if auto is used
95  Eigen::Matrix3f robotOrientation = FramedPosePtr::dynamicCast(placement.robotPose)
96  ->toGlobalEigen(localRobot)
97  .block<3, 3>(0, 0);
98  Eigen::Matrix3f graspOrientation = FramedPosePtr::dynamicCast(placement.grasp.framedPose)
99  ->toGlobalEigen(localRobot)
100  .block<3, 3>(0, 0);
101  Eigen::Matrix3f graspToRobotTransform = robotOrientation.inverse() * graspOrientation;
102  Eigen::Vector3f graspXAxisInRobotCoordinates =
103  graspToRobotTransform * Eigen::Vector3f::UnitX();
104  graspXAxisInRobotCoordinates.normalize();
105  float x = graspXAxisInRobotCoordinates(0);
106  float y = graspXAxisInRobotCoordinates(1);
107  float z = graspXAxisInRobotCoordinates(2);
108  // Eigen::Vector2f Projection2D {graspXAxisInRobotCoordinates(0), graspXAxisInRobotCoordinates(1)};
109  // TODO: Don't hardcode this. Use the left eefName smhw;
110  // TODO: check if the values are sensible;
111  bool isInSegment = false;
112  if (placement.grasp.eefName == "Hand_L_EEF")
113  {
114  // isInSegment = vectorIsWithinCircleSegment(Projection2D, 0.333 * M_PI , 1.1666 * M_PI);
115  isInSegment = (x > -0.1 && (y < 0 || x * x + z * z > 0.5 * 0.5));
116  }
117  else
118  {
119  // isInSegment = vectorIsWithinCircleSegment(Projection2D, -M_PI / 6., 0.666 * M_PI);
120  isInSegment = (x > -0.1 && (y > 0 || x * x + z * z > 0.5 * 0.5));
121  }
122  if (isInSegment)
123  {
124  result.push_back(placement);
125  }
126  }
127  return result;
128 }
129 
130 Ice::Int
131 NaturalGraspFilter::getHash(const Ice::Current&) const
132 {
133  return std::hash<std::string>{}(graspSelectionManager->ice_getIdentity().category + "." +
134  graspSelectionManager->ice_getIdentity().name);
135 }
136 
137 bool
138 NaturalGraspFilter::vectorIsWithinCircleSegment(const Eigen::Vector2f vector,
139  double angle1,
140  double angle2)
141 {
142  ARMARX_CHECK_EXPRESSION(std::abs(angle2 - angle1) < M_PI)
143  << "Works only for circle segments smaller than Pi and is " << std::abs(angle2 - angle1);
144  Eigen::Rotation2Df rot1(angle1);
145  Eigen::Rotation2Df rot2(angle2);
146  Eigen::Vector2f boundary1 = rot1 * Eigen::Vector2f::UnitX();
147  Eigen::Vector2f boundary2 = rot2 * Eigen::Vector2f::UnitX();
148  bool vectorIsCounterClockwiseToBoundary1 =
149  boundary1(0) * vector(1) - boundary1(1) * vector(0) > 0;
150  bool vectorIsClockwiseToBoundary2 = -boundary2(0) * vector(1) + boundary2(1) * vector(0) > 0;
151  return vectorIsCounterClockwiseToBoundary1 && vectorIsClockwiseToBoundary2;
152 }
RemoteRobot.h
armarx::NaturalGraspFilter::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: NaturalGraspFilter.cpp:59
armarx::NaturalGraspFilter::filterPlacements
GraspingPlacementList filterPlacements(const GraspingPlacementList &placements, const Ice::Current &) override
Definition: NaturalGraspFilter.cpp:86
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:512
armarx::NaturalGraspFilter::NaturalGraspFilter
NaturalGraspFilter()
Definition: NaturalGraspFilter.cpp:36
NaturalGraspFilter.h
armarx::GraspSelectionCriterionBase::graspSelectionManager
GraspSelectionManagerInterfacePrx graspSelectionManager
Definition: GraspSelectionCriterionBase.h:63
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:281
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:131
armarx::NaturalGraspFilter::filterGrasps
GeneratedGraspList filterGrasps(const GeneratedGraspList &grasps, const Ice::Current &) override
Definition: NaturalGraspFilter.cpp:65
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:181
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
MemoryXTypesObjectFactories.h
armarx::NaturalGraspFilter::onConnectGraspSelectionCriterion
void onConnectGraspSelectionCriterion() override
Definition: NaturalGraspFilter.cpp:48
armarx::NaturalGraspFilter::onInitGraspSelectionCriterion
void onInitGraspSelectionCriterion() override
Definition: NaturalGraspFilter.cpp:41
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
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:407
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:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27