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
26
28
29using namespace armarx;
30using namespace FindAndGraspObjectGroup;
31
32// DO NOT EDIT NEXT LINE
33MoveJointsVelControl::SubClassRegistry
36
41
42void
44{
46 //NameValueMap targetJointVelocities;
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);
145}
146
147void
149{
150 //setJointVelToZero();
151 //removeCondition(targetReachedCondition);
152 //removeTimeoutEvent(timeoutEvent);
153}
154
155void
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
181void
183{
184 // set vel to zero
185 ARMARX_DEBUG << "setting joint velocity to zero " << flush;
186
187 std::string kinChainName = getInput<std::string>("kinematicChain");
189
190 if (!context)
191 {
192 ARMARX_WARNING << "Need a RobotStatechartContext" << flush;
194 }
195
196 if (!context->robotStateComponent)
197 {
198 ARMARX_WARNING << "No RobotStatechartContext->robotStateComponent" << flush;
200 }
201
203
204 if (!robot->hasRobotNodeSet(kinChainName))
205 {
206 ARMARX_WARNING << "Robot does not know kinematic chain with name " << kinChainName << flush;
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
224std::string
226{
227 return "MoveJointsVelControl";
228}
229
230// DO NOT EDIT NEXT FUNCTION
void onBreak() override
Virtual function, in which the behaviour of state is defined, when it is abnormally exited....
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
MoveJointsVelControl(XMLStateConstructorParams stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Mimics the behaviour of the VirtualRobot::Robot class while redirecting everything to an Ice proxy.
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
RobotStateComponentInterfacePrx robotStateComponent
Prx for the RobotState.
KinematicUnitInterfacePrx kinematicUnitPrx
ContextType * getContext() const
Definition StateBase.h:71
void sendEvent(const EventPtr event, StateBasePtr eventProcessor=nullptr)
Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere...
Definition StateUtil.cpp:40
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition State.h:620
bool isInputParameterSet(const std::string &key) const
Checks whether a given input parameter is set or not.
Definition State.cpp:536
XMLStateTemplate(const XMLStateConstructorParams &params)
Definition XMLState.h:149
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< SingleTypeVariantList > SingleTypeVariantListPtr
const LogSender::manipulator flush
Definition LogSender.h:251
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64