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
33 CheckObjectInHand::SubClassRegistry CheckObjectInHand::Registry(CheckObjectInHand::GetName(),
35 
38 {
39 }
40 
41 void
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 =
52  rsContext->getChannelRef(rsContext->getKinematicUnitObserverName(), "jointtorques");
53  //IF TACTILE IMPLMENTED
54  //ChannelRefPtr tactileChannelRef = context->getChannelRef(context->getHapticUnitObserverName(), "tactileSensors");
55 
56 
58 
59  for (size_t i = 0; i < jointNames->getSize(); i++)
60  {
61  dataFields.addVariant(
62  rsContext->getDatafieldRef(tempChannelRef, jointNames->getVariant(i)->getString()));
63  }
64 
65  NameValueMap jointNamesAndAngles;
66  NameValueMap jointNamesAndTorques;
67  NameControlModeMap controlModes;
68  SingleTypeVariantList currentJointAngles;
69  SingleTypeVariantList currentJointNames;
70 
71  if (jointNames->getSize() == jointAngles->getSize())
72  {
73  for (int j = 0; j < jointNames->getSize(); j++)
74  {
75  jointNamesAndAngles[jointNames->getVariant(j)->getString()] =
76  jointAngles->getVariant(j)->getFloat();
77  jointNamesAndTorques[jointNames->getVariant(j)->getString()] =
78  dataFields.getVariant(j)->getFloat() * 1.1;
79  controlModes[jointNames->getVariant(j)->getString()] = eTorqueControl;
80  currentJointAngles.addVariant(jointAngles->getVariant(j)->getFloat());
81  currentJointNames.addVariant(jointNames->getVariant(j)->getString());
82  }
83  }
84  else
85  {
86  throw LocalException("stateCheckObjectInHand: List lengths do not match!");
87  }
88 
89 
90  rsContext->kinematicUnitPrx->switchControlMode(controlModes);
91  rsContext->kinematicUnitPrx->setJointTorques(jointNamesAndTorques);
92 
93  ARMARX_LOG << eINFO << "Installing timeoutGrasp condition";
94  float timeOutCheckObjectInHand = getInput<float>("timeoOutCheckObjectInHand");
96  setTimeoutEvent(timeOutCheckObjectInHand, createEvent<EvCheckObjectInHandTimeout>());
97  setLocal("TempJointAngles", currentJointAngles);
98  setLocal("TempJointNames", currentJointNames);
99  ARMARX_VERBOSE << "Done CheckObjectInHand::onEnter()";
100 }
101 
102 void
104 {
105  while (!isRunningTaskStopped()) // stop run function if returning true
106  {
108  getLocal<SingleTypeVariantList>("jointNamesAndAngles");
109  SingleTypeVariantListPtr jointNames = getLocal<SingleTypeVariantList>("TempJointNames");
110  RobotStatechartContext* rsContext = getContext<RobotStatechartContext>();
111  ChannelRefPtr tempChannelRef =
112  rsContext->getChannelRef(rsContext->getKinematicUnitObserverName(), "jointangles");
114  float threshhold = 0.0;
115 
116  for (size_t i = 0; i < jointNames->getSize(); i++)
117  {
118  dataFields.addVariant(
119  rsContext->getDatafieldRef(tempChannelRef, jointNames->getVariant(i)->getString()));
120  }
121 
122  float meanSquaredJointAnglesDiff = 0.0;
123 
124  for (int j = 0; j < jointNames->getSize(); j++)
125  {
126  float
127  jointAngleDiff; // = (jointNamesAndAngles[jointNames->getVariant(j)->getString()]-dataFields.getVariant(j)->getFloat());
128  meanSquaredJointAnglesDiff += jointAngleDiff * jointAngleDiff;
129  }
130 
131  meanSquaredJointAnglesDiff = sqrt(meanSquaredJointAnglesDiff);
132 
133  if (meanSquaredJointAnglesDiff < threshhold)
134  {
135  return;
136  }
137  }
138 }
139 
140 void
142 {
143  // put your user code for the breaking point here
144  // execution time should be short (<100ms)
145 }
146 
147 void
149 {
151  // MoveBack
152 }
153 
154 // DO NOT EDIT NEXT FUNCTION
155 std::string
157 {
158  return "CheckObjectInHand";
159 }
160 
161 // DO NOT EDIT NEXT FUNCTION
164 {
165  return XMLStateFactoryBasePtr(new CheckObjectInHand(stateData));
166 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::HapticsGroup::CheckObjectInHand::CheckObjectInHand
CheckObjectInHand(XMLStateConstructorParams stateData)
Definition: CheckObjectInHand.cpp:36
CheckObjectInHand.h
armarx::RobotStatechartContext::getKinematicUnitObserverName
std::string getKinematicUnitObserverName()
Definition: RobotStatechartContext.h:103
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:141
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::SingleTypeVariantList::getVariant
VariantPtr getVariant(int index) const
Definition: SingleTypeVariantList.cpp:166
armarx::HapticsGroup::CheckObjectInHand::Registry
static SubClassRegistry Registry
Definition: CheckObjectInHand.h:52
armarx::HapticsGroup::CheckObjectInHand::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CheckObjectInHand.cpp:163
armarx::HapticsGroup::CheckObjectInHand::run
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
Definition: CheckObjectInHand.cpp:103
armarx::State::setLocal
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition: State.cpp:464
armarx::StatechartContext::getChannelRef
ChannelRefPtr getChannelRef(const std::string &observerName, const std::string &channelName) override
Definition: StatechartContext.cpp:152
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::HapticsGroup::CheckObjectInHand
Definition: CheckObjectInHand.h:36
IceInternal::Handle< SingleTypeVariantList >
DatafieldRef.h
armarx::RobotStatechartContext
Definition: RobotStatechartContext.h:79
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:162
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::StateController::isRunningTaskStopped
bool isRunningTaskStopped() const
isRunningTaskStopped checks whether the RunningTask, that executes run() is requested to stop.
Definition: StateController.cpp:1179
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:46
armarx::StatechartContext::getDatafieldRef
DatafieldRefPtr getDatafieldRef(const DataFieldIdentifier &datafieldIdentifier) override
Definition: StatechartContext.cpp:181
armarx::HapticsGroup::CheckObjectInHand::GetName
static std::string GetName()
Definition: CheckObjectInHand.cpp:156
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:165
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:148
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:197
armarx::HapticsGroup::CheckObjectInHand::condSqueezeWithJointTorquesTimeout
ActionEventIdentifier condSqueezeWithJointTorquesTimeout
Definition: CheckObjectInHand.h:58
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:88
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
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:203
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
armarx::SingleTypeVariantList::addVariant
void addVariant(const Variant &variant)
Definition: SingleTypeVariantList.cpp:106