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
37using namespace armarx;
38using namespace MotionControlGroup;
39
40// DO NOT EDIT NEXT LINE
41CalculateJointAngleConfiguration::SubClassRegistry
44
50
51void
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 {
72 ikType = VirtualRobot::IKSolver::Position;
73 }
74
75
77
78 if (!context)
79 {
80 ARMARX_WARNING << "Need a RobotStatechartContext" << flush;
82 }
83
84 if (!context->robotStateComponent)
85 {
86 ARMARX_WARNING << "No RobotStatechartContext->robotStateComponent" << flush;
88 }
89
91
92 if (!robot)
93 {
94 ARMARX_WARNING << "Could not create a local clone of RemoteRobot" << flush;
96 }
97
98 if (!robot->hasRobotNodeSet(kinChainName))
99 {
100 ARMARX_WARNING << "Robot does not know kinematic chain with name " << kinChainName << flush;
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;
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);
157 }
158}
159
160// DO NOT EDIT NEXT FUNCTION
161std::string
163{
164 return "CalculateJointAngleConfiguration";
165}
166
167// DO NOT EDIT NEXT FUNCTION
static VirtualRobot::LinkedCoordinate createLinkedCoordinate(const VirtualRobot::RobotPtr &virtualRobot, const FramedPositionPtr &position, const FramedOrientationPtr &orientation)
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
RobotStateComponentInterfacePrx robotStateComponent
Prx for the RobotState.
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
void addVariant(const Variant &variant)
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
ContextType * getContext() const
Definition StateBase.h:71
void sendEvent(const EventPtr event, StateBasePtr eventProcessor=nullptr)
Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere...
Definition StateUtil.cpp:40
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition State.cpp:482
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition State.h:620
The Variant class is described here: Variants.
Definition Variant.h:224
XMLStateTemplate(const XMLStateConstructorParams &params)
Definition XMLState.h:149
#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
#define ARMARX_LOG
Definition Logging.h:165
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
const VariantTypeId String
Definition Variant.h:921
const VariantTypeId Float
Definition Variant.h:919
This file offers overloads of toIce() and fromIce() functions for STL container types.
const LogSender::manipulator flush
Definition LogSender.h:251
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64