TwoArmVisualServoTowardsTargetPose.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::VisualServoGroup
19  * @author Markus Grotz ( markus dot grotz 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 
26 #include "VisualServoGroupStatechartContext.generated.h"
27 
29 #include <RobotAPI/interface/observers/ObserverFilters.h>
30 
31 #include <IceUtil/UUID.h>
32 
33 using namespace armarx;
34 using namespace VisualServoGroup;
35 
36 // DO NOT EDIT NEXT LINE
37 TwoArmVisualServoTowardsTargetPose::SubClassRegistry TwoArmVisualServoTowardsTargetPose::Registry(TwoArmVisualServoTowardsTargetPose::GetName(), &TwoArmVisualServoTowardsTargetPose::CreateInstance);
38 
39 
40 
42  XMLStateTemplate<TwoArmVisualServoTowardsTargetPose>(stateData), TwoArmVisualServoTowardsTargetPoseGeneratedBase<TwoArmVisualServoTowardsTargetPose>(stateData)
43 {
44 }
45 
47 {
48  // put your user code for the enter-point here
49  // execution time should be short (<100ms)
50 
51  if (in.isStartTimeRefSet())
52  {
53  ARMARX_INFO << "Using existing StartTime for Two Arm Visual Servo.";
54  local.setTimerCreated(false);
55  }
56  else
57  {
58  ARMARX_INFO << "Creating new StartTime for Two Arm Visual Servo.";
59  in.setStartTimeRef(getContext<StatechartContext>()->systemObserverPrx->startTimer("TwoArmVisualServoTimer_" + IceUtil::generateUUID()));
60  local.setTimerCreated(true);
61  }
62 
63  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
64  if (context->getTCPControlUnit()->isRequested())
65  {
66  ARMARX_INFO << "TCPControlUnit is already requested.";
67  local.setTCPControlUnitRequested(false);
68  }
69  else
70  {
71  ARMARX_INFO << "Requesting TCPControlUnit.";
72  context->getTCPControlUnit()->request();
73  local.setTCPControlUnitRequested(true);
74  }
75 
76  std::string leftTcpName = context->getRobot()->getRobotNodeSet(in.getLeftHandKinematicChainName())->getTCP()->getName();
77  local.setLeftTcpName(leftTcpName);
78 
79  std::string rightTcpName = context->getRobot()->getRobotNodeSet(in.getRightHandKinematicChainName())->getTCP()->getName();
80  local.setRightTcpName(rightTcpName);
81 
82  local.setLeftTargetReached(false);
83  local.setRightTargetReached(false);
84 
85  memoryx::ChannelRefBaseSequence instancesLeft = context->getObjectMemoryObserver()->getObjectInstances(in.getLeftHandMemoryChannel());
86  memoryx::ChannelRefBaseSequence instancesRight = context->getObjectMemoryObserver()->getObjectInstances(in.getRightHandMemoryChannel());
87  DataFieldIdentifierPtr leftPosId;
88  DataFieldIdentifierPtr rightPosId;
89 
90  if (instancesLeft.size() == 0)
91  {
92  ARMARX_WARNING << "No instances of the hand in the memory: " << in.getLeftHandMemoryChannel()->get<std::string>("className");
93  auto list = context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getLeftHandMemoryChannel()->get<std::string>("className"));
94 
95  for (auto& entry : list)
96  {
97  ARMARX_INFO << "obj: " << ChannelRefPtr::dynamicCast(entry)->getDataField("className")->getString();
98  }
99  return;
100  }
101  else
102  {
103  leftPosId = ChannelRefPtr::dynamicCast(instancesLeft.front())->getDataFieldIdentifier("position");
104  }
105 
106  if (instancesRight.size() == 0)
107  {
108  ARMARX_WARNING << "No instances of the hand in the memory: " << in.getRightHandMemoryChannel()->get<std::string>("className");
109  auto list = context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getRightHandMemoryChannel()->get<std::string>("className"));
110 
111  for (auto& entry : list)
112  {
113  ARMARX_INFO << "obj: " << ChannelRefPtr::dynamicCast(entry)->getDataField("className")->getString();
114  }
115  return;
116  }
117  else
118  {
119  rightPosId = ChannelRefPtr::dynamicCast(instancesRight.front())->getDataFieldIdentifier("position");
120  }
121 
122  // auto leftPosId = in.getLeftHandMemoryChannel()->getDataFieldIdentifier("position");
123  // auto rightPosId = in.getRightHandMemoryChannel()->getDataFieldIdentifier("position");
124 
125  local.setPoseUpdateTimerChannelRef(ChannelRefPtr::dynamicCast(context->systemObserverPrx->startTimer("objectPoseUpdatedTimer")));
126  auto elapsed = local.getPoseUpdateTimerChannelRef()->getDataFieldIdentifier("elapsedMs");
127  if (in.getWatchObjectPoseUpdates())
128  {
129  Literal left(leftPosId, checks::updated);
130  Literal right(rightPosId, checks::updated);
131  Literal minDelay(elapsed, checks::larger, {100});
132  installConditionForObjectPoseUpdated((left || right) && minDelay);
133  }
134 
135  if (in.isRightHandForceFieldRefSet() && in.isLeftHandForceFieldRefSet())
136  {
137  Literal rightForceLiteral(in.getRightHandForceFieldRef()->getDataFieldIdentifier(), checks::magnitudelarger, Literal::createParameterList(in.getForceThreshold()));
138  Literal leftForceLiteral(in.getLeftHandForceFieldRef()->getDataFieldIdentifier(), checks::magnitudelarger, Literal::createParameterList(in.getForceThreshold()));
139 
140  installConditionForForceThresholdExceeded(rightForceLiteral || leftForceLiteral);
141  }
142 }
143 
145 {
146  while (!isRunningTaskStopped())
147  {
148  if (in.isRightHandForceFieldRefSet() && in.isLeftHandForceFieldRefSet())
149  {
150  ARMARX_INFO << deactivateSpam(0.5) << "Right Forces: " << in.getRightHandForceFieldRef()->get<FramedDirection>()->toEigen().norm();
151  ARMARX_INFO << deactivateSpam(0.5) << "Left Forces: " << in.getLeftHandForceFieldRef()->get<FramedDirection>()->toEigen().norm();
152  }
153  else
154  {
155  ARMARX_INFO << deactivateSpam(5) << "No Force refs set!";
156  }
157  TimeUtil::MSSleep(100);
158  }
159 }
160 
161 
162 
164 {
165  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
166  context->getTCPControlUnit()->release();
167 
168  context->systemObserverPrx->removeTimer(local.getPoseUpdateTimerChannelRef());
169 
170  if (local.getTimerCreated())
171  {
172  ARMARX_INFO << "Removing timer.";
173  getContext<StatechartContext>()->systemObserverPrx->removeTimer(in.getStartTimeRef());
174  }
175  if (local.getTCPControlUnitRequested())
176  {
177  ARMARX_INFO << "Releasing TCPControlUnit.";
178  getContext<VisualServoGroupStatechartContext>()->getTCPControlUnit()->release();
179  }
180 }
181 
182 
183 // DO NOT EDIT NEXT FUNCTION
185 {
186 
188 }
189 
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::TwoArmVisualServoTowardsTargetPose
TwoArmVisualServoTowardsTargetPose(XMLStateConstructorParams stateData)
Definition: TwoArmVisualServoTowardsTargetPose.cpp:41
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
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:94
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:129
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::run
void run() override
Definition: TwoArmVisualServoTowardsTargetPose.cpp:144
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::Registry
static SubClassRegistry Registry
Definition: TwoArmVisualServoTowardsTargetPose.h:44
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::onEnter
void onEnter() override
Definition: TwoArmVisualServoTowardsTargetPose.cpp:46
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: TwoArmVisualServoTowardsTargetPose.cpp:184
IceInternal::Handle< DataFieldIdentifier >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose::onExit
void onExit() override
Definition: TwoArmVisualServoTowardsTargetPose.cpp:163
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
TimeUtil.h
TwoArmVisualServoTowardsTargetPose.h
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::Literal
Definition: Term.h:208
armarx::VisualServoGroup::TwoArmVisualServoTowardsTargetPose
Definition: TwoArmVisualServoTowardsTargetPose.h:31
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28