DetectForceSpike.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ForceTorqueUtility
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include <thread>
24 
25 #include "DetectForceSpike.h"
26 
28 
29 using namespace armarx;
30 using namespace ForceTorqueUtility;
31 
32 // DO NOT EDIT NEXT LINE
33 DetectForceSpike::SubClassRegistry DetectForceSpike::Registry(DetectForceSpike::GetName(), &DetectForceSpike::CreateInstance);
34 
35 
36 
38 {
39  ARMARX_CHECK_EXPRESSION(in.getTriggerInAxisDirection() || in.getTriggerCounterAxisDirection());
40  ARMARX_CHECK_GREATER_EQUAL(in.getWindowSizeMs(), 10);
41  ARMARX_CHECK_GREATER_EQUAL(in.getWindowSizeMs(), 10);
42 
43  const float forceThreshold = in.getForceThreshold();
44  ARMARX_CHECK_GREATER(forceThreshold, 0);
45 
46  DatafieldRefPtr forceDf = DatafieldRefPtr::dynamicCast(getForceTorqueObserver()->getForceDatafield(in.getFTDatafieldName()));
47  const Eigen::Vector3f weights = in.getForceWeights()->toEigen();
48  const Eigen::Vector3f axis = in.getAxis()->toEigen().normalized();
49  auto getForceAlongAxis = [&]() -> float
50  {
51  return forceDf->getDataField()->get<FramedDirection>()->toEigen().cwiseProduct(weights).transpose() * axis;
52  };
53 
54  in.getTriggerInAxisDirection();
55  in.getTriggerCounterAxisDirection();
56  std::deque<float> spikes(in.getWindowSizeMs() / 10, getForceAlongAxis());
57 
58  while (!isRunningTaskStopped()) // stop run function if returning true
59  {
60  spikes.push_back(getForceAlongAxis());
61  spikes.pop_front();
62 
63  float refValue = spikes.at(0);
64  bool low = true;
65  bool risingEdgeDetected = false;
66  bool fallingEdgeDetected = false;
67 
68  bool f2rDetected = false;
69  bool r2fDetected = false;
70 
71  for (const float spike : spikes)
72  {
73  if (low)
74  {
75  if (spike < refValue)
76  {
77  refValue = spike;
78  }
79  else if (spike > refValue + forceThreshold)
80  {
81  low = false;
82  risingEdgeDetected = true;
83  f2rDetected |= fallingEdgeDetected;
84  }
85  }
86 
87  if (!low)
88  {
89  if (spike > refValue)
90  {
91  refValue = spike;
92  }
93  else if (spike < refValue - forceThreshold)
94  {
95  low = true;
96  fallingEdgeDetected = true;
97  r2fDetected |= risingEdgeDetected;
98  }
99  }
100  }
101  if ((in.getTriggerInAxisDirection() && r2fDetected) || (in.getTriggerCounterAxisDirection() && f2rDetected))
102  {
103  emitSuccess();
104  return;
105  }
106 
107  std::this_thread::sleep_for(std::chrono::milliseconds{10});
108  }
109  emitFailure();
110 }
111 
112 
113 // DO NOT EDIT NEXT FUNCTION
115 {
116  return XMLStateFactoryBasePtr(new DetectForceSpike(stateData));
117 }
118 
armarx::ForceTorqueUtility::DetectForceSpike::Registry
static SubClassRegistry Registry
Definition: DetectForceSpike.h:44
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::ForceTorqueUtility::DetectForceSpike::DetectForceSpike
DetectForceSpike(const XMLStateConstructorParams &stateData)
Definition: DetectForceSpike.h:32
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
ARMARX_CHECK_GREATER
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
Definition: ExpressionException.h:116
armarx::ForceTorqueUtility::DetectForceSpike::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: DetectForceSpike.cpp:114
armarx::ForceTorqueUtility::DetectForceSpike::run
void run() override
Definition: DetectForceSpike.cpp:37
IceInternal::Handle< DatafieldRef >
FramedPose.h
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:83
DetectForceSpike.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28