JointPositionControlApply.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 Peter Kaiser ( peter dot kaiser at 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 #include "../MotionControlGroupStatechartContext.h"
27 
28 using namespace armarx;
29 using namespace MotionControlGroup;
30 
31 // DO NOT EDIT NEXT LINE
32 JointPositionControlApply::SubClassRegistry JointPositionControlApply::Registry(JointPositionControlApply::GetName(), &JointPositionControlApply::CreateInstance);
33 
34 
35 
38  JointPositionControlApplyGeneratedBase<JointPositionControlApply>(stateData)
39 {
40 }
41 
43 {
44  MotionControlGroupStatechartContext* context = getContext<MotionControlGroupStatechartContext>();
45 
46  auto targetValues = in.getJointTargetValues();
47 
48  float jointVelocity = 0;
49 
50  if (isInputParameterSet("JointVelocity"))
51  {
52  jointVelocity = in.getJointVelocity();
53  }
54 
55  // Set default velocity and control mode values
56  NameValueMap targetVelocities;
57  NameControlModeMap jointControlModes;
58 
59  for (auto& pair : targetValues)
60  {
61  targetVelocities[pair.first] = jointVelocity;
62  jointControlModes[pair.first] = ePositionControl;
63  }
64 
65  // Override velocity values with specifically given speed values (if available)
66  if (isInputParameterSet("JointVelocities"))
67  {
68  auto givenVelocities = in.getJointVelocities();
69 
70  for (auto& pair : givenVelocities)
71  {
72  targetVelocities[pair.first] = pair.second;
73  }
74  }
75 
76  std::set<std::string> blacklist;
77 
78  float tolerance = in.getJointTargetTolerance();
79 
80  for (auto& pair : targetValues)
81  {
82  DataFieldIdentifierBasePtr dataFieldIdentifier = new DataFieldIdentifier(context->getKinematicUnitObserverName(), "jointangles", pair.first);
83 
84  if (!dataFieldIdentifier)
85  {
86  ARMARX_WARNING << "Could not generate DataFieldIdentifier from '" << pair.first << "'";
87  blacklist.insert(pair.first);
88  continue;
89  }
90 
91  VariantBasePtr dataField = context->getKinematicUnitObserver()->getDataField(dataFieldIdentifier);
92 
93  if (!dataField || !dataField->getInitialized())
94  {
95  ARMARX_WARNING << "Could not obtain DataField of '" << pair.first << "'";
96  blacklist.insert(pair.first);
97  continue;
98  }
99 
100  float currentValue = dataField->getFloat();
101 
102  if (currentValue >= pair.second - tolerance && currentValue <= pair.second + tolerance)
103  {
104  // Target is reached. Set velocity to zero
105  targetVelocities[pair.first] = 0;
106  }
107  else
108  {
109  targetVelocities[pair.first] = fabs(targetVelocities[pair.first]);
110  }
111  }
112 
113  // Create and install success condition
114  Term condition;
115  bool stillWaitingForJoint = false;
116 
117  for (auto& pair : targetValues)
118  {
119  if (blacklist.find(pair.first) != blacklist.end())
120  {
121  // We failed to get the current value of this joint.
122  // It is probably a virtual joint that is not handled by the kinematic unit.
123  // Those joints are sometimes part of some RobotNodeSets.
124  continue;
125  }
126 
127  if (targetVelocities[pair.first] == 0)
128  {
129  // Target already reached, omit check
130  continue;
131  }
132 
133  Literal approx(DataFieldIdentifier(context->getKinematicUnitObserverName(), "jointangles", pair.first), checks::approx, {pair.second, tolerance});
134  condition = condition || approx;
135 
136  stillWaitingForJoint = true;
137  }
138 
139  if (stillWaitingForJoint)
140  {
141  installConditionForJointReachedTarget(condition);
142  }
143 
144  // Create and install timeout condition
145  int timeout = in.getTimeout();
146 
147  if (timeout > 0)
148  {
149  setTimeoutEvent(timeout, createEventTimeout());
150  }
151 
152  context->getKinematicUnit()->switchControlMode(jointControlModes);
153  context->getKinematicUnit()->setJointAngles(targetValues);
154 
155  // Position control with velocity targets is currently not supported by the kinematic unit.
156  // However, once it is supported uncommenting the following line will enable it
157  //context->getKinematicUnit()->setJointVelocities(targetVelocities);
158 
159  if (!stillWaitingForJoint)
160  {
161  // All joints reached their target
162  emitSuccess();
163  }
164 }
165 
167 {
168 }
169 
171 {
172 }
173 
175 {
176 }
177 
178 
179 // DO NOT EDIT NEXT FUNCTION
181 {
182  return XMLStateFactoryBasePtr(new JointPositionControlApply(stateData));
183 }
184 
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnitObserverName
std::string getKinematicUnitObserverName()
Definition: MotionControlGroupStatechartContext.h:108
armarx::MotionControlGroup::JointPositionControlApply::onExit
void onExit() override
Definition: JointPositionControlApply.cpp:174
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:66
armarx::MotionControlGroup::JointPositionControlApply::onEnter
void onEnter() override
Definition: JointPositionControlApply.cpp:42
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::Term
Definition: Term.h:111
armarx::MotionControlGroup::JointPositionControlApply
Definition: JointPositionControlApply.h:31
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::MotionControlGroup::JointPositionControlApply::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: JointPositionControlApply.cpp:180
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::MotionControlGroup::JointPositionControlApply::JointPositionControlApply
JointPositionControlApply(XMLStateConstructorParams stateData)
Definition: JointPositionControlApply.cpp:36
armarx::MotionControlGroup::JointPositionControlApply::run
void run() override
Definition: JointPositionControlApply.cpp:166
JointPositionControlApply.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnit
KinematicUnitInterfacePrx getKinematicUnit()
Definition: MotionControlGroupStatechartContext.h:100
armarx::Literal
Definition: Term.h:208
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::MotionControlGroup::JointPositionControlApply::Registry
static SubClassRegistry Registry
Definition: JointPositionControlApply.h:45
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnitObserver
KinematicUnitObserverInterfacePrx getKinematicUnitObserver()
Definition: MotionControlGroupStatechartContext.h:104
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48
armarx::MotionControlGroup::JointPositionControlApply::onBreak
void onBreak() override
Definition: JointPositionControlApply.cpp:170