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
30using namespace armarx;
31using namespace MotionControlGroup;
32
33// DO NOT EDIT NEXT LINE
34CalculateRelativePosition::SubClassRegistry
35 CalculateRelativePosition::Registry(CalculateRelativePosition::GetName(),
37
40 CalculateRelativePositionGeneratedBase<CalculateRelativePosition>(stateData)
41{
42}
43
44void
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
143void
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
157void
159{
160 // put your user code for the breaking point here
161 // execution time should be short (<100ms)
162}
163
164void
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
uint8_t index
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPosition class.
Definition FramedPose.h:158
CalculateRelativePosition(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
custom implementation of the StatechartContext for a statechart
Class for legacy to stay compatible with old statecharts.
Definition XMLState.h:147
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
Definition FramedPose.h:207
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64