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