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