ROSEndEffector
ROS End-Effector package: provides a ROS-based set of standard interfaces to command robotics end-effectors in an agnostic fashion.
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ROSEE::ActionComposed Class Reference

A ActionComposed, which is formed by one or more Primitives (or even other composed). More...

#include <ActionComposed.h>

+ Inheritance diagram for ROSEE::ActionComposed:
+ Collaboration diagram for ROSEE::ActionComposed:

Public Types

typedef std::shared_ptr< ActionComposedPtr
 
typedef std::shared_ptr< const ActionComposedConstPtr
 
- Public Types inherited from ROSEE::ActionGeneric
typedef std::shared_ptr< ActionGenericPtr
 
typedef std::shared_ptr< const ActionGenericConstPtr
 
- Public Types inherited from ROSEE::Action
enum  Type {
  Primitive,
  Generic,
  Composed,
  Timed,
  None
}
 Enum useful to discriminate each action when, for example, we want to parse a file if you change this enum, change also the ROSEEControl.msg accordingly. More...
 
typedef std::shared_ptr< ActionPtr
 
typedef std::shared_ptr< const ActionConstPtr
 

Public Member Functions

 ActionComposed ()
 
 ActionComposed (std::string name)
 
 ActionComposed (std::string name, bool independent)
 
 ActionComposed (const ActionComposed &other)
 Copy costructor. More...
 
unsigned int numberOfInnerActions () const
 
bool isIndependent () const
 
std::vector< std::string > getInnerActionsNames () const
 
virtual void print () const override
 Print info about this action (name, jointpos, inner actions names, and other) More...
 
virtual void emitYaml (YAML::Emitter &out) const override
 Emit info in a file with yaml format. More...
 
virtual bool fillFromYaml (YAML::const_iterator yamlIt) override
 Fill the internal data with infos taken from yaml file. More...
 
virtual bool sumAction (ROSEE::Action::Ptr action, double jointPosScaleFactor=1.0, unsigned int jointPosIndex=0)
 Function to add another action to this one. More...
 
bool empty ()
 Check if the action composed is empty. More...
 
- Public Member Functions inherited from ROSEE::ActionGeneric
 ActionGeneric ()
 default costructor. More...
 
 ActionGeneric (std::string actionName, ROSEE::JointPos jointPos)
 Simpliest costructor, need only essential infos. More...
 
 ActionGeneric (std::string actionName, ROSEE::JointPos jointPos, JointsInvolvedCount jic)
 Another costructor. More...
 
 ActionGeneric (std::string actionName, ROSEE::JointPos jointPos, JointsInvolvedCount jic, std::set< std::string > fingersInvolved)
 
JointPos getJointPos () const override
 Get the joint position related to this action, overriden from Action. More...
 
std::vector< ROSEE::JointPosgetAllJointPos () const override
 Get the joint position related to this action, overriden from Action Necessary to not make this class abstact, even if for a composed action we store only a single JointPos, so this function return a single element vector. More...
 
- Public Member Functions inherited from ROSEE::Action
virtual ~Action ()
 
std::string getName () const
 Get the name of the action. More...
 
Type getType () const
 
std::set< std::string > getFingersInvolved () const
 Get for fingersInvolved. More...
 
JointsInvolvedCount getJointsInvolvedCount () const
 Get for jointsInvolvedCount. More...
 

Protected Member Functions

bool checkIndependency (ROSEE::Action::Ptr action)
 
- Protected Member Functions inherited from ROSEE::ActionGeneric
 ActionGeneric (std::string actionName)
 this costructor is only for derived class More...
 
- Protected Member Functions inherited from ROSEE::Action
 Action ()
 
 Action (std::string actionName, Action::Type type)
 

Protected Attributes

std::vector< std::string > innerActionsNames
 
unsigned int nInnerActions
 
bool independent
 
- Protected Attributes inherited from ROSEE::ActionGeneric
JointPos jointPos
 
