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 
29 #include <RobotSkillTemplates/statecharts/ForceControlGroup/ForceControlGroupStatechartContext.generated.h>
30 
31 using namespace armarx;
32 using namespace ForceControlGroup;
33 
34 // DO NOT EDIT NEXT LINE
35 WaitForForce::SubClassRegistry WaitForForce::Registry(WaitForForce::GetName(),
37 
39  XMLStateTemplate<WaitForForce>(stateData), WaitForForceGeneratedBase<WaitForForce>(stateData)
40 {
41 }
42 
43 void
45 {
46  // put your user code for the enter-point here
47  // execution time should be short (<100ms)
48 
49  TimeUtil::MSSleep(in.getDelay());
50 
51  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
52  DatafieldRefBasePtr rawforce;
53  DatafieldRefBasePtr rawtorque;
54  DatafieldRefBasePtr force;
55  DatafieldRefBasePtr torque;
56  if (in.isObserverSensorChannelNameSet())
57  {
58  rawforce = c->getForceTorqueObserver()->getDataFieldRef(
59  new DataFieldIdentifier(c->getForceTorqueObserver()->getObserverName(),
60  in.getObserverSensorChannelName(),
61  "forces"));
62  rawtorque = c->getForceTorqueObserver()->getDataFieldRef(
63  new DataFieldIdentifier(c->getForceTorqueObserver()->getObserverName(),
64  in.getObserverSensorChannelName(),
65  "torques"));
66  }
67  else if (in.isSensorNameSet())
68  {
69  std::string sensor = in.getSensorName();
70 
71  rawforce = c->getForceTorqueObserver()->getForceDatafield(sensor);
72  rawtorque = c->getForceTorqueObserver()->getTorqueDatafield(sensor);
73  }
74 
75  else
76  {
77  throw LocalException() << "Either SensorName or ObserverSensorChannelName must be set!";
78  }
79  force = c->getForceTorqueObserver()->createNulledDatafield(rawforce);
80  torque = c->getForceTorqueObserver()->createNulledDatafield(rawtorque);
81 
82  local.setforceRef(DatafieldRefPtr::dynamicCast(force));
83  local.settorqueRef(DatafieldRefPtr::dynamicCast(torque));
84  ARMARX_IMPORTANT << "Installing force torque condition: force threshold: "
85  << in.getForceMagnitudeThreshold()
86  << " torque threshold: " << in.getTorqueMagnitudeThreshold();
87  Literal literalForce(
88  force, "magnitudelarger", Literal::createParameterList(in.getForceMagnitudeThreshold()));
89  Literal literalTorque(
90  torque, "magnitudelarger", Literal::createParameterList(in.getTorqueMagnitudeThreshold()));
91  installConditionForForceTorqueThresholdReached(literalForce || literalTorque);
92 }
93 
94 void
96 {
97  while (!isRunningTaskStopped())
98  {
99  ARMARX_INFO << deactivateSpam(0.5) << "Current force value: "
100  << local.getforceRef()->getDataField()->get<FramedDirection>()->toEigen().norm()
101  << " threshold: " << in.getForceMagnitudeThreshold();
102  TimeUtil::MSSleep(100);
103  }
104 }
105 
106 void
108 {
109  // put your user code for the exit point here
110  // execution time should be short (<100ms)
111  ForceControlGroupStatechartContext* c = getContext<ForceControlGroupStatechartContext>();
112  ARMARX_INFO << deactivateSpam(0.5) << "Final force value: "
113  << local.getforceRef()->getDataField()->get<FramedDirection>()->toEigen().norm()
114  << " threshold: " << in.getForceMagnitudeThreshold();
115 
116  c->getForceTorqueObserver()->removeFilteredDatafield(local.getforceRef());
117  c->getForceTorqueObserver()->removeFilteredDatafield(local.gettorqueRef());
118 }
119 
120 // DO NOT EDIT NEXT FUNCTION
123 {
124  return XMLStateFactoryBasePtr(new WaitForForce(stateData));
125 }
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::ForceControlGroup::WaitForForce
Definition: WaitForForce.h:31
armarx::ForceControlGroup::WaitForForce::Registry
static SubClassRegistry Registry
Definition: WaitForForce.h:43
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:100
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:142
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::ForceControlGroup::WaitForForce::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: WaitForForce.cpp:122
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle
Definition: forward_declarations.h:8
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::ForceControlGroup::WaitForForce::onEnter
void onEnter() override
Definition: WaitForForce.cpp:44
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::toEigen
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
Definition: PointToModelICP.h:67
WaitForForce.h
armarx::ForceControlGroup::WaitForForce::run
void run() override
Definition: WaitForForce.cpp:95
TimeUtil.h
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:86
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
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:27
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:107