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"
27 
29 
30 #include <VirtualRobot/Robot.h>
31 #include <VirtualRobot/XML/RobotIO.h>
32 #include <VirtualRobot/IK/GazeIK.h>
33 #include <VirtualRobot/LinkedCoordinate.h>
34 
35 #include <Eigen/Eigen>
36 
37 #include <memory>
38 
39 using namespace armarx;
40 using namespace MotionControlGroup;
41 
42 // DO NOT EDIT NEXT LINE
43 CalculateGazeIk::SubClassRegistry CalculateGazeIk::Registry(CalculateGazeIk::GetName(), &CalculateGazeIk::CreateInstance);
44 
45 
46 
48  XMLStateTemplate < CalculateGazeIk > (stateData),
49  CalculateGazeIkGeneratedBase<CalculateGazeIk>(stateData)
50 {
51 }
52 
54 {
55  // put your user code for the enter-point here
56  // execution time should be short (<100ms)
57 }
58 
60 {
61  // put your user code for the execution-phase here
62  // runs in seperate thread, thus can do complex operations
63  // should check constantly whether isRunningTaskStopped() returns true
64  MotionControlGroupStatechartContext* context = getContext<MotionControlGroupStatechartContext>();
65  VirtualRobot::RobotPtr robot = context->getRobotPool()->getRobot();//->createLocalClone();
66 
67 
68  while (!isRunningTaskStopped()) // stop run function if returning true
69  {
70  // VirtualRobot::RobotPtr robotSnapshot(new RemoteRobot(context->getRobotStateComponent()->getRobotSnapshot("CalculateTCPPoseTime")));
72 
73  if (!robot)
74  {
75  ARMARX_WARNING << "No robot!";
76  emitFailure();
77  }
78 
79  VirtualRobot::RobotNodeSetPtr kinematicChain = robot->getRobotNodeSet(in.getKinematicChainName());
80 
81  if (!kinematicChain)
82  {
83  ARMARX_WARNING << "No kinematicChain with name " << in.getKinematicChainName();
84  emitFailure();
85  }
86 
87  std::string tcpName;
88 
89  if (in.isVirtualPrismaticJointNameSet())
90  {
91  tcpName = in.getVirtualPrismaticJointName();
92  }
93  else
94  {
95  tcpName = kinematicChain->getTCP()->getName();
96  }
97 
98  VirtualRobot::RobotNodePrismaticPtr virtualJoint = std::dynamic_pointer_cast<VirtualRobot::RobotNodePrismatic>(robot->getRobotNode(tcpName));
99 
100  if (!virtualJoint)
101  {
102  ARMARX_WARNING << "No virtualJoint with name '" << tcpName << "'";
103  emitFailure();
104  }
105 
106  VirtualRobot::GazeIK ikSolver(kinematicChain, virtualJoint);
107  ikSolver.setup(30, 50, 30);
108  ikSolver.enableJointLimitAvoidance(true);
109 
111  FramedPositionPtr targetPosition = in.getGazeTarget();
112  FramedOrientationPtr targetOrientation = new FramedOrientation(m, targetPosition->frame, targetPosition->agent);
113  FramedPosePtr target = new FramedPose(targetPosition, targetOrientation, targetPosition->frame, targetPosition->agent);
114  ARMARX_INFO << "Target: " << target->output();
115  Eigen::Matrix4f goalInRoot = target->toRootEigen(robot);
116  Eigen::Vector3f targetPosRootFrame = goalInRoot.block<3, 1>(0, 3);
117  Eigen::Matrix4f globalPose = target->toGlobalEigen(robot);
118 
119  // Visualize gaze target via debug drawer interface
120  context->getDebugDrawerTopicProxy()->setPoseDebugLayerVisu("gazeTarget", new FramedPose(globalPose, GlobalFrame, ""));
121 
122  ARMARX_INFO << "Calculating IK for target position in root frame " << targetPosRootFrame;
123 
124  if (!ikSolver.solve(globalPose.block<3, 1>(0, 3)))
125  {
126  ARMARX_WARNING << "IKSolver found no solution! ";
127  emitFailure();
128  return;
129  }
130  else
131  {
132  std::map<std::string, float> targetJointValues;
133 
134  for (int i = 0; i < (signed int)kinematicChain->getSize(); i++)
135  {
136  if (kinematicChain->getNode(i)->getName().compare(tcpName) != 0)
137  {
138  targetJointValues[kinematicChain->getNode(i)->getName()] = kinematicChain->getNode(i)->getJointValue();
139  ARMARX_INFO << " joint: " << kinematicChain->getNode(i)->getName() << " value: " << kinematicChain->getNode(i)->getJointValue();
140  }
141  }
142 
143  ARMARX_INFO << "number of joints to be set: " << targetJointValues.size();
144  ARMARX_INFO << "setting output: targetJointValues";
145  out.setTargetJointValues(targetJointValues);
146  emitCalculationDone();
147  return;
148  }
149  }
150 
151 }
152 
153 
154 
156 {
157  // put your user code for the exit point here
158  // execution time should be short (<100ms)
159 
160 }
161 
162 // DO NOT EDIT NEXT FUNCTION
164 {
165  return XMLStateFactoryBasePtr(new CalculateGazeIk(stateData));
166 }
167 
RemoteRobot.h
armarx::MotionControlGroup::CalculateGazeIk::onExit
void onExit() override
Definition: CalculateGazeIk.cpp:155
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:66
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::MotionControlGroup::CalculateGazeIk::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CalculateGazeIk.cpp:163
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getDebugDrawerTopicProxy
DebugDrawerInterfacePrx getDebugDrawerTopicProxy()
Definition: MotionControlGroupStatechartContext.h:116
CalculateGazeIk.h
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotPool
const RobotPoolPtr & getRobotPool() const
Robot Pool containing collision robots.
Definition: MotionControlGroupStatechartContext.h:130
IceInternal::Handle< FramedPosition >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotStateComponent
RobotStateComponentInterfacePrx getRobotStateComponent()
Definition: MotionControlGroupStatechartContext.h:96
MotionControlGroupStatechartContext.h
armarx::MotionControlGroup::CalculateGazeIk::onEnter
void onEnter() override
Definition: CalculateGazeIk.cpp:53
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::MotionControlGroup::CalculateGazeIk::Registry
static SubClassRegistry Registry
Definition: CalculateGazeIk.h:44
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::MotionControlGroup::CalculateGazeIk::CalculateGazeIk
CalculateGazeIk(XMLStateConstructorParams stateData)
Definition: CalculateGazeIk.cpp:47
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::MotionControlGroup::CalculateGazeIk::run
void run() override
Definition: CalculateGazeIk.cpp:59
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::MotionControlGroup::CalculateGazeIk
Definition: CalculateGazeIk.h:31
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18