jointik.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-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
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "jointik.h"
25
26#include <memory>
27
28using namespace armarx;
29
30void
32{
33
34 std::scoped_lock lock(dataMutex);
35
36 //Check if all sensor values have at least been reported once to avoid segfaults
37 if (!(reportedJointAnglesBool && reportedJointVelocitiesBool && globalPos))
38 {
39 return;
40 }
41
42
43 RemoteRobot::synchronizeLocalClone(robot, robotStateComponent);
44
45 NameValueMap newValues;
46 for (size_t i = 0; i < allRobotNodeSet.size(); i++)
47 {
48 std::string name = allRobotNodeSet[i]->getName();
49 //Sort non movable joints out
50 if (name == "VirtualCentralGaze" || name == "Hand Palm 1 L" || name == "Hand Palm 1 R")
51 {
52 continue;
53 }
54 newValues[name] = reportedJointAngles[name] +
55 (((float)interval) / 1000.0) * reportedJointVelocities[name];
56 }
57
58 robot->setJointValues(newValues);
59
60 //HeadIK part
61 VirtualRobot::RobotNodeSetPtr kinematicChain = robot->getRobotNodeSet(headIKName);
62
63 //ARMARX_LOG << "kinematic chain name" << kinematicChain->getName();
64 VirtualRobot::RobotNodePrismaticPtr virtualJoint;
65
66 for (size_t i = 0; i < kinematicChain->getSize(); i++)
67 {
68 //TODO VirtualCentralGaze in Armar4 nicht vorhanden
69 if (kinematicChain->getNode(i)->getName().compare("VirtualCentralGaze") == 0)
70 {
71 virtualJoint = std::dynamic_pointer_cast<VirtualRobot::RobotNodePrismatic>(
72 kinematicChain->getNode(i));
73 }
74 }
75
76 VirtualRobot::GazeIK ikSolver(kinematicChain, virtualJoint);
77 ikSolver.enableJointLimitAvoidance(true);
78 //ikSolver.setup(10, 30, 20);
79
80 Eigen::Vector3f targetPosition = globalPos->toEigen();
81 bool solutionFound = ikSolver.solve(targetPosition);
82
83 if (solutionFound)
84 {
85 NameValueMap targetJointAngles;
86 for (size_t i = 0; i < kinematicChain->getSize(); i++)
87 {
88 std::string jointName = kinematicChain->getNode(i)->getName();
89 if (jointName.compare("VirtualCentralGaze") != 0)
90 {
91 targetJointAngles[jointName] = kinematicChain->getNode(i)->getJointValue();
92 }
93 }
94
95 std::scoped_lock lock(mutex);
96
97 jointAngles.swap(targetJointAngles);
98 }
99 else
100 {
101 ARMARX_WARNING_S << "HeadIK no solution found";
102 }
103}
104
105void
106JointIK::setRobot(std::string nodeSetName,
107 std::string headIKName,
108 RobotStateComponentInterfacePrx robotStateComponent)
109{
110 std::scoped_lock lock(mutex);
111
113 robotStateComponent, VirtualRobot::RobotIO::RobotDescription::eFull);
114
115 VirtualRobot::RobotNodeSetPtr nodeSetPtr = this->robot->getRobotNodeSet(nodeSetName);
116 this->allRobotNodeSet = nodeSetPtr->getAllRobotNodes();
117 this->headIKName = headIKName;
118 this->robotStateComponent = robotStateComponent;
119}
120
121void
122JointIK::setBools(bool armar4, bool velocityBased)
123{
124 std::scoped_lock lock(mutex);
125
126 this->armar4 = armar4;
127 this->velocityBased = velocityBased;
128}
129
130void
131JointIK::reportJointAngles(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
132{
133 std::scoped_lock lock(dataMutex);
134
135 reportedJointAnglesBool = true;
136 this->reportedJointAngles = values;
137}
138
139void
140JointIK::reportJointVelocities(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
141{
142 std::scoped_lock lock(dataMutex);
143
144 reportedJointVelocitiesBool = true;
145 this->reportedJointVelocities = values;
146}
147
148void
149JointIK::reportHeadTargetChanged(const NameValueMap& targetJointAngles,
150 const FramedPositionBasePtr& targetPosition)
151{
152 std::scoped_lock lock(dataMutex);
153
154 globalPos = FramedPositionPtr::dynamicCast(targetPosition);
155}
156
157void
159{
160 std::scoped_lock lock(dataMutex);
161
162 // globalPos = {};
163 reportedJointVelocitiesBool = false;
164 reportedJointAnglesBool = false;
165}
#define float
Definition 16_Level.h:22
constexpr T c
void reportJointAngles(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition jointik.cpp:131
void reportHeadTargetChanged(const NameValueMap &targetJointAngles, const FramedPositionBasePtr &targetPosition)
Definition jointik.cpp:149
void onStop() override
Definition jointik.cpp:158
void reportJointVelocities(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition jointik.cpp:140
void setBools(bool armar4, bool velocityBased)
Definition jointik.cpp:122
void calc() override
Definition jointik.cpp:31
void setRobot(std::string nodeSetName, std::string headIKName, RobotStateComponentInterfacePrx robotStateComponent)
Definition jointik.cpp:106
std::mutex mutex
Definition reflex.h:138
std::string name
Definition reflex.h:148
std::map< std::string, float > jointAngles
Definition reflex.h:140
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx