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 
29 
31 
32 #include <RobotSkillTemplates/statecharts/MotionAndPlatformControlGroup/MoveJointPlatformTrajectory.generated.h>
33 
34 #include "MotionAndPlatformControlGroupStatechartContext.generated.h"
35 
37 {
39  public MoveJointPlatformTrajectoryGeneratedBase<MoveJointPlatformTrajectory>
40  {
41  public:
43  std::pair<DataFieldIdentifierBaseList, DataFieldIdentifierBaseList>;
44  using TimedVariantBaseLists = std::pair<TimedVariantBaseList, TimedVariantBaseList>;
45 
48  MoveJointPlatformTrajectoryGeneratedBase<MoveJointPlatformTrajectory>(stateData)
49  {
50  }
51 
52  // inherited from StateBase
53  void onEnter() override;
54  void run() override;
55  void onBreak() override;
56  void onExit() override;
57  void waitForDone(bool stopAtCurrentPos);
58 
59  // static functions for AbstractFactory Method
61  static SubClassRegistry Registry;
62 
64  getDataFieldIdentifiers(const std::string& channelName)
65  {
67  datafields.first.reserve(3);
68  datafields.second.reserve(jointNames.size() - 3);
69  {
70  const std::string platformObserverName =
71  context->getProperty<std::string>("PlatformUnitObserverName").getValue();
72  if (channelName == "jointangles")
73  {
74  datafields.first.emplace_back(
75  new DataFieldIdentifier{platformObserverName, "platformPose", "positionX"});
76  datafields.first.emplace_back(
77  new DataFieldIdentifier{platformObserverName, "platformPose", "positionY"});
78  datafields.first.emplace_back(
79  new DataFieldIdentifier{platformObserverName, "platformPose", "rotation"});
80  }
81  else
82  {
83  datafields.first.emplace_back(new DataFieldIdentifier{
84  platformObserverName, "platformVelocity", "velocityX"});
85  datafields.first.emplace_back(new DataFieldIdentifier{
86  platformObserverName, "platformVelocity", "velocityY"});
87  datafields.first.emplace_back(new DataFieldIdentifier{
88  platformObserverName, "platformVelocity", "velocityRotation"});
89  }
90  };
91  const std::string observerName =
92  context->getProperty<std::string>("KinematicUnitObserverName").getValue();
93  for (size_t i = 3; i < jointNames.size(); ++i)
94  {
95  datafields.second.emplace_back(
96  new DataFieldIdentifier{observerName, channelName, jointNames.at(i)});
97  }
98  return datafields;
99  }
100 
101  void ctrlInterpol();
102  void ctrlNative();
103 
104  //helper
105  bool
107  {
108  return currentCfgIt == lastCfgIt;
109  }
110 
111  bool
113  {
114  return currentCfgIt == t->end();
115  }
116 
117  void
119  {
120  cachedValues.first =
121  context->getPlatformUnitObserver()->getDataFields(datafieldsValues.first);
122  cachedValues.second =
123  context->getKinematicUnitObserver()->getDataFields(datafieldsValues.second);
124  }
125 
126  void
128  {
129  cachedVelocities.first =
130  context->getPlatformUnitObserver()->getDataFields(datafieldsVelocities.first);
131  cachedVelocities.second =
132  context->getKinematicUnitObserver()->getDataFields(datafieldsVelocities.second);
133  }
134 
135  static float
136  getFromList(const TimedVariantBaseLists& lists, std::size_t i)
137  {
138  if (i < 3)
139  {
140  return lists.first.at(i)->getFloat();
141  }
142  return lists.second.at(i - 3)->getFloat();
143  }
144 
145  float getValue(std::size_t i) const;
146  float getVelocity(std::size_t i) const;
147 
148  void
150  {
151  const auto x = vels.at("x");
152  const auto y = vels.at("y");
153  const auto a = vels.at("alpha");
154  context->getPlatformUnit()->move(x, y, a);
155  ARMARX_INFO << "VelocitiesPlatform: x= " << x << " y=" << y << " alpha=" << a;
156  vels.erase("x");
157  vels.erase("y");
158  vels.erase("alpha");
160  {
161  vels["x"] = x;
162  vels["y"] = y;
163  vels["alpha"] = a;
164  };
165  context->getKinematicUnit()->setJointVelocities(vels);
166  }
167 
168  void
170  {
171  const auto x = values.at("x");
172  const auto y = values.at("y");
173  const auto a = values.at("alpha");
174  context->getPlatformUnit()->moveTo(
176  values.erase("x");
177  values.erase("y");
178  values.erase("alpha");
180  {
181  values["x"] = x;
182  values["y"] = y;
183  values["alpha"] = a;
184  };
185  context->getKinematicUnit()->setJointAngles(values);
186  }
187 
188  void
190  {
191  const auto x = vels.at("x");
192  const auto y = vels.at("y");
193  const auto a = vels.at("alpha");
194  context->getPlatformUnit()->setMaxVelocities((x + y) / 2.0, a);
195  vels.erase("x");
196  vels.erase("y");
197  vels.erase("alpha");
199  {
200  vels["x"] = x;
201  vels["y"] = y;
202  vels["alpha"] = a;
203  };
204  context->getKinematicUnit()->setJointVelocities(vels);
205  }
206 
207  void
208  setControlMode(ControlMode mode)
209  {
210  NameControlModeMap controlModes;
211  for (size_t i = 3; i < jointNames.size(); i++)
212  {
213  controlModes[jointNames.at(i)] = mode;
214  }
215  context->getKinematicUnit()->switchControlMode(controlModes);
216  }
217 
218  bool
220  {
221  return jointNames.size() >= 3 && jointNames.at(0) == "x" && jointNames.at(1) == "y" &&
222  jointNames.at(2) == "alpha";
223  }
224 
225  float
226  getMaxVel(std::size_t joint) const
227  {
228  return (joint < jointMaxSpeeds.size() ? jointMaxSpeeds.at(joint)
229  : in.getJointMaxSpeed());
230  }
231 
232  float
233  getMaxAcc(std::size_t joint) const
234  {
235  return (joint < maxAccs.size() ? maxAccs.at(joint) : in.getJointMaxAccel());
236  }
237 
238  float
239  getTargetTolerance(std::size_t i) const
240  {
242  }
243 
244  float
245  getConfigChangeTolerance(std::size_t i) const
246  {
247  if (doingLastConfig())
248  {
249  return getTargetTolerance(i);
250  }
251  return i < 2 ? in.getNextConfigTranslationTolerance() : in.getNextConfigTolerance();
252  }
253 
254  static int sign(double val);
255 
256  // DO NOT INSERT ANY CLASS MEMBERS,
257  // use stateparameters instead,
258  // if classmember are neccessary nonetheless, reset them in onEnter
260  std::vector<std::string> jointNames;
261  std::vector<float> maxAccs;
262  std::vector<float> jointMaxSpeeds;
263  MotionAndPlatformControlGroupStatechartContext* context;
264 
267 
270 
271  const std::chrono::milliseconds period{10};
272  const float secondsPerPeriod = static_cast<float>(period.count()) / 1000.f;
275  typename Trajectory::ordered_view::const_iterator lastCfgIt;
276  typename Trajectory::ordered_view::const_iterator currentCfgIt;
277 
278  ConditionIdentifier targetReachedCondition;
279  ActionEventIdentifier timeoutEvent;
280  };
281 } // namespace armarx::MotionAndPlatformControlGroup
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getConfigChangeTolerance
float getConfigChangeTolerance(std::size_t i) const
Definition: MoveJointPlatformTrajectory.h:245
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onEnter
void onEnter() override
Definition: MoveJointPlatformTrajectory.cpp:44
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onBreak
void onBreak() override
Definition: MoveJointPlatformTrajectory.cpp:116
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getMaxAcc
float getMaxAcc(std::size_t joint) const
Definition: MoveJointPlatformTrajectory.h:233
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::targetReachedCondition
ConditionIdentifier targetReachedCondition
Definition: MoveJointPlatformTrajectory.h:278
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getMaxVel
float getMaxVel(std::size_t joint) const
Definition: MoveJointPlatformTrajectory.h:226
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointNames
std::vector< std::string > jointNames
Definition: MoveJointPlatformTrajectory.h:260
OnScopeExit.h
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::ctrlNative
void ctrlNative()
Definition: MoveJointPlatformTrajectory.cpp:464
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::MoveJointPlatformTrajectory
MoveJointPlatformTrajectory(const XMLStateConstructorParams &stateData)
Definition: MoveJointPlatformTrajectory.h:46
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::maxAccs
std::vector< float > maxAccs
Definition: MoveJointPlatformTrajectory.h:261
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setValues
void setValues(NameValueMap &values)
Definition: MoveJointPlatformTrajectory.h:169
XMLState.h
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::run
void run() override
Definition: MoveJointPlatformTrajectory.cpp:81
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::lastCfgIt
Trajectory::ordered_view::const_iterator lastCfgIt
Definition: MoveJointPlatformTrajectory.h:275
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::DataFieldIdentifierBaseLists
std::pair< DataFieldIdentifierBaseList, DataFieldIdentifierBaseList > DataFieldIdentifierBaseLists
Definition: MoveJointPlatformTrajectory.h:43
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::currentCfgIt
Trajectory::ordered_view::const_iterator currentCfgIt
Definition: MoveJointPlatformTrajectory.h:276
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory
Definition: MoveJointPlatformTrajectory.h:38
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::validJointNames
bool validJointNames() const
Definition: MoveJointPlatformTrajectory.h:219
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::timeoutEvent
ActionEventIdentifier timeoutEvent
Definition: MoveJointPlatformTrajectory.h:279
ARMARX_ON_SCOPE_EXIT
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
Definition: OnScopeExit.h:120
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cacheValues
void cacheValues()
Definition: MoveJointPlatformTrajectory.h:118
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getValue
float getValue(std::size_t i) const
Definition: MoveJointPlatformTrajectory.cpp:518
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::Registry
static SubClassRegistry Registry
Definition: MoveJointPlatformTrajectory.h:61
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getFromList
static float getFromList(const TimedVariantBaseLists &lists, std::size_t i)
Definition: MoveJointPlatformTrajectory.h:136
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setMaxVelocity
void setMaxVelocity(NameValueMap &vels)
Definition: MoveJointPlatformTrajectory.h:189
RobotAPIObjectFactories.h
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::sign
static int sign(double val)
Definition: MoveJointPlatformTrajectory.cpp:530
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setControlMode
void setControlMode(ControlMode mode)
Definition: MoveJointPlatformTrajectory.h:208
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getDataFieldIdentifiers
DataFieldIdentifierBaseLists getDataFieldIdentifiers(const std::string &channelName)
Definition: MoveJointPlatformTrajectory.h:64
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:146
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getTargetTolerance
float getTargetTolerance(std::size_t i) const
Definition: MoveJointPlatformTrajectory.h:239
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::onExit
void onExit() override
Definition: MoveJointPlatformTrajectory.cpp:109
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointMaxSpeeds
std::vector< float > jointMaxSpeeds
Definition: MoveJointPlatformTrajectory.h:262
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::context
MotionAndPlatformControlGroupStatechartContext * context
Definition: MoveJointPlatformTrajectory.h:263
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::ctrlInterpol
void ctrlInterpol()
Definition: MoveJointPlatformTrajectory.cpp:172
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cacheVelocities
void cacheVelocities()
Definition: MoveJointPlatformTrajectory.h:127
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::getVelocity
float getVelocity(std::size_t i) const
Definition: MoveJointPlatformTrajectory.cpp:524
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:88
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::datafieldsVelocities
DataFieldIdentifierBaseLists datafieldsVelocities
Definition: MoveJointPlatformTrajectory.h:268
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::doingLastConfig
bool doingLastConfig() const
Definition: MoveJointPlatformTrajectory.h:106
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::period
const std::chrono::milliseconds period
Definition: MoveJointPlatformTrajectory.h:271
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::doneAllConfigs
bool doneAllConfigs() const
Definition: MoveJointPlatformTrajectory.h:112
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::waitForDone
void waitForDone(bool stopAtCurrentPos)
Definition: MoveJointPlatformTrajectory.cpp:123
armarx::MotionAndPlatformControlGroup
Definition: MotionAndPlatformControlGroupRemoteStateOfferer.h:29
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::jointTargetTolerance
float jointTargetTolerance
Definition: MoveJointPlatformTrajectory.h:273
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::t
TrajectoryPtr t
Definition: MoveJointPlatformTrajectory.h:259
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::secondsPerPeriod
const float secondsPerPeriod
Definition: MoveJointPlatformTrajectory.h:272
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::datafieldsValues
DataFieldIdentifierBaseLists datafieldsValues
Definition: MoveJointPlatformTrajectory.h:265
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cachedValues
TimedVariantBaseLists cachedValues
Definition: MoveJointPlatformTrajectory.h:266
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:274
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::setVelocity
void setVelocity(NameValueMap &vels)
Definition: MoveJointPlatformTrajectory.h:149
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::cachedVelocities
TimedVariantBaseLists cachedVelocities
Definition: MoveJointPlatformTrajectory.h:269
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveJointPlatformTrajectory.cpp:166
armarx::MotionAndPlatformControlGroup::MoveJointPlatformTrajectory::TimedVariantBaseLists
std::pair< TimedVariantBaseList, TimedVariantBaseList > TimedVariantBaseLists
Definition: MoveJointPlatformTrajectory.h:44