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 
41 using namespace armarx;
42 using namespace MotionControlGroup;
43 
44 // DO NOT EDIT NEXT LINE
45 CalculateGazeIk::SubClassRegistry CalculateGazeIk::Registry(CalculateGazeIk::GetName(),
47 
50  CalculateGazeIkGeneratedBase<CalculateGazeIk>(stateData)
51 {
52 }
53 
54 void
56 {
57  // put your user code for the enter-point here
58  // execution time should be short (<100ms)
59 }
60 
61 void
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 
118  FramedPositionPtr targetPosition = in.getGazeTarget();
119  FramedOrientationPtr targetOrientation =
120  new FramedOrientation(m, targetPosition->frame, targetPosition->agent);
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 
164 void
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
174 {
175  return XMLStateFactoryBasePtr(new CalculateGazeIk(stateData));
176 }
RemoteRobot.h
armarx::MotionControlGroup::CalculateGazeIk::onExit
void onExit() override
Definition: CalculateGazeIk.cpp:165
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:75
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:668
armarx::MotionControlGroup::CalculateGazeIk::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CalculateGazeIk.cpp:173
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getDebugDrawerTopicProxy
DebugDrawerInterfacePrx getDebugDrawerTopicProxy()
Definition: MotionControlGroupStatechartContext.h:144
CalculateGazeIk.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotPool
const RobotPoolPtr & getRobotPool() const
Robot Pool containing collision robots.
Definition: MotionControlGroupStatechartContext.h:160
IceInternal::Handle< FramedPosition >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotStateComponent
RobotStateComponentInterfacePrx getRobotStateComponent()
Definition: MotionControlGroupStatechartContext.h:114
MotionControlGroupStatechartContext.h
armarx::MotionControlGroup::CalculateGazeIk::onEnter
void onEnter() override
Definition: CalculateGazeIk.cpp:55
armarx::MotionControlGroup::CalculateGazeIk::Registry
static SubClassRegistry Registry
Definition: CalculateGazeIk.h:43
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::MotionControlGroup::CalculateGazeIk::CalculateGazeIk
CalculateGazeIk(XMLStateConstructorParams stateData)
Definition: CalculateGazeIk.cpp:48
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::MotionControlGroup::CalculateGazeIk::run
void run() override
Definition: CalculateGazeIk.cpp:62
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::MotionControlGroup::CalculateGazeIk
Definition: CalculateGazeIk.h:31
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19