VisualServoTowardsTargetPoseTest.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::VisualServoGroup
19  * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "VisualServoGroupStatechartContext.generated.h"
27 
28 using namespace armarx;
29 using namespace VisualServoGroup;
30 
31 
33 
34 
35 // DO NOT EDIT NEXT LINE
36 VisualServoTowardsTargetPoseTest::SubClassRegistry VisualServoTowardsTargetPoseTest::Registry(VisualServoTowardsTargetPoseTest::GetName(), &VisualServoTowardsTargetPoseTest::CreateInstance);
37 
38 
39 
41  XMLStateTemplate<VisualServoTowardsTargetPoseTest>(stateData), VisualServoTowardsTargetPoseTestGeneratedBase<VisualServoTowardsTargetPoseTest>(stateData)
42 {
43 }
44 
46 {
47  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
48 
49  std::string handName = in.getHandNameInMemory();
50  ARMARX_IMPORTANT << "Get hand memory channel. Hand name = " << handName;
51  ChannelRefBasePtr handMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(handName, 150, armarx::DEFAULT_VIEWTARGET_PRIORITY);
52 
53  if (handMemoryChannel)
54  {
55  handMemoryChannel->validate();
56  local.setHandMemoryChannel(ChannelRefPtr::dynamicCast(handMemoryChannel));
57  }
58  else
59  {
60  ARMARX_ERROR << "Could not find hand in prior memory. handMemoryChannel is null: handName = " << handName;
61  }
62 
63  sleep(1); // wait for hant to be localized
64 
65  Eigen::Vector3f targetPosition1(300, 650, 1050);
66  Eigen::Vector3f targetPosition2(150, 800, 1050);
67  //Eigen::Vector3f targetOrientationVector(-1,1,0);
68  Eigen::Vector3f targetOrientationVector(0, 1, 0);
69  targetOrientationVector.normalize();
70 
71  Eigen::Matrix3f targetOrientation = getOrientationFromApproachVector(targetOrientationVector);
72  FramedPosePtr targetPose1 = new FramedPose(targetOrientation, targetPosition1, context->getRobot()->getRootNode()->getName(), context->getRobot()->getName());
73  FramedPosePtr targetPose2 = new FramedPose(targetOrientation, targetPosition2, context->getRobot()->getRootNode()->getName(), context->getRobot()->getName());
74 
75  local.setTcpTargetPose1(targetPose1);
76  local.setTcpTargetPose2(targetPose2);
77 
78 
79  ARMARX_IMPORTANT << "Requesting TCPControlUnit";
80  context->getTCPControlUnit()->request();
81  ARMARX_IMPORTANT << "Requested TCPControlUnit";
82 
83  //ARMARX_IMPORTANT << "Activating Automatic View Selection";
84  //context->getViewSelection()->activateAutomaticViewSelection();
85 
86  Eigen::Matrix4f viewTargetEigen = context->getRobot()->getRobotNode(in.getHandNameInRobotModel())->getPoseInRootFrame();
87  viewTargetEigen(2, 3) += 50;
88  FramedPositionPtr viewTarget = new FramedPosition(viewTargetEigen, context->getRobot()->getRootNode()->getName(), context->getRobot()->getName());
89  ARMARX_IMPORTANT << "addManualViewTarget: " << *viewTarget;
90  context->getViewSelection()->addManualViewTarget(viewTarget);
91  context->getViewSelection()->addManualViewTarget(viewTarget);
92  context->getViewSelection()->addManualViewTarget(viewTarget);
93 
94  sleep(7); // wait for IK to be solved
95 
96 }
97 
98 Eigen::Matrix3f VisualServoTowardsTargetPoseTest::getOrientationFromApproachVector(const Eigen::Vector3f& approachVector)
99 {
100  Eigen::Vector3f z = approachVector; // TCP z: Direction of Hand
101  Eigen::Vector3f y(0, 0, -1); // TCP y: Down for normal orientation
102  Eigen::Vector3f x = y.cross(z); // calculate x for right hand coord system
103  Eigen::Matrix3f orientation;
104  orientation.block<3, 1>(0, 0) = x;
105  orientation.block<3, 1>(0, 1) = y;
106  orientation.block<3, 1>(0, 2) = z;
107  return orientation;
108 }
109 
111 {
112  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
113 
114  ChannelRefPtr handMemoryChannel = local.getHandMemoryChannel();
115 
116 
117 
118  int counter = 0;
119 
120  while (!isRunningTaskStopped()) // stop run function if returning true
121  {
122  memoryx::ChannelRefBaseSequence instances = context->getObjectMemoryObserver()->getObjectInstances(handMemoryChannel);
123 
124  if (instances.size() == 0)
125  {
126  ARMARX_WARNING << "No instances of the hand in the memory";
127  }
128  else
129  {
130  FramedPose handPoseFromKinematicModel(context->getRobot()->getRobotNode(in.getHandNameInRobotModel())->getPoseInRootFrame(),
131  context->getRobot()->getRootNode()->getName(),
132  context->getRobot()->getName());
133 
134  FramedPositionPtr position = ChannelRefPtr::dynamicCast(instances.front())->get<FramedPosition>("position");
135  FramedOrientationPtr orientation = ChannelRefPtr::dynamicCast(instances.front())->get<FramedOrientation>("orientation");
136  FramedPose handPoseFromMemoryX = FramedPose(orientation->toEigen(), position->toEigen(), position->getFrame(), position->agent);
137 
138  ARMARX_INFO << "handPoseFromKinematicModel: " << handPoseFromKinematicModel;
139  ARMARX_INFO << "handPoseFromMemoryX: " << handPoseFromMemoryX;
140  }
141 
142 
143  // Manual view selection:
144  if (counter % 30 == 0) // 30 * 100ms = 3s
145  {
146  Eigen::Matrix4f viewTargetEigen = context->getRobot()->getRobotNode(in.getHandNameInRobotModel())->getPoseInRootFrame();
147  //viewTargetEigen(0,3) -= 250;
148  FramedPositionPtr viewTarget = new FramedPosition(viewTargetEigen, context->getRobot()->getRootNode()->getName(), context->getRobot()->getName());
149  ARMARX_IMPORTANT << "addManualViewTarget: " << *viewTarget;
150  context->getViewSelection()->addManualViewTarget(viewTarget);
151  }
152 
153  usleep(100000); // 100ms
154  }
155 
156 }
157 
159 {
160  // put your user code for the breaking point here
161  // execution time should be short (<100ms)
162 }
163 
165 {
166  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
167  ARMARX_IMPORTANT << "Releasing TCPControlUnit";
168  context->getTCPControlUnit()->release();
169 }
170 
171 
172 // DO NOT EDIT NEXT FUNCTION
174 {
176 }
177 
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::run
void run() override
Definition: VisualServoTowardsTargetPoseTest.cpp:110
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
VisualServoTowardsTargetPoseTest.h
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::Registry
static SubClassRegistry Registry
Definition: VisualServoTowardsTargetPoseTest.h:45
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
IceInternal::Handle< FramedPose >
ObjectMemoryObserver.h
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:199
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::VisualServoTowardsTargetPoseTest
VisualServoTowardsTargetPoseTest(const XMLStateConstructorParams &stateData)
Definition: VisualServoTowardsTargetPoseTest.cpp:40
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest
Definition: VisualServoTowardsTargetPoseTest.h:31
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::onEnter
void onEnter() override
Definition: VisualServoTowardsTargetPoseTest.cpp:45
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::onBreak
void onBreak() override
Definition: VisualServoTowardsTargetPoseTest.cpp:158
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: VisualServoTowardsTargetPoseTest.cpp:173
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VisualServoGroup::VisualServoTowardsTargetPoseTest::onExit
void onExit() override
Definition: VisualServoTowardsTargetPoseTest.cpp:164