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