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
29using namespace armarx;
30using namespace VisualServoGroup;
31
32
34
35
36// DO NOT EDIT NEXT LINE
37VisualServoTowardsTargetPoseTest::SubClassRegistry
38 VisualServoTowardsTargetPoseTest::Registry(VisualServoTowardsTargetPoseTest::GetName(),
40
47
48void
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
114Eigen::Matrix3f
115VisualServoTowardsTargetPoseTest::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
128void
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
186void
188{
189 // put your user code for the breaking point here
190 // execution time should be short (<100ms)
191}
192
193void
195{
196 VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
197 ARMARX_IMPORTANT << "Releasing TCPControlUnit";
198 context->getTCPControlUnit()->release();
199}
200
201// DO NOT EDIT NEXT FUNCTION
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPose class.
Definition FramedPose.h:281
The FramedPosition class.
Definition FramedPose.h:158
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Class for legacy to stay compatible with old statecharts.
Definition XMLState.h:147
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#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_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
Definition FramedPose.h:207
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64
IceInternal::Handle< FramedPose > FramedPosePtr
Definition FramedPose.h:272