VisualServoTowardsObject.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2014-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 RobotSkillTemplates::GraspObjectGroup
19  * @author David ( david dot schiebener at kit dot edu )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 #include "VisualServoGroupStatechartContext.generated.h"
28 
29 
30 // MemoryX
36 
37 // Simox
38 #include <VirtualRobot/Grasping/Grasp.h>
39 #include <VirtualRobot/Grasping/GraspSet.h>
40 #include <VirtualRobot/ManipulationObject.h>
41 #include <VirtualRobot/XML/ObjectIO.h>
42 
43 
44 using namespace armarx;
45 using namespace VisualServoGroup;
46 
47 
48 // DO NOT EDIT NEXT LINE
49 VisualServoTowardsObject::SubClassRegistry
52 
55  VisualServoTowardsObjectGeneratedBase<VisualServoTowardsObject>(stateData)
56 {
57 }
58 
59 void
61 {
62  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
63  local.setStartTimeRef(ChannelRefPtr::dynamicCast(
64  context->systemObserverPrx->startTimer("VisualServoTowardsObjectStartTime")));
65 
66  // get grasp definition
67  std::string objectName;
68  memoryx::PersistentObjectClassSegmentBasePrx classesSegmentPrx =
69  context->getPriorKnowledge()->getObjectClassesSegment();
70  memoryx::CommonStorageInterfacePrx databasePrx =
71  context->getPriorKnowledge()->getCommonStorage();
72  memoryx::GridFileManagerPtr fileManager(new memoryx::GridFileManager(databasePrx));
73  memoryx::EntityBasePtr entity;
74  if (in.isPreselectedObjectInstanceChannelSet())
75  {
76  ARMARX_INFO << "Using preselected instance "
77  << in.getPreselectedObjectInstanceChannel()->output();
78  objectName =
79  in.getPreselectedObjectInstanceChannel()->getDataField("className")->getString();
80  }
81  else
82  {
83  objectName = in.getObjectMemoryChannel()->getDataField("className")->getString();
84  }
85  entity = classesSegmentPrx->getEntityByName(objectName);
86  auto chainName = in.getKinematicChainName();
87 
88  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(entity);
90  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
91  VirtualRobot::ManipulationObjectPtr mo = simoxWrapper->getManipulationObject();
92 
93  auto tcpObjectClass = classesSegmentPrx->getObjectClassByName(in.getHandNameInMemory());
94  if (tcpObjectClass)
95  {
96  context->getEntityDrawerTopic()->setObjectVisu(
97  "VisualServoHandVisu", "tcpTarget_" + chainName, tcpObjectClass, new Pose());
98  context->getEntityDrawerTopic()->updateObjectTransparency(
99  "VisualServoHandVisu", "tcpTarget_" + chainName, 0.3);
100  }
101  else
102  {
103  ARMARX_IMPORTANT << "Could not get objectclass for tcp " << in.getHandNameInMemory()
104  << " for visu purposes";
105  }
106  Eigen::Matrix4f transformationFromObjectToTCPPose = Eigen::Matrix4f::Identity();
107 
108  std::string graspSetName = getInput<std::string>("GraspSetName");
109 
110  if (mo->getGraspSet(graspSetName))
111  {
112  std::string graspName = getInput<std::string>("GraspDefinitionName");
113  VirtualRobot::GraspPtr grasp = mo->getGraspSet(graspSetName)->getGrasp(graspName);
114 
115  if (!grasp)
116  {
117  ARMARX_ERROR << "No grasp with name " << graspName << " found! ";
118  return;
119  }
120  else
121  {
122  transformationFromObjectToTCPPose = grasp->getTransformation().inverse();
123  ARMARX_IMPORTANT << "Successfully loaded " << graspName << "";
124  ARMARX_VERBOSE << "Grasp " << graspName << " has value:\n"
125  << grasp->getTransformation();
126  }
127  }
128  else
129  {
130  ARMARX_ERROR << "No grasp set with name " << graspSetName << " found! ";
131  return;
132  }
133 
134  Eigen::Matrix4f desiredTcpOffsetToObjectEigen = transformationFromObjectToTCPPose;
135  PosePtr desiredTcpOffsetToObject = new Pose(desiredTcpOffsetToObjectEigen);
136  setLocal("DesiredTcpOffsetToObject", *desiredTcpOffsetToObject);
137 
138  // request IK units
139  ARMARX_VERBOSE << "Requesting TCP control unit";
140  context->getTCPControlUnit()->request();
141  ARMARX_VERBOSE << "Requested TCP control unit";
142 }
143 
144 void
146 {
147  // put your user code for the exit point here
148  // execution time should be short (<100ms)
149 
150  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
151  ARMARX_VERBOSE << "Releasing TCP control unit";
152  context->getTCPControlUnit()->release();
153  ARMARX_VERBOSE << "Released TCP control unit";
154 
155  // this pose was set in GetObjectPose
156  context->getDebugDrawerTopic()->removePoseDebugLayerVisu("objectPose");
157  context->systemObserverPrx->removeTimer(local.getStartTimeRef());
158  context->getEntityDrawerTopic()->removeLayer("VisualServoHandVisu");
159 }
160 
161 // DO NOT EDIT NEXT FUNCTION
162 std::string
164 {
165  return "VisualServoTowardsObject";
166 }
167 
168 // DO NOT EDIT NEXT FUNCTION
171 {
172  return XMLStateFactoryBasePtr(new VisualServoTowardsObject(stateData));
173 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
VisualServoTowardsObject.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
GridFileManager.h
armarx::VisualServoGroup::VisualServoTowardsObject
Definition: VisualServoTowardsObject.h:33
ObjectClass.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::VisualServoGroup::VisualServoTowardsObject::Registry
static SubClassRegistry Registry
Definition: VisualServoTowardsObject.h:46
IceInternal::Handle< ObjectClass >
armarx::VisualServoGroup::VisualServoTowardsObject::VisualServoTowardsObject
VisualServoTowardsObject(XMLStateConstructorParams stateData)
Definition: VisualServoTowardsObject.cpp:53
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
MemoryXCoreObjectFactories.h
armarx::VisualServoGroup::VisualServoTowardsObject::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: VisualServoTowardsObject.cpp:170
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
SimoxObjectWrapper.h
armarx::VisualServoGroup::VisualServoTowardsObject::onExit
void onExit() override
Definition: VisualServoTowardsObject.cpp:145
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:41
MemoryXTypesObjectFactories.h
armarx::VisualServoGroup::VisualServoTowardsObject::GetName
static std::string GetName()
Definition: VisualServoTowardsObject.cpp:163
armarx::VisualServoGroup::VisualServoTowardsObject::onEnter
void onEnter() override
Definition: VisualServoTowardsObject.cpp:60
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27