CalculateGazeIk.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::MotionControlGroup
19 * @author Manfred Kroehnert ( Manfred dot Kroehnert at 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 "CalculateGazeIk.h"
26
27#include <memory>
28
29#include <Eigen/Eigen>
30
31#include <VirtualRobot/IK/GazeIK.h>
32#include <VirtualRobot/LinkedCoordinate.h>
33#include <VirtualRobot/Robot.h>
34#include <VirtualRobot/RobotNodeSet.h> // IWYU pragma: keep
35#include <VirtualRobot/XML/RobotIO.h>
36
38
40
41using namespace armarx;
42using namespace MotionControlGroup;
43
44// DO NOT EDIT NEXT LINE
45CalculateGazeIk::SubClassRegistry CalculateGazeIk::Registry(CalculateGazeIk::GetName(),
47
50 CalculateGazeIkGeneratedBase<CalculateGazeIk>(stateData)
51{
52}
53
54void
56{
57 // put your user code for the enter-point here
58 // execution time should be short (<100ms)
59}
60
61void
63{
64 // put your user code for the execution-phase here
65 // runs in seperate thread, thus can do complex operations
66 // should check constantly whether isRunningTaskStopped() returns true
68 getContext<MotionControlGroupStatechartContext>();
69 VirtualRobot::RobotPtr robot = context->getRobotPool()->getRobot(); //->createLocalClone();
70
71
72 while (!isRunningTaskStopped()) // stop run function if returning true
73 {
74 // VirtualRobot::RobotPtr robotSnapshot(new RemoteRobot(context->getRobotStateComponent()->getRobotSnapshot("CalculateTCPPoseTime")));
76
77 if (!robot)
78 {
79 ARMARX_WARNING << "No robot!";
80 emitFailure();
81 }
82
83 VirtualRobot::RobotNodeSetPtr kinematicChain =
84 robot->getRobotNodeSet(in.getKinematicChainName());
85
86 if (!kinematicChain)
87 {
88 ARMARX_WARNING << "No kinematicChain with name " << in.getKinematicChainName();
89 emitFailure();
90 }
91
92 std::string tcpName;
93
94 if (in.isVirtualPrismaticJointNameSet())
95 {
96 tcpName = in.getVirtualPrismaticJointName();
97 }
98 else
99 {
100 tcpName = kinematicChain->getTCP()->getName();
101 }
102
103 VirtualRobot::RobotNodePrismaticPtr virtualJoint =
104 std::dynamic_pointer_cast<VirtualRobot::RobotNodePrismatic>(
105 robot->getRobotNode(tcpName));
106
107 if (!virtualJoint)
108 {
109 ARMARX_WARNING << "No virtualJoint with name '" << tcpName << "'";
110 emitFailure();
111 }
112
113 VirtualRobot::GazeIK ikSolver(kinematicChain, virtualJoint);
114 ikSolver.setup(30, 50, 30);
115 ikSolver.enableJointLimitAvoidance(true);
116
117 Eigen::Matrix3f m = Eigen::Matrix3f::Identity();
118 FramedPositionPtr targetPosition = in.getGazeTarget();
119 FramedOrientationPtr targetOrientation =
120 new FramedOrientation(m, targetPosition->frame, targetPosition->agent);
121 FramedPosePtr target = new FramedPose(
122 targetPosition, targetOrientation, targetPosition->frame, targetPosition->agent);
123 ARMARX_INFO << "Target: " << target->output();
124 Eigen::Matrix4f goalInRoot = target->toRootEigen(robot);
125 Eigen::Vector3f targetPosRootFrame = goalInRoot.block<3, 1>(0, 3);
126 Eigen::Matrix4f globalPose = target->toGlobalEigen(robot);
127
128 // Visualize gaze target via debug drawer interface
129 context->getDebugDrawerTopicProxy()->setPoseDebugLayerVisu(
130 "gazeTarget", new FramedPose(globalPose, GlobalFrame, ""));
131
132 ARMARX_INFO << "Calculating IK for target position in root frame " << targetPosRootFrame;
133
134 if (!ikSolver.solve(globalPose.block<3, 1>(0, 3)))
135 {
136 ARMARX_WARNING << "IKSolver found no solution! ";
137 emitFailure();
138 return;
139 }
140 else
141 {
142 std::map<std::string, float> targetJointValues;
143
144 for (int i = 0; i < (signed int)kinematicChain->getSize(); i++)
145 {
146 if (kinematicChain->getNode(i)->getName().compare(tcpName) != 0)
147 {
148 targetJointValues[kinematicChain->getNode(i)->getName()] =
149 kinematicChain->getNode(i)->getJointValue();
150 ARMARX_INFO << " joint: " << kinematicChain->getNode(i)->getName()
151 << " value: " << kinematicChain->getNode(i)->getJointValue();
152 }
153 }
154
155 ARMARX_INFO << "number of joints to be set: " << targetJointValues.size();
156 ARMARX_INFO << "setting output: targetJointValues";
157 out.setTargetJointValues(targetJointValues);
158 emitCalculationDone();
159 return;
160 }
161 }
162}
163
164void
166{
167 // put your user code for the exit point here
168 // execution time should be short (<100ms)
169}
170
171// DO NOT EDIT NEXT FUNCTION
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPose class.
Definition FramedPose.h:281
CalculateGazeIk(XMLStateConstructorParams stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
custom implementation of the StatechartContext for a statechart
const RobotPoolPtr & getRobotPool() const
Robot Pool containing collision robots.
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
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_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
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< 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