PlayMMMFileState.cpp
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::PlayMMMFile
17  * @author Philipp Schmidt ( ufedv at student dot 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 #include "PlayMMMFileState.h"
24 
25 #include <unordered_set>
26 
27 using namespace armarx;
28 using namespace PlayMMMFile;
29 
30 // DO NOT EDIT NEXT LINE
31 PlayMMMFileState::SubClassRegistry PlayMMMFileState::Registry(PlayMMMFileState::GetName(),
33 
36 
37 void
39 {
40  TrajectoryPtr dummyTraj = new Trajectory();
41  // put your user code for the enter-point here
42  // execution time should be short (<100ms)
43  ARMARX_LOG << "onEnter()";
44 
45  //Load blacklist
46  std::unordered_set<std::string> jointsBlacklist;
47  if (in.isblacklistSet())
48  {
49  std::string s = in.getblacklist();
50  std::string delimiter = ",";
51  size_t pos = 0;
52  std::string token;
53  while ((pos = s.find(delimiter)) != std::string::npos)
54  {
55  token = s.substr(0, pos);
56  jointsBlacklist.insert(token);
57  s.erase(0, pos + delimiter.length());
58  }
59  jointsBlacklist.insert(s);
60  }
61 
62  //Load motion
63  this->getMmmPlayer()->loadMMMFile(in.getmmmFile(), "");
64  if (this->getMmmPlayer()->isMotionLoaded())
65  {
66  this->getTrajectoryPlayer()->setIsVelocityControl(true);
67  TrajectoryPtr trajectory = TrajectoryPtr::dynamicCast(this->getMmmPlayer()->getJointTraj());
68  float start = in.isStartTimeSet() ? in.getStartTime() : trajectory->begin()->timestamp;
69  float end = in.isEndTimeSet() ? in.getEndTime() : trajectory->rbegin()->timestamp;
70  ARMARX_INFO << "StartTime of Traj: " << trajectory->begin()->timestamp;
71  ARMARX_INFO << "EndTime of Traj: " << trajectory->rbegin()->timestamp;
72  ARMARX_INFO << "names: " << trajectory->getDimensionNames();
73  TrajectoryPtr trajPart = trajectory->getPart(start, end, 0);
74  ARMARX_INFO << "StartTime of Traj: " << trajPart->begin()->timestamp;
75  ARMARX_INFO << "EndTime of Traj: " << trajPart->rbegin()->timestamp;
76  ARMARX_INFO << " amplitude: " << trajPart->getAmplitude(0, 0, start, end);
77  ARMARX_INFO << "names: " << trajPart->getDimensionNames();
78  this->getTrajectoryPlayer()->loadJointTraj(trajPart);
79  this->getTrajectoryPlayer()->loadBasePoseTraj(this->getMmmPlayer()->getBasePoseTraj());
80 
81  //Delete joint names that are not available on robot
82  auto map = trajPart->getStatesMap<float>(trajPart->begin()->timestamp);
83  for (auto it = map.begin(); it != map.end();)
84  {
85  if (!this->getKinematicUnitObserver()->existsDataField("jointangles", it->first))
86  {
87  map.erase(it++);
88  }
89  else
90  {
91  ++it;
92  }
93  }
94 
95  Ice::StringSeq jointNames = this->getMmmPlayer()->getJointNames();
96  for (size_t i = 0; i < jointNames.size(); ++i)
97  {
98  //Set joints in use
99  bool enabled = !jointsBlacklist.count(jointNames.at(i));
100  if (enabled)
101  {
102  ARMARX_LOG << jointNames.at(i) << " enabled";
103  this->getTrajectoryPlayer()->setJointsInUse(jointNames.at(i), true);
104  }
105  else
106  {
107  ARMARX_LOG << jointNames.at(i) << " disabled";
108  this->getTrajectoryPlayer()->setJointsInUse(jointNames.at(i), false);
109  map.erase(jointNames.at(i));
110  }
111  }
112 
113  //Set speed
114  this->getTrajectoryPlayer()->setEndTime(this->getTrajectoryPlayer()->getTrajEndTime() /
115  in.getspeed());
116 
117  //Pass joint map
118  local.setinitialJointValues(map);
119  }
120  else
121  {
122  cancelSubstates();
123  this->emitFailure();
124  return;
125  }
126 }
127 
128 void
130 {
131  // // put your user code for the execution-phase here
132  // // runs in seperate thread, thus can do complex operations
133  // // should check constantly whether isRunningTaskStopped() returns true
134  // while (!isRunningTaskStopped()) // stop run function if returning true
135  // {
136 
137  // }
138 }
139 
140 //void PlayMMMFileState::onBreak()
141 //{
142 // // put your user code for the breaking point here
143 // // execution time should be short (<100ms)
144 //}
145 
146 void
148 {
149  // put your user code for the exit point here
150  // execution time should be short (<100ms)
151 
152  ARMARX_LOG << "onExit()";
153  this->getTrajectoryPlayer()->stopTrajectoryPlayer();
154 }
155 
156 // DO NOT EDIT NEXT FUNCTION
159 {
160  return XMLStateFactoryBasePtr(new PlayMMMFileState(stateData));
161 }
armarx::PlayMMMFile::PlayMMMFileState::onEnter
void onEnter() override
Definition: PlayMMMFileState.cpp:38
armarx::PlayMMMFile::PlayMMMFileState::onExit
void onExit() override
Definition: PlayMMMFileState.cpp:147
armarx::PlayMMMFile::PlayMMMFileState::Registry
static SubClassRegistry Registry
Definition: PlayMMMFileState.h:46
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
RobotAPIObjectFactories.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< Trajectory >
armarx::PlayMMMFile::PlayMMMFileState::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: PlayMMMFileState.cpp:158
armarx::PlayMMMFile::PlayMMMFileState::run
void run() override
Definition: PlayMMMFileState.cpp:129
PlayMMMFileState.h
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:165
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::PlayMMMFile::PlayMMMFileState::PlayMMMFileState
PlayMMMFileState(const XMLStateConstructorParams &stateData)
Definition: PlayMMMFileState.h:32
Trajectory.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27