MoveJointsVelControl.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::FindAndGraspObjectGroup
19  * @author Valerij Wittenbeck ( valerij dot wittenbeck at student dot 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 "MoveJointsVelControl.h"
27 
28 using namespace armarx;
29 using namespace FindAndGraspObjectGroup;
30 
31 // DO NOT EDIT NEXT LINE
33 
34 
35 
38 {
39 }
40 
42 {
43  RobotStatechartContext* context = getContext<RobotStatechartContext>();
44  //NameValueMap targetJointVelocities;
45  SingleTypeVariantListPtr jointNames = getInput<SingleTypeVariantList>("jointNames");
46  SingleTypeVariantListPtr targetJointValues = getInput<SingleTypeVariantList>("targetJointValues");
47  float maxVel = 0.5f;
48 
49  if (isInputParameterSet("jointMaxSpeed"))
50  {
51  maxVel = fabs(getInput<float>("jointMaxSpeed"));
52  }
53 
54  if (jointNames->getSize() != targetJointValues->getSize())
55  {
56  throw LocalException("Sizes of joint name list and joint value list do not match!");
57  }
58 
59  //RobotStatechartContext* context = getContext<RobotStatechartContext>();
60  VirtualRobot::RobotPtr robot(new RemoteRobot(context->robotStateComponent->getRobotSnapshot("moveJointsVel")));
61 
62  Eigen::VectorXf errorJoint(jointNames->getSize());
63  Eigen::VectorXf currentJoint(jointNames->getSize());
64  ARMARX_VERBOSE << "number of joints that will be set: " << jointNames->getSize() << ", maxVel:" << maxVel << flush;
65 
66  for (int i = 0; i < jointNames->getSize(); i++)
67  {
68  currentJoint(i) = robot->getRobotNode(jointNames->getVariant(i)->getString())->getJointValue();
69  errorJoint(i) = targetJointValues->getVariant(i)->getFloat() - currentJoint(i);
70  }
71 
72  /*
73  for (int i=0; i<jointNames->getSize(); i++)
74  {
75  float newVel = targetJointValues->getVariant(i)->getFloat() - robot->getRobotNode(jointNames->getVariant(i)->getString())->getJointValue();
76  if (newVel>maxVel)
77  newVel = maxVel;
78  if (newVel<-maxVel)
79  newVel = -maxVel;
80  targetJointVelocities[jointNames->getVariant(i)->getString()] = newVel;
81  ARMARX_VERBOSE << "setting joint angle for joint '" << jointNames->getVariant(i)->getString() << "' to " << newVel << std::endl;
82  }*/
83  /*
84  // now install the condition
85  Term cond;
86  float tolerance = getInput<float>("jointTargetTolerance");
87  for (int i=0; i<jointNames->getSize(); i++)
88  {
89  ARMARX_DEBUG << "creating condition (" << i << " of " << jointNames->getSize() << ")" << flush;
90  ParameterList inrangeCheck;
91  inrangeCheck.push_back(new Variant(targetJointValues->getVariant(i)->getFloat()-tolerance));
92  inrangeCheck.push_back(new Variant(targetJointValues->getVariant(i)->getFloat()+tolerance));
93 
94  Literal inrangeLiteral(DataFieldIdentifier(context->getKinematicUnitObserverName(), "jointangles", jointNames->getVariant(i)->getString()), "inrange", inrangeCheck);
95  cond = cond && inrangeLiteral;
96  }
97 
98  ARMARX_VERBOSE << "installing condition: EvJointTargetReached" << std::endl;
99  targetReachedCondition = installCondition<MotionControl::EvJointTargetReached>(cond);
100  ARMARX_VERBOSE << "condition installed: EvJointTargetReached" << std::endl;
101  timeoutEvent = setTimeoutEvent(getInput<int>("timeoutInMs"), createEvent<MotionControl::EvTimeout>());
102  ARMARX_VERBOSE << "timeout installed" << std::endl;*/
103 
104  float VEL_GAIN = 1.0f;
105  float VEL_MAX_COEFF = 0.02f;
106  ARMARX_VERBOSE << "JointVelocities " << errorJoint;
107  //ARMARX_VERBOSE << "JointVelocities GAIN" << errorJoint * VEL_GAIN;
108  float maxV = fabs(errorJoint.maxCoeff());
109 
110  if (maxV > VEL_MAX_COEFF)
111  {
112  errorJoint = errorJoint / maxV * VEL_MAX_COEFF * VEL_GAIN;
113  ARMARX_VERBOSE << "JointVelocities VEL_MAX_COEFF" << errorJoint;
114  }
115 
116 
117  // build name value map
118  NameValueMap targetVelocities;
119  NameValueMap targetAngles;
120  NameControlModeMap controlModes;
121  //const std::vector< VirtualRobot::RobotNodePtr > nodes = robotNodeSet->getAllRobotNodes();
122 
123  for (int i = 0; i < jointNames->getSize(); i++)
124  {
125  std::string jName = jointNames->getVariant(i)->getString();
126  targetVelocities[jName] = errorJoint(i) * VEL_GAIN;
127  targetAngles[jName] = currentJoint(i) + errorJoint(i);
128  controlModes[jName] = ePositionVelocityControl;
129  //controlModes[n->getName()] = eVelocityControl;
130  }
131 
132  // execute velocities
133  context->kinematicUnitPrx->switchControlMode(controlModes);
134  context->kinematicUnitPrx->setJointAngles(targetAngles);
135  context->kinematicUnitPrx->setJointVelocities(targetVelocities);
136 
137  //context->kinematicUnitPrx->setJointVelocities(targetJointVelocities);
138  sendEvent<EvJointsActuated>();
139 }
140 
141 
142 
144 {
145  //setJointVelToZero();
146  //removeCondition(targetReachedCondition);
147  //removeTimeoutEvent(timeoutEvent);
148 }
149 
151 {
152  /*
153  SingleTypeVariantListPtr jointNames = getInput<SingleTypeVariantList>("jointNames");
154  SingleTypeVariantListPtr targetJointValues = getInput<SingleTypeVariantList>("targetJointValues");
155  SingleTypeVariantList resultList(VariantType::Float);
156 
157  for(int i=0; i<jointNames->getSize(); i++ )
158  {
159  DataFieldIdentifierPtr datafield = new DataFieldIdentifier(context->getKinematicUnitObserverName(),"jointangles", jointNames->getVariant(i)->getString());
160  float jointAngleActual = context->kinematicUnitObserverPrx->getDataField(datafield)->getFloat();
161  float jointTargetValue = targetJointValues->getVariant(i)->getFloat();
162  resultList.addVariant(Variant(jointAngleActual - jointTargetValue));
163  }
164  setOutput("jointDistancesToTargets", resultList);
165  */
166  //ARMARX_INFO << "output: " << StateUtilFunctions::getDictionaryString(getOutputParameters()) << std::endl;
167 
168  //setJointVelToZero();
169 
170  //removeCondition(targetReachedCondition);
171  //removeTimeoutEvent(timeoutEvent);
172 }
173 
174 //TODO: this is a straight copy from LiftObject, find a better way
176 {
177  // set vel to zero
178  ARMARX_DEBUG << "setting joint velocity to zero " << flush;
179 
180  std::string kinChainName = getInput<std::string>("kinematicChain");
181  RobotStatechartContext* context = getContext<RobotStatechartContext>();
182 
183  if (!context)
184  {
185  ARMARX_WARNING << "Need a RobotStatechartContext" << flush;
186  sendEvent<Failure>();
187  }
188 
189  if (!context->robotStateComponent)
190  {
191  ARMARX_WARNING << "No RobotStatechartContext->robotStateComponent" << flush;
192  sendEvent<Failure>();
193  }
194 
196 
197  if (!robot->hasRobotNodeSet(kinChainName))
198  {
199  ARMARX_WARNING << "Robot does not know kinematic chain with name " << kinChainName << flush;
200  sendEvent<Failure>();
201  }
202 
203  VirtualRobot::RobotNodeSetPtr rns = robot->getRobotNodeSet(kinChainName);
204 
205 
206  NameValueMap targetJointVelocities;
207 
208  for (unsigned int i = 0; i < rns->getSize(); i++)
209  {
210  targetJointVelocities[rns->getNode(i)->getName()] = 0;
211  }
212 
213  context->kinematicUnitPrx->setJointVelocities(targetJointVelocities);
214 }
215 
216 // DO NOT EDIT NEXT FUNCTION
218 {
219  return "MoveJointsVelControl";
220 }
221 
222 // DO NOT EDIT NEXT FUNCTION
224 {
225  return XMLStateFactoryBasePtr(new MoveJointsVelControl(stateData));
226 }
227 
RemoteRobot.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::onEnter
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
Definition: MoveJointsVelControl.cpp:41
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::navigation::platform_controller::platform_global_trajectory::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformGlobalTrajectoryController.h:93
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::onExit
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
Definition: MoveJointsVelControl.cpp:150
armarx::State::isInputParameterSet
bool isInputParameterSet(const std::string &key) const
Checks whether a given input parameter is set or not.
Definition: State.cpp:489
IceInternal::Handle< SingleTypeVariantList >
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveJointsVelControl.cpp:223
armarx::RobotStatechartContext
Definition: RobotStatechartContext.h:66
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::Registry
static SubClassRegistry Registry
Definition: MoveJointsVelControl.h:49
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::onBreak
void onBreak() override
Virtual function, in which the behaviour of state is defined, when it is abnormally exited....
Definition: MoveJointsVelControl.cpp:143
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::setJointVelToZero
void setJointVelToZero()
Definition: MoveJointsVelControl.cpp:175
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::RemoteRobot
Mimics the behaviour of the VirtualRobot::Robot class while redirecting everything to an Ice proxy.
Definition: RemoteRobot.h:139
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:328
armarx::RobotStatechartContext::robotStateComponent
RobotStateComponentInterfacePrx robotStateComponent
Prx for the RobotState.
Definition: RobotStatechartContext.h:96
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::GetName
static std::string GetName()
Definition: MoveJointsVelControl.cpp:217
armarx::FindAndGraspObjectGroup::MoveJointsVelControl::MoveJointsVelControl
MoveJointsVelControl(XMLStateConstructorParams stateData)
Definition: MoveJointsVelControl.cpp:36
MoveJointsVelControl.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::RobotStatechartContext::kinematicUnitPrx
KinematicUnitInterfacePrx kinematicUnitPrx
Definition: RobotStatechartContext.h:97
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::FindAndGraspObjectGroup::MoveJointsVelControl
Definition: MoveJointsVelControl.h:34