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