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