VisualServoWrapper.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::FindAndGraspObjectGroup
19  * @author Valerij Wittenbeck ( valerij dot wittenbeck at student dot kit dot edu )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "VisualServoWrapper.h"
26 
27 #include <VirtualRobot/Grasping/Grasp.h>
28 
29 using namespace armarx;
30 using namespace FindAndGraspObjectGroup;
31 
32 // DO NOT EDIT NEXT LINE
33 VisualServoWrapper::SubClassRegistry
36 
39 {
40 }
41 
42 void
44 {
45  //We need to set some inputs as outputs, because the successor state might be this state again ... and again .. and again.
46  setOutput("objectInstanceChannel", getInput<ChannelRef>("objectInstanceChannel"));
47  setOutput("markerInstanceChannel", getInput<ChannelRef>("markerInstanceChannel"));
48 
49  //NEW: object pose updates HERE (instead of StateWaitForUpdates in VisualServo)
50  //setLocal("referenceFrame", "Platform");
51 
52  // retrieve channels
53  ChannelRefPtr objectInstanceChannel = getInput<ChannelRef>("objectInstanceChannel");
54 
55  float minRecognitionLikelihood = getInput<float>("minObjectCertainty");
56 
57  Literal objectUpdated(*objectInstanceChannel->getDataFieldIdentifier("existenceCertainty"),
58  "larger",
59  Literal::createParameterList(minRecognitionLikelihood));
60  condObjectPoseUpdated = installCondition(objectUpdated, createEvent<EvObjectPoseUpdate>());
61 
62  // object lost condition (Failure)
63  //ChannelRefPtr objectChannel = getInput<ChannelRef>("objectChannel");
64  Literal objectLost(*objectInstanceChannel->getDataFieldIdentifier("existenceCertainty"),
65  "smaller",
66  Literal::createParameterList(minRecognitionLikelihood));
67  condObjectLost = installCondition(objectLost, createEvent<Failure>());
68 
69 
70  //=============
71 
72  //TODO here:
73  //-(OK..)channels -> positions
74  //-(OK..)perform relative transform ( given by graspDefinition )
75  //-(OK..)set positions to output! ( setLocal() )
76  //-(OK..)install condition for object pose update --> has to be done in the parent state (VisualServoingExampleStatechart)
77 
78  //TAKEN FROM VisualServo.cpp, StateCalcVelocities::onEnter()
79 
80  FindAndGraspObjectContext* context = getContext<FindAndGraspObjectContext>();
81 
82  // RemoteRobot::synchronizeLocalClone(vsContext->localRobot, vsContext->robotStateComponent);
83  // VirtualRobot::RobotPtr localRobot = vsContext->localRobot;
84  // PosePtr globalRobotPose = PosePtr::dynamicCast(vsContext->simulatorProxy->getRobotPose(localRobot->getName()));
85  // localRobot->setGlobalPose(globalRobotPose->toEigen());
87  //PosePtr globalRobotPose = PosePtr::dynamicCast(context->simulatorProxy->getRobotPose(localRobot->getName()));
88  //localRobot->setGlobalPose(globalRobotPose->toEigen());
89 
90  // retrieve pose of object (assumed to be global)
91  Eigen::Matrix4f objectGlobalPose = Eigen::Matrix4f::Identity();
92  FramedPositionPtr objectPosition = objectInstanceChannel->get<FramedPosition>("position");
93  std::string objectFrame = objectPosition->frame;
94  objectGlobalPose.block(0, 3, 3, 1) = objectPosition->toEigen();
95  objectGlobalPose.block(0, 0, 3, 3) =
96  objectInstanceChannel->get<FramedOrientation>("orientation")->toEigen();
97 
98  if (!objectFrame.empty())
99  {
100  ARMARX_WARNING << "object frame is currently assumed to be empty" << flush;
101  }
102 
103  std::string targetFrame = getInput<std::string>("referenceFrame");
104  VirtualRobot::RobotNodePtr targetNode = localRobot->getRobotNode(targetFrame);
105 
106  if (!targetNode)
107  {
108  ARMARX_ERROR << "Target frame not part of robot" << std::endl;
109  }
110 
111  ARMARX_DEBUG << "robot global pose: " << localRobot->getGlobalPose() << std::endl;
112  ARMARX_DEBUG << "target frame global pose: " << targetNode->getGlobalPose() << std::endl;
113 
114 
115  Eigen::Matrix4f objectFramedPose = targetNode->getGlobalPose().inverse() * objectGlobalPose;
116 
117  ARMARX_DEBUG << "object global pose: " << objectGlobalPose << std::endl;
118  ARMARX_DEBUG << "object framed pose: " << objectFramedPose << std::endl;
119 
120  Eigen::Matrix4f graspFramedPose =
121  objectFramedPose * context->graspDefinition->getTransformation().inverse();
122 
123  ARMARX_DEBUG << "grasp framed pose: " << graspFramedPose << std::endl;
124 
125  //construct framed pose; then setLocal()
126  FramedPosePtr targetFramedPose = new FramedPose(graspFramedPose, targetFrame);
127  // vsContext->simulatorProxy->visualizePose("myDebugPose_1", new FramedPose(targetNode->getGlobalPose() * graspFramedPose, ""));
128  setLocal(
129  "targetPose",
130  targetFramedPose); //TODO: make sure there is a "targetPose" input field in THIS state and in the VisualServo (child) state...
131 
132  //RobotStatechartContext* rsContext = getContext<RobotStatechartContext>();
133  if (context->headIKUnitPrx)
134  {
135  Eigen::Vector3f viewTarget;
136  viewTarget << targetFramedPose->position->x, targetFramedPose->position->y,
137  targetFramedPose->position->z;
138  FramedDirectionPtr viewTargetPositionPtr =
139  new FramedDirection(viewTarget, targetFramedPose->frame);
140  ARMARX_IMPORTANT << "Set head target to " << viewTarget.transpose();
141  context->headIKUnitPrx->setHeadTarget(context->headIKKinematicChainName,
142  viewTargetPositionPtr);
143  }
144  else
145  {
146  ARMARX_IMPORTANT << "No head unit ";
147  }
148 
149  ARMARX_VERBOSE << "Done VisualServoWrapper::onEnter()" << flush;
150 }
151 
152 void
154 {
155  //NEW: object pose updates:
158  condObjectLost); //TODO (maybe): move this to the VSExample (where the condObjectUpdate also is)
159 }
160 
161 // DO NOT EDIT NEXT FUNCTION
162 std::string
164 {
165  return "VisualServoWrapper";
166 }
167 
168 // DO NOT EDIT NEXT FUNCTION
171 {
172  return XMLStateFactoryBasePtr(new VisualServoWrapper(stateData));
173 }
armarx::FindAndGraspObjectGroup::VisualServoWrapper::onExit
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
Definition: VisualServoWrapper.cpp:153
armarx::StateUtility::removeCondition
void removeCondition(ConditionIdentifier conditionId)
Utility function to remove an installed condition on the distributed conditionhandler.
Definition: StateUtil.cpp:148
armarx::FindAndGraspObjectContext::robotStateComponent
RobotStateComponentInterfacePrx robotStateComponent
Definition: FindAndGraspObjectContext.h:183
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::FindAndGraspObjectGroup::VisualServoWrapper::onEnter
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
Definition: VisualServoWrapper.cpp:43
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:48
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
armarx::FindAndGraspObjectContext::headIKKinematicChainName
std::string headIKKinematicChainName
Definition: FindAndGraspObjectContext.h:191
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::State::setOutput
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition: State.cpp:482
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:142
armarx::FindAndGraspObjectGroup::VisualServoWrapper::condObjectLost
ConditionIdentifier condObjectLost
Definition: VisualServoWrapper.h:57
armarx::State::setLocal
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition: State.cpp:464
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::FindAndGraspObjectContext
Definition: FindAndGraspObjectContext.h:81
IceInternal::Handle< ChannelRef >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::FindAndGraspObjectGroup::VisualServoWrapper
Definition: VisualServoWrapper.h:37
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:157
armarx::StateUtility::installCondition
ConditionIdentifier installCondition(const Term &condition, const EventPtr evt, const std::string &desc="")
Utility function to install a condition on the distributed conditionhandler.
Definition: StateUtil.cpp:102
VisualServoWrapper.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:215
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
armarx::FindAndGraspObjectGroup::VisualServoWrapper::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: VisualServoWrapper.cpp:170
armarx::Literal
Definition: Term.h:208
armarx::FindAndGraspObjectGroup::VisualServoWrapper::condObjectPoseUpdated
ConditionIdentifier condObjectPoseUpdated
Definition: VisualServoWrapper.h:56
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::FindAndGraspObjectGroup::VisualServoWrapper::Registry
static SubClassRegistry Registry
Definition: VisualServoWrapper.h:51
armarx::FindAndGraspObjectGroup::VisualServoWrapper::GetName
static std::string GetName()
Definition: VisualServoWrapper.cpp:163
armarx::FindAndGraspObjectContext::graspDefinition
VirtualRobot::GraspPtr graspDefinition
Definition: FindAndGraspObjectContext.h:178
armarx::FindAndGraspObjectGroup::VisualServoWrapper::VisualServoWrapper
VisualServoWrapper(XMLStateConstructorParams stateData)
Definition: VisualServoWrapper.cpp:37
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::FindAndGraspObjectContext::headIKUnitPrx
HeadIKUnitInterfacePrx headIKUnitPrx
Definition: FindAndGraspObjectContext.h:190
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19