- Protected Attributes inherited from ROSEE::Action
std::string name
 
Action::Type type
 
std::set< std::string > fingersInvolved
 
JointsInvolvedCount jointsInvolvedCount
 

Detailed Description

A ActionComposed, which is formed by one or more Primitives (or even other composed).

It is useful for example to create an action that grasp only with bending the tips (e.g. to take a dish from above) If the ActionComposed has the boolean value independent to true, it means that include indipendent sub-actions, so, each joint is used by at maximum by ONLY ONE of the sub-action inside. In this case the jointsInvolvedCount will contain only 0 or 1 values. If the ActionComposed is not independent, each joint position is calculated as the mean of all the joint position of the contained sub-actions that uses that joint. So each mean can include different primitives, so we used the jointsInvolvedCount vector to store the number of sub action that use each joint.

Todo:
A removeAction function? difficult to implement, and useless?

Definition at line 44 of file ActionComposed.h.

Member Typedef Documentation

typedef std::shared_ptr<const ActionComposed> ROSEE::ActionComposed::ConstPtr

Definition at line 49 of file ActionComposed.h.

typedef std::shared_ptr<ActionComposed> ROSEE::ActionComposed::Ptr

Definition at line 48 of file ActionComposed.h.

Constructor & Destructor Documentation

ROSEE::ActionComposed::ActionComposed ( )

Definition at line 21 of file ActionComposed.cpp.

21  : ActionGeneric() {
22  independent = true;
23  nInnerActions = 0;
24  type = Action::Type::Composed;
25 }
unsigned int nInnerActions
Action::Type type
Definition: Action.h:147
ActionGeneric()
default costructor.
ROSEE::ActionComposed::ActionComposed ( std::string  name)

Definition at line 27 of file ActionComposed.cpp.

27  : ActionGeneric(name) {
28  independent = true;
29  nInnerActions = 0;
30  type = Action::Type::Composed;
31 }
std::string name
Definition: Action.h:146
unsigned int nInnerActions
Action::Type type
Definition: Action.h:147
ActionGeneric()
default costructor.
ROSEE::ActionComposed::ActionComposed ( std::string  name,
bool  independent 
)

Definition at line 33 of file ActionComposed.cpp.

33  : ActionGeneric(name) {
34  this->independent = independent;
35  nInnerActions = 0;
36  type = Action::Type::Composed;
37 }
std::string name
Definition: Action.h:146
unsigned int nInnerActions
Action::Type type
Definition: Action.h:147
ActionGeneric()
default costructor.
ROSEE::ActionComposed::ActionComposed ( const ActionComposed other)

Copy costructor.

Member Function Documentation

bool ROSEE::ActionComposed::checkIndependency ( ROSEE::Action::Ptr  action)
protected

Definition at line 147 of file ActionComposed.cpp.

147  {
148 
149  if (!independent) {
150 
151  } else if (nInnerActions == 0 ) {
152 
153  for ( auto jic : action->getJointsInvolvedCount() ){
154  if ( jic.second > 1 ) {
155  // if action is dependent we check that all its joints are setted only once
156  // so we can teoretically add a "dipendent" action if all its joints are setted once
157  return false; //cant add this primitive
158  }
159  }
160 
161  } else {
162 
163  for ( auto jic : action->getJointsInvolvedCount() ){
164  if ( jic.second + jointsInvolvedCount.at(jic.first) > 1 ) {
165  // we use the sum so also if action is dependent we check that all its joints are setted once
166  // so we can teoretically add a "dipendent" action if all its joints are setted once
167  return false; //cant add this primitive
168  }
169  }
170  }
171 
172  return true;
173 }
unsigned int nInnerActions
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
void ROSEE::ActionComposed::emitYaml ( YAML::Emitter &  out) const
overridevirtual

Emit info in a file with yaml format.

Parameters
outa YAML::Emitter& object to emit the infos

Reimplemented from ROSEE::ActionGeneric.

Definition at line 208 of file ActionComposed.cpp.

208  {
209 
210  out << YAML::BeginMap << YAML::Key << name << YAML::Value << YAML::BeginMap ;
211  out << YAML::Key << "Type" << YAML::Value << type;
212  out << YAML::Key << "Independent" << YAML::Value << independent;
213  out << YAML::Key << "NInnerActions" << YAML::Value << nInnerActions;
214  out << YAML::Key << "InnerActionsNames" << YAML::Value << YAML::Flow << innerActionsNames;
215  out << YAML::Key << "FingersInvolved" << YAML::Value << YAML::Flow << fingersInvolved;
216  out << YAML::Key << "JointsInvolvedCount" << YAML::Value << YAML::BeginMap;
217  for (const auto &jointCount : jointsInvolvedCount ) {
218  out << YAML::Key << jointCount.first;
219  out << YAML::Value << jointCount.second;
220  }
221  out << YAML::EndMap;
222 
223  out << YAML::Key << "JointPos" << YAML::Value << YAML::BeginMap;
224  for (const auto &joint : jointPos) {
225  out << YAML::Key << joint.first;
226  out << YAML::Value << YAML::Flow << joint.second; //vector of double is emitted like Seq
227  }
228  out << YAML::EndMap;
229  out << YAML::EndMap;
230  out << YAML::EndMap;
231 }
std::string name
Definition: Action.h:146
std::vector< std::string > innerActionsNames
unsigned int nInnerActions
Action::Type type
Definition: Action.h:147
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
std::set< std::string > fingersInvolved
Definition: Action.h:148
bool ROSEE::ActionComposed::empty ( )

Check if the action composed is empty.

Returns
true if empty, false otherwise

Definition at line 51 of file ActionComposed.cpp.

51  {
52  return (nInnerActions == 0);
53 }
unsigned int nInnerActions
bool ROSEE::ActionComposed::fillFromYaml ( YAML::const_iterator  yamlIt)
overridevirtual

Fill the internal data with infos taken from yaml file.

Parameters
yamlIta yamlt iterator to a node which has loaded the file
Returns
false if some error happened

Reimplemented from ROSEE::ActionGeneric.

Definition at line 234 of file ActionComposed.cpp.

234  {
235 
236  name = yamlIt->first.as<std::string>();
237 
238  for (auto keyValue = yamlIt->second.begin(); keyValue != yamlIt->second.end(); ++keyValue ) {
239 
240  std::string key = keyValue->first.as<std::string>();
241 
242  if ( key.compare ("Independent") == 0 ) {
243  independent = keyValue->second.as<bool>();
244 
245  } else if ( key.compare ("NInnerActions") == 0 ) {
246  nInnerActions = keyValue->second.as <unsigned int>();
247 
248  } else if ( key.compare ("Type") == 0 ) {
249  if (ROSEE::Action::Type::Composed != static_cast<ROSEE::Action::Type> ( keyValue->second.as <unsigned int>() )) {
250  std::cout << "[COMPOSED ACTION::" << __func__ << "] Error, found type " << keyValue->second.as <unsigned int>()
251  << "instead of Composed type (" << ROSEE::Action::Type::Composed << ")" << std::endl;
252  return false;
253  }
254  type = ROSEE::Action::Type::Composed;
255 
256  } else if ( key.compare ("InnerActionsNames") == 0 ) {
257  innerActionsNames = keyValue->second.as <std::vector <std::string> >();
258 
259  } else if ( key.compare ("FingersInvolved") == 0 ) {
260  auto tempVect = keyValue->second.as <std::vector <std::string> > ();
261  fingersInvolved.insert ( tempVect.begin(), tempVect.end() );
262 
263  } else if ( key.compare ("JointsInvolvedCount") == 0 ) {
264  jointsInvolvedCount = keyValue->second.as < JointsInvolvedCount >();
265 
266  } else if ( key.compare ("JointPos") == 0 ) {
267  jointPos = keyValue->second.as < JointPos >();
268 
269  } else {
270  std::cout << "[COMPOSED ACTION PARSER] Error, not known key " << key << std::endl;
271  return false;
272  }
273  }
274  return true;
275 }
std::string name
Definition: Action.h:146
std::vector< std::string > innerActionsNames
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
unsigned int nInnerActions
Action::Type type
Definition: Action.h:147
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
std::set< std::string > fingersInvolved
Definition: Action.h:148
std::vector< std::string > ROSEE::ActionComposed::getInnerActionsNames ( ) const
Returns
std::vector <std::string> all the names that compose this action

Definition at line 47 of file ActionComposed.cpp.

47  {
48  return innerActionsNames;
49 }
std::vector< std::string > innerActionsNames
bool ROSEE::ActionComposed::isIndependent ( ) const
Returns
bool true if this action must contain only independent primitives

Definition at line 43 of file ActionComposed.cpp.

43  {
44  return independent;
45 }
unsigned int ROSEE::ActionComposed::numberOfInnerActions ( ) const
Returns
unsigned int the number of the actions that compose this one

Definition at line 39 of file ActionComposed.cpp.

39  {
40  return nInnerActions;
41 }
unsigned int nInnerActions
void ROSEE::ActionComposed::print ( ) const
overridevirtual

Print info about this action (name, jointpos, inner actions names, and other)

Reimplemented from ROSEE::Action.

Definition at line 176 of file ActionComposed.cpp.

176  {
177 
178  std::stringstream output;
179 
180  output << "Composed Action '" << name;
181  independent ? output << "' (independent):" : output << "' (not independent):" ;
182  output << std::endl;
183 
184  output << "Composed by " << nInnerActions << " inner action: [" ;
185  for (auto it : innerActionsNames) {
186  output << it << ", ";
187  }
188  output.seekp (-2, output.cur); //to remove the last comma (and space)
189  output << "]" << std::endl;
190 
191  output << "Fingers involved: [" ;
192  for (auto it : fingersInvolved) {
193  output << it << ", ";
194  }
195  output.seekp (-2, output.cur); //to remove the last comma (and space)
196  output << "]" << std::endl;
197 
198  output << "Each joint influenced by x inner action:" << std::endl;
199  output << jointsInvolvedCount;
200 
201  output << "JointPos:" << std::endl;
202  output << jointPos << std::endl;
203 
204  std::cout << output.str();
205 }
std::string name
Definition: Action.h:146
std::vector< std::string > innerActionsNames
unsigned int nInnerActions
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
std::set< std::string > fingersInvolved
Definition: Action.h:148
bool ROSEE::ActionComposed::sumAction ( ROSEE::Action::Ptr  action,
double  jointPosScaleFactor = 1.0,
unsigned int  jointPosIndex = 0 
)
virtual

Function to add another action to this one.

Parameters
actionThe action to be added to the ActionComposed
jointPosScaleFactorHow much scale the joint position values for the action to be inserted. Default to 1
jointPosIndex(default == 0) the wanted jointPos or action to insert. Error the index is greater than the number of joint pos in the action. First element has index 0.
Returns
False if the ActionComposed is independent and we try to add an action that is dependent from one of the already present

Definition at line 56 of file ActionComposed.cpp.

57 {
58 
59  if ( ! checkIndependency(action) ) {
60  return false; //cant add this primitive
61  }
62 
63  if ( jointPosIndex > action->getAllJointPos().size()-1 ) {
64  std::cerr << "[ACTIONCOMPOSED:: " << __func__ << "] The given jointPosindex " << jointPosIndex
65  << " exceed the number " << action->getAllJointPos().size() << " of jointpos of passed action" << std::endl;
66  return false;
67  }
68 
69  if ( nInnerActions > 0 &&
70  (! ROSEE::Utils::keys_equal(action->getAllJointPos().at(jointPosIndex), jointPos)) ) {
71  std::cerr << "[ACTIONCOMPOSED:: " << __func__ << "] The action passed as argument has different keys in jointPosmap"
72  << " respect to the others inserted in this composed action " << std::endl;
73  return false;
74  }
75 
76  if (jointPosScaleFactor < 0) {
77  std::cerr << "[ACTIONCOMPOSED:: " << __func__ << "] You can not scale the joint position of the action to be inserted by a "
78  << "value less than 0; jointPosScaleFactor passed is: " << jointPosScaleFactor << std::endl;
79  return false;
80  }
81 
82  if (jointPosScaleFactor > 1) {
83  std::wcerr << "[ACTIONCOMPOSED:: " << __func__ << "] WARNING, You are scaling with a value greater than 1 "
84  << " this could cause to command position over the joint limits; jointPosScaleFactor passed is: " << jointPosScaleFactor << std::endl;
85  }
86 
87  JointPos actionJP = action->getAllJointPos().at(jointPosIndex) * jointPosScaleFactor;
88  JointsInvolvedCount actionJIC = action->getJointsInvolvedCount();
89 
90  if (nInnerActions == 0) { //first primitive inserted
91 
92  for (auto joint : actionJP ){
93  jointsInvolvedCount.insert ( std::make_pair (joint.first, actionJIC.at(joint.first) ) );
94  jointPos.insert ( std::make_pair (joint.first, joint.second) );
95  }
96 
97  } else {
98 
99  if (independent) {
100 
101  //if here, action is independent, we can add the joints states
102  for ( auto joint : actionJP ){
103 
104  if ( actionJIC.at (joint.first ) > 0 ) {
105 
106  jointPos.at(joint.first) = joint.second;
107  jointsInvolvedCount.at(joint.first) += actionJIC.at (joint.first );
108  //+= or = is the same for the checks done before
109  }
110  }
111 
112  } else {
113  // for each joint, add the state's value to the mean
114  // (number of element in the previous mean is given by jointsInvolvedCount.at(x))
115 
116  for ( auto joint : actionJP ) {
117  if ( actionJIC.at( joint.first ) == 0 ) {
118  continue; //if the action that is being added has this joint not setted, not consider it
119  }
120 
121  //update the count
122  jointsInvolvedCount.at(joint.first) += actionJIC.at (joint.first );
123 
124  //iterate all dofs of jointPos
125  for (unsigned int dof = 0; dof < joint.second.size(); dof++ ) {
126 
127  double mean = jointPos.at( joint.first ).at(dof) +
128  ( (joint.second.at(dof) - jointPos.at(joint.first).at(dof)) /
129  jointsInvolvedCount.at(joint.first) );
130 
131  jointPos.at(joint.first).at(dof) = mean;
132  }
133  }
134  }
135  }
136 
137  innerActionsNames.push_back ( action->getName() );
138  for ( auto it: action->getFingersInvolved() ) {
139  fingersInvolved.insert ( it );
140  }
141 
142  nInnerActions ++;
143 
144  return true;
145 }
std::vector< std::string > innerActionsNames
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
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
unsigned int nInnerActions
bool checkIndependency(ROSEE::Action::Ptr action)
JointsInvolvedCount jointsInvolvedCount
Definition: Action.h:149
std::set< std::string > fingersInvolved
Definition: Action.h:148

Member Data Documentation

bool ROSEE::ActionComposed::independent
protected

Definition at line 116 of file ActionComposed.h.

std::vector< std::string > ROSEE::ActionComposed::innerActionsNames
protected

Definition at line 113 of file ActionComposed.h.

unsigned int ROSEE::ActionComposed::nInnerActions
protected

Definition at line 114 of file ActionComposed.h.


The documentation for this class was generated from the following files: