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 
32 #include "../MotionControlGroupStatechartContext.h"
33 
34 using namespace armarx;
35 using namespace MotionControlGroup;
36 
37 // DO NOT EDIT NEXT LINE
38 SetZeroVelocity::SubClassRegistry SetZeroVelocity::Registry(SetZeroVelocity::GetName(),
40 
43  SetZeroVelocityGeneratedBase<SetZeroVelocity>(stateData)
44 {
45 }
46 
47 void
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"];
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 
119 void
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"];
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 
177 void
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
206 {
207  return XMLStateFactoryBasePtr(new SetZeroVelocity(stateData));
208 }
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getLocalRobot
const VirtualRobot::RobotPtr getLocalRobot()
Definition: MotionControlGroupStatechartContext.h:96
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnitObserverName
std::string getKinematicUnitObserverName()
Definition: MotionControlGroupStatechartContext.h:132
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:75
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:100
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:142
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getTCPControlUnit
TCPControlUnitInterfacePrx getTCPControlUnit()
Definition: MotionControlGroupStatechartContext.h:138
armarx::MotionControlGroup::SetZeroVelocity::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: SetZeroVelocity.cpp:205
armarx::Term
Definition: Term.h:111
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle
Definition: forward_declarations.h:8
SetZeroVelocity.h
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobot
const std::shared_ptr< RemoteRobot > getRobot()
Definition: MotionControlGroupStatechartContext.h:90
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::MotionControlGroup::SetZeroVelocity::onExit
void onExit() override
Definition: SetZeroVelocity.cpp:178
armarx::MotionControlGroup::SetZeroVelocity::onEnter
void onEnter() override
Definition: SetZeroVelocity.cpp:48
armarx::MotionControlGroup::SetZeroVelocity::Registry
static SubClassRegistry Registry
Definition: SetZeroVelocity.h:43
armarx::MotionControlGroup::SetZeroVelocity::run
void run() override
Definition: SetZeroVelocity.cpp:120
armarx::channels::KinematicUnitObserver::jointVelocities
const KinematicUnitDatafieldCreator jointVelocities("jointVelocities")
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:88
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnit
KinematicUnitInterfacePrx getKinematicUnit()
Definition: MotionControlGroupStatechartContext.h:120
TimeUtil.h
armarx::MotionControlGroup::SetZeroVelocity::SetZeroVelocity
SetZeroVelocity(XMLStateConstructorParams stateData)
Definition: SetZeroVelocity.cpp:41
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:27
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnitObserver
KinematicUnitObserverInterfacePrx getKinematicUnitObserver()
Definition: MotionControlGroupStatechartContext.h:126