CheckForcesChanged.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::CheckForcesChangedGroup
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 "CheckForcesChanged.h"
26 
28 
29 
30 using namespace armarx;
31 using namespace CoupledInteractionGroup;
32 
33 // DO NOT EDIT NEXT LINE
34 CheckForcesChanged::SubClassRegistry
37 
40  CheckForcesChangedGeneratedBase<CheckForcesChanged>(stateData)
41 {
42 }
43 
44 bool
46  FramedDirectionPtr refForces,
47  FramedDirectionPtr refTorques,
48  float forceThreshhold,
49  float torqueThreshhold)
50 {
52  getContext<CoupledInteractionGroupStatechartContext>();
53 
54  DatafieldRefPtr force = DatafieldRefPtr::dynamicCast(
55  context->getForceTorqueUnitObserver()->getForceDatafield(tcpName));
56  DatafieldRefPtr torque = DatafieldRefPtr::dynamicCast(
57  context->getForceTorqueUnitObserver()->getTorqueDatafield(tcpName));
58 
59  FramedDirectionPtr curForce = force->getDataField()->get<FramedDirection>();
60  curForce = FramedDirection::ChangeFrame(context->getRobot(), *curForce, "Armar3_Base");
61  FramedDirectionPtr curTorque = torque->getDataField()->get<FramedDirection>();
62  curTorque = FramedDirection::ChangeFrame(context->getRobot(), *curTorque, "Armar3_Base");
63 
64  float deviationForce = (curForce->toEigen() - refForces->toEigen()).norm();
65  float deviationTorque = (curTorque->toEigen() - refTorques->toEigen()).norm();
66 
67  if (deviationForce > forceThreshhold && deviationTorque > torqueThreshhold)
68  {
69  return true;
70  }
71 
72  return false;
73 }
74 
75 void
77 {
79  getContext<CoupledInteractionGroupStatechartContext>();
80 
81  //std::string leftTcpName("Wrist 2 L");// = in.getLeftHandName();
82  //std::string rightTcpName ("Wrist 2 R");//= in.getRightHandName();
83  std::string leftTcpName = in.getLeftHandName();
84  std::string rightTcpName = in.getRightHandName();
85  //leftTcpName = std::string("Wrist 2 L");
86  //rightTcpName = std::string("Wrist 2 R");
87  FramedDirectionPtr leftRefForce = in.getReferenceForcesLeftHand();
88  FramedDirectionPtr leftRefTorque = in.getReferenceTorquesLeftHand();
89  FramedDirectionPtr rightRefForce = in.getReferenceForcesRightHand();
90  FramedDirectionPtr rightRefTorque = in.getReferenceTorquesRightHand();
91  FramedPositionPtr rightInitPos = in.getInitialRightHandPosition();
92  FramedPositionPtr leftInitPos = in.getInitialLeftHandPosition();
93  float maxDisplacement = in.getMaxDisplacement();
94  bool maxDisplacementReached = false;
95  Eigen::Matrix4f leftTcpPoseBase =
96  context->getRobot()->getRobotNode(in.getLeftHandName())->getPoseInRootFrame();
97  Eigen::Matrix4f rightTcpPoseBase =
98  context->getRobot()->getRobotNode(in.getRightHandName())->getPoseInRootFrame();
99  float leftDisplacement =
100  (leftTcpPoseBase(0, 3) - leftInitPos->x) * (leftTcpPoseBase(0, 3) - leftInitPos->x);
101  leftDisplacement +=
102  (leftTcpPoseBase(1, 3) - leftInitPos->y) * (leftTcpPoseBase(1, 3) - leftInitPos->y);
103  leftDisplacement +=
104  (leftTcpPoseBase(2, 3) - leftInitPos->z) * (leftTcpPoseBase(2, 3) - leftInitPos->z);
105  leftDisplacement = fabs(leftTcpPoseBase(2, 3) - leftInitPos->z);
106  float rightDisplacement =
107  (rightTcpPoseBase(0, 3) - rightInitPos->x) * (rightTcpPoseBase(0, 3) - rightInitPos->x);
108  rightDisplacement +=
109  (rightTcpPoseBase(1, 3) - rightInitPos->y) * (rightTcpPoseBase(1, 3) - rightInitPos->y);
110  rightDisplacement +=
111  (rightTcpPoseBase(2, 3) - rightInitPos->z) * (rightTcpPoseBase(2, 3) - rightInitPos->z);
112  rightDisplacement = fabs(rightTcpPoseBase(2, 3) - rightInitPos->z);
113 
114  if (leftDisplacement > maxDisplacement || rightDisplacement > maxDisplacement)
115  {
116  maxDisplacementReached = true;
117  }
118 
119  ARMARX_INFO << "Place Table displacements " << leftDisplacement << " " << maxDisplacement << " "
120  << rightDisplacement << " " << maxDisplacement;
121  float minForceThreshold = in.getMinForceThreshhold();
122  float velocityInmms = in.getVelocityInMillimetersSecond();
123 
124  if (maxDisplacementReached)
125  {
126  out.setRightHandStopped(in.getRightHandStopped());
127  out.setLeftHandStopped(in.getLeftHandStopped());
128  out.setLeftHandChecked(true);
129  out.setRightHandChecked(true);
130  Eigen::Vector3f handPositionVelocity;
131  handPositionVelocity << 0, 0, 0;
132  Eigen::Vector3f handOrientationVelocity;
133  handOrientationVelocity << 0, 0, 0;
134 
135  out.setNewHandPositionVelocity(
136  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
137  out.setNewHandOrientationVelocity(
138  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
139  emitEvNotChecked();
140  ARMARX_INFO << "Max Displacement reached";
141  }
142  else
143  {
144  if (in.getLeftHandChecked())
145  {
146  ARMARX_INFO << "Checked Forces in Left Hand";
147  }
148 
149  if (in.getRightHandChecked())
150  {
151  ARMARX_INFO << "Checked Forces in Right Hand";
152  }
153 
154  if (in.getLeftHandStopped())
155  {
156  ARMARX_INFO << "Stopped Left Hand";
157  }
158 
159  if (in.getLeftHandStopped())
160  {
161  ARMARX_INFO << "Stopped Right Hand";
162  }
163 
164  if (!in.getLeftHandStopped() && !in.getLeftHandChecked())
165  {
166  out.setRightHandStopped(in.getRightHandStopped());
167 
168  if (in.getRightHandStopped())
169  {
170  out.setLeftHandChecked(false);
171  }
172  else
173  {
174  out.setLeftHandChecked(true);
175  }
176 
177  out.setRightHandChecked(false);
178 
180  leftTcpName, leftRefForce, leftRefTorque, minForceThreshold, 0.0))
181  {
182  Eigen::Vector3f handPositionVelocity;
183  handPositionVelocity << 0, 0, 0;
184  Eigen::Vector3f handOrientationVelocity;
185  handOrientationVelocity << 0, 0, 0;
186 
187  out.setNewHandPositionVelocity(
188  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
189  out.setNewHandOrientationVelocity(
190  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
191  out.setLeftHandStopped(true);
192 
193  emitEvForcesLeftHandChanged();
194  }
195  else
196  {
197  Eigen::Vector3f handPositionVelocity;
198  handPositionVelocity << 0, 0, -velocityInmms;
199  Eigen::Vector3f handOrientationVelocity;
200  handOrientationVelocity << 0, 0, 0;
201  out.setNewHandPositionVelocity(
202  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
203  out.setNewHandOrientationVelocity(
204  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
205  out.setLeftHandStopped(in.getLeftHandStopped());
206  emitEvForcesLeftHandNotChanged();
207  }
208  }
209  else if (!in.getRightHandStopped() && !in.getRightHandChecked())
210  {
211  out.setLeftHandStopped(in.getLeftHandStopped());
212 
213  if (in.getLeftHandStopped())
214  {
215  out.setRightHandChecked(false);
216  }
217  else
218  {
219  out.setRightHandChecked(true);
220  }
221 
222  out.setLeftHandChecked(false);
223 
225  rightTcpName, rightRefForce, rightRefTorque, minForceThreshold, 0.0))
226  {
227  Eigen::Vector3f handPositionVelocity;
228  handPositionVelocity << 0, 0, 0;
229  Eigen::Vector3f handOrientationVelocity;
230  handOrientationVelocity << 0, 0, 0;
231 
232  out.setNewHandPositionVelocity(
233  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
234  out.setNewHandOrientationVelocity(
235  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
236  out.setRightHandStopped(true);
237  emitEvForcesRightHandChanged();
238  }
239  else
240  {
241  Eigen::Vector3f handPositionVelocity;
242  handPositionVelocity << 0, 0, -velocityInmms;
243  Eigen::Vector3f handOrientationVelocity;
244  handOrientationVelocity << 0, 0, 0;
245  out.setNewHandPositionVelocity(
246  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
247  out.setNewHandOrientationVelocity(
248  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
249  out.setRightHandStopped(in.getRightHandStopped());
250  emitEvForcesRightHandNotChanged();
251  }
252  }
253  else if ((in.getRightHandStopped() && in.getLeftHandStopped()))
254  {
255  out.setRightHandStopped(in.getRightHandStopped());
256  out.setLeftHandStopped(in.getLeftHandStopped());
257  out.setLeftHandChecked(true);
258  out.setRightHandChecked(true);
259  Eigen::Vector3f handPositionVelocity;
260  handPositionVelocity << 0, 0, 0;
261  Eigen::Vector3f handOrientationVelocity;
262  handOrientationVelocity << 0, 0, 0;
263 
264  out.setNewHandPositionVelocity(
265  FramedDirection(handPositionVelocity, "Armar3_Base", "Armar3"));
266  out.setNewHandOrientationVelocity(
267  FramedDirection(handOrientationVelocity, "Armar3_Base", "Armar3"));
268  emitEvNotChecked();
269  ARMARX_INFO << "Left and Right Hand stopped";
270  }
271  }
272 }
273 
274 void
276 {
277  // put your user code for the execution-phase here
278  // runs in seperate thread, thus can do complex operations
279  // should check constantly whether isRunningTaskStopped() returns true
280 }
281 
282 void
284 {
285  // put your user code for the breaking point here
286  // execution time should be short (<100ms)
287 }
288 
289 void
291 {
292 
293  // put your user code for the exit point here
294  // execution time should be short (<100ms)
295 }
296 
297 // DO NOT EDIT NEXT FUNCTION
298 std::string
300 {
301  return "CheckForcesChanged";
302 }
303 
304 // DO NOT EDIT NEXT FUNCTION
307 {
308  return XMLStateFactoryBasePtr(new CheckForcesChanged(stateData));
309 }
RemoteRobot.h
armarx::CoupledInteractionGroup::CheckForcesChanged::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CheckForcesChanged.cpp:306
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::CoupledInteractionGroup::CoupledInteractionGroupStatechartContext::getForceTorqueUnitObserver
ForceTorqueUnitObserverInterfacePrx getForceTorqueUnitObserver()
Definition: CoupledInteractionGroupStatechartContext.h:221
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
CheckForcesChanged.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< FramedDirection >
armarx::CoupledInteractionGroup::CheckForcesChanged::GetName
static std::string GetName()
Definition: CheckForcesChanged.cpp:299
armarx::CoupledInteractionGroup::CoupledInteractionGroupStatechartContext::getRobot
const VirtualRobot::RobotPtr getRobot()
Definition: CoupledInteractionGroupStatechartContext.h:133
armarx::CoupledInteractionGroup::CheckForcesChanged::onBreak
void onBreak() override
Definition: CheckForcesChanged.cpp:283
armarx::FramedDirection::ChangeFrame
static FramedDirectionPtr ChangeFrame(const VirtualRobot::RobotPtr &robot, const FramedDirection &framedVec, const std::string &newFrame)
Definition: FramedPose.cpp:91
armarx::CoupledInteractionGroup::CheckForcesChanged::checkForceTorqueExceeded
bool checkForceTorqueExceeded(std::string tcpName, FramedDirectionPtr refForces, FramedDirectionPtr refTorques, float forceThreshhold, float torqueThreshhold)
Definition: CheckForcesChanged.cpp:45
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::CoupledInteractionGroup::CheckForcesChanged
Definition: CheckForcesChanged.h:37
armarx::CoupledInteractionGroup::CheckForcesChanged::onExit
void onExit() override
Definition: CheckForcesChanged.cpp:290
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::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
armarx::Vector3::toEigen
virtual Eigen::Vector3f toEigen() const
Definition: Pose.cpp:134
armarx::CoupledInteractionGroup::CheckForcesChanged::run
void run() override
Definition: CheckForcesChanged.cpp:275
armarx::CoupledInteractionGroup::CoupledInteractionGroupStatechartContext
Definition: CoupledInteractionGroupStatechartContext.h:118
armarx::CoupledInteractionGroup::CheckForcesChanged::Registry
static SubClassRegistry Registry
Definition: CheckForcesChanged.h:55
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::CoupledInteractionGroup::CheckForcesChanged::CheckForcesChanged
CheckForcesChanged(XMLStateConstructorParams stateData)
Definition: CheckForcesChanged.cpp:38
armarx::CoupledInteractionGroup::CheckForcesChanged::onEnter
void onEnter() override
Definition: CheckForcesChanged.cpp:76
norm
double norm(const Point &a)
Definition: point.hpp:102