ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
ActionPinchLoose.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 ("pinchLoose", 2, 3, ActionPrimitive::Type::PinchLoose) { }
23 
25  ActionPinchGeneric ("pinchLoose", 2, maxStoredActionStates, ActionPrimitive::Type::PinchLoose) { }
26 
27 ROSEE::ActionPinchLoose::ActionPinchLoose (std::string tip1, std::string tip2) :
28  ActionPinchGeneric ("pinchLoose", 2, 3, ActionPrimitive::Type::PinchLoose) {
29  fingersInvolved.insert (tip1);
30  fingersInvolved.insert (tip2);
31 }
32 
33 ROSEE::ActionPinchLoose::ActionPinchLoose (std::pair <std::string, std::string> tipNames,
34  JointPos jp, double distance) :
35  ActionPinchGeneric ("pinchLoose", 2, 3, ActionPrimitive::Type::PinchLoose ) {
36 
37  fingersInvolved.insert (tipNames.first);
38  fingersInvolved.insert (tipNames.second);
39 
40  //different from insertState, here we are sure the set is empty (we are in costructor)
41  actionStates.insert (std::make_pair (jp, distance) );
42 }
43 
44 std::vector < ROSEE::ActionPinchLoose::StateWithDistance > ROSEE::ActionPinchLoose::getActionStates() const {
45 
46  std::vector < ROSEE::ActionPinchLoose::StateWithDistance > retVect;
47  retVect.reserve ( actionStates.size() );
48 
49  for (auto it : actionStates ) {
50  retVect.push_back(it);
51  }
52 
53  return retVect;
54 }
55 
57  return (actionStates.begin()->first);
58 }
59 
61  auto it = actionStates.begin();
62  unsigned int i = 1;
63  while (i < index ) {
64  ++ it;
65  ++ i;
66  }
67  return (it->first);
68 }
69 
70 std::vector < ROSEE::JointPos > ROSEE::ActionPinchLoose::getAllJointPos() const {
71 
72  std::vector < JointPos > retVect;
73  retVect.reserve(actionStates.size());
74 
75  for (auto it : actionStates ) {
76  retVect.push_back(it.first);
77  }
78 
79  return retVect;
80 }
81 
83 
84  auto pairRet = actionStates.insert ( std::make_pair (jp, dist) ) ;
85 
86  if (actionStates.size() > maxStoredActionStates) {
87  //max capacity reached, we have to delete the last one
88  auto it = pairRet.first;
89 
90  if (++(it) == actionStates.end() ){
91  // the new inserted is the last one and has to be erased
92  actionStates.erase(pairRet.first);
93  return false;
94  }
95 
96  // the new inserted is not the last one that has to be erased
97  auto lastElem = actionStates.end();
98  --lastElem;
99  actionStates.erase(lastElem);
100  }
101 
102  return true;
103 }
104 
105 
107 
108  std::stringstream output;
109 
110  output << "ActionName: " << name << std::endl;
111 
112  output << "FingersInvolved: [";
113  for (auto fingName : fingersInvolved){
114  output << fingName << ", " ;
115  }
116  output.seekp (-2, output.cur); //to remove the last comma (and space)
117  output << "]" << std::endl;
118 
119  output << "JointsInvolvedCount: " << std::endl;;
120  output << jointsInvolvedCount << std::endl;
121 
122  unsigned int nActState = 1;
123  for (auto itemSet : actionStates) { //the element in the set
124  output << "Action_State_" << nActState << " :" << std::endl;
125 
126  output << "\t" << "JointStates:" << std::endl;
127  output << itemSet.first;
128  output << "\t" << "Distance:" << std::endl;
129  output << "\t\tdistance " << 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::ActionPinchLoose::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 << "JointStates" << 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 << "distance" << 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::ActionPinchLoose::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 ActionPinchLoose::" << __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 distance;
211  for(YAML::const_iterator asEl = keyValue->second.begin(); asEl != keyValue->second.end(); ++asEl) {
212 
213  //asEl can be the map JointStates or the map Optional
214  if (asEl->first.as<std::string>().compare ("JointStates") == 0 ) {
215  jointPos = asEl->second.as < JointPos >();
216 
217  } else if (asEl->first.as<std::string>().compare ("Optional") == 0 ) {
218  distance = asEl->second["distance"].as < double >();
219 
220  } else {
221  //ERRROr, only joinstates and optional at this level
222  std::cerr << "[ERROR ActionPinchLoose::" << __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, distance));
229 
230  } else {
231  std::cerr << "[ERROR ActionPinchLoose::" << __func__ << "not know key " << key <<
232  " found in the yaml file" << std::endl;
233  return false;
234  }
235  }
236 
237  return true;
238 }
239 
240 
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::vector< ROSEE::JointPos > getAllJointPos() const override
Return all the joint position stored.
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
std::set< StateWithDistance, distComp > actionStates
For each pair, we want a set of action because we want to store (in general) more possible way to do ...
A base virtual class for the PinchTight and PinchLoose classes.
JointPos getJointPos() const override
Get the position related to this action.
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 ...
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
const unsigned int maxStoredActionStates
bool fillFromYaml(YAML::const_iterator yamlIt) override
function to fill members of the Action with infos taken from yaml files
void emitYaml(YAML::Emitter &) const override
Function to fill the argument passed with info about the action.
std::vector< ROSEE::ActionPinchLoose::StateWithDistance > getActionStates() const
Specific get for this action to return the state with distance info.
std::set< std::string > fingersInvolved
Definition: Action.h:148
bool insertActionState(JointPos, double distance)
function to insert a single action in the actionStates set of possible action.