WaitForForce.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::ForceControlGroup
19  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "WaitForForce.h"
26 
28 #include <RobotSkillTemplates/statecharts/ForceControlGroup/ForceControlGroupStatechartContext.generated.h>
29 
30 using namespace armarx;
31 using namespace ForceControlGroup;
32 
33 // DO NOT EDIT NEXT LINE
34 WaitForForce::SubClassRegistry WaitForForce::Registry(WaitForForce::GetName(), &WaitForForce::CreateInstance);
35 
36 
37 
39  XMLStateTemplate<WaitForForce>(stateData), WaitForForceGeneratedBase<WaitForForce>(stateData)
40 {
41 }
42 
44 {
45  // put your user code for the enter-point here
46  // execution time should be short (<100ms)
47 
48  TimeUtil::MSSleep(in.getDelay());
49 
50  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
51  DatafieldRefBasePtr rawforce;
52  DatafieldRefBasePtr rawtorque;
53  DatafieldRefBasePtr force;
54  DatafieldRefBasePtr torque;
55  if (in.isObserverSensorChannelNameSet())
56  {
57  rawforce = c->getForceTorqueObserver()->getDataFieldRef(new DataFieldIdentifier(c->getForceTorqueObserver()->getObserverName(),
58  in.getObserverSensorChannelName(),
59  "forces"));
60  rawtorque = c->getForceTorqueObserver()->getDataFieldRef(new DataFieldIdentifier(c->getForceTorqueObserver()->getObserverName(),
61  in.getObserverSensorChannelName(),
62  "torques"));
63  }
64  else if (in.isSensorNameSet())
65  {
66  std::string sensor = in.getSensorName();
67 
68  rawforce = c->getForceTorqueObserver()->getForceDatafield(sensor);
69  rawtorque = c->getForceTorqueObserver()->getTorqueDatafield(sensor);
70  }
71 
72  else
73  {
74  throw LocalException() << "Either SensorName or ObserverSensorChannelName must be set!";
75  }
76  force = c->getForceTorqueObserver()->createNulledDatafield(rawforce);
77  torque = c->getForceTorqueObserver()->createNulledDatafield(rawtorque);
78 
79  local.setforceRef(DatafieldRefPtr::dynamicCast(force));
80  local.settorqueRef(DatafieldRefPtr::dynamicCast(torque));
81  ARMARX_IMPORTANT << "Installing force torque condition: force threshold: " << in.getForceMagnitudeThreshold()
82  << " torque threshold: " << in.getTorqueMagnitudeThreshold();
83  Literal literalForce(force, "magnitudelarger", Literal::createParameterList(in.getForceMagnitudeThreshold()));
84  Literal literalTorque(torque, "magnitudelarger", Literal::createParameterList(in.getTorqueMagnitudeThreshold()));
85  installConditionForForceTorqueThresholdReached(literalForce || literalTorque);
86 
87 }
88 
90 {
91  while (!isRunningTaskStopped())
92  {
93  ARMARX_INFO << deactivateSpam(0.5) << "Current force value: " << local.getforceRef()->getDataField()->get<FramedDirection>()->toEigen().norm() << " threshold: " << in.getForceMagnitudeThreshold();
94  TimeUtil::MSSleep(100);
95  }
96 
97 }
98 
99 
100 
102 {
103  // put your user code for the exit point here
104  // execution time should be short (<100ms)
105  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
106  ARMARX_INFO << deactivateSpam(0.5) << "Final force value: " << local.getforceRef()->getDataField()->get<FramedDirection>()->toEigen().norm() << " threshold: " << in.getForceMagnitudeThreshold();
107 
108  c->getForceTorqueObserver()->removeFilteredDatafield(local.getforceRef());
109  c->getForceTorqueObserver()->removeFilteredDatafield(local.gettorqueRef());
110 }
111 
112 
113 // DO NOT EDIT NEXT FUNCTION
115 {
116  return XMLStateFactoryBasePtr(new WaitForForce(stateData));
117 }
118 
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::ForceControlGroup::WaitForForce
Definition: WaitForForce.h:31
armarx::ForceControlGroup::WaitForForce::Registry
static SubClassRegistry Registry
Definition: WaitForForce.h:44
armarx::TimeUtil::MSSleep
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:94
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:129
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ForceControlGroup::WaitForForce::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: WaitForForce.cpp:114
IceInternal::Handle
Definition: forward_declarations.h:8
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::ForceControlGroup::WaitForForce::onEnter
void onEnter() override
Definition: WaitForForce.cpp:43
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
WaitForForce.h
armarx::ForceControlGroup::WaitForForce::run
void run() override
Definition: WaitForForce.cpp:89
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
TimeUtil.h
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:83
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ForceControlGroup::WaitForForce::WaitForForce
WaitForForce(XMLStateConstructorParams stateData)
Definition: WaitForForce.cpp:38
armarx::Literal
Definition: Term.h:208
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48
armarx::ForceControlGroup::WaitForForce::onExit
void onExit() override
Definition: WaitForForce.cpp:101