ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
ActionGeneric.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 <copyright holder> <email>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 
21 
22 ROSEE::ActionGeneric::ActionGeneric(std::string actionName) : Action (actionName, Action::Type::Generic) {}
23 
25 
26  //HACK TODO now consider the position 0 as not used joint
27  for (auto jp : jointPos) {
28  bool zeros = std::all_of(jp.second.begin(), jp.second.end(), [](double i) { return i==0.0; });
29  if (zeros) {
30  jointsInvolvedCount.insert (std::make_pair (jp.first, 0) );
31  } else {
32  jointsInvolvedCount.insert (std::make_pair (jp.first, 1) );
33 
34  }
35  }
36 
37  this->jointPos = jointPos;
38 
39 }
40 
42  Action(actionName, Action::Type::Generic) {
43 
44  if (jic.empty()) {
45  //HACK TODO now consider the position 0 as not used joint
46  for (auto jp : jointPos) {
47  bool zeros = std::all_of(jp.second.begin(), jp.second.end(), [](double i) { return i==0.0; });
48  if (zeros) {
49  jointsInvolvedCount.insert (std::make_pair (jp.first, 0) );
50  } else {
51  jointsInvolvedCount.insert (std::make_pair (jp.first, 1) );
52 
53  }
54  }
55 
56  } else {
57  if ( ! ROSEE::Utils::keys_equal(jointPos, jic) ) {
59  }
60  this->jointsInvolvedCount = jic;
61  }
62 
63  this->jointPos = jointPos;
64 
65 }
66 
67 
69  std::set<std::string> fingersInvolved) : Action(actionName, Action::Type::Generic) {
70 
71  if (jic.empty()) {
72  //HACK TODO now consider the position 0 as not used joint
73  for (auto jp : jointPos) {
74  bool zeros = std::all_of(jp.second.begin(), jp.second.end(), [](double i) { return i==0.0; });
75  if (zeros) {
76  jointsInvolvedCount.insert (std::make_pair (jp.first, 0) );
77  } else {
78  jointsInvolvedCount.insert (std::make_pair (jp.first, 1) );
79 
80  }
81  }
82 
83  } else {
84 
85  if ( ! ROSEE::Utils::keys_equal(jointPos, jic) ) {
87  }
88 
89  this->jointsInvolvedCount = jic;
90  }
91 
92  this->jointPos = jointPos;
93 
94  this->fingersInvolved = fingersInvolved;
95 }
96 
98  return jointPos;
99 }
100 
101 std::vector<ROSEE::JointPos> ROSEE::ActionGeneric::getAllJointPos() const {
102  std::vector < JointPos> vect;
103  vect.push_back ( jointPos ) ;
104  return vect;
105 }
106 
107 void ROSEE::ActionGeneric::emitYaml(YAML::Emitter& out) const {
108 
109  out << YAML::BeginMap << YAML::Key << name << YAML::Value << YAML::BeginMap ;
110  out << YAML::Key << "Type" << YAML::Value << type;
111  out << YAML::Key << "FingersInvolved" << YAML::Value << YAML::Flow << fingersInvolved;
112  out << YAML::Key << "JointsInvolvedCount" << YAML::Value << YAML::BeginMap;
113  for (const auto &jointCount : jointsInvolvedCount ) {
114  out << YAML::Key << jointCount.first;
115  out << YAML::Value << jointCount.second;
116  }
117  out << YAML::EndMap;
118 
119  out << YAML::Key << "JointPos" << YAML::Value << YAML::BeginMap;
120  for (const auto &joint : jointPos) {
121  out << YAML::Key << joint.first;
122  out << YAML::Value << YAML::Flow << joint.second; //vector of double is emitted like Seq
123  }
124  out << YAML::EndMap;
125  out << YAML::EndMap;
126  out << YAML::EndMap;
127 }
128 
129 
130 bool ROSEE::ActionGeneric::fillFromYaml(YAML::const_iterator yamlIt) {
131 
132  name = yamlIt->first.as<std::string>();
133  type = ROSEE::Action::Type::Generic;
134 
135  for (auto keyValue = yamlIt->second.begin(); keyValue != yamlIt->second.end(); ++keyValue ) {
136 
137  std::string key = keyValue->first.as<std::string>();
138 
139  if ( key.compare ("FingersInvolved") == 0 ) {
140  // if <not_inserted> tempVect is a empty vector
141  auto tempVect = keyValue->second.as <std::vector <std::string> > ();
142  fingersInvolved.insert ( tempVect.begin(), tempVect.end() );
143 
144  } else if ( key.compare ("Type") == 0 ) {
145  if (ROSEE::Action::Type::Generic != static_cast<ROSEE::Action::Type> ( keyValue->second.as <unsigned int>() )) {
146  std::cout << "[GENERIC ACTION::" << __func__ << "] Error, found type " << keyValue->second.as <unsigned int>()
147  << "instead of generic type (" << ROSEE::Action::Type::Generic << ")" << std::endl;
148  return false;
149  }
150  type = ROSEE::Action::Type::Generic;
151 
152  } else if ( key.compare ("JointsInvolvedCount") == 0 ) {
153  jointsInvolvedCount = keyValue->second.as < JointsInvolvedCount >();
154 
155  } else if ( key.compare ("JointPos") == 0 ) {
156  jointPos = keyValue->second.as < JointPos >();
157 
158  } else {
159  std::cout << "[GENERIC ACTION::" << __func__ << "] Error, not known key " << key << std::endl;
160  return false;
161  }
162  }
163 
166  }
167 
168  return true;
169 }
170 
171 
172 
173 
174 
Type
Enum useful to discriminate each action when, for example, we want to parse a file if you change thi...
Definition: Action.h:82
std::string name
Definition: Action.h:146
std::map< std::string, std::vector< double > > JointPos
The map to describe the position of all actuated joints.
Definition: Action.h:40
std::map< std::string, unsigned int > JointsInvolvedCount
The map to describe, how many times a joint is set by the action.
Definition: Action.h:63
The pure virtual class representing an Action.
Definition: Action.h:71
std::vector< ROSEE::JointPos > getAllJointPos() const override
Get the joint position related to this action, overriden from Action Necessary to not make this class...
bool keys_equal(std::map< keyType, valueType1 > const &lhs, std::map< keyType, valueType2 > const &rhs)
Return false if two maps have different keys.
Definition: Utils.h:182
virtual void emitYaml(YAML::Emitter &out) const override
Function to fill the argument passed with info about the action.
Action::Type type
Definition: Action.h:147
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
JointPos getJointPos() const override
Get the joint position related to this action, overriden from Action.
ActionGeneric()
default costructor.
std::set< std::string > fingersInvolved
Definition: Action.h:148
virtual bool fillFromYaml(YAML::const_iterator yamlIt) override
function to fill members of the Action with infos taken from yaml files