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