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