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
29using namespace armarx;
30using namespace FindAndGraspObjectGroup;
31
32// DO NOT EDIT NEXT LINE
33VisualServoWrapper::SubClassRegistry
36
41
42void
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));
61
62 // object lost condition (Failure)
63 //ChannelRefPtr objectChannel = getInput<ChannelRef>("objectChannel");
64 Literal objectLost(*objectInstanceChannel->getDataFieldIdentifier("existenceCertainty"),
65 "smaller",
66 Literal::createParameterList(minRecognitionLikelihood));
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
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
152void
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
162std::string
164{
165 return "VisualServoWrapper";
166}
167
168// DO NOT EDIT NEXT FUNCTION
RobotStateComponentInterfacePrx robotStateComponent
VisualServoWrapper(XMLStateConstructorParams stateData)
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
FramedDirection is a 3 dimensional direction vector with a reference frame.
Definition FramedPose.h:87
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPose class.
Definition FramedPose.h:281
The FramedPosition class.
Definition FramedPose.h:158
Literals are part of the user front end of the ArmarX condition mechanism.
Definition Term.h:209
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition Term.cpp:142
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
ContextType * getContext() const
Definition StateBase.h:71
void removeCondition(ConditionIdentifier conditionId)
Utility function to remove an installed condition on the distributed conditionhandler.
EventPtr createEvent()
Utility function to create a new Event.
Definition StateUtil.h:63
ConditionIdentifier installCondition(const Term &condition, const EventPtr evt, const std::string &desc="")
Utility function to install a condition on the distributed conditionhandler.
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition State.cpp:482
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition State.h:620
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition State.cpp:464
XMLStateTemplate(const XMLStateConstructorParams &params)
Definition XMLState.h:149
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< FramedDirection > FramedDirectionPtr
Definition FramedPose.h:84
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
const LogSender::manipulator flush
Definition LogSender.h:251
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
IceInternal::Handle< FramedPose > FramedPosePtr
Definition FramedPose.h:272