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
29using namespace armarx;
30using namespace HapticsGroup;
31
32// DO NOT EDIT NEXT LINE
33CheckObjectInHand::SubClassRegistry CheckObjectInHand::Registry(CheckObjectInHand::GetName(),
35
40
41void
43{
44 ARMARX_VERBOSE << "Entering CheckObjectInHand::onEnter()";
45
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");
97 setLocal("TempJointAngles", currentJointAngles);
98 setLocal("TempJointNames", currentJointNames);
99 ARMARX_VERBOSE << "Done CheckObjectInHand::onEnter()";
100}
101
102void
104{
105 while (!isRunningTaskStopped()) // stop run function if returning true
106 {
107 SingleTypeVariantListPtr jointAngles =
108 getLocal<SingleTypeVariantList>("jointNamesAndAngles");
109 SingleTypeVariantListPtr jointNames = getLocal<SingleTypeVariantList>("TempJointNames");
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
140void
142{
143 // put your user code for the breaking point here
144 // execution time should be short (<100ms)
145}
146
147void
153
154// DO NOT EDIT NEXT FUNCTION
155std::string
157{
158 return "CheckObjectInHand";
159}
160
161// DO NOT EDIT NEXT FUNCTION
CheckObjectInHand(XMLStateConstructorParams stateData)
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,...
ActionEventIdentifier condSqueezeWithJointTorquesTimeout
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
void run() override
Virtual function, that can be reimplemented to calculate complex operations.
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
KinematicUnitInterfacePrx kinematicUnitPrx
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
void addVariant(const Variant &variant)
VariantPtr getVariant(int index) const
ContextType * getContext() const
Definition StateBase.h:71
bool isRunningTaskStopped() const
isRunningTaskStopped checks whether the RunningTask, that executes run() is requested to stop.
void removeTimeoutEvent(const ActionEventIdentifier &id)
ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs, const EventPtr &evt)
Utility function to start a timer on the systemObserver and register an event on the conditionHandler...
EventPtr createEvent()
Utility function to create a new Event.
Definition StateUtil.h:63
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
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getLocal(const std::string &key) const
getLocal can be used to access a specific input parameter.
Definition State.h:653
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition State.cpp:464
DatafieldRefPtr getDatafieldRef(const DataFieldIdentifier &datafieldIdentifier) override
ChannelRefPtr getChannelRef(const std::string &observerName, const std::string &channelName) override
XMLStateTemplate(const XMLStateConstructorParams &params)
Definition XMLState.h:149
#define ARMARX_LOG
Definition Logging.h:165
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
const VariantTypeId DatafieldRef
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< SingleTypeVariantList > SingleTypeVariantListPtr
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64