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