CheckObjectInHand.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::HapticsGroup
19  * @author [Author Name] ( [Author Email] )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "CheckObjectInHand.h"
26 
28 
29 using namespace armarx;
30 using namespace HapticsGroup;
31 
32 // DO NOT EDIT NEXT LINE
34 
35 
36 
38  XMLStateTemplate < CheckObjectInHand > (stateData)
39 {
40 }
41 
43 {
44  ARMARX_VERBOSE << "Entering CheckObjectInHand::onEnter()";
45 
46  RobotStatechartContext* rsContext = getContext<RobotStatechartContext>();
47  SingleTypeVariantListPtr jointNames = getInput<SingleTypeVariantList>("jointNames");
48  SingleTypeVariantListPtr jointAngles = getInput<SingleTypeVariantList>("jointAngles");
49  SingleTypeVariantListPtr jointTorques = getInput<SingleTypeVariantList>("jointTorques");
50 
51  ChannelRefPtr tempChannelRef = rsContext->getChannelRef(rsContext->getKinematicUnitObserverName(), "jointtorques");
52  //IF TACTILE IMPLMENTED
53  //ChannelRefPtr tactileChannelRef = context->getChannelRef(context->getHapticUnitObserverName(), "tactileSensors");
54 
55 
57 
58  for (size_t i = 0; i < jointNames->getSize(); i++)
59  {
60  dataFields.addVariant(rsContext->getDatafieldRef(tempChannelRef, jointNames->getVariant(i)->getString()));
61  }
62 
63  NameValueMap jointNamesAndAngles;
64  NameValueMap jointNamesAndTorques;
65  NameControlModeMap controlModes;
66  SingleTypeVariantList currentJointAngles;
67  SingleTypeVariantList currentJointNames;
68 
69  if (jointNames->getSize() == jointAngles->getSize())
70  {
71  for (int j = 0; j < jointNames->getSize(); j++)
72  {
73  jointNamesAndAngles[jointNames->getVariant(j)->getString()] = jointAngles->getVariant(j)->getFloat();
74  jointNamesAndTorques[jointNames->getVariant(j)->getString()] = dataFields.getVariant(j)->getFloat() * 1.1;
75  controlModes[jointNames->getVariant(j)->getString()] = eTorqueControl;
76  currentJointAngles.addVariant(jointAngles->getVariant(j)->getFloat());
77  currentJointNames.addVariant(jointNames->getVariant(j)->getString());
78  }
79  }
80  else
81  {
82  throw LocalException("stateCheckObjectInHand: List lengths do not match!");
83  }
84 
85 
86  rsContext->kinematicUnitPrx->switchControlMode(controlModes);
87  rsContext->kinematicUnitPrx->setJointTorques(jointNamesAndTorques);
88 
89  ARMARX_LOG << eINFO << "Installing timeoutGrasp condition";
90  float timeOutCheckObjectInHand = getInput<float>("timeoOutCheckObjectInHand");
91  condSqueezeWithJointTorquesTimeout = setTimeoutEvent(timeOutCheckObjectInHand, createEvent<EvCheckObjectInHandTimeout>());
92  setLocal("TempJointAngles", currentJointAngles);
93  setLocal("TempJointNames", currentJointNames);
94  ARMARX_VERBOSE << "Done CheckObjectInHand::onEnter()";
95 }
96 
98 {
99  while (!isRunningTaskStopped()) // stop run function if returning true
100  {
101  SingleTypeVariantListPtr jointAngles = getLocal<SingleTypeVariantList>("jointNamesAndAngles");
102  SingleTypeVariantListPtr jointNames = getLocal<SingleTypeVariantList>("TempJointNames");
103  RobotStatechartContext* rsContext = getContext<RobotStatechartContext>();
104  ChannelRefPtr tempChannelRef = rsContext->getChannelRef(rsContext->getKinematicUnitObserverName(), "jointangles");
106  float threshhold = 0.0;
107 
108  for (size_t i = 0; i < jointNames->getSize(); i++)
109  {
110  dataFields.addVariant(rsContext->getDatafieldRef(tempChannelRef, jointNames->getVariant(i)->getString()));
111  }
112 
113  float meanSquaredJointAnglesDiff = 0.0;
114 
115  for (int j = 0; j < jointNames->getSize(); j++)
116  {
117  float jointAngleDiff;// = (jointNamesAndAngles[jointNames->getVariant(j)->getString()]-dataFields.getVariant(j)->getFloat());
118  meanSquaredJointAnglesDiff += jointAngleDiff * jointAngleDiff;
119  }
120 
121  meanSquaredJointAnglesDiff = sqrt(meanSquaredJointAnglesDiff);
122 
123  if (meanSquaredJointAnglesDiff < threshhold)
124  {
125  return;
126  }
127  }
128 
129 }
130 
132 {
133  // put your user code for the breaking point here
134  // execution time should be short (<100ms)
135 }
136 
138 {
140  // MoveBack
141 
142 }
143 
144 // DO NOT EDIT NEXT FUNCTION
146 {
147  return "CheckObjectInHand";
148 }
149 
150 // DO NOT EDIT NEXT FUNCTION
152 {
153  return XMLStateFactoryBasePtr(new CheckObjectInHand(stateData));
154 }
155 
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::HapticsGroup::CheckObjectInHand::CheckObjectInHand
CheckObjectInHand(XMLStateConstructorParams stateData)
Definition: CheckObjectInHand.cpp:37
CheckObjectInHand.h
armarx::RobotStatechartContext::getKinematicUnitObserverName
std::string getKinematicUnitObserverName()
Definition: RobotStatechartContext.h:88
armarx::HapticsGroup::CheckObjectInHand::onBreak
void onBreak() override
Virtual function, in which the behaviour of state is defined, when it is abnormally exited....
Definition: CheckObjectInHand.cpp:131
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::SingleTypeVariantList::getVariant
VariantPtr getVariant(int index) const
Definition: SingleTypeVariantList.cpp:154
armarx::HapticsGroup::CheckObjectInHand::Registry
static SubClassRegistry Registry
Definition: CheckObjectInHand.h:50
armarx::HapticsGroup::CheckObjectInHand::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CheckObjectInHand.cpp:151
armarx::navigation::platform_controller::platform_global_trajectory::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformGlobalTrajectoryController.h:93
armarx::HapticsGroup::CheckObjectInHand::run
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
Definition: CheckObjectInHand.cpp:97
armarx::State::setLocal
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition: State.cpp:417
armarx::StatechartContext::getChannelRef
ChannelRefPtr getChannelRef(const std::string &observerName, const std::string &channelName) override
Definition: StatechartContext.cpp:142
armarx::HapticsGroup::CheckObjectInHand
Definition: CheckObjectInHand.h:34
IceInternal::Handle< SingleTypeVariantList >
DatafieldRef.h
armarx::RobotStatechartContext
Definition: RobotStatechartContext.h:66
armarx::StateUtility::setTimeoutEvent
ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs, const EventPtr &evt)
Utility function to start a timer on the systemObserver and register an event on the conditionHandler...
Definition: StateUtil.cpp:144
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::StateController::isRunningTaskStopped
bool isRunningTaskStopped() const
isRunningTaskStopped checks whether the RunningTask, that executes run() is requested to stop.
Definition: StateController.cpp:1091
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:47
armarx::StatechartContext::getDatafieldRef
DatafieldRefPtr getDatafieldRef(const DataFieldIdentifier &datafieldIdentifier) override
Definition: StatechartContext.cpp:169
armarx::HapticsGroup::CheckObjectInHand::GetName
static std::string GetName()
Definition: CheckObjectInHand.cpp:145
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
armarx::HapticsGroup::CheckObjectInHand::onExit
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
Definition: CheckObjectInHand.cpp:137
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:169
armarx::HapticsGroup::CheckObjectInHand::condSqueezeWithJointTorquesTimeout
ActionEventIdentifier condSqueezeWithJointTorquesTimeout
Definition: CheckObjectInHand.h:56
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::channels::KinematicUnitObserver::jointTorques
const KinematicUnitDatafieldCreator jointTorques("jointTorques")
armarx::HapticsGroup::CheckObjectInHand::onEnter
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
Definition: CheckObjectInHand.cpp:42
armarx::StateUtility::removeTimeoutEvent
void removeTimeoutEvent(const ActionEventIdentifier &id)
Definition: StateUtil.cpp:178
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
armarx::SingleTypeVariantList::addVariant
void addVariant(const Variant &variant)
Definition: SingleTypeVariantList.cpp:100