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