CalculateRelativePosition.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::MotionControlGroup
19  * @author Simon Ottenhaus ( simon dot ottenhaus 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 <VirtualRobot/RobotNodeSet.h>
27 
29 
30 using namespace armarx;
31 using namespace MotionControlGroup;
32 
33 // DO NOT EDIT NEXT LINE
34 CalculateRelativePosition::SubClassRegistry
35  CalculateRelativePosition::Registry(CalculateRelativePosition::GetName(),
37 
40  CalculateRelativePositionGeneratedBase<CalculateRelativePosition>(stateData)
41 {
42 }
43 
44 void
46 {
47  if (!in.isTcpRelativeOrientationsSet() && !in.isTcpRelativePositionsSet())
48  {
49  ARMARX_ERROR << "TcpRelativeOrientations and/or TcpRelativePositions has to be set, but "
50  "none has been set.";
51  emitFailure();
52  return;
53  }
54  int index = in.getIndex();
55  ARMARX_IMPORTANT << "relative position control index: " << index;
56 
58  getContext<MotionControlGroupStatechartContext>();
59  auto robot = context->getRobot();
60  std::string rootFrameName = robot->getRootNode()->getName();
61  std::string agentName = robot->getName();
62 
63  Eigen::Vector3f positionOffset = Eigen::Vector3f::Zero();
64  Eigen::Matrix3f orientationOffset = Eigen::Matrix3f::Identity();
65 
66  bool doPos = in.isTcpRelativePositionsSet();
67  bool doOri = in.isTcpRelativeOrientationsSet();
68  out.setIndex(index + 1);
69 
70  auto localRobot = robot->clone();
71  if (doPos)
72  {
73  std::vector<FramedDirectionPtr> relativePositions = in.getTcpRelativePositions();
74  assert(index >= 0);
75  if (static_cast<std::size_t>(index) >= relativePositions.size())
76  {
77  emitDone();
78  return;
79  }
80  positionOffset = relativePositions.at(index)->toRootEigen(localRobot);
81  }
82  if (doOri)
83  {
84  std::vector<FramedOrientationPtr> relativeOrientations = in.getTcpRelativeOrientations();
85  assert(index >= 0);
86  if (static_cast<std::size_t>(index) >= relativeOrientations.size())
87  {
88  emitDone();
89  return;
90  }
91  orientationOffset = relativeOrientations.at(index)->toRootEigen(localRobot);
92  }
93 
94  Eigen::Vector3f referencePosition;
95  Eigen::Matrix3f referenceOrientation;
96 
97  if (in.getRelativeMode() == "Initial")
98  {
99  referencePosition = in.getInitalTcpPosition()->toRootEigen(localRobot);
100  referenceOrientation = in.getInitailTcpOrientation()->toRootEigen(localRobot);
101  }
102  else if (in.getRelativeMode() == "TCP")
103  {
104  Eigen::Matrix4f currentTcpPose =
105  robot->getRobotNodeSet(in.getKinematicChainName())->getTCP()->getPoseInRootFrame();
106  referencePosition = currentTcpPose.block<3, 1>(0, 3);
107  referenceOrientation = currentTcpPose.block<3, 3>(0, 0);
108  }
109  else if (in.getRelativeMode() == "Previous")
110  {
111  referencePosition =
112  doPos ? in.getTcpPosition()->toRootEigen(localRobot) : Eigen::Vector3f::Zero();
113  referenceOrientation =
114  doOri ? in.getTcpOrientation()->toRootEigen(localRobot) : Eigen::Matrix3f::Identity();
115  }
116  else
117  {
118  ARMARX_ERROR << "Invalid RelativeMode '" << in.getRelativeMode()
119  << "'. Valid options: 'Initial' 'TCP' 'Previous'";
120  emitFailure();
121  return;
122  }
123 
124  if (doPos)
125  {
126  Eigen::Vector3f targetPosition = referencePosition + positionOffset;
127  FramedPositionPtr targetPositionPtr =
128  new FramedPosition(targetPosition, rootFrameName, agentName);
129  out.setTcpPosition(targetPositionPtr);
130  }
131 
132  if (doOri)
133  {
134  Eigen::Matrix3f targetOrientation = referenceOrientation * orientationOffset;
135  FramedOrientationPtr targetOrientationPtr =
136  new FramedOrientation(targetOrientation, rootFrameName, agentName);
137  out.setTcpOrientation(targetOrientationPtr);
138  }
139 
140  emitPositionControl();
141 }
142 
143 void
145 {
146  // put your user code for the execution-phase here
147  // runs in seperate thread, thus can do complex operations
148  // should check constantly whether isRunningTaskStopped() returns true
149 
150  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
151  // while (!isRunningTaskStopped()) // stop run function if returning true
152  // {
153  // // do your calculations
154  // }
155 }
156 
157 void
159 {
160  // put your user code for the breaking point here
161  // execution time should be short (<100ms)
162 }
163 
164 void
166 {
167  // put your user code for the exit point here
168  // execution time should be short (<100ms)
169 }
170 
171 // DO NOT EDIT NEXT FUNCTION
174 {
175  return XMLStateFactoryBasePtr(new CalculateRelativePosition(stateData));
176 }
armarx::MotionControlGroup::CalculateRelativePosition::onBreak
void onBreak() override
Definition: CalculateRelativePosition.cpp:158
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:75
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
CalculateRelativePosition.h
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::MotionControlGroup::CalculateRelativePosition::CalculateRelativePosition
CalculateRelativePosition(const XMLStateConstructorParams &stateData)
Definition: CalculateRelativePosition.cpp:38
IceInternal::Handle< FramedPosition >
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobot
const std::shared_ptr< RemoteRobot > getRobot()
Definition: MotionControlGroupStatechartContext.h:90
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::MotionControlGroup::CalculateRelativePosition
Definition: CalculateRelativePosition.h:31
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
MotionControlGroupStatechartContext.h
armarx::MotionControlGroup::CalculateRelativePosition::run
void run() override
Definition: CalculateRelativePosition.cpp:144
armarx::MotionControlGroup::CalculateRelativePosition::Registry
static SubClassRegistry Registry
Definition: CalculateRelativePosition.h:45
armarx::MotionControlGroup::CalculateRelativePosition::onEnter
void onEnter() override
Definition: CalculateRelativePosition.cpp:45
armarx::MotionControlGroup::CalculateRelativePosition::onExit
void onExit() override
Definition: CalculateRelativePosition.cpp:165
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:39
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::MotionControlGroup::CalculateRelativePosition::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CalculateRelativePosition.cpp:173
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27