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