MoveJointPlatformTrajectory.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotSkillTemplates::MotionAndPlatformControlGroup
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <chrono>
26 
30 
31 #include <RobotSkillTemplates/statecharts/MotionAndPlatformControlGroup/MoveJointPlatformTrajectory.generated.h>
32 
33 #include "MotionAndPlatformControlGroupStatechartContext.generated.h"
34 
36 {
38  public MoveJointPlatformTrajectoryGeneratedBase < MoveJointPlatformTrajectory >
39  {
40  public:
41  using DataFieldIdentifierBaseLists = std::pair<DataFieldIdentifierBaseList, DataFieldIdentifierBaseList>;
42  using TimedVariantBaseLists = std::pair<TimedVariantBaseList, TimedVariantBaseList>;
43 
45  XMLStateTemplate < MoveJointPlatformTrajectory > (stateData), MoveJointPlatformTrajectoryGeneratedBase < MoveJointPlatformTrajectory > (stateData)
46  {
47  }
48 
49  // inherited from StateBase
50  void onEnter() override;
51  void run() override;
52  void onBreak() override;
53  void onExit() override;
54  void waitForDone(bool stopAtCurrentPos);
55 
56  // static functions for AbstractFactory Method
58  static SubClassRegistry Registry;
59 
60  DataFieldIdentifierBaseLists getDataFieldIdentifiers(const std::string& channelName)
61  {
63  datafields.first.reserve(3);
64  datafields.second.reserve(jointNames.size() - 3);
65  {
66  const std::string platformObserverName = context->getProperty<std::string>("PlatformUnitObserverName").getValue();
67  if (channelName == "jointangles")
68  {
69  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformPose", "positionX"});
70  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformPose", "positionY"});
71  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformPose", "rotation"});
72  }
73  else
74  {
75  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformVelocity", "velocityX"});
76  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformVelocity", "velocityY"});
77  datafields.first.emplace_back(new DataFieldIdentifier {platformObserverName, "platformVelocity", "velocityRotation"});
78  }
79  };
80  const std::string observerName = context->getProperty<std::string>("KinematicUnitObserverName").getValue();
81  for (size_t i = 3; i < jointNames.size(); ++i)
82  {
83  datafields.second.emplace_back(new DataFieldIdentifier {observerName, channelName, jointNames.at(i)});
84  }
85  return datafields;
86  }
87  void ctrlInterpol();
88  void ctrlNative();
89 
90  //helper
91  bool doingLastConfig() const
92  {
93  return currentCfgIt == lastCfgIt;
94  }
95  bool doneAllConfigs() const
96  {
97  return currentCfgIt == t->end();
98  }
99 
100  void cacheValues()
101  {
102  cachedValues.first = context->getPlatformUnitObserver()->getDataFields(datafieldsValues.first);
103  cachedValues.second = context->getKinematicUnitObserver()->getDataFields(datafieldsValues.second);
104  }
106  {
107  cachedVelocities.first = context->getPlatformUnitObserver()->getDataFields(datafieldsVelocities.first);
108  cachedVelocities.second = context->getKinematicUnitObserver()->getDataFields(datafieldsVelocities.second);
109  }
110 
111  static float getFromList(const TimedVariantBaseLists& lists, std::size_t i)
112  {
113  if (i < 3)
114  {
115  return lists.first.at(i)->getFloat();
116  }
117  return lists.second.at(i - 3)->getFloat();
118  }
119  float getValue(std::size_t i) const;
120  float getVelocity(std::size_t i) const;
121 
123  {
124  const auto x = vels.at("x");
125  const auto y = vels.at("y");
126  const auto a = vels.at("alpha");
127  context->getPlatformUnit()->move(x, y, a);
128  ARMARX_INFO << "VelocitiesPlatform: x= " << x << " y=" << y << " alpha=" << a;
129  vels.erase("x");
130  vels.erase("y");
131  vels.erase("alpha");
133  {
134  vels["x"] = x;
135  vels["y"] = y;
136  vels["alpha"] = a;
137  };
138  context->getKinematicUnit()->setJointVelocities(vels);
139  }
141  {
142  const auto x = values.at("x");
143  const auto y = values.at("y");
144  const auto a = values.at("alpha");
145  context->getPlatformUnit()->moveTo(x, y, a, translationTargetTolerance, jointTargetTolerance);
146  values.erase("x");
147  values.erase("y");
148  values.erase("alpha");
150  {
151  values["x"] = x;
152  values["y"] = y;
153  values["alpha"] = a;
154  };
155  context->getKinematicUnit()->setJointAngles(values);
156  }
158  {
159  const auto x = vels.at("x");
160  const auto y = vels.at("y");
161  const auto a = vels.at("alpha");
162  context->getPlatformUnit()->setMaxVelocities((x + y) / 2.0, a);
163  vels.erase("x");
164  vels.erase("y");
165  vels.erase("alpha");
167  {
168  vels["x"] = x;
169  vels["y"] = y;
170  vels["alpha"] = a;
171  };
172  context->getKinematicUnit()->setJointVelocities(vels);
173  }
174  void setControlMode(ControlMode mode)
175  {
176  NameControlModeMap controlModes;
177  for (size_t i = 3; i < jointNames.size(); i++)
178  {
179  controlModes[jointNames.at(i)] = mode;
180  }
181  context->getKinematicUnit()->switchControlMode(controlModes);
182  }
183  bool validJointNames() const
184  {
185  return jointNames.size() >= 3
186  && jointNames.at(0) == "x"
187  && jointNames.at(1) == "y"
188  && jointNames.at(2) == "alpha";
189  }
190 
191  float getMaxVel(std::size_t joint) const
192  {
193  return (joint < jointMaxSpeeds.size() ? jointMaxSpeeds.at(joint) : in.getJointMaxSpeed());
194  }
195  float getMaxAcc(std::size_t joint) const
196  {
197  return (joint < maxAccs.size() ? maxAccs.at(joint) : in.getJointMaxAccel());
198  }
199 
200  float getTargetTolerance(std::size_t i) const
201  {
203  }
204 
205  float getConfigChangeTolerance(std::size_t i) const
206  {
207  if (doingLastConfig())
208  {
209  return getTargetTolerance(i);
210  }
211  return i < 2 ? in.getNextConfigTranslationTolerance() : in.getNextConfigTolerance();
212  }
213 
214 
215 
216  static int sign(double val);
217 
218  // DO NOT INSERT ANY CLASS MEMBERS,
219  // use stateparameters instead,
220  // if classmember are neccessary nonetheless, reset them in onEnter
222  std::vector<std::string> jointNames;
223  std::vector<float> maxAccs;
224  std::vector<float> jointMaxSpeeds;
225  MotionAndPlatformControlGroupStatechartContext* context;
226 
229 
232 
233  const std::chrono::milliseconds period
234  {
235  10
236  };
237  const float secondsPerPeriod = static_cast<float>(period.count()) / 1000.f;
240  typename Trajectory::ordered_view::const_iterator lastCfgIt;
241  typename Trajectory::ordered_view::const_iterator currentCfgIt;
242 
243  ConditionIdentifier targetReachedCondition;
244  ActionEventIdentifier timeoutEvent;
245  };
246 }
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getConfigChangeTolerance
float getConfigChangeTolerance(std::size_t i) const
Definition: MoveJointPlatformTrajectory.h:205
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onEnter
void onEnter() override
Definition: MoveJointPlatformTrajectory.cpp:42
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onBreak
void onBreak() override
Definition: MoveJointPlatformTrajectory.cpp:110
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getMaxAcc
float getMaxAcc(std::size_t joint) const
Definition: MoveJointPlatformTrajectory.h:195
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::targetReachedCondition
ConditionIdentifier targetReachedCondition
Definition: MoveJointPlatformTrajectory.h:243
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getMaxVel
float getMaxVel(std::size_t joint) const
Definition: MoveJointPlatformTrajectory.h:191
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointNames
std::vector< std::string > jointNames
Definition: MoveJointPlatformTrajectory.h:222
OnScopeExit.h
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::ctrlNative
void ctrlNative()
Definition: MoveJointPlatformTrajectory.cpp:424
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::MoveJointPlatformTrajectory
MoveJointPlatformTrajectory(const XMLStateConstructorParams &stateData)
Definition: MoveJointPlatformTrajectory.h:44
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::maxAccs
std::vector< float > maxAccs
Definition: MoveJointPlatformTrajectory.h:223
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setValues
void setValues(NameValueMap &values)
Definition: MoveJointPlatformTrajectory.h:140
XMLState.h
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::run
void run() override
Definition: MoveJointPlatformTrajectory.cpp:78
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::lastCfgIt
Trajectory::ordered_view::const_iterator lastCfgIt
Definition: MoveJointPlatformTrajectory.h:240
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::DataFieldIdentifierBaseLists
std::pair< DataFieldIdentifierBaseList, DataFieldIdentifierBaseList > DataFieldIdentifierBaseLists
Definition: MoveJointPlatformTrajectory.h:41
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::currentCfgIt
Trajectory::ordered_view::const_iterator currentCfgIt
Definition: MoveJointPlatformTrajectory.h:241
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory
Definition: MoveJointPlatformTrajectory.h:37
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::validJointNames
bool validJointNames() const
Definition: MoveJointPlatformTrajectory.h:183
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::timeoutEvent
ActionEventIdentifier timeoutEvent
Definition: MoveJointPlatformTrajectory.h:244
ARMARX_ON_SCOPE_EXIT
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
Definition: OnScopeExit.h:112
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cacheValues
void cacheValues()
Definition: MoveJointPlatformTrajectory.h:100
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getValue
float getValue(std::size_t i) const
Definition: MoveJointPlatformTrajectory.cpp:490
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::Registry
static SubClassRegistry Registry
Definition: MoveJointPlatformTrajectory.h:58
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getFromList
static float getFromList(const TimedVariantBaseLists &lists, std::size_t i)
Definition: MoveJointPlatformTrajectory.h:111
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setMaxVelocity
void setMaxVelocity(NameValueMap &vels)
Definition: MoveJointPlatformTrajectory.h:157
RobotAPIObjectFactories.h
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::sign
static int sign(double val)
Definition: MoveJointPlatformTrajectory.cpp:500
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setControlMode
void setControlMode(ControlMode mode)
Definition: MoveJointPlatformTrajectory.h:174
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getDataFieldIdentifiers
DataFieldIdentifierBaseLists getDataFieldIdentifiers(const std::string &channelName)
Definition: MoveJointPlatformTrajectory.h:60
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getTargetTolerance
float getTargetTolerance(std::size_t i) const
Definition: MoveJointPlatformTrajectory.h:200
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onExit
void onExit() override
Definition: MoveJointPlatformTrajectory.cpp:105
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointMaxSpeeds
std::vector< float > jointMaxSpeeds
Definition: MoveJointPlatformTrajectory.h:224
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::context
MotionAndPlatformControlGroupStatechartContext * context
Definition: MoveJointPlatformTrajectory.h:225
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::ctrlInterpol
void ctrlInterpol()
Definition: MoveJointPlatformTrajectory.cpp:164
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cacheVelocities
void cacheVelocities()
Definition: MoveJointPlatformTrajectory.h:105
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getVelocity
float getVelocity(std::size_t i) const
Definition: MoveJointPlatformTrajectory.cpp:495
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::datafieldsVelocities
DataFieldIdentifierBaseLists datafieldsVelocities
Definition: MoveJointPlatformTrajectory.h:230
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::doingLastConfig
bool doingLastConfig() const
Definition: MoveJointPlatformTrajectory.h:91
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::period
const std::chrono::milliseconds period
Definition: MoveJointPlatformTrajectory.h:234
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::doneAllConfigs
bool doneAllConfigs() const
Definition: MoveJointPlatformTrajectory.h:95
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::waitForDone
void waitForDone(bool stopAtCurrentPos)
Definition: MoveJointPlatformTrajectory.cpp:116
armarx::MotionAndPlatformControlGroup
Definition: MotionAndPlatformControlGroupRemoteStateOfferer.h:28
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointTargetTolerance
float jointTargetTolerance
Definition: MoveJointPlatformTrajectory.h:238
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::t
TrajectoryPtr t
Definition: MoveJointPlatformTrajectory.h:221
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::secondsPerPeriod
const float secondsPerPeriod
Definition: MoveJointPlatformTrajectory.h:237
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::datafieldsValues
DataFieldIdentifierBaseLists datafieldsValues
Definition: MoveJointPlatformTrajectory.h:227
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cachedValues
TimedVariantBaseLists cachedValues
Definition: MoveJointPlatformTrajectory.h:228
armarx::DataFieldIdentifier
DataFieldIdentifier provide the basis to identify data field within a distributed ArmarX scenario.
Definition: DataFieldIdentifier.h:48
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::translationTargetTolerance
float translationTargetTolerance
Definition: MoveJointPlatformTrajectory.h:239
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setVelocity
void setVelocity(NameValueMap &vels)
Definition: MoveJointPlatformTrajectory.h:122
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cachedVelocities
TimedVariantBaseLists cachedVelocities
Definition: MoveJointPlatformTrajectory.h:231
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveJointPlatformTrajectory.cpp:157
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::TimedVariantBaseLists
std::pair< TimedVariantBaseList, TimedVariantBaseList > TimedVariantBaseLists
Definition: MoveJointPlatformTrajectory.h:42