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