CalculateJointAngleConfiguration.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 Valerij Wittenbeck ( valerij dot wittenbeck at student dot kit dot edu )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 // Core
29 
30 // Virtual Robot
32 #include <VirtualRobot/IK/GenericIKSolver.h>
33 #include <VirtualRobot/LinkedCoordinate.h>
34 
35 using namespace armarx;
36 using namespace MotionControlGroup;
37 
38 // DO NOT EDIT NEXT LINE
40 
41 
42 
45 {
46 }
47 
49 {
50  //RobotStatechartContext* context = getContext<RobotStatechartContext>();
51  //VirtualRobot::RobotPtr robot(new RemoteRobot(context->robotStateComponent->getRobotSnapshot("CalculateTCPPoseTime")));
52 
53  // TODO: with the following line, the IK doesn't find a solution, so this terrible hack must be used. Fix it!!!
54  //VirtualRobot::RobotPtr robot = robotPtr->clone("CalculateTCPPoseClone");
55  //std::string robotModelFile;
56  //ArmarXDataPath::getAbsolutePath("Armar4/Armar4/ArmarIV.xml", robotModelFile);
57  //ArmarXDataPath::getAbsolutePath("Armar3/robotmodel/ArmarIII.xml", robotModelFile);
58  //VirtualRobot::RobotPtr robot = VirtualRobot::RobotIO::loadRobot(robotModelFile.c_str());
59 
60  std::string kinChainName = getInput<std::string>("kinematicChainName");
61  float maxError = getInput<float>("targetPositionDistanceTolerance");
62  float maxErrorRot = getInput<float>("targetOrientationDistanceTolerance");
63  bool withOrientation = getInput<bool>("ikWithOrientation");
64  VirtualRobot::IKSolver::CartesianSelection ikType = VirtualRobot::IKSolver::All;
65 
66  if (!withOrientation)
67  {
69  }
70 
71 
72  RobotStatechartContext* context = getContext<RobotStatechartContext>();
73 
74  if (!context)
75  {
76  ARMARX_WARNING << "Need a RobotStatechartContext" << flush;
77  sendEvent<Failure>();
78  }
79 
80  if (!context->robotStateComponent)
81  {
82  ARMARX_WARNING << "No RobotStatechartContext->robotStateComponent" << flush;
83  sendEvent<Failure>();
84  }
85 
87 
88  if (!robot)
89  {
90  ARMARX_WARNING << "Could not create a local clone of RemoteRobot" << flush;
91  sendEvent<Failure>();
92  }
93 
94  if (!robot->hasRobotNodeSet(kinChainName))
95  {
96  ARMARX_WARNING << "Robot does not know kinematic chain with name " << kinChainName << flush;
97  sendEvent<Failure>();
98  }
99 
100  ARMARX_INFO << "Setting up ik solver for kin chain: " << kinChainName << ", max position error:" << maxError << ", max orientation erorr " << maxErrorRot << ", useOrientation:" << withOrientation << armarx::flush;
101  VirtualRobot::GenericIKSolverPtr ikSolver(new VirtualRobot::GenericIKSolver(robot->getRobotNodeSet(kinChainName), VirtualRobot::JacobiProvider::eSVDDamped));
102  ikSolver->setVerbose(true);
103  ikSolver->setMaximumError(maxError, maxErrorRot);
104  ikSolver->setupJacobian(0.6f, 10);
105 
106  VirtualRobot::LinkedCoordinate target = FramedPose::createLinkedCoordinate(robot, getInput<FramedPosition>("targetTCPPosition"), getInput<FramedOrientation>("targetTCPOrientation"));
107  ARMARX_INFO << "IK target: " << target.getPose() << armarx::flush;
108  Eigen::Matrix4f goalInRoot = target.getInFrame(robot->getRootNode());
109 
110  // // test
111  // VirtualRobot::RobotNodePtr rn = robot->getRobotNode("LeftTCP");
112  // Eigen::Matrix4f goalTmpTCP = rn->getPoseInRootFrame();
113  Eigen::Matrix4f goalGlobal = robot->getRootNode()->toGlobalCoordinateSystem(goalInRoot);
114  ARMARX_INFO << "GOAL in root: " << goalInRoot << armarx::flush;
115  ARMARX_INFO << "GOAL global: " << goalGlobal << armarx::flush;
116  // ARMARX_INFO << "GOAL TCP:" << goalTmpTCP << endl;
117 
118  ARMARX_INFO << "Calculating IK" << flush;
119 
120  //if (!ikSolver->solve(goalGlobal, VirtualRobot::IKSolver::Position, 50))
121  if (!ikSolver->solve(goalGlobal, ikType, 5))
122  {
123  ARMARX_WARNING << "IKSolver found no solution! " << flush;
124  sendEvent<Failure>();
125  }
126  else
127  {
128 
131  VirtualRobot::RobotNodeSetPtr kinematicChain = robot->getRobotNodeSet(kinChainName);
132 
133  for (int i = 0; i < (signed int)kinematicChain->getSize(); i++)
134  {
135  jointNames.addVariant(Variant(kinematicChain->getNode(i)->getName()));
136  targetJointValues.addVariant(Variant(kinematicChain->getNode(i)->getJointValue()));
137  ARMARX_LOG << " joint: " << kinematicChain->getNode(i)->getName() << " value: " << kinematicChain->getNode(i)->getJointValue() << flush;
138  }
139 
140  ARMARX_LOG << "number of joints to be set: " << jointNames.getSize() << flush;
141  ARMARX_LOG << "setting output: jointNames" << flush;
142  setOutput("jointNames", jointNames);
143  ARMARX_LOG << "setting output: targetJointValues" << flush;
144  setOutput("targetJointValues", targetJointValues);
145  sendEvent<EvCalculationDone>();
146  }
147 }
148 
149 // DO NOT EDIT NEXT FUNCTION
151 {
152  return "CalculateJointAngleConfiguration";
153 }
154 
155 // DO NOT EDIT NEXT FUNCTION
157 {
159 }
160 
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
RemoteRobot.h
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::State::setOutput
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition: State.cpp:434
armarx::FramedPose::createLinkedCoordinate
static VirtualRobot::LinkedCoordinate createLinkedCoordinate(const VirtualRobot::RobotPtr &virtualRobot, const FramedPositionPtr &position, const FramedOrientationPtr &orientation)
Definition: FramedPose.cpp:1133
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::CalculateJointAngleConfiguration::Registry
static SubClassRegistry Registry
Definition: CalculateJointAngleConfiguration.h:47
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RobotStatechartContext
Definition: RobotStatechartContext.h:66
armarx::MotionControlGroup::CalculateJointAngleConfiguration::run
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
Definition: CalculateJointAngleConfiguration.cpp:48
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:47
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::MotionControlGroup::CalculateJointAngleConfiguration::GetName
static std::string GetName()
Definition: CalculateJointAngleConfiguration.cpp:150
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
CalculateJointAngleConfiguration.h
armarx::MotionControlGroup::CalculateJointAngleConfiguration::CalculateJointAngleConfiguration
CalculateJointAngleConfiguration(XMLStateConstructorParams stateData)
Definition: CalculateJointAngleConfiguration.cpp:43
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:328
armarx::RobotStatechartContext::robotStateComponent
RobotStateComponentInterfacePrx robotStateComponent
Prx for the RobotState.
Definition: RobotStatechartContext.h:96
armarx::MotionControlGroup::CalculateJointAngleConfiguration
Definition: CalculateJointAngleConfiguration.h:34
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::MotionControlGroup::CalculateJointAngleConfiguration::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CalculateJointAngleConfiguration.cpp:156
armarx::VariantType::String
const VariantTypeId String
Definition: Variant.h:920
armarx::VariantType::SingleTypeVariantList
const VariantTypeId SingleTypeVariantList
Definition: SingleTypeVariantList.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SingleTypeVariantList::getSize
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:124
armarx::SingleTypeVariantList::addVariant
void addVariant(const Variant &variant)
Definition: SingleTypeVariantList.cpp:100
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
FinalState.h