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