MovePlatformRelative.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::PlatformGroup
19  * @author Mirko Waechter ( mirko dot waechter 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 
25 #include "MovePlatformRelative.h"
26 #include "PlatformContext.h"
27 
30 
31 using namespace armarx;
32 using namespace PlatformGroup;
33 
34 // DO NOT EDIT NEXT LINE
35 MovePlatformRelative::SubClassRegistry MovePlatformRelative::Registry(MovePlatformRelative::GetName(), &MovePlatformRelative::CreateInstance);
36 
37 
38 
40  XMLStateTemplate<MovePlatformRelative>(stateData), MovePlatformRelativeGeneratedBase<MovePlatformRelative>(stateData)
41 {
42 }
43 
45 {
46  if (!in.getUsePlatformNativeController())
47  {
48  // put your user code for the enter-point here
49  // execution time should be short (<100ms)
50  PlatformContext* c = getContext<PlatformContext>();
51  auto pose = in.getRelativePose();
52 
53  DatafieldRefPtr posXRef = new DatafieldRef(c->platformUnitObserverPrx, "platformPose", "positionX");
54  DatafieldRefPtr posYRef = new DatafieldRef(c->platformUnitObserverPrx, "platformPose", "positionY");
55  DatafieldRefPtr oriRef = new DatafieldRef(c->platformUnitObserverPrx, "platformPose", "rotation");
56  float posX = posXRef->getDataField()->getFloat();
57  float posY = posYRef->getDataField()->getFloat();
58  float ori = oriRef->getDataField()->getFloat();
59 
60  Eigen::Vector3f localPos {pose->x, pose->y, 0};
62  m = Eigen::AngleAxisf(ori, Eigen::Vector3f::UnitZ());
63  Eigen::Vector3f globalPos = m * localPos;
64 
65  if (pose->z > M_PI)
66  {
67  pose->z -= M_PI * 2.0;
68  }
69 
70  if (pose->z < -M_PI)
71  {
72  pose->z += M_PI * 2.0;
73  }
74 
75  float targetX = posX + globalPos[0];
76  float targetY = posY + globalPos[1];
77  float targetOri = ori + pose->z;
78 
79  while (targetOri < 0)
80  {
81  targetOri += M_PI * 2.0;
82  }
83 
84  while (targetOri > M_PI * 2.0)
85  {
86  targetOri -= M_PI * 2.0;
87  }
88 
89  ARMARX_INFO << "Relative target pose: " << pose->toEigen().transpose();
90  ARMARX_INFO << "Global target pose: " << targetX << " " << targetY << " " << targetOri;
91  local.setAbsoluteTargetPosition(new Vector3(targetX, targetY, targetOri));
92  }
93  else
94  {
95  cancelSubstates();
96  PlatformContext* context = getContext<PlatformContext>();
97  std::string platformDatafieldName = in.getPlatformPoseDatafieldName();
98  DataFieldIdentifierPtr posXId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".positionX");
99  DataFieldIdentifierPtr posYId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".positionY");
100  DataFieldIdentifierPtr rotId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".rotation");
101  auto datafields = context->platformUnitObserverPrx->getDataFields({posXId, posYId, rotId});
102  auto relativeTargetPose = in.getRelativePose();
103 
104  Vector3Ptr target = new Vector3(datafields.at(0)->getFloat() + relativeTargetPose->x,
105  datafields.at(1)->getFloat() + relativeTargetPose->y,
106  math::MathUtils::angleMod2PI(datafields.at(2)->getFloat() + relativeTargetPose->z));
107 
108 
109 
110  Literal checkX(*posXId, checks::approx, {target->x, in.getPositionalAccuracy()});
111  Literal checkY(*posYId, checks::approx, {target->y, in.getPositionalAccuracy()});
112 
113  Literal checkAngle(*rotId, checks::approx, {target->z, in.getOrientationalAccuracy()});
114  // for special case of orientations around 2*PI
115  float checkAngleValueOffset;
116 
117  if (target->z > M_PI)
118  {
119  checkAngleValueOffset = - 2 * M_PI;
120  }
121  else
122  {
123  checkAngleValueOffset = 2 * M_PI;
124  }
125 
126  Literal checkAngle2(rotId, checks::approx, { (float)(target->z + checkAngleValueOffset), in.getOrientationalAccuracy()});
127 
128  installConditionForTargetReached(checkX && checkY && (checkAngle || checkAngle2));
129  setTimeoutEvent(in.getTimeout(), this->createEventTimeout());
130  // usleep(20000);
131 
132  }
133 }
134 
136 {
137  std::string platformDatafieldName = in.getPlatformPoseDatafieldName();
138  if (in.getUsePlatformNativeController())
139  {
140  PlatformContext* context = getContext<PlatformContext>();
141  DataFieldIdentifierPtr posXId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".positionX");
142  DataFieldIdentifierPtr posYId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".positionY");
143  DataFieldIdentifierPtr rotId = new DataFieldIdentifier(context->platformUnitObserverName + "." + platformDatafieldName + ".rotation");
144  auto datafields = context->platformUnitObserverPrx->getDataFields({posXId, posYId, rotId});
145  auto relativeTargetPose = in.getRelativePose();
146  Vector3Ptr target = new Vector3(datafields.at(0)->getFloat() + relativeTargetPose->x,
147  datafields.at(1)->getFloat() + relativeTargetPose->y,
148  math::MathUtils::angleMod2PI(datafields.at(2)->getFloat() + relativeTargetPose->z));
149  context->platformUnitPrx->moveRelative(relativeTargetPose->x, relativeTargetPose->y, relativeTargetPose->z, in.getPositionalAccuracy(), in.getOrientationalAccuracy());
150  auto getError = [&]()
151  {
152  auto datafields = context->platformUnitObserverPrx->getDataFields({posXId, posYId, rotId});
153 
154  Eigen::Vector2f currentPos(datafields.at(0)->getFloat(),
155  datafields.at(1)->getFloat());
156  float yaw = datafields.at(2)->getFloat();
157  Eigen::Vector2f posError = currentPos - target->toEigen().head(2);
158  float yawError = yaw - target->z;
159  return Vector3Ptr(new Vector3(posError(0), posError(1), yawError));
160  };
161  while (!isRunningTaskStopped())
162  {
163  auto error = getError();
164  ARMARX_INFO << deactivateSpam(1) << VAROUT(*error);
165 
166  usleep(20000);
167  }
168  auto error = getError();
169  ARMARX_INFO << "remaining error: " << *error;
170  out.setRemainingError(error);
171  }
172 }
173 
174 
175 
177 {
178  // put your user code for the exit point here
179  // execution time should be short (<100ms)
180  waitForRunningTaskToFinish();
181 
182 }
183 
184 
185 // DO NOT EDIT NEXT FUNCTION
187 {
188  return XMLStateFactoryBasePtr(new MovePlatformRelative(stateData));
189 }
190 
armarx::PlatformGroup::MovePlatformRelative::run
void run() override
Definition: MovePlatformRelative.cpp:135
MathUtils.h
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::getError
void getError(Eigen::Matrix4f &currentPose, Eigen::Vector3d &targetPosition, Eigen::Quaterniond &targetQuaternion, double &posiError, double &oriError)
Definition: utils.cpp:94
IceInternal::Handle< DatafieldRef >
armarx::math::MathUtils::angleMod2PI
static float angleMod2PI(float value)
Definition: MathUtils.h:135
armarx::PlatformGroup::MovePlatformRelative::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MovePlatformRelative.cpp:186
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
DatafieldRef.h
MovePlatformRelative.h
armarx::PlatformGroup::MovePlatformRelative::Registry
static SubClassRegistry Registry
Definition: MovePlatformRelative.h:44
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::PlatformGroup::MovePlatformRelative
Definition: MovePlatformRelative.h:31
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::PlatformGroup::MovePlatformRelative::onExit
void onExit() override
Definition: MovePlatformRelative.cpp:176
armarx::PlatformContext::platformUnitObserverName
std::string platformUnitObserverName
Definition: PlatformContext.h:124
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:169
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::PlatformGroup::MovePlatformRelative::MovePlatformRelative
MovePlatformRelative(XMLStateConstructorParams stateData)
Definition: MovePlatformRelative.cpp:39
PlatformContext.h
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
armarx::PlatformGroup::MovePlatformRelative::onEnter
void onEnter() override
Definition: MovePlatformRelative.cpp:44
float
#define float
Definition: 16_Level.h:22
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
armarx::PlatformContext::platformUnitPrx
PlatformUnitInterfacePrx platformUnitPrx
Definition: PlatformContext.h:120
armarx::Vector3Ptr
IceInternal::Handle< Vector3 > Vector3Ptr
Definition: Pose.h:165
armarx::Literal
Definition: Term.h:208
armarx::PlatformContext
Definition: PlatformContext.h:64
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48
armarx::PlatformContext::platformUnitObserverPrx
PlatformUnitObserverInterfacePrx platformUnitObserverPrx
Definition: PlatformContext.h:121