20 type = Action::Type::Timed;
33 std::vector<ROSEE::JointPos> jpVect;
34 jpVect.reserve (actionsNamesOrdered.size());
35 for (
auto actName : actionsNamesOrdered) {
36 jpVect.push_back( actionsJointPosMap.at (actName) );
43 std::vector<ROSEE::JointsInvolvedCount> jcVect;
44 jcVect.reserve (actionsNamesOrdered.size());
45 for (
auto actName : actionsNamesOrdered) {
46 jcVect.push_back( actionsJointCountMap.at (actName) );
53 std::vector <std::pair <double,double> > timeVect;
54 timeVect.reserve (actionsNamesOrdered.size());
55 for (
auto actName : actionsNamesOrdered) {
56 timeVect.push_back( actionsTimeMarginsMap.at (actName) );
63 auto it = actionsJointPosMap.find(actionName);
65 if ( it != actionsJointPosMap.end() ) {
69 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: action " << actionName <<
" not present in this composed timed action" << std::endl;
76 auto it = actionsJointCountMap.find(actionName);
78 if ( it != actionsJointCountMap.end() ) {
82 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: action " << actionName <<
" not present in this composed timed action" << std::endl;
90 auto it = actionsTimeMarginsMap.find(actionName);
92 if ( it != actionsTimeMarginsMap.end() ) {
93 return ( it->second );
96 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: action " << actionName <<
" not present in this composed timed action" << std::endl;
97 return std::make_pair(-1, -1);
102 return actionsNamesOrdered;
108 std::stringstream output;
110 output <<
"Timed Action '" <<
name <<
"'" << std::endl;
112 output <<
"\tNice TimeLine:" << std::endl <<
"\t\t";
113 for (
auto it : actionsNamesOrdered ) {
114 output << actionsTimeMarginsMap.at(it).first <<
" -----> " << it <<
" -----> " << actionsTimeMarginsMap.at(it).second <<
" + ";
116 output.seekp (-3, output.cur);
119 output <<
"\tJointPos of each actions, in order of execution:\n";
120 for (
auto actionName : actionsNamesOrdered ) {
121 output <<
"\t" << actionName << std::endl;
122 output << actionsJointPosMap.at(actionName) << std::endl;
125 output <<
"\tJointPos final, the sum of all joint pos of each inner action:\n";
126 output << jointPosFinal << std::endl;
128 output <<
"\tFingers involved: [" ;
129 for (
auto it : fingersInvolved) {
130 output << it <<
", ";
132 output.seekp (-2, output.cur);
133 output <<
"]" << std::endl;
135 output <<
"\tEach joint influenced by x inner action:" << std::endl;
136 output << jointsInvolvedCount << std::endl;
138 std::cout << output.str();
144 out << YAML::BeginMap << YAML::Key <<
name << YAML::Value << YAML::BeginMap ;
145 std::string timeline;
146 for (
auto it : actionsNamesOrdered ) {
147 timeline += (std::to_string(actionsTimeMarginsMap.at(it).first) +
" -----> " +
148 it +
" -----> " + std::to_string(actionsTimeMarginsMap.at(it).second) +
" + ");
150 if (! timeline.empty() ) {
151 timeline.pop_back(); timeline.pop_back(); timeline.pop_back();
154 out << YAML::Comment(timeline);
155 out << YAML::Key <<
"Type" << YAML::Value << type;
156 out << YAML::Key <<
"FingersInvolved" << YAML::Value << YAML::Flow << fingersInvolved;
158 out << YAML::Key <<
"JointsInvolvedCount" << YAML::Value << YAML::BeginMap;
159 for (
const auto &jointCount : jointsInvolvedCount ) {
160 out << YAML::Key << jointCount.first;
161 out << YAML::Value << jointCount.second;
165 out << YAML::Key <<
"ActionsJointPosFinal" << YAML::Value << YAML::BeginMap;
166 for (
const auto joint : jointPosFinal ) {
167 out << YAML::Key << joint.first;
168 out << YAML::Value << YAML::Flow << joint.second;
172 out << YAML::Key <<
"ActionsNamesOrdered" << YAML::Value << YAML::Flow << actionsNamesOrdered;
174 out << YAML::Key <<
"ActionsTimeMargins" << YAML::Value << YAML::BeginMap;
175 for (
const std::string action : actionsNamesOrdered ){
177 out << YAML::Key << action << YAML::Value << YAML::BeginMap;
179 out << YAML::Key <<
"marginBefore" <<
180 YAML::Value << actionsTimeMarginsMap.at(action).first;
182 out << YAML::Key <<
"marginAfter" <<
183 YAML::Value << actionsTimeMarginsMap.at(action).second;
188 out << YAML::Key <<
"ActionsJointPos" << YAML::Value << YAML::BeginMap;
189 for (
const std::string action : actionsNamesOrdered ){
191 out << YAML::Key << action << YAML::Value << YAML::BeginMap;
193 for (
const auto joint : actionsJointPosMap.at(action) ) {
194 out << YAML::Key << joint.first;
195 out << YAML::Value << YAML::Flow << joint.second;
202 out << YAML::Key <<
"ActionsJointCount" << YAML::Value << YAML::BeginMap;
203 for (
const std::string action : actionsNamesOrdered ){
205 out << YAML::Key << action << YAML::Value << YAML::BeginMap;
207 for (
const auto joint : actionsJointCountMap.at(action) ) {
208 out << YAML::Key << joint.first;
209 out << YAML::Value << YAML::Flow << joint.second;
224 name = yamlIt->first.as<std::string>();
225 type = Action::Type::Timed;
227 for (
auto keyValue = yamlIt->second.begin(); keyValue != yamlIt->second.end(); ++keyValue ) {
229 std::string key = keyValue->first.as<std::string>();
231 if ( key.compare (
"FingersInvolved") == 0 ) {
232 auto tempVect = keyValue->second.as <std::vector <std::string> > ();
233 fingersInvolved.insert ( tempVect.begin(), tempVect.end() );
235 }
else if ( key.compare (
"Type") == 0 ) {
236 if (ROSEE::Action::Type::Timed !=
static_cast<ROSEE::Action::Type> ( keyValue->second.as <
unsigned int>() )) {
237 std::cout <<
"[Timed ACTION::" << __func__ <<
"] Error, found type " << keyValue->second.as <
unsigned int>()
238 <<
"instead of Timed type (" << ROSEE::Action::Type::Timed <<
")" << std::endl;
241 type = ROSEE::Action::Type::Timed;
243 }
else if ( key.compare (
"ActionsNamesOrdered") == 0 ) {
244 actionsNamesOrdered = keyValue->second.as < std::vector <std::string> > ();
246 }
else if ( key.compare (
"JointsInvolvedCount") == 0 ) {
249 }
else if ( key.compare(
"ActionsTimeMargins") == 0 ) {
251 for (
auto tMargins = keyValue->second.begin(); tMargins != keyValue->second.end(); ++tMargins ) {
253 std::string actNAme = tMargins->first.as<std::string>();
254 double before = tMargins->second[
"marginBefore"].as<
double>();
255 double after = tMargins->second[
"marginAfter"].as<
double>();
256 actionsTimeMarginsMap.insert (std::make_pair (actNAme, std::make_pair(before, after) ) ) ;
259 }
else if ( key.compare(
"ActionsJointPos") == 0) {
261 for (
auto jPos = keyValue->second.begin(); jPos != keyValue->second.end(); ++jPos ) {
263 std::string actName = jPos->first.as<std::string>();
265 actionsJointPosMap.insert (std::make_pair (actName, jp) );
268 }
else if ( key.compare(
"ActionsJointCount") == 0) {
270 for (
auto jCount = keyValue->second.begin(); jCount != keyValue->second.end(); ++jCount ) {
272 std::string actName = jCount->first.as<std::string>();
274 actionsJointCountMap.insert (std::make_pair (actName, jc) );
278 }
else if ( key.compare(
"ActionsJointPosFinal") == 0) {
280 jointPosFinal = keyValue->second.as <
JointPos >();
283 std::cerr <<
"[TIMEDACTION::" << __func__ <<
"] Error, not known key " << key << std::endl;
292 unsigned int jointPosIndex,
double percentJointPos, std::string newActionName) {
294 std::string usedName = (newActionName.empty()) ? action->getName() : newActionName;
296 unsigned int count = actionsJointPosMap.count( usedName );
299 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: " << usedName <<
" already present. Failed Insertion" << std::endl;
303 if (marginAfter < 0 || marginBefore < 0) {
304 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: Please pass positive time margins" << std::endl;
308 if ( jointPosIndex > action->getAllJointPos().size()-1 ) {
309 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] ERROR: you pass index "<< jointPosIndex <<
310 " but there are only " << action->getAllJointPos().size() <<
" JointPos in the action passed as argument" << std::endl;
314 if (percentJointPos < 0 || percentJointPos > 1) {
315 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] Please insert percentage for jointpos between 0 and 1. Passed: "
316 << percentJointPos << std::endl;
320 if ( actionsNamesOrdered.size() > 0 &&
323 std::cerr <<
"[ACTIONTIMED:: " << __func__ <<
"] The action passed as argument has different keys in jointPosmap"
324 <<
" respect to the others inserted in this timed action " << std::endl;
328 ROSEE::JointPos insertingJP = (percentJointPos)*(action->getAllJointPos().at( jointPosIndex )) ;
329 actionsJointPosMap.insert (std::make_pair ( usedName, insertingJP) );
330 actionsTimeMarginsMap.insert ( std::make_pair( usedName, std::make_pair(marginBefore, marginAfter)));
331 actionsNamesOrdered.push_back ( usedName );
332 actionsJointCountMap.insert (std::make_pair (usedName, action->getJointsInvolvedCount()));
335 for (
auto it: action->getFingersInvolved() ) {
336 fingersInvolved.insert ( it );
339 if (actionsNamesOrdered.size() == 1 ) {
341 jointsInvolvedCount = action->getJointsInvolvedCount();
342 jointPosFinal = insertingJP;
347 for (
auto jic : action->getJointsInvolvedCount() ) {
348 jointsInvolvedCount.at(jic.first) += jic.second;
350 if (jic.second > 0) {
352 jointPosFinal.at(jic.first) = insertingJP.at(jic.first);