SetZeroVelocity.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 Mirko Waechter ( mirko dot waechter 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
25#include "SetZeroVelocity.h"
26
27#include <VirtualRobot/RobotConfig.h>
28#include <VirtualRobot/RobotNodeSet.h>
29
31
33
34using namespace armarx;
35using namespace MotionControlGroup;
36
37// DO NOT EDIT NEXT LINE
38SetZeroVelocity::SubClassRegistry SetZeroVelocity::Registry(SetZeroVelocity::GetName(),
40
43 SetZeroVelocityGeneratedBase<SetZeroVelocity>(stateData)
44{
45}
46
47void
49{
50
52 getContext<MotionControlGroupStatechartContext>();
53
54 if (context->getTCPControlUnit()->isRequested())
55 {
56 context->getTCPControlUnit()->release();
57 usleep(100000);
58 }
59 int decelerationTime = in.getDecelerationTime();
60
61 if (decelerationTime != 0)
62 {
63 setTimeoutEvent(decelerationTime, createEventTimeout());
64 }
65
66 std::string kinematicChain = "";
67
68 if (in.isKinematicChainNameSet())
69 {
70 kinematicChain = in.getKinematicChainName();
71 }
72
73 Ice::StringSeq nodeNames, realJointNames;
74
75 if (kinematicChain.empty())
76 {
77 nodeNames = context->getLocalRobot()->getRobotNodeNames();
78 }
79 else
80 {
81 VirtualRobot::RobotNodeSetPtr nodeSet =
82 context->getLocalRobot()->getRobotNodeSet(kinematicChain);
83
84 for (size_t i = 0; i < nodeSet->getSize(); ++i)
85 {
86 nodeNames.push_back(nodeSet->getNode(i)->getName());
87 }
88 }
89
90
91 Term zeroVeloCondition;
92
93 auto channels = context->getKinematicUnitObserver()->getAvailableChannels(false);
94 ChannelRegistryEntry& channelEntry = channels["jointvelocities"];
95 NameValueMap jointVelocities;
96
97 for (auto& nodeName : nodeNames)
98 {
99 if (channelEntry.dataFields.count(nodeName) == 0 ||
100 !channelEntry.dataFields[nodeName].value->getInitialized())
101 {
102 continue;
103 }
104
105 jointVelocities[nodeName] = channelEntry.dataFields[nodeName].value->getFloat();
106 zeroVeloCondition =
107 zeroVeloCondition &&
108 Literal(context->getKinematicUnitObserverName() + ".jointvelocities." + nodeName,
109 "inrange",
110 Literal::createParameterList(-0.01f, 0.01f));
111 realJointNames.push_back(nodeName);
112 }
113
114
115 installConditionForZeroVelocityReached(zeroVeloCondition);
116 out.setJointNames(realJointNames);
117}
118
119void
121{
123 getContext<MotionControlGroupStatechartContext>();
124
125
126 std::string kinematicChain = "";
127
128 if (in.isKinematicChainNameSet())
129 {
130 kinematicChain = in.getKinematicChainName();
131 }
132
133 NameValueMap jointMap = context->getRobot()->getSharedRobot()->getConfig();
134
135 // std::vector<VirtualRobot::RobotNodePtr> nodes;
136 // if(kinematicChain.empty())
137 // {
138 // context->getRobot()->getRobotNodes(nodes);
139 // }
140 // else
141 // {
142 // VirtualRobot::RobotNodeSetPtr nodeSet = context->getRobot()->getRobotNodeSet(kinematicChain);
143 // nodes = nodeSet->getAllRobotNodes();
144 // }
145
146 auto channels = context->getKinematicUnitObserver()->getAvailableChannels(false);
147 ChannelRegistryEntry& channelEntry = channels["jointvelocities"];
148 NameValueMap jointVelocities;
149 Ice::StringSeq validJoints;
150
151 for (auto& node : jointMap)
152 {
153 if (channelEntry.dataFields.count(node.first) == 0 ||
154 !channelEntry.dataFields[node.first].value->getInitialized())
155 {
156 continue;
157 }
158
159 jointVelocities[node.first] = channelEntry.dataFields[node.first].value->getFloat();
160 validJoints.push_back(node.first);
161 }
162
163
164 while (!isRunningTaskStopped())
165 {
166
167 for (auto& jointName : validJoints)
168 {
169 jointVelocities[jointName] *= 0.5;
170 }
171
172 context->getKinematicUnit()->setJointVelocities(jointVelocities);
174 }
175}
176
177void
179{
181 getContext<MotionControlGroupStatechartContext>();
182 NameValueMap config = context->getRobot()->getSharedRobot()->getConfig();
183 auto nodes = out.getJointNames();
184 Ice::StringSeq activeJoints;
185 std::vector<float> jointTargets;
186 ARMARX_VERBOSE << config;
187 for (auto nodeName : nodes)
188 {
189 auto it = config.find(nodeName);
190
191 if (it != config.end())
192 {
193
194 jointTargets.push_back(it->second);
195 activeJoints.push_back(it->first);
196 }
197 }
198
199 out.setJointNames(activeJoints);
200 out.setCurrentJointPositions(jointTargets);
201}
202
203// DO NOT EDIT NEXT FUNCTION
Literals are part of the user front end of the ArmarX condition mechanism.
Definition Term.h:209
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition Term.cpp:142
custom implementation of the StatechartContext for a statechart
SetZeroVelocity(XMLStateConstructorParams stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Terms are part of the user front end of the ArmarX condition mechanism.
Definition Term.h:112
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition TimeUtil.cpp:100
Class for legacy to stay compatible with old statecharts.
Definition XMLState.h:147
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64