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 "DetectForceSpike.h"
24
25#include <thread>
26
28
29using namespace armarx;
30using namespace ForceTorqueUtility;
31
32// DO NOT EDIT NEXT LINE
33DetectForceSpike::SubClassRegistry DetectForceSpike::Registry(DetectForceSpike::GetName(),
35
36void
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(
47 getForceTorqueObserver()->getForceDatafield(in.getFTDatafieldName()));
48 const Eigen::Vector3f weights = in.getForceWeights()->toEigen();
49 const Eigen::Vector3f axis = in.getAxis()->toEigen().normalized();
50 auto getForceAlongAxis = [&]() -> float
51 {
52 return forceDf->getDataField()
53 ->get<FramedDirection>()
54 ->toEigen()
55 .cwiseProduct(weights)
56 .transpose() *
57 axis;
58 };
59
60 in.getTriggerInAxisDirection();
61 in.getTriggerCounterAxisDirection();
62 std::deque<float> spikes(in.getWindowSizeMs() / 10, getForceAlongAxis());
63
64 while (!isRunningTaskStopped()) // stop run function if returning true
65 {
66 spikes.push_back(getForceAlongAxis());
67 spikes.pop_front();
68
69 float refValue = spikes.at(0);
70 bool low = true;
71 bool risingEdgeDetected = false;
72 bool fallingEdgeDetected = false;
73
74 bool f2rDetected = false;
75 bool r2fDetected = false;
76
77 for (const float spike : spikes)
78 {
79 if (low)
80 {
81 if (spike < refValue)
82 {
83 refValue = spike;
84 }
85 else if (spike > refValue + forceThreshold)
86 {
87 low = false;
88 risingEdgeDetected = true;
89 f2rDetected |= fallingEdgeDetected;
90 }
91 }
92
93 if (!low)
94 {
95 if (spike > refValue)
96 {
97 refValue = spike;
98 }
99 else if (spike < refValue - forceThreshold)
100 {
101 low = true;
102 fallingEdgeDetected = true;
103 r2fDetected |= risingEdgeDetected;
104 }
105 }
106 }
107 if ((in.getTriggerInAxisDirection() && r2fDetected) ||
108 (in.getTriggerCounterAxisDirection() && f2rDetected))
109 {
110 emitSuccess();
111 return;
112 }
113
114 std::this_thread::sleep_for(std::chrono::milliseconds{10});
115 }
116 emitFailure();
117}
118
119// DO NOT EDIT NEXT FUNCTION
DetectForceSpike(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
FramedDirection is a 3 dimensional direction vector with a reference frame.
Definition FramedPose.h:87
#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...
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< DatafieldRef > DatafieldRefPtr
Definition Observer.h:43
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)