ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
ActionMultiplePinchTight.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 IIT-HHCM
3  * Author: Davide Torielli
4  * email: davide.torielli@iit.it
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
20 
22  ActionPinchGeneric ("multiplePinchTight", 3, ActionPrimitive::Type::MultiplePinchTight) { }
23 
25  ActionPinchGeneric ("multiplePinchTight", maxStoredActionStates, ActionPrimitive::Type::MultiplePinchTight) { }
26 
28  std::set <std::string> fingerNamesSet,
29  JointPos jp, double depthSum) :
30  ActionPinchGeneric ( ("multiplePinchTight_" + std::to_string(fingerNamesSet.size())),
31  fingerNamesSet.size(), 3, ActionPrimitive::Type::MultiplePinchTight ) {
32 
33  //different from insertState, here we are sure the set is empty (we are in costructor)
34  fingersInvolved = fingerNamesSet;
35  actionStates.insert (std::make_pair (jp, depthSum) );
36 }
37 
38 
40  return (actionStates.begin()->first);
41 }
42 
44  auto it = actionStates.begin();
45  unsigned int i = 1;
46  while (i < index ) {
47  ++ it;
48  }
49  return (it->first);
50 }
51 
52 std::vector < ROSEE::JointPos > ROSEE::ActionMultiplePinchTight::getAllJointPos() const{
53 
54  std::vector < JointPos > retVect;
55  retVect.reserve ( actionStates.size() );
56 
57  for (auto it : actionStates ) {
58  retVect.push_back(it.first);
59  }
60 
61  return retVect;
62 }
63 
64 
65 std::vector < ROSEE::ActionMultiplePinchTight::StateWithDepth > ROSEE::ActionMultiplePinchTight::getActionStates () const {
66 
67  std::vector < ROSEE::ActionMultiplePinchTight::StateWithDepth > retVect;
68  retVect.reserve ( actionStates.size() );
69 
70  for (auto it : actionStates ) {
71  retVect.push_back(it);
72  }
73 
74  return retVect;
75 
76 }
77 
79  ROSEE::JointPos jp, double depthSum) {
80 
81  auto pairRet = actionStates.insert ( std::make_pair (jp, depthSum) ) ;
82 
83  if (! pairRet.second ) {
84  //TODO print error no insertion because depth is equal... very improbable
85  return false;
86  }
87 
88  if (actionStates.size() > maxStoredActionStates) {
89  //max capacity reached, we have to delete the last one
90  auto it = pairRet.first;
91 
92  if ( (++it) == actionStates.end() ){
93  // the new inserted is the last one and has to be erased
94  actionStates.erase(pairRet.first);
95  return false;
96  }
97 
98  // the new inserted is not the last one that has to be erased
99  auto lastElem = actionStates.end();
100  --lastElem;
101  actionStates.erase(lastElem);
102  }
103 
104  return true;
105 }
106 
107 
109 
110  std::stringstream output;
111  output << "ActionName: " << name << std::endl;
112 
113  output << "FingersInvolved: [";
114  for (auto fingName : fingersInvolved){
115  output << fingName << ", " ;
116  }
117  output.seekp (-2, output.cur); //to remove the last comma (and space)
118  output << "]" << std::endl;
119 
120  output << "JointsInvolvedCount: " << std::endl;;
121  output << jointsInvolvedCount << std::endl;
122 
123  unsigned int nActState = 1;
124  for (auto itemSet : actionStates) { //the element in the set
125  output << "Action_State_" << nActState << " :" << std::endl;
126 
127  output << "\t" << "JointStates:" << std::endl;
128  output << itemSet.first;
129  output << "\t" << "DepthSum:" << itemSet.second <<std::endl;
130 
131  nActState++;
132  }
133  output << std::endl;
134 
135  std::cout << output.str();
136 
137 }
138 
139 
140 void ROSEE::ActionMultiplePinchTight::emitYaml ( YAML::Emitter& out ) const {
141 
142  out << YAML::Key << YAML::Flow << fingersInvolved;
143 
144  unsigned int nCont = 1;
145  out << YAML::Value << YAML::BeginMap;
146  out << YAML::Key << "PrimitiveType" << YAML::Value << primitiveType;
147  out << YAML::Key << "ActionName" << YAML::Value << name;
148  out << YAML::Key << "JointsInvolvedCount" << YAML::Value << YAML::BeginMap;
149  for (const auto &jointCount : jointsInvolvedCount ) {
150  out << YAML::Key << jointCount.first;
151  out << YAML::Value << jointCount.second;
152  }
153  out << YAML::EndMap;
154 
155  for (const auto & actionState : actionStates) { //.second is the set of ActionState
156 
157  std::string contSeq = "ActionState_" + std::to_string(nCont);
158  out << YAML::Key << contSeq;
159 
160  out << YAML::Value << YAML::BeginMap;
161  //actionState.first, the jointstates map
162  out << YAML::Key << "JointPos" << YAML::Value << YAML::BeginMap;
163  for (const auto &joint : actionState.first) {
164  out << YAML::Key << joint.first;
165  out << YAML::Value << YAML::Flow << joint.second; //vector of double is emitted like Seq
166  }
167  out << YAML::EndMap;
168 
169  //actionState.second, the optional
170  out << YAML::Key << "Optional" << YAML::Value << YAML::BeginMap;
171  out << YAML::Key << "DepthSum" << YAML::Value << actionState.second;
172  out << YAML::EndMap;
173 
174  out << YAML::EndMap;
175  nCont++;
176  }
177  out << YAML::EndMap;
178 
179 }
180 
181 
182 bool ROSEE::ActionMultiplePinchTight::fillFromYaml ( YAML::const_iterator yamlIt ) {
183 
184  std::vector <std::string> fingInvolvedVect = yamlIt->first.as <std::vector < std::string >> ();
185  for (const auto &it : fingInvolvedVect) {
186  fingersInvolved.insert(it);
187  }
188 
189  for ( YAML::const_iterator keyValue = yamlIt->second.begin(); keyValue != yamlIt->second.end(); ++keyValue) {
190 
191  std::string key = keyValue->first.as<std::string>();
192  if (key.compare("JointsInvolvedCount") == 0) {
193  jointsInvolvedCount = keyValue->second.as < JointsInvolvedCount > ();
194 
195  } else if (key.compare ("ActionName") == 0 ) {
196  name = keyValue->second.as <std::string> ();
197 
198  } else if (key.compare ("PrimitiveType") == 0) {
200  keyValue->second.as <unsigned int>() );
201  if (parsedType != primitiveType ) {
202  std::cerr << "[ERROR ActionMultPinch::" << __func__ << " parsed a type " << parsedType <<
203  " but this object has primitive type " << primitiveType << std::endl;
204  return false;
205  }
206 
207  } else if (key.compare(0, 12, "ActionState_") == 0) { //compare 12 caracters from index 0 of key
208 
209  JointPos jointPos;
210  double depthSum;
211  for(YAML::const_iterator asEl = keyValue->second.begin(); asEl != keyValue->second.end(); ++asEl) {
212 
213  //asEl can be the map JointPos or the map Optional
214  if (asEl->first.as<std::string>().compare ("JointPos") == 0 ) {
215  jointPos = asEl->second.as < JointPos >();
216 
217  } else if (asEl->first.as<std::string>().compare ("Optional") == 0 ) {
218  depthSum = asEl->second["DepthSum"].as < double >();
219 
220  } else {
221  //ERRROr, only JointPos and Optional at this level
222  std::cerr << "[ERROR ActionMultPinch::" << __func__ << "not know key "
223  << asEl->first.as<std::string>() <<
224  " found in the yaml file at this level" << std::endl;
225  return false;
226  }
227  }
228  actionStates.insert ( std::make_pair (jointPos, depthSum));
229 
230  } else {
231  std::cerr << "[ERROR ActionMultPinch::" << __func__ << "not know key " << key <<
232  " found in the yaml file" << std::endl;
233  return false;
234  }
235  }
236 
238 
239  return true;
240 }
unsigned int nFingersInvolved
Type
Enum useful to discriminate each action when, for example, we want to parse a file if you change thi...
Definition: Action.h:82
Virtual class, Base of all the primitive actions.
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
A base virtual class for the PinchTight and PinchLoose classes.
STL namespace.
void print() const override
Overridable functions, if we want to make them more action-specific.
Type
Enum useful to discriminate each primitive action when, for example, we want to parse a file if you ...
std::set< StateWithDepth, depthComp > actionStates
For each multiple pinch possible, we want a set of action because we want to store (in general) more ...
void emitYaml(YAML::Emitter &) const override
Function to fill the argument passed with info about the action.
bool insertActionState(JointPos, double depthSum)
function to insert a single action in the actionStates set of possible action.
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
JointPos getJointPos() const override
Get the position related to this action.
const unsigned int maxStoredActionStates
bool fillFromYaml(YAML::const_iterator yamlIt) override
function to fill members of the Action with infos taken from yaml files
std::vector< ROSEE::JointPos > getAllJointPos() const override
Return all the joint position stored.
std::set< std::string > fingersInvolved
Definition: Action.h:148
std::vector< ROSEE::ActionMultiplePinchTight::StateWithDepth > getActionStates() const
Specific get for the ActionMultiplePinchTight to return the state with the paired depthSum